From 54f4658dae711ef092f935a37d231264d09033eb Mon Sep 17 00:00:00 2001 From: Christian Schwinne Date: Thu, 26 Aug 2021 11:04:27 +0200 Subject: [PATCH] Added JSON API over serial support (#2156) * Added JSON API over serial support * Disable Serial API if pin 3 is used Disable serial response if pin 1 is used --- CHANGELOG.md | 6 ++++++ wled00/wled.h | 2 +- wled00/wled_serial.cpp | 27 ++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b3ad8d0..219549ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ### Builds after release 0.12.0 +#### Build 2108250 + +- Added Sync groups (PR #2150) +- Added JSON API over Serial support +- Live color correction (PR #1902) + #### Build 2108180 - Fixed JSON IR remote not working with codes greater than 0xFFFFFF (fixes #2135) diff --git a/wled00/wled.h b/wled00/wled.h index f02ebcb0..411ca22a 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2108180 +#define VERSION 2108250 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index de4c3697..bcc5a061 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -21,6 +21,8 @@ enum class AdaState { void handleSerial() { + if (pinManager.isPinAllocated(3)) return; + #ifdef WLED_ENABLE_ADALIGHT static auto state = AdaState::Header_A; static uint16_t count = 0; @@ -32,13 +34,35 @@ void handleSerial() while (Serial.available() > 0) { yield(); - byte next = Serial.read(); + byte next = Serial.peek(); switch (state) { case AdaState::Header_A: if (next == 'A') state = AdaState::Header_d; else if (next == 0xC9) { //TPM2 start byte state = AdaState::TPM2_Header_Type; } + else if (next == '{') { //JSON API + 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()); + 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); + } + } break; case AdaState::Header_d: if (next == 'd') state = AdaState::Header_a; @@ -98,6 +122,7 @@ void handleSerial() } break; } + Serial.read(); //discard the byte } #endif }