Update wled_serial.cpp (#2667)

Add Continuous Serial Streaming feature to wled_serial.

Co-authored-by: Christian Schwinne <dev.aircoookie@gmail.com>
This commit is contained in:
ChuckMash 2022-09-26 01:08:31 -07:00 committed by GitHub
parent b6adbc926f
commit 7cd9e8860d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,8 @@ enum class AdaState {
}; };
uint16_t currentBaud = 1152; //default baudrate 115200 (divided by 100) uint16_t currentBaud = 1152; //default baudrate 115200 (divided by 100)
bool continuousSendLED = false;
uint32_t lastUpdate = 0;
void updateBaudRate(uint32_t rate){ void updateBaudRate(uint32_t rate){
uint16_t rate100 = rate/100; uint16_t rate100 = rate/100;
@ -34,6 +36,37 @@ void updateBaudRate(uint32_t rate){
Serial.begin(rate); 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<used; i++) {
Serial.print(strip.getPixelColor(i));
if (i != used-1) Serial.write(',');
}
Serial.println("]");
}
}
// RGB LED data returned as bytes in TPM2 format. Faster, and slightly less easy to use on the other end.
void sendBytes(){
if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut) {
Serial.write(0xC9); Serial.write(0xDA);
uint16_t used = strip.getLengthTotal();
uint16_t len = used*3;
Serial.write(highByte(len));
Serial.write(lowByte(len));
for (uint16_t i=0; i < used; i++) {
uint32_t c = strip.getPixelColor(i);
Serial.write(qadd8(W(c), R(c))); //R, add white channel to RGB channels as a simple RGBW -> 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() void handleSerial()
{ {
if (pinManager.isPinAllocated(hardwareRX)) return; if (pinManager.isPinAllocated(hardwareRX)) return;
@ -71,31 +104,12 @@ void handleSerial()
} else if (next == 0xB6) {updateBaudRate(1000000); } else if (next == 0xB6) {updateBaudRate(1000000);
} else if (next == 0xB7) {updateBaudRate(1500000); } 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. } else if (next == 'l') {sendJSON(); // Send LED data as JSON Array
if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut){ } else if (next == 'L') {sendBytes(); // Send LED data as TPM2 Data Packet
uint16_t used = strip.getLengthTotal();
Serial.write('['); } else if (next == 'o') {continuousSendLED = false; // Disable Continuous Serial Streaming
for (uint16_t i=0; i<used; i+=1) { } else if (next == 'O') {continuousSendLED = true; // Enable Continuous Serial Streaming
Serial.print(strip.getPixelColor(i));
if (i != used-1) Serial.write(',');
}
Serial.println("]");
}
} else if (next == 'L') { //RGB LED data returned as bytes in tpm2 format. Faster, and slightly less easy to use on the other end.
if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut) {
Serial.write(0xC9); Serial.write(0xDA);
uint16_t used = strip.getLengthTotal();
uint16_t len = used*3;
Serial.write(highByte(len));
Serial.write(lowByte(len));
for (uint16_t i=0; i < used; i++) {
uint32_t c = strip.getPixelColor(i);
Serial.write(qadd8(W(c), R(c))); //R, add white channel to RGB channels as a simple RGBW -> 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 == '{') { //JSON API } else if (next == '{') { //JSON API
bool verboseResponse = false; bool verboseResponse = false;
if (!requestJSONBufferLock(16)) return; if (!requestJSONBufferLock(16)) return;
@ -177,7 +191,19 @@ void handleSerial()
} }
break; break;
} }
// All other received bytes will disable Continuous Serial Streaming
if (continuousSendLED && next != 'O'){
continuousSendLED = false;
}
Serial.read(); //discard the byte Serial.read(); //discard the byte
} }
#endif #endif
// If Continuous Serial Streaming is enabled, send new LED data as bytes
if (continuousSendLED && (lastUpdate != strip.getLastShow())){
sendBytes();
lastUpdate = strip.getLastShow();
}
} }