Implement triple buffering
This commit is contained in:
parent
738301d113
commit
503690e9da
1
microcontroller/.gitignore
vendored
1
microcontroller/.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.vscode
|
.vscode
|
||||||
.mypy_cache
|
.mypy_cache
|
||||||
venv
|
venv
|
||||||
|
build
|
39
microcontroller/dmx_buffers.h
Normal file
39
microcontroller/dmx_buffers.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#ifndef DMX_TRIPLE_BUFFER_H
|
||||||
|
#define DMX_TRIPLE_BUFFER_H
|
||||||
|
|
||||||
|
struct InternalBuffer {
|
||||||
|
char data[512] = {0};
|
||||||
|
bool is_fresh = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DMXTripleBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
DMXTripleBuffer() {
|
||||||
|
fill_buffer = &_buffers[0];
|
||||||
|
drain_buffer = &_buffers[1];
|
||||||
|
off_buffer = &_buffers[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_fill_complete() {
|
||||||
|
fill_pos = 0;
|
||||||
|
fill_buffer->is_fresh = true;
|
||||||
|
std::swap(fill_buffer, off_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_drain_complete() {
|
||||||
|
drain_pos = 0;
|
||||||
|
drain_buffer->is_fresh = false;
|
||||||
|
if (off_buffer->is_fresh) {
|
||||||
|
std::swap(drain_buffer, off_buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
InternalBuffer *fill_buffer, *drain_buffer, *off_buffer;
|
||||||
|
size_t fill_pos = 0, drain_pos = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
InternalBuffer _buffers[3];
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
117
microcontroller/test.ino
Normal file
117
microcontroller/test.ino
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
// #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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user