Matrix/API/main.py

118 lines
2.3 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
2023-11-23 09:07:17 +00:00
from flask import Flask, request, 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
2023-11-23 09:07:17 +00:00
api = Flask(__name__)
2023-11-23 07:15:02 +00:00
2023-11-23 09:07:41 +00:00
@api.route('/text', methods=['POST'])
def display_text():
response = { 'success': True }
try:
global matrix
data = request.get_json()
text = data.get('text')
x_coord = data.get('x')
y_coord = data.get('y')
matrix.text(x_coord, y_coord, text)
2023-11-23 09:44:57 +00:00
except Exception as e:
print(e)
2023-11-23 09:07:41 +00:00
response['success'] = False
return jsonify(response)
2023-11-23 09:46:15 +00:00
@api.route('/pixel', methods=['POST'])
def set_pixel():
response = { 'success': True }
try:
global matrix
data = request.get_json()
x_coord = data.get('x')
y_coord = data.get('y')
matrix.pixel(x_coord, y_coord)
except Exception as e:
print(e)
response['success'] = False
return jsonify(response)
2023-11-23 09:46:43 +00:00
@api.route('/color', methods=['POST'])
def set_color():
response = { 'success': True }
try:
global matrix
data = request.get_json()
red = data.get('r')
green = data.get('g')
blue = data.get('b')
matrix.set_color(red, green, blue)
except Exception as e:
print(e)
response['success'] = False
return jsonify(response)
2023-11-23 09:07:41 +00:00
@api.route('/update', methods=['GET'])
def update_matrix():
response = { 'success': True }
try:
global matrix
matrix.update()
2023-11-23 09:44:57 +00:00
except Exception as e:
print(e)
2023-11-23 09:07:41 +00:00
response['success'] = False
return jsonify(response)
2023-11-23 07:49:47 +00:00
2023-11-23 07:15:02 +00:00
2023-11-23 09:07:17 +00:00
class Matrix(SampleBase):
2023-11-22 15:22:19 +00:00
def __init__(self, *args, **kwargs):
2023-11-23 09:07:17 +00:00
super(Matrix, 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)
2023-11-23 09:07:17 +00:00
2023-11-23 07:49:47 +00:00
self.canvas = self.matrix.CreateFrameCanvas()
2023-11-23 09:07:17 +00:00
self.canvas.Clear()
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
time.sleep(.05)
2023-11-23 07:15:02 +00:00
2023-11-23 09:44:57 +00:00
def clear(self):
self.canvas.Clear()
2023-11-23 07:49:47 +00:00
def update(self):
self.canvas = self.matrix.SwapOnVSync(self.canvas)
2023-11-23 09:44:57 +00:00
self.clear()
2023-11-23 09:07:17 +00:00
2023-11-23 09:46:15 +00:00
def set_color(self, r, g, b):
self.color = graphics.Color(r, g, b)
def pixel(self, x, y):
self.canvas.SetPixel(x, y, self.color.red, self.color.green, self.color.blue)
2023-11-23 09:07:17 +00:00
def text(self, x, y, t):
graphics.DrawText(self.canvas, self.font, x*9, (y+1)*18, self.color, t)
2023-11-22 14:41:13 +00:00
2023-11-22 15:21:15 +00:00
2023-11-23 09:07:17 +00:00
matrix = None
2023-11-22 15:21:15 +00:00
if __name__ == "__main__":
2023-11-23 09:07:17 +00:00
matrix = Matrix()
threading.Thread(target=lambda: serve(api, host="0.0.0.0", port=8080)).start()
2023-11-23 07:15:02 +00:00
2023-11-23 09:07:17 +00:00
if not matrix.process():
matrix.print_help()