diff --git a/arduino/arduino.ino b/arduino/arduino.ino index 5fa28c0..5ba69f8 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -18,30 +18,30 @@ void setup() { Serial.begin(9600); } -int hex; +uint8_t hex; +uint8_t stored[3]; + int rgb; int led; -int stored[3]; - void loop() { if (Serial.available() > 0) { int data; - switch (data = Serial.read()) { - case 'n': - nextColour(); - break; - case 'l': // end of LED - nextLed(); + switch (data = Serial.read()) { + case 'r': + resetState(); break; case 'q': complete(); break; - case 'r': - resetState(); + case 'l': // end of LED + nextLed(); + break; + case 'n': + nextColour(); break; default: - hex = (hex << 1) + charMap(data); + hex = (hex << 4) + charMap(data); break; } } @@ -54,10 +54,10 @@ void nextColour() { void nextLed() { CRGB color = CRGB(stored[0], stored[1], stored[2]); - + int strip = led / NUM_SQUARES_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++) { leds[strip][i] = color; } @@ -75,8 +75,8 @@ void resetState() { led = rgb = hex = 0; } -int charMap(char that) { - switch (that) { +uint8_t charMap(char c) { + switch (c) { case '0': return 0; case '1': @@ -109,6 +109,7 @@ int charMap(char that) { return 14; case 'f': return 15; + default: + return 0; } - return -1; } diff --git a/python/server.py b/python/server.py index 93a4c30..805149d 100755 --- a/python/server.py +++ b/python/server.py @@ -1,15 +1,48 @@ #!/usr/bin/python3 +from flask import Flask, request +import threading import serial -with serial.Serial('/dev/ttyACM0') as ser: - while True: - for i in range(30): # selected LED - for j in range(30): # LED - for k in range(3): # colour - for l in range(2): - ser.write(b'f' if (i==j and k==1) else b'0') - ser.write(b'n') - ser.write(b'l') - ser.write(b'q') +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(): + 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')