hzeller / rpi-rgb-led-matrix

Controlling up to three chains of 64x64, 32x32, 16x32 or similar RGB LED displays using Raspberry Pi GPIO
GNU General Public License v2.0
3.64k stars 1.16k forks source link

Flask App question #1507

Open galaxyZ2 opened 1 year ago

galaxyZ2 commented 1 year ago

Hello Everyone,

I am totally new to programming, and I choose a hobby project in the form of a raspberry pi 4 with an adafruit-hat controlling a 32x32 matrix.

I wrote a python script, which is working quite beautifully, however when I use the text mover functions (either of them) the text I initially set will change to "utf-8" and keep moving according to the function.

What could cause this?

Any help is much appreciated!

galaxyZ2 commented 1 year ago

Apologies, I dont know how to comment the code in a readable format:

''' from flask import Flask, request, render_template, redirect from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, SelectField import logging, os from pprint import pprint

app = Flask(name) app.config['SECRET_KEY'] = 'secretkey'

Initialize and configure LED matrix

options = RGBMatrixOptions() options.rows = 32 options.cols = 32 options.chain_length = 1 options.brightness = 100 options.pwm_bits = 1 options.pwm_lsb_nanoseconds = 100 options.gpio_slowdown = 5 options.hardware_mapping = 'adafruit-hat' matrix = RGBMatrix(options = options)

Create a canvas to draw on

canvas = matrix.CreateFrameCanvas() x = int y = int message = str color = str

Load default font.

font = graphics.Font()

Read available fonts into variable

class DisplayForm(FlaskForm): message = StringField('Messages') color = StringField('Color') submit = SubmitField('Submit')

@app.route('/display', methods=['GET','POST']) def display(): global color, x, y

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')

form = DisplayForm()
global message
if form.validate_on_submit():
    if not form.message.data or not form.color.data:
       return render_template('display.html', form=form, error_mesage='Please enter the values')
    x = 0
    y = 20
    message = form.message.data
    color_code = form.color.data
    r, g, b = map(int, color_code.split(","))
    color = graphics.Color(r,g,b)
    font.LoadFont('/home/linux/rpi-rgb-led-matrix/fonts/7x13.bdf')
    canvas.Clear()
    graphics.DrawText(canvas, font, x, y, color, message)
    matrix.SwapOnVSync(canvas)
    #return 'Displaying message: {}'.format(message)
return render_template('display.html', form=form, color=color, x=x, y=y)

@app.route('/stop', methods=['GET', 'POST']) def stop(): form = DisplayForm()

Clear the canvas

canvas.Clear()
#Push the cleared canvas to the matrix
matrix.SwapOnVSync(canvas)
return render_template('display.html', form=form, x=x, y=y)

@app.route('/move_left', methods=['GET', 'POST']) def move_left(): global x, message x = x - 1 canvas.Clear() graphics.DrawText(canvas, font, x, y, color, message) matrix.SwapOnVSync(canvas) return redirect('/')

@app.route('/move_right', methods=['GET', 'POST']) def move_right(): global x x = x + 1 canvas.Clear() graphics.DrawText(canvas, font, x, y, color, message) matrix.SwapOnVSync(canvas) return redirect('/')

@app.route('/move_up', methods=['GET', 'POST']) def move_up(): global y y -= 1 canvas.Clear() graphics.DrawText(canvas, font, x, y, color, message) matrix.SwapOnVSync(canvas) return redirect('/')

@app.route('/move_down', methods=['GET', 'POST']) def move_down(): global y, message y += 1 canvas.Clear() graphics.DrawText(canvas, font, x, y, color, message) matrix.SwapOnVSync(canvas) return redirect('/')

if name == 'main': app.run(host='192.168.1.112', port=1234) '''

hzeller commented 1 year ago

Usually you'd do triple ticks to have code shown properly formatted.

It looks like you're not assigning the return value of SwapToVSync() to canvas. Without that, you don't have double-buffering.

  canvas = matrix.SwapOnVSync(canvas)
ledvinap commented 1 year ago

(can you repost formated code / edit post? there is 'add code' icon above post form)

My guess is that message variable in display() is local, other functions refer message from global scope

galaxyZ2 commented 1 year ago

(can you repost formated code / edit post? there is 'add code' icon above post form)

My guess is that message variable in display() is local, other functions refer message from global scope

BINGO!! Thank you! What a small mistake, but it works beautifully if i change it! Thank you!

galaxyZ2 commented 1 year ago

Usually you'd do triple ticks to have code shown properly formatted.

It looks like you're not assigning the return value of SwapToVSync() to canvas. Without that, you don't have double-buffering.

  canvas = matrix.SwapOnVSync(canvas)

Thank you! It ended up being the second comment! All good thank you for the ideas!

hzeller commented 1 year ago

You still need canvas = matrix.SwapOnVSync(canvas) for a flickering-free image transition.

galaxyZ2 commented 1 year ago

You still need canvas = matrix.SwapOnVSync(canvas) for a flickering-free image transition.

Forgot to mention that I did add the line as suggested! Much appreciated!