dancefloor-monorepo/python/server.py

49 lines
939 B
Python
Executable File

#!/usr/bin/python3
from flask import Flask, request
import threading
import serial
app = Flask(__name__)
data = ""
lock = threading.Lock()
@app.route('/update', methods=['PUT'])
def update():
grid = request.json['data']
grid_string = prepare_grid(grid)
with lock:
data = grid_string
return grid_string
def prepare_grid(grid):
# follow down up down down up down
HEIGHT = 5
WIDTH = 6
out = ""
for x in range(WIDTH):
for y in (range(0,HEIGHT,1) if x%3 == 1 else range(HEIGHT-1,-1,-1)):
for r in range(3):
out += hex(grid[x][y][r])[2:] + 'n'
out += 'l'
out += 'q'
return out
def serial_func():
with serial.Serial('/dev/ttyACM0', 9600, timeout=1) as ser:
with lock:
if data != '':
serial.write(bytes(data, 'ascii'))
data = ''
serial_thread = threading.Thread(target=serial_func)
serial_thread.start()
if __name__ == '__main__':
app.run(host='0.0.0.0')