light_maymays/microcontroller/test.ino

117 lines
2.6 KiB
C++

// #include "dmx_buffers.h"
unsigned long tic_loop = 0;
unsigned long time_since_last_sync;
const unsigned long SYNC_TIMEOUT = 1000;
const unsigned int FRAME_TIME = 20; // 20 ms -> 50 FPS
DMXTripleBuffer buffer;
bool packet_ready = true;
unsigned long pkt_count = 0, oof_ctr = 0, foo_ctr = 0;
void setup()
{
Serial.begin(500000); // USB
Serial.setRxBufferSize(512); // to fit the full DMX packet. (default is 128)
while (!Serial)
{
// spin until serial is up
}
// DBG
// Serial.println("SANITY CHECK AAAAAAAAAAAAAAAAAAAA");
// Serial.printf("drain_pos: %d\nstd::min(128, 512-drain_pos): %d\n", buffer.drain_pos, std::min((size_t) 128, 512-buffer.drain_pos));
Serial1.begin(250000, SERIAL_8N2); // DMX
while (!Serial1)
{
// spin until serial1 is up
}
// Serial.printf("Is this stupid? %d %X %X\n", Serial1.availableForWrite(), USS(0), USS(1));
Serial.println();
Serial.println("Sync.");
time_since_last_sync = millis();
}
void loop()
{
// output
if (packet_ready)
{
send_dmx_header();
packet_ready = false;
pkt_count += 1;
}
size_t n = Serial1.availableForWrite();
if (!n) {
oof_ctr += 1;
} else {
foo_ctr += 1;
}
size_t written = Serial1.write(
buffer.drain_buffer->data + buffer.drain_pos,
std::min(n, 512 - buffer.drain_pos));
buffer.drain_pos += written;
if (buffer.drain_pos == 512)
{
buffer.on_drain_complete();
packet_ready = true;
}
// input
n = Serial.available();
unsigned long now = millis();
if (!n)
{
if (now - time_since_last_sync > 1000)
{
buffer.fill_pos = 0;
Serial.println("Sync.");
// Serial.printf("(pkts: %lu, fill: %d, drain: %d, foo: %lu, oof: %lu)\n", pkt_count, buffer.fill_pos, buffer.drain_pos, foo_ctr, oof_ctr);
time_since_last_sync = now;
}
return;
}
time_since_last_sync = now;
size_t read = Serial.read(
buffer.fill_buffer->data + buffer.fill_pos,
std::min(n, 512 - buffer.fill_pos));
buffer.fill_pos += read;
if (buffer.fill_pos == 512)
{
buffer.on_fill_complete();
Serial.println("Ack.");
}
}
void send_dmx_header()
{
Serial1.flush();
Serial1.begin(90000, SERIAL_8N2);
// send the break as a "slow" byte
Serial1.write(0);
// switch back to the original baud rate
Serial1.flush();
Serial1.begin(250000, SERIAL_8N2);
Serial1.write(0); // Start-Byte
}