Better python
- Improved arduino types - A more final python server
This commit is contained in:
parent
44faa12c83
commit
8b89bdeee6
@ -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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user