70 lines
1.5 KiB
C++
70 lines
1.5 KiB
C++
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
|
|
} |