2020-03-31 00:38:08 +00:00
|
|
|
#include "wled.h"
|
|
|
|
|
|
|
|
/*
|
2020-05-22 21:30:55 +00:00
|
|
|
* Adalight and TPM2 handler
|
2020-03-31 00:38:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum class AdaState {
|
|
|
|
Header_A,
|
|
|
|
Header_d,
|
|
|
|
Header_a,
|
|
|
|
Header_CountHi,
|
|
|
|
Header_CountLo,
|
|
|
|
Header_CountCheck,
|
|
|
|
Data_Red,
|
|
|
|
Data_Green,
|
2020-05-22 21:30:55 +00:00
|
|
|
Data_Blue,
|
|
|
|
TPM2_Header_Type,
|
|
|
|
TPM2_Header_CountHi,
|
2021-11-16 22:20:26 +00:00
|
|
|
TPM2_Header_CountLo,
|
2020-03-31 00:38:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void handleSerial()
|
|
|
|
{
|
2021-08-26 09:04:27 +00:00
|
|
|
if (pinManager.isPinAllocated(3)) return;
|
|
|
|
|
2020-03-31 00:38:08 +00:00
|
|
|
#ifdef WLED_ENABLE_ADALIGHT
|
|
|
|
static auto state = AdaState::Header_A;
|
|
|
|
static uint16_t count = 0;
|
|
|
|
static uint16_t pixel = 0;
|
|
|
|
static byte check = 0x00;
|
|
|
|
static byte red = 0x00;
|
|
|
|
static byte green = 0x00;
|
|
|
|
|
|
|
|
while (Serial.available() > 0)
|
|
|
|
{
|
|
|
|
yield();
|
2021-08-26 09:04:27 +00:00
|
|
|
byte next = Serial.peek();
|
2020-03-31 00:38:08 +00:00
|
|
|
switch (state) {
|
|
|
|
case AdaState::Header_A:
|
|
|
|
if (next == 'A') state = AdaState::Header_d;
|
2020-05-23 14:09:49 +00:00
|
|
|
else if (next == 0xC9) { //TPM2 start byte
|
2020-05-22 21:30:55 +00:00
|
|
|
state = AdaState::TPM2_Header_Type;
|
|
|
|
}
|
2021-11-16 22:20:26 +00:00
|
|
|
else if (next == 'I') {
|
|
|
|
handleImprovPacket();
|
|
|
|
return;
|
|
|
|
} else if (next == 'v') {
|
|
|
|
Serial.print("WLED"); Serial.write(' '); Serial.println(VERSION);
|
|
|
|
} else if (next == '{') { //JSON API
|
2021-08-26 09:04:27 +00:00
|
|
|
bool verboseResponse = false;
|
|
|
|
{
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
Serial.setTimeout(100);
|
|
|
|
DeserializationError error = deserializeJson(doc, Serial);
|
|
|
|
if (error) return;
|
|
|
|
fileDoc = &doc;
|
|
|
|
verboseResponse = deserializeState(doc.as<JsonObject>());
|
|
|
|
fileDoc = nullptr;
|
|
|
|
}
|
|
|
|
//only send response if TX pin is unused for other purposes
|
|
|
|
if (verboseResponse && !pinManager.isPinAllocated(1)) {
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
JsonObject state = doc.createNestedObject("state");
|
|
|
|
serializeState(state);
|
|
|
|
JsonObject info = doc.createNestedObject("info");
|
|
|
|
serializeInfo(info);
|
|
|
|
|
|
|
|
serializeJson(doc, Serial);
|
2021-11-16 22:20:26 +00:00
|
|
|
Serial.println();
|
2021-08-26 09:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 00:38:08 +00:00
|
|
|
break;
|
|
|
|
case AdaState::Header_d:
|
|
|
|
if (next == 'd') state = AdaState::Header_a;
|
|
|
|
else state = AdaState::Header_A;
|
|
|
|
break;
|
|
|
|
case AdaState::Header_a:
|
|
|
|
if (next == 'a') state = AdaState::Header_CountHi;
|
|
|
|
else state = AdaState::Header_A;
|
|
|
|
break;
|
|
|
|
case AdaState::Header_CountHi:
|
|
|
|
pixel = 0;
|
|
|
|
count = next * 0x100;
|
|
|
|
check = next;
|
|
|
|
state = AdaState::Header_CountLo;
|
|
|
|
break;
|
|
|
|
case AdaState::Header_CountLo:
|
|
|
|
count += next + 1;
|
|
|
|
check = check ^ next ^ 0x55;
|
|
|
|
state = AdaState::Header_CountCheck;
|
|
|
|
break;
|
|
|
|
case AdaState::Header_CountCheck:
|
|
|
|
if (check == next) state = AdaState::Data_Red;
|
|
|
|
else state = AdaState::Header_A;
|
|
|
|
break;
|
2020-05-22 21:30:55 +00:00
|
|
|
case AdaState::TPM2_Header_Type:
|
|
|
|
state = AdaState::Header_A; //(unsupported) TPM2 command or invalid type
|
|
|
|
if (next == 0xDA) state = AdaState::TPM2_Header_CountHi; //TPM2 data
|
|
|
|
else if (next == 0xAA) Serial.write(0xAC); //TPM2 ping
|
|
|
|
break;
|
|
|
|
case AdaState::TPM2_Header_CountHi:
|
|
|
|
pixel = 0;
|
|
|
|
count = (next * 0x100) /3;
|
|
|
|
state = AdaState::TPM2_Header_CountLo;
|
|
|
|
break;
|
|
|
|
case AdaState::TPM2_Header_CountLo:
|
2020-05-23 14:09:49 +00:00
|
|
|
count += next /3;
|
2020-05-22 21:30:55 +00:00
|
|
|
state = AdaState::Data_Red;
|
|
|
|
break;
|
2020-03-31 00:38:08 +00:00
|
|
|
case AdaState::Data_Red:
|
|
|
|
red = next;
|
|
|
|
state = AdaState::Data_Green;
|
|
|
|
break;
|
|
|
|
case AdaState::Data_Green:
|
|
|
|
green = next;
|
|
|
|
state = AdaState::Data_Blue;
|
|
|
|
break;
|
|
|
|
case AdaState::Data_Blue:
|
|
|
|
byte blue = next;
|
2020-04-29 23:52:36 +00:00
|
|
|
if (!realtimeOverride) setRealtimePixel(pixel++, red, green, blue, 0);
|
2020-03-31 00:38:08 +00:00
|
|
|
if (--count > 0) state = AdaState::Data_Red;
|
|
|
|
else {
|
|
|
|
if (!realtimeMode && bri == 0) strip.setBrightness(briLast);
|
2020-04-12 22:42:27 +00:00
|
|
|
realtimeLock(realtimeTimeoutMs, REALTIME_MODE_ADALIGHT);
|
2020-03-31 00:38:08 +00:00
|
|
|
|
2020-04-29 23:52:36 +00:00
|
|
|
if (!realtimeOverride) strip.show();
|
2020-03-31 00:38:08 +00:00
|
|
|
state = AdaState::Header_A;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-08-26 09:04:27 +00:00
|
|
|
Serial.read(); //discard the byte
|
2020-03-31 00:38:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|