Matrix/API/main.py

50 lines
1.1 KiB
Python
Raw Normal View History

2023-11-22 15:21:15 +00:00
#!/usr/bin/env python
2023-11-22 15:44:26 +00:00
from deps.samplebase import SampleBase
2023-11-22 15:21:15 +00:00
from rgbmatrix import graphics
2023-11-23 07:15:02 +00:00
from flask import Flask, jsonify
2023-11-23 07:49:47 +00:00
from waitress import serve
import time, threading
2023-11-22 14:41:13 +00:00
2023-11-23 07:15:02 +00:00
app = Flask(__name__)
2023-11-23 07:49:47 +00:00
display_text = ""
2023-11-23 07:15:02 +00:00
count = 0
@app.route('/api/test', methods=['GET'])
def get_data():
2023-11-23 07:49:47 +00:00
global display_text, count
count = count + 1
display_text = f"Test #{count}"
data = {"success": True, "count": count, "text": display_text}
2023-11-23 07:15:02 +00:00
return jsonify(data)
2023-11-23 06:38:21 +00:00
class Text(SampleBase):
2023-11-22 15:22:19 +00:00
def __init__(self, *args, **kwargs):
2023-11-23 06:38:21 +00:00
super(Text, self).__init__(*args, **kwargs)
2023-11-22 14:41:13 +00:00
2023-11-22 15:22:19 +00:00
def run(self):
2023-11-23 07:49:47 +00:00
self.font = graphics.Font()
self.font.LoadFont("deps/fonts/9x18B.bdf")
self.color = graphics.Color(255, 255, 255)
self.canvas = self.matrix.CreateFrameCanvas()
2023-11-22 14:41:13 +00:00
2023-11-22 15:21:15 +00:00
while True:
2023-11-23 07:49:47 +00:00
self.update()
time.sleep(.05)
2023-11-23 07:15:02 +00:00
2023-11-23 07:49:47 +00:00
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)
2023-11-22 14:41:13 +00:00
2023-11-22 15:21:15 +00:00
if __name__ == "__main__":
2023-11-23 07:49:47 +00:00
text = Text()
threading.Thread(target=lambda: serve(app, host="0.0.0.0", port=8080)).start()
2023-11-23 07:15:02 +00:00
2023-11-23 07:49:47 +00:00
if not text.process():
text.print_help()