Better python

- Improved arduino types
- A more final python server
This commit is contained in:
Jake Hillion 2020-04-28 13:39:58 +01:00
parent 44faa12c83
commit 8b89bdeee6
2 changed files with 61 additions and 27 deletions

View File

@ -18,30 +18,30 @@ void setup() {
Serial.begin(9600); Serial.begin(9600);
} }
int hex; uint8_t hex;
uint8_t stored[3];
int rgb; int rgb;
int led; int led;
int stored[3];
void loop() { void loop() {
if (Serial.available() > 0) { if (Serial.available() > 0) {
int data; int data;
switch (data = Serial.read()) { switch (data = Serial.read()) {
case 'n': case 'r':
nextColour(); resetState();
break;
case 'l': // end of LED
nextLed();
break; break;
case 'q': case 'q':
complete(); complete();
break; break;
case 'r': case 'l': // end of LED
resetState(); nextLed();
break;
case 'n':
nextColour();
break; break;
default: default:
hex = (hex << 1) + charMap(data); hex = (hex << 4) + charMap(data);
break; break;
} }
} }
@ -54,10 +54,10 @@ void nextColour() {
void nextLed() { void nextLed() {
CRGB color = CRGB(stored[0], stored[1], stored[2]); CRGB color = CRGB(stored[0], stored[1], stored[2]);
int strip = led / NUM_SQUARES_PER_STRIP; int strip = led / NUM_SQUARES_PER_STRIP;
int start = (led * NUM_LEDS_PER_SQUARE) - (strip * NUM_LEDS_PER_STRIP); int start = (led * NUM_LEDS_PER_SQUARE) - (strip * NUM_LEDS_PER_STRIP);
for (int i = start; i < start + NUM_LEDS_PER_SQUARE; i++) { for (int i = start; i < start + NUM_LEDS_PER_SQUARE; i++) {
leds[strip][i] = color; leds[strip][i] = color;
} }
@ -75,8 +75,8 @@ void resetState() {
led = rgb = hex = 0; led = rgb = hex = 0;
} }
int charMap(char that) { uint8_t charMap(char c) {
switch (that) { switch (c) {
case '0': case '0':
return 0; return 0;
case '1': case '1':
@ -109,6 +109,7 @@ int charMap(char that) {
return 14; return 14;
case 'f': case 'f':
return 15; return 15;
default:
return 0;
} }
return -1;
} }

View File

@ -1,15 +1,48 @@
#!/usr/bin/python3 #!/usr/bin/python3
from flask import Flask, request
import threading
import serial import serial
with serial.Serial('/dev/ttyACM0') as ser: app = Flask(__name__)
while True:
for i in range(30): # selected LED data = ""
for j in range(30): # LED lock = threading.Lock()
for k in range(3): # colour
for l in range(2): @app.route('/update', methods=['PUT'])
ser.write(b'f' if (i==j and k==1) else b'0') def update():
ser.write(b'n') grid = request.json['data']
ser.write(b'l') grid_string = prepare_grid(grid)
ser.write(b'q')
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():
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)
serial_thread.start()
if __name__ == '__main__':
app.run(host='0.0.0.0')