From f9e969e6e815362fb7284ba4e6e04daf70090fc7 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Mon, 30 Aug 2021 10:18:31 +0200 Subject: [PATCH] Cleanup --- microcontroller/test.ino | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/microcontroller/test.ino b/microcontroller/test.ino index 2231e22..6ae7007 100644 --- a/microcontroller/test.ino +++ b/microcontroller/test.ino @@ -19,7 +19,8 @@ } */ -struct InternalBuffer { +struct InternalBuffer +{ char data[512] = {0}; bool is_fresh = false; }; @@ -27,29 +28,32 @@ struct InternalBuffer { class DMXTripleBuffer { public: - - DMXTripleBuffer() { + DMXTripleBuffer() + { fill_buffer = &_buffers[0]; drain_buffer = &_buffers[1]; off_buffer = &_buffers[2]; } - - void on_fill_complete() { + + void on_fill_complete() + { fill_pos = 0; fill_buffer->is_fresh = true; std::swap(fill_buffer, off_buffer); } - - void on_drain_complete() { + + void on_drain_complete() + { drain_pos = 0; drain_buffer->is_fresh = false; - if (off_buffer->is_fresh) { + 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]; }; @@ -58,18 +62,17 @@ private: Some globals */ +DMXTripleBuffer buffer; // Triple buffer instance +bool packet_ready = true; // flag to write header + +// send a "Sync." every second if no data is coming in 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; - /* - main + setup + + initialize both Serial connections and write the initial "Sync." */ void setup() @@ -80,7 +83,7 @@ void setup() { // spin until serial is up } - + Serial1.begin(250000, SERIAL_8N2); // DMX while (!Serial1) { @@ -92,6 +95,15 @@ void setup() time_since_last_sync = millis(); } +/* + loop + + continuously poll Serial1 for writing and Serial for reading + only read/write as much as fits into the UART buffers to avoid blocking + + since Serial1 is set to 250000 baud, it should be able to write 50 DMX packets per second + */ + void loop() { @@ -101,7 +113,6 @@ void loop() { send_dmx_header(); packet_ready = false; - pkt_count += 1; } size_t n = Serial1.availableForWrite(); @@ -133,7 +144,7 @@ void loop() } return; } - + time_since_last_sync = now; size_t read = Serial.read(