From 7cd9e8860de527b7116e028ee62f9e8d82c7422b Mon Sep 17 00:00:00 2001 From: ChuckMash <86080247+ChuckMash@users.noreply.github.com> Date: Mon, 26 Sep 2022 01:08:31 -0700 Subject: [PATCH] Update wled_serial.cpp (#2667) Add Continuous Serial Streaming feature to wled_serial. Co-authored-by: Christian Schwinne --- wled00/wled_serial.cpp | 80 ++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index f6323a06..2f457887 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -20,6 +20,8 @@ enum class AdaState { }; uint16_t currentBaud = 1152; //default baudrate 115200 (divided by 100) +bool continuousSendLED = false; +uint32_t lastUpdate = 0; void updateBaudRate(uint32_t rate){ uint16_t rate100 = rate/100; @@ -33,7 +35,38 @@ void updateBaudRate(uint32_t rate){ Serial.flush(); Serial.begin(rate); } - + +// RGB LED data return as JSON array. Slow, but easy to use on the other end. +void sendJSON(){ + if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut) { + uint16_t used = strip.getLengthTotal(); + Serial.write('['); + for (uint16_t i=0; i RGB map + Serial.write(qadd8(W(c), G(c))); //G + Serial.write(qadd8(W(c), B(c))); //B + } + Serial.write(0x36); Serial.write('\n'); + } +} + void handleSerial() { if (pinManager.isPinAllocated(hardwareRX)) return; @@ -70,32 +103,13 @@ void handleSerial() } else if (next == 0xB5) {updateBaudRate( 921600); } else if (next == 0xB6) {updateBaudRate(1000000); } else if (next == 0xB7) {updateBaudRate(1500000); - - } else if (next == 'l') { //RGB(W) LED data return as JSON array. Slow, but easy to use on the other end. - if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut){ - uint16_t used = strip.getLengthTotal(); - Serial.write('['); - for (uint16_t i=0; i RGB map - Serial.write(qadd8(W(c), G(c))); //G - Serial.write(qadd8(W(c), B(c))); //B - } - Serial.write(0x36); Serial.write('\n'); - } + + } else if (next == 'l') {sendJSON(); // Send LED data as JSON Array + } else if (next == 'L') {sendBytes(); // Send LED data as TPM2 Data Packet + + } else if (next == 'o') {continuousSendLED = false; // Disable Continuous Serial Streaming + } else if (next == 'O') {continuousSendLED = true; // Enable Continuous Serial Streaming + } else if (next == '{') { //JSON API bool verboseResponse = false; if (!requestJSONBufferLock(16)) return; @@ -177,7 +191,19 @@ void handleSerial() } break; } + + // All other received bytes will disable Continuous Serial Streaming + if (continuousSendLED && next != 'O'){ + continuousSendLED = false; + } + Serial.read(); //discard the byte } #endif + + // If Continuous Serial Streaming is enabled, send new LED data as bytes + if (continuousSendLED && (lastUpdate != strip.getLastShow())){ + sendBytes(); + lastUpdate = strip.getLastShow(); + } }