threaded api, parallel to matrix

This commit is contained in:
waltem01 2023-11-23 08:49:47 +01:00
parent cb26cc6b0a
commit e7e02b73b0

View File

@ -3,42 +3,47 @@ from deps.samplebase import SampleBase
from rgbmatrix import graphics from rgbmatrix import graphics
from flask import Flask, jsonify from flask import Flask, jsonify
import time from waitress import serve
import time, threading
app = Flask(__name__) app = Flask(__name__)
display_text = ""
count = 0 count = 0
@app.route('/api/test', methods=['GET']) @app.route('/api/test', methods=['GET'])
def get_data(): def get_data():
app.matrix.text = f"Test #{++count}" global display_text, count
data = {"success": True} count = count + 1
display_text = f"Test #{count}"
data = {"success": True, "count": count, "text": display_text}
return jsonify(data) return jsonify(data)
class Text(SampleBase): class Text(SampleBase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(Text, self).__init__(*args, **kwargs) super(Text, self).__init__(*args, **kwargs)
self.text = ""
def run(self): def run(self):
offscreen_canvas = self.matrix.CreateFrameCanvas() self.font = graphics.Font()
font = graphics.Font() self.font.LoadFont("deps/fonts/9x18B.bdf")
font.LoadFont("deps/fonts/9x18B.bdf") self.color = graphics.Color(255, 255, 255)
text_color = graphics.Color(255, 255, 255) self.canvas = self.matrix.CreateFrameCanvas()
while True: while True:
offscreen_canvas.Clear() self.update()
graphics.DrawText(offscreen_canvas, font, 0, 18, text_color, self.text) time.sleep(.05)
offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
time.sleep(0.05) def update(self):
self.canvas.Clear()
graphics.DrawText(self.canvas, self.font, 0, 18, self.color, display_text)
self.canvas = self.matrix.SwapOnVSync(self.canvas)
if __name__ == "__main__": if __name__ == "__main__":
app.matrix = Text() text = Text()
app.run(debug=True) threading.Thread(target=lambda: serve(app, host="0.0.0.0", port=8080)).start()
if not app.matrix.process(): if not text.process():
app.matrix.print_help() text.print_help()