Skip to content

Commit 54f4658

Browse files
authored
Added JSON API over serial support (wled#2156)
* Added JSON API over serial support * Disable Serial API if pin 3 is used Disable serial response if pin 1 is used
1 parent dbc67e0 commit 54f4658

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
### Builds after release 0.12.0
44

5+
#### Build 2108250
6+
7+
- Added Sync groups (PR #2150)
8+
- Added JSON API over Serial support
9+
- Live color correction (PR #1902)
10+
511
#### Build 2108180
612

713
- Fixed JSON IR remote not working with codes greater than 0xFFFFFF (fixes #2135)

wled00/wled.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
// version code in format yymmddb (b = daily build)
11-
#define VERSION 2108180
11+
#define VERSION 2108250
1212

1313
//uncomment this if you have a "my_config.h" file you'd like to use
1414
//#define WLED_USE_MY_CONFIG

wled00/wled_serial.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ enum class AdaState {
2121

2222
void handleSerial()
2323
{
24+
if (pinManager.isPinAllocated(3)) return;
25+
2426
#ifdef WLED_ENABLE_ADALIGHT
2527
static auto state = AdaState::Header_A;
2628
static uint16_t count = 0;
@@ -32,13 +34,35 @@ void handleSerial()
3234
while (Serial.available() > 0)
3335
{
3436
yield();
35-
byte next = Serial.read();
37+
byte next = Serial.peek();
3638
switch (state) {
3739
case AdaState::Header_A:
3840
if (next == 'A') state = AdaState::Header_d;
3941
else if (next == 0xC9) { //TPM2 start byte
4042
state = AdaState::TPM2_Header_Type;
4143
}
44+
else if (next == '{') { //JSON API
45+
bool verboseResponse = false;
46+
{
47+
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
48+
Serial.setTimeout(100);
49+
DeserializationError error = deserializeJson(doc, Serial);
50+
if (error) return;
51+
fileDoc = &doc;
52+
verboseResponse = deserializeState(doc.as<JsonObject>());
53+
fileDoc = nullptr;
54+
}
55+
//only send response if TX pin is unused for other purposes
56+
if (verboseResponse && !pinManager.isPinAllocated(1)) {
57+
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
58+
JsonObject state = doc.createNestedObject("state");
59+
serializeState(state);
60+
JsonObject info = doc.createNestedObject("info");
61+
serializeInfo(info);
62+
63+
serializeJson(doc, Serial);
64+
}
65+
}
4266
break;
4367
case AdaState::Header_d:
4468
if (next == 'd') state = AdaState::Header_a;
@@ -98,6 +122,7 @@ void handleSerial()
98122
}
99123
break;
100124
}
125+
Serial.read(); //discard the byte
101126
}
102127
#endif
103128
}

0 commit comments

Comments
 (0)