From f88a987204d257381d6b7bdcd92675eaf0f03383 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Sat, 28 Aug 2021 23:20:59 +0200 Subject: [PATCH] Add initial MCU software --- microcontroller/ctrl.py | 24 ++++++++ microcontroller/test.ino | 70 ++++++++++++++++++++++ microcontroller/works_static.ino.bak | 87 ++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 microcontroller/ctrl.py create mode 100644 microcontroller/test.ino create mode 100644 microcontroller/works_static.ino.bak diff --git a/microcontroller/ctrl.py b/microcontroller/ctrl.py new file mode 100644 index 0000000..58f2479 --- /dev/null +++ b/microcontroller/ctrl.py @@ -0,0 +1,24 @@ +import serial + +channels = [ + 192, # pan + 0, # tilt + 134, # dimmer + 255, # R + 255, # G + 255, # B + 0, # W + 1, # movement speed + 0, # RST +] + +ser = serial.Serial("/dev/ttyUSB0", 115200) + +payload = bytearray() +payload.extend(5 * [0]) +payload.extend(channels) + +ser.write(payload) +ser.flush() + +print(ser.read_all()) diff --git a/microcontroller/test.ino b/microcontroller/test.ino new file mode 100644 index 0000000..373db9a --- /dev/null +++ b/microcontroller/test.ino @@ -0,0 +1,70 @@ +unsigned long tic_loop = 0; +const unsigned int FRAME_TIME = 25; // 20 ms -> 50 FPS + +byte channels_buffer[512] = {0}; +byte read_buffer[9] = {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()); + Serial.println(); + Serial.println("INIT"); + + Serial1.begin(250000, SERIAL_8N2); // DMX + tic_loop = millis(); +} + +void loop() +{ + update_buffer(); + + // this section gets executed at a maximum rate of around 40Hz + if ((millis() - tic_loop) > FRAME_TIME) + { + tic_loop = millis(); + + send_dmx_header(); + Serial1.write(channels_buffer, bytes_to_write); + } + delay(1); +} + +void update_buffer() +{ + if (!Serial.available()) return; + Serial.read(read_buffer + bytes_read, 1); + Serial.print("."); + + bytes_read += 1; + if (bytes_read == NUM_CHANNELS) { + bytes_read = 0; + memcpy(channels_buffer + START_ADDR - 1, read_buffer, NUM_CHANNELS); + Serial.println(); + Serial.println("Updated."); + } +} + +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 diff --git a/microcontroller/works_static.ino.bak b/microcontroller/works_static.ino.bak new file mode 100644 index 0000000..ef1daea --- /dev/null +++ b/microcontroller/works_static.ino.bak @@ -0,0 +1,87 @@ +unsigned long tic_loop = 0; +const unsigned int FRAME_TIME = 25; // 20 ms -> 50 FPS + +byte channels_buffer[512] = {0}; +unsigned int bytes_to_write = 512; + +const unsigned int START_ADDR = 10; +const unsigned int NUM_CHANNELS = 9; + +byte dmx_data[] = { + 0, + 0, + 134, + 255, + 0, + 0, + 0, + 0, + 0, +}; + +void setup() +{ + Serial.begin(115200); // USB + + Serial1.begin(250000, SERIAL_8N2); // DMX + tic_loop = millis(); + + for (int i = 0; i < NUM_CHANNELS; ++i) + { + channels_buffer[START_ADDR - 1 + i] = dmx_data[i]; + } +} + +void loop() +{ + // update_buffer(); + + // this section gets executed at a maximum rate of around 40Hz + if ((millis() - tic_loop) > FRAME_TIME) + { + tic_loop = millis(); + + send_dmx_header(); + Serial1.write(channels_buffer, bytes_to_write); + } + delay(1); +} + +void update_buffer() +{ + int n = Serial.available(); + + if (n < 1) + return; + + n -= 1; + + if (Serial.read() == n) + { + Serial.read(channels_buffer, n); + bytes_to_write = n; + } + else + { + // incomplete + while (Serial.available()) + Serial.read(); + } +} + +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