From 3237a9bf6a6d634442089d00d800512a19791104 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Sun, 29 Aug 2021 11:44:33 +0200 Subject: [PATCH] Make MCU dumber --- microcontroller/ctrl.py | 29 +++++++++++------ microcontroller/test.ino | 70 +++++++++++++++++++++++----------------- 2 files changed, 60 insertions(+), 39 deletions(-) diff --git a/microcontroller/ctrl.py b/microcontroller/ctrl.py index 58f2479..b65d116 100644 --- a/microcontroller/ctrl.py +++ b/microcontroller/ctrl.py @@ -1,24 +1,35 @@ import serial +import time channels = [ 192, # pan 0, # tilt 134, # dimmer 255, # R - 255, # G - 255, # B + 0x88, # G + 0, # B 0, # W 1, # movement speed 0, # RST ] -ser = serial.Serial("/dev/ttyUSB0", 115200) +start_addr = 10 -payload = bytearray() -payload.extend(5 * [0]) -payload.extend(channels) +with serial.Serial("/dev/ttyUSB0", 115200) as ser: -ser.write(payload) -ser.flush() + payload = bytearray(512) + # payload.extend(channels) + payload[(start_addr - 1) : (start_addr - 1 + len(channels))] = channels -print(ser.read_all()) + print(payload) + + while True: + + ser.write(payload) + ser.flush() + + time.sleep(1/50) + + print(".") + + #print(ser.read_all()) diff --git a/microcontroller/test.ino b/microcontroller/test.ino index 373db9a..59b576e 100644 --- a/microcontroller/test.ino +++ b/microcontroller/test.ino @@ -1,54 +1,61 @@ unsigned long tic_loop = 0; -const unsigned int FRAME_TIME = 25; // 20 ms -> 50 FPS +const unsigned int FRAME_TIME = 20; // 20 ms -> 50 FPS -byte channels_buffer[512] = {0}; -byte read_buffer[9] = {0}; +const size_t UNIVERSE_SIZE = 512; + +byte channels_buffer[UNIVERSE_SIZE] = {0}; size_t bytes_read = 0; -const unsigned int START_ADDR = 10; -const unsigned int NUM_CHANNELS = 9; - -unsigned int bytes_to_write = START_ADDR + NUM_CHANNELS; - void setup() { Serial.begin(115200); // USB - - while (!Serial.available()); + + while (!Serial) + { + // spin until serial is up + } Serial.println(); - Serial.println("INIT"); + Serial.println("Init."); Serial1.begin(250000, SERIAL_8N2); // DMX - tic_loop = millis(); } void loop() { - update_buffer(); + bool packet_ready = update_buffer(); - // this section gets executed at a maximum rate of around 40Hz - if ((millis() - tic_loop) > FRAME_TIME) + if (packet_ready) { - tic_loop = millis(); - - send_dmx_header(); - Serial1.write(channels_buffer, bytes_to_write); + send_packet(); } - delay(1); } -void update_buffer() +void send_packet() { - if (!Serial.available()) return; - Serial.read(read_buffer + bytes_read, 1); - Serial.print("."); + send_dmx_header(); + Serial1.write(channels_buffer, UNIVERSE_SIZE); +} - bytes_read += 1; - if (bytes_read == NUM_CHANNELS) { +bool update_buffer() +{ + size_t n = Serial.available(); + if (!n) + return false; + int bytes_received = Serial.read(channels_buffer + bytes_read, std::min(n, UNIVERSE_SIZE - bytes_read)); + bytes_read += bytes_received; + + // Serial.printf("rx n=%d rec=%d total=%d\n", n, bytes_received, bytes_read); + + if (bytes_read == UNIVERSE_SIZE) + { bytes_read = 0; - memcpy(channels_buffer + START_ADDR - 1, read_buffer, NUM_CHANNELS); - Serial.println(); - Serial.println("Updated."); + // Serial.println(); + // Serial.println("Updated."); + return true; + } + else + { + return false; } } @@ -56,15 +63,18 @@ void send_dmx_header() { Serial1.flush(); Serial1.begin(90000, SERIAL_8N2); + /* while (Serial1.available()) Serial1.read(); + */ // send the break as a "slow" byte Serial1.write(0); // switch back to the original baud rate Serial1.flush(); Serial1.begin(250000, SERIAL_8N2); + /* while (Serial1.available()) Serial1.read(); - + */ Serial1.write(0); // Start-Byte } \ No newline at end of file