mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-25 11:31:46 +00:00
added comments
This commit is contained in:
parent
1e618efed0
commit
90c32d3223
57
API/main.py
57
API/main.py
@ -11,102 +11,136 @@ api = Flask(__name__)
|
|||||||
|
|
||||||
@api.route('/text', methods=['POST'])
|
@api.route('/text', methods=['POST'])
|
||||||
def display_text():
|
def display_text():
|
||||||
|
# prepare response data
|
||||||
response = { 'success': True }
|
response = { 'success': True }
|
||||||
try:
|
try:
|
||||||
global matrix
|
global matrix
|
||||||
|
|
||||||
|
# receive client data
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
# try unpacking text and x,y coordinates
|
||||||
text = data.get('text')
|
text = data.get('text')
|
||||||
x_coord = data.get('x')
|
x_coord = data.get('x')
|
||||||
y_coord = data.get('y')
|
y_coord = data.get('y')
|
||||||
|
|
||||||
|
# call matrix method with data
|
||||||
matrix.text(x_coord, y_coord, text)
|
matrix.text(x_coord, y_coord, text)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# error handling
|
||||||
print(e)
|
print(e)
|
||||||
response['success'] = False
|
response['success'] = False
|
||||||
|
|
||||||
|
# respond to client
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
@api.route('/pixel', methods=['POST'])
|
@api.route('/pixel', methods=['POST'])
|
||||||
def set_pixel():
|
def set_pixel():
|
||||||
|
# prepare response data
|
||||||
response = { 'success': True }
|
response = { 'success': True }
|
||||||
try:
|
try:
|
||||||
global matrix
|
global matrix
|
||||||
|
|
||||||
|
# receive client data
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
# try unpacking x,y coordinates
|
||||||
x_coord = data.get('x')
|
x_coord = data.get('x')
|
||||||
y_coord = data.get('y')
|
y_coord = data.get('y')
|
||||||
|
|
||||||
|
# call matrix method with data
|
||||||
matrix.pixel(x_coord, y_coord)
|
matrix.pixel(x_coord, y_coord)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# error handling
|
||||||
print(e)
|
print(e)
|
||||||
response['success'] = False
|
response['success'] = False
|
||||||
|
|
||||||
|
# respond to client
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
@api.route('/circle', methods=['POST'])
|
@api.route('/circle', methods=['POST'])
|
||||||
def draw_circle():
|
def draw_circle():
|
||||||
|
# prepare response data
|
||||||
response = { 'success': True }
|
response = { 'success': True }
|
||||||
try:
|
try:
|
||||||
global matrix
|
global matrix
|
||||||
|
|
||||||
|
# receive client data
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
# try unpacking radius and x,y coordinates
|
||||||
radius = data.get('r')
|
radius = data.get('r')
|
||||||
x_coord = data.get('x')
|
x_coord = data.get('x')
|
||||||
y_coord = data.get('y')
|
y_coord = data.get('y')
|
||||||
|
|
||||||
|
# call matrix method with data
|
||||||
matrix.circle(x_coord, y_coord, radius)
|
matrix.circle(x_coord, y_coord, radius)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# error handling
|
||||||
print(e)
|
print(e)
|
||||||
response['success'] = False
|
response['success'] = False
|
||||||
|
|
||||||
|
# respond to client
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
@api.route('/rectangle', methods=['POST'])
|
@api.route('/rectangle', methods=['POST'])
|
||||||
def draw_rectangle():
|
def draw_rectangle():
|
||||||
|
# prepare response data
|
||||||
response = { 'success': True }
|
response = { 'success': True }
|
||||||
try:
|
try:
|
||||||
global matrix
|
global matrix
|
||||||
|
|
||||||
|
# receive client data
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
# try unpacking rectangle width and height
|
||||||
width = data.get('w')
|
width = data.get('w')
|
||||||
height = data.get('h')
|
height = data.get('h')
|
||||||
|
# try unpacking x,y coordinates
|
||||||
x_coord = data.get('x')
|
x_coord = data.get('x')
|
||||||
y_coord = data.get('y')
|
y_coord = data.get('y')
|
||||||
|
|
||||||
|
# call matrix method with data
|
||||||
matrix.rectangle(x_coord, y_coord, width, height)
|
matrix.rectangle(x_coord, y_coord, width, height)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# error handling
|
||||||
print(e)
|
print(e)
|
||||||
response['success'] = False
|
response['success'] = False
|
||||||
|
|
||||||
|
# respond to client
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
@api.route('/color', methods=['POST'])
|
@api.route('/color', methods=['POST'])
|
||||||
def set_color():
|
def set_color():
|
||||||
|
# prepare response data
|
||||||
response = { 'success': True }
|
response = { 'success': True }
|
||||||
try:
|
try:
|
||||||
global matrix
|
global matrix
|
||||||
|
|
||||||
|
# receive client data
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
# try unpacking red, green and blue color values
|
||||||
red = data.get('r')
|
red = data.get('r')
|
||||||
green = data.get('g')
|
green = data.get('g')
|
||||||
blue = data.get('b')
|
blue = data.get('b')
|
||||||
|
|
||||||
|
# call matrix method with data
|
||||||
matrix.set_color(red, green, blue)
|
matrix.set_color(red, green, blue)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# error handling
|
||||||
print(e)
|
print(e)
|
||||||
response['success'] = False
|
response['success'] = False
|
||||||
|
|
||||||
|
# respond to client
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
@api.route('/clear', methods=['GET'])
|
@api.route('/clear', methods=['GET'])
|
||||||
def update_matrix():
|
def update_matrix():
|
||||||
|
# prepare response data
|
||||||
response = { 'success': True }
|
response = { 'success': True }
|
||||||
|
|
||||||
try:
|
try:
|
||||||
global matrix
|
global matrix
|
||||||
|
|
||||||
|
# receive client data
|
||||||
matrix.clear()
|
matrix.clear()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# error handling
|
||||||
print(e)
|
print(e)
|
||||||
response['success'] = False
|
response['success'] = False
|
||||||
|
|
||||||
@ -115,7 +149,6 @@ def update_matrix():
|
|||||||
@api.route('/update', methods=['GET'])
|
@api.route('/update', methods=['GET'])
|
||||||
def update_matrix():
|
def update_matrix():
|
||||||
response = { 'success': True }
|
response = { 'success': True }
|
||||||
|
|
||||||
try:
|
try:
|
||||||
global matrix
|
global matrix
|
||||||
matrix.update()
|
matrix.update()
|
||||||
@ -123,53 +156,73 @@ def update_matrix():
|
|||||||
print(e)
|
print(e)
|
||||||
response['success'] = False
|
response['success'] = False
|
||||||
|
|
||||||
|
# respond to client
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
|
|
||||||
|
# derived class to access rgb-led-matrix
|
||||||
class Matrix(SampleBase):
|
class Matrix(SampleBase):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(Matrix, self).__init__(*args, **kwargs)
|
super(Matrix, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# run display program
|
||||||
def run(self):
|
def run(self):
|
||||||
|
# initialize font
|
||||||
self.font = graphics.Font()
|
self.font = graphics.Font()
|
||||||
self.font.LoadFont("deps/fonts/9x18B.bdf")
|
self.font.LoadFont("deps/fonts/9x18B.bdf")
|
||||||
|
# initialize color
|
||||||
self.color = graphics.Color(255, 255, 255)
|
self.color = graphics.Color(255, 255, 255)
|
||||||
|
|
||||||
|
# initialize canvas
|
||||||
self.canvas = self.matrix.CreateFrameCanvas()
|
self.canvas = self.matrix.CreateFrameCanvas()
|
||||||
self.canvas.Clear()
|
self.canvas.Clear()
|
||||||
|
|
||||||
|
# wait in idle
|
||||||
while True:
|
while True:
|
||||||
time.sleep(.05)
|
time.sleep(.05)
|
||||||
|
|
||||||
|
# clear canvas
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self.canvas.Clear()
|
self.canvas.Clear()
|
||||||
|
|
||||||
|
# swap canvas with buffer
|
||||||
def update(self):
|
def update(self):
|
||||||
self.canvas = self.matrix.SwapOnVSync(self.canvas)
|
self.canvas = self.matrix.SwapOnVSync(self.canvas)
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
|
# set current color
|
||||||
def set_color(self, r, g, b):
|
def set_color(self, r, g, b):
|
||||||
self.color = graphics.Color(r, g, b)
|
self.color = graphics.Color(r, g, b)
|
||||||
|
|
||||||
|
# set specified pixel to current color
|
||||||
def pixel(self, x, y):
|
def pixel(self, x, y):
|
||||||
self.canvas.SetPixel(x, y, self.color.red, self.color.green, self.color.blue)
|
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):
|
def text(self, x, y, t):
|
||||||
graphics.DrawText(self.canvas, self.font, x*9, (y+1)*18, self.color, 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):
|
def circle(self, x, y, r):
|
||||||
graphics.DrawCircle(self.canvas, self.font, x, y, r, self.color)
|
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):
|
def rectangle(self, x, y, w, h):
|
||||||
|
# loop through each point in dimensions
|
||||||
for i in range(w):
|
for i in range(w):
|
||||||
for j in range(h):
|
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)
|
self.canvas.SetPixel(x+i, y+j, self.color.red, self.color.green, self.color.blue)
|
||||||
|
|
||||||
|
|
||||||
|
# initialize global matrix variable
|
||||||
matrix = None
|
matrix = None
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# set matrix to instance
|
||||||
matrix = Matrix()
|
matrix = Matrix()
|
||||||
|
# run rest api in thread, serving with 'waitress'
|
||||||
threading.Thread(target=lambda: serve(api, host="0.0.0.0", port=8080)).start()
|
threading.Thread(target=lambda: serve(api, host="0.0.0.0", port=8080)).start()
|
||||||
|
|
||||||
|
# help menu on call
|
||||||
if not matrix.process():
|
if not matrix.process():
|
||||||
matrix.print_help()
|
matrix.print_help()
|
||||||
|
Loading…
Reference in New Issue
Block a user