This commit is contained in:
Kai Vogelgesang 2021-08-30 10:10:33 +02:00
parent 503690e9da
commit d76bfcafe8
Signed by: kai
GPG Key ID: 0A95D3B6E62C0879
2 changed files with 64 additions and 58 deletions

View File

@ -1,39 +0,0 @@
#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

View File

@ -1,6 +1,63 @@
// #include "dmx_buffers.h"
/*
Triple buffering data structure
Example writing:
----------------
DMXTripleBuffer buffer;
bytes_written = producer.write(
buffer.fill_buffer + buffer.fill_pos, // destination pointer
512 - buffer.fill_pos // maximum allowed
);
buffer.fill_pos += bytes_written;
if buffer.fill_pos == 512 {
buffer.on_fill_complete(); // swap buffers
buffer.fill_pos = 0; // reset fill pos
}
*/
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];
};
/*
Some globals
*/
unsigned long tic_loop = 0;
unsigned long time_since_last_sync;
const unsigned long SYNC_TIMEOUT = 1000;
@ -9,7 +66,11 @@ 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;
unsigned long pkt_count = 0;
/*
main
*/
void setup()
{
@ -20,21 +81,12 @@ void setup()
// 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();
@ -52,12 +104,6 @@ void loop()
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,
@ -83,7 +129,6 @@ void loop()
{
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;