Make MCU dumber

This commit is contained in:
Kai Vogelgesang 2021-08-29 11:44:33 +02:00
parent 3fea0fe57c
commit 3237a9bf6a
Signed by: kai
GPG Key ID: 0A95D3B6E62C0879
2 changed files with 60 additions and 39 deletions

View File

@ -1,24 +1,35 @@
import serial import serial
import time
channels = [ channels = [
192, # pan 192, # pan
0, # tilt 0, # tilt
134, # dimmer 134, # dimmer
255, # R 255, # R
255, # G 0x88, # G
255, # B 0, # B
0, # W 0, # W
1, # movement speed 1, # movement speed
0, # RST 0, # RST
] ]
ser = serial.Serial("/dev/ttyUSB0", 115200) start_addr = 10
payload = bytearray() with serial.Serial("/dev/ttyUSB0", 115200) as ser:
payload.extend(5 * [0])
payload.extend(channels)
ser.write(payload) payload = bytearray(512)
ser.flush() # 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())

View File

@ -1,54 +1,61 @@
unsigned long tic_loop = 0; 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}; const size_t UNIVERSE_SIZE = 512;
byte read_buffer[9] = {0};
byte channels_buffer[UNIVERSE_SIZE] = {0};
size_t bytes_read = 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() void setup()
{ {
Serial.begin(115200); // USB Serial.begin(115200); // USB
while (!Serial.available()); while (!Serial)
{
// spin until serial is up
}
Serial.println(); Serial.println();
Serial.println("INIT"); Serial.println("Init.");
Serial1.begin(250000, SERIAL_8N2); // DMX Serial1.begin(250000, SERIAL_8N2); // DMX
tic_loop = millis();
} }
void loop() void loop()
{ {
update_buffer(); bool packet_ready = update_buffer();
// this section gets executed at a maximum rate of around 40Hz if (packet_ready)
if ((millis() - tic_loop) > FRAME_TIME)
{ {
tic_loop = millis(); send_packet();
send_dmx_header();
Serial1.write(channels_buffer, bytes_to_write);
} }
delay(1);
} }
void update_buffer() void send_packet()
{ {
if (!Serial.available()) return; send_dmx_header();
Serial.read(read_buffer + bytes_read, 1); Serial1.write(channels_buffer, UNIVERSE_SIZE);
Serial.print("."); }
bytes_read += 1; bool update_buffer()
if (bytes_read == NUM_CHANNELS) { {
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; bytes_read = 0;
memcpy(channels_buffer + START_ADDR - 1, read_buffer, NUM_CHANNELS); // Serial.println();
Serial.println(); // Serial.println("Updated.");
Serial.println("Updated."); return true;
}
else
{
return false;
} }
} }
@ -56,15 +63,18 @@ void send_dmx_header()
{ {
Serial1.flush(); Serial1.flush();
Serial1.begin(90000, SERIAL_8N2); Serial1.begin(90000, SERIAL_8N2);
/*
while (Serial1.available()) while (Serial1.available())
Serial1.read(); Serial1.read();
*/
// send the break as a "slow" byte // send the break as a "slow" byte
Serial1.write(0); Serial1.write(0);
// switch back to the original baud rate // switch back to the original baud rate
Serial1.flush(); Serial1.flush();
Serial1.begin(250000, SERIAL_8N2); Serial1.begin(250000, SERIAL_8N2);
/*
while (Serial1.available()) while (Serial1.available())
Serial1.read(); Serial1.read();
*/
Serial1.write(0); // Start-Byte Serial1.write(0); // Start-Byte
} }