mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-11-08 22:43:50 +00:00
229 lines
5.1 KiB
Python
229 lines
5.1 KiB
Python
#!/usr/bin/env python
|
|
from deps.samplebase import SampleBase
|
|
from rgbmatrix import graphics
|
|
|
|
from flask import Flask, request, jsonify
|
|
from waitress import serve
|
|
import time, threading
|
|
|
|
|
|
api = Flask(__name__)
|
|
|
|
@api.route('/text', methods=['POST'])
|
|
def display_text():
|
|
# prepare response data
|
|
response = { 'success': True }
|
|
try:
|
|
global matrix
|
|
|
|
# receive client data
|
|
data = request.get_json()
|
|
# try unpacking text and x,y coordinates
|
|
text = data.get('text')
|
|
x_coord = data.get('x')
|
|
y_coord = data.get('y')
|
|
|
|
# call matrix method with data
|
|
matrix.text(x_coord, y_coord, text)
|
|
except Exception as e:
|
|
# error handling
|
|
print(e)
|
|
response['success'] = False
|
|
|
|
# respond to client
|
|
return jsonify(response)
|
|
|
|
@api.route('/pixel', methods=['POST'])
|
|
def set_pixel():
|
|
# prepare response data
|
|
response = { 'success': True }
|
|
try:
|
|
global matrix
|
|
|
|
# receive client data
|
|
data = request.get_json()
|
|
# try unpacking x,y coordinates
|
|
x_coord = data.get('x')
|
|
y_coord = data.get('y')
|
|
|
|
# call matrix method with data
|
|
matrix.pixel(x_coord, y_coord)
|
|
except Exception as e:
|
|
# error handling
|
|
print(e)
|
|
response['success'] = False
|
|
|
|
# respond to client
|
|
return jsonify(response)
|
|
|
|
@api.route('/circle', methods=['POST'])
|
|
def draw_circle():
|
|
# prepare response data
|
|
response = { 'success': True }
|
|
try:
|
|
global matrix
|
|
|
|
# receive client data
|
|
data = request.get_json()
|
|
# try unpacking radius and x,y coordinates
|
|
radius = data.get('r')
|
|
x_coord = data.get('x')
|
|
y_coord = data.get('y')
|
|
|
|
# call matrix method with data
|
|
matrix.circle(x_coord, y_coord, radius)
|
|
except Exception as e:
|
|
# error handling
|
|
print(e)
|
|
response['success'] = False
|
|
|
|
# respond to client
|
|
return jsonify(response)
|
|
|
|
@api.route('/rectangle', methods=['POST'])
|
|
def draw_rectangle():
|
|
# prepare response data
|
|
response = { 'success': True }
|
|
try:
|
|
global matrix
|
|
|
|
# receive client data
|
|
data = request.get_json()
|
|
# try unpacking rectangle width and height
|
|
width = data.get('w')
|
|
height = data.get('h')
|
|
# try unpacking x,y coordinates
|
|
x_coord = data.get('x')
|
|
y_coord = data.get('y')
|
|
|
|
# call matrix method with data
|
|
matrix.rectangle(x_coord, y_coord, width, height)
|
|
except Exception as e:
|
|
# error handling
|
|
print(e)
|
|
response['success'] = False
|
|
|
|
# respond to client
|
|
return jsonify(response)
|
|
|
|
@api.route('/color', methods=['POST'])
|
|
def set_color():
|
|
# prepare response data
|
|
response = { 'success': True }
|
|
try:
|
|
global matrix
|
|
|
|
# receive client data
|
|
data = request.get_json()
|
|
# try unpacking red, green and blue color values
|
|
red = data.get('r')
|
|
green = data.get('g')
|
|
blue = data.get('b')
|
|
|
|
# call matrix method with data
|
|
matrix.set_color(red, green, blue)
|
|
except Exception as e:
|
|
# error handling
|
|
print(e)
|
|
response['success'] = False
|
|
|
|
# respond to client
|
|
return jsonify(response)
|
|
|
|
@api.route('/clear', methods=['GET'])
|
|
def update_matrix():
|
|
# prepare response data
|
|
response = { 'success': True }
|
|
try:
|
|
global matrix
|
|
|
|
# receive client data
|
|
matrix.clear()
|
|
except Exception as e:
|
|
# error handling
|
|
print(e)
|
|
response['success'] = False
|
|
|
|
return jsonify(response)
|
|
|
|
@api.route('/update', methods=['GET'])
|
|
def update_matrix():
|
|
response = { 'success': True }
|
|
try:
|
|
global matrix
|
|
matrix.update()
|
|
except Exception as e:
|
|
print(e)
|
|
response['success'] = False
|
|
|
|
# respond to client
|
|
return jsonify(response)
|
|
|
|
|
|
# derived class to access rgb-led-matrix
|
|
class Matrix(SampleBase):
|
|
def __init__(self, *args, **kwargs):
|
|
super(Matrix, self).__init__(*args, **kwargs)
|
|
|
|
# run display program
|
|
def run(self):
|
|
# initialize font
|
|
self.font = graphics.Font()
|
|
self.font.LoadFont("deps/fonts/9x18B.bdf")
|
|
# initialize color
|
|
self.color = graphics.Color(255, 255, 255)
|
|
|
|
# initialize canvas
|
|
self.canvas = self.matrix.CreateFrameCanvas()
|
|
self.canvas.Clear()
|
|
|
|
# wait in idle
|
|
while True:
|
|
time.sleep(.05)
|
|
|
|
# clear canvas
|
|
def clear(self):
|
|
self.canvas.Clear()
|
|
|
|
# swap canvas with buffer
|
|
def update(self):
|
|
self.canvas = self.matrix.SwapOnVSync(self.canvas)
|
|
self.clear()
|
|
|
|
# set current color
|
|
def set_color(self, r, g, b):
|
|
self.color = graphics.Color(r, g, b)
|
|
|
|
# set specified pixel to current color
|
|
def pixel(self, x, y):
|
|
self.canvas.SetPixel(x, y, self.color.red, self.color.green, self.color.blue)
|
|
|
|
# display text at position with current color
|
|
def text(self, x, y, t):
|
|
graphics.DrawText(self.canvas, self.font, x*9, (y+1)*18, self.color, t)
|
|
|
|
# display circle at position with radius in current color
|
|
def circle(self, x, y, r):
|
|
graphics.DrawCircle(self.canvas, self.font, x, y, r, self.color)
|
|
|
|
# display rectangle at position with dimensions in current color
|
|
def rectangle(self, x, y, w, h):
|
|
# loop through each point in dimensions
|
|
for i in range(w):
|
|
for j in range(h):
|
|
# set next pixel in rectangle
|
|
self.canvas.SetPixel(x+i, y+j, self.color.red, self.color.green, self.color.blue)
|
|
|
|
|
|
# initialize global matrix variable
|
|
matrix = None
|
|
if __name__ == "__main__":
|
|
# set matrix to instance
|
|
matrix = Matrix()
|
|
# run rest api in thread, serving with 'waitress'
|
|
threading.Thread(target=lambda: serve(api, host="0.0.0.0", port=8080)).start()
|
|
|
|
# help menu on call
|
|
if not matrix.process():
|
|
matrix.print_help()
|