From e3d6c64bd21ec84663e56991095f50d1165bba1a Mon Sep 17 00:00:00 2001 From: waltem01 Date: Fri, 24 Nov 2023 07:06:43 +0100 Subject: [PATCH] receive formdata and convert to int --- API/main.py | 52 +++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/API/main.py b/API/main.py index b3d1be7..02dc1ad 100755 --- a/API/main.py +++ b/API/main.py @@ -20,11 +20,11 @@ def display_text(): global matrix # receive client data - data = request.get_json() + data = request.form # try unpacking text and x,y coordinates text = data.get('text') - x_coord = data.get('x') - y_coord = data.get('y') + x_coord = int(data.get('x')) + y_coord = int(data.get('y')) # call matrix method with data matrix.text(x_coord, y_coord, text) @@ -45,10 +45,10 @@ def set_pixel(): global matrix # receive client data - data = request.get_json() + data = request.form # try unpacking x,y coordinates - x_coord = data.get('x') - y_coord = data.get('y') + x_coord = int(data.get('x')) + y_coord = int(data.get('y')) # call matrix method with data matrix.pixel(x_coord, y_coord) @@ -69,11 +69,11 @@ def draw_circle(): global matrix # receive client data - data = request.get_json() + data = request.form # try unpacking radius and x,y coordinates - radius = data.get('r') - x_coord = data.get('x') - y_coord = data.get('y') + radius = int(data.get('r')) + x_coord = int(data.get('x')) + y_coord = int(data.get('y')) # call matrix method with data matrix.circle(x_coord, y_coord, radius) @@ -94,13 +94,13 @@ def draw_rectangle(): global matrix # receive client data - data = request.get_json() + data = request.form # try unpacking rectangle width and height - width = data.get('w') - height = data.get('h') + width = int(data.get('w')) + height = int(data.get('h')) # try unpacking x,y coordinates - x_coord = data.get('x') - y_coord = data.get('y') + x_coord = int(data.get('x')) + y_coord = int(data.get('y')) # call matrix method with data matrix.rectangle(x_coord, y_coord, width, height) @@ -121,11 +121,11 @@ def set_color(): global matrix # receive client data - data = request.get_json() + data = request.form # try unpacking red, green and blue color values - red = data.get('r') - green = data.get('g') - blue = data.get('b') + red = int(data.get('r')) + green = int(data.get('g')) + blue = int(data.get('b')) # call matrix method with data matrix.set_color(red, green, blue) @@ -145,7 +145,7 @@ def clear_canvas(): try: global matrix - # receive client data + # clear matrix canvas matrix.clear() except Exception as e: # error handling @@ -160,6 +160,8 @@ def update_matrix(): response = { 'success': True } try: global matrix + + # swap matrix canvas matrix.update() except Exception as e: print(e) @@ -200,23 +202,23 @@ class Matrix(SampleBase): self.clear() # set current color - def set_color(self, r, g, b): + def set_color(self, r: int, g: int, b: int): self.color = graphics.Color(r, g, b) # set specified pixel to current color - def pixel(self, x, y): + def pixel(self, x: int, y: int): 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: int, y: int, t: int): 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: int, y: int, r: int): 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: int, y: int, w: int, h: int): # loop through each point in dimensions for i in range(w): for j in range(h):