From 10fc9fe268cbcfd6d264a1915c5acd210914b4f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Kristan?= Date: Tue, 2 Nov 2021 13:12:14 +0100 Subject: [PATCH] Updated usermods for WLED-SR FX mode names. --- .../usermod.cpp | 54 ++---------------- .../usermod_bme280.cpp | 54 ++---------------- usermods/ST7789_display/ST7789_display.h | 30 +--------- usermods/TTGO-T-Display/usermod.cpp | 57 +++---------------- .../usermod.cpp | 54 ++---------------- .../usermod_bme280.cpp | 54 ++---------------- .../usermod_v2_four_line_display.h | 26 +-------- wled00/fcn_declare.h | 2 + wled00/json.cpp | 39 ++++++++++++- 9 files changed, 71 insertions(+), 299 deletions(-) diff --git a/usermods/Enclosure_with_OLED_temp_ESP07/usermod.cpp b/usermods/Enclosure_with_OLED_temp_ESP07/usermod.cpp index 9724a1b7..c4359a38 100644 --- a/usermods/Enclosure_with_OLED_temp_ESP07/usermod.cpp +++ b/usermods/Enclosure_with_OLED_temp_ESP07/usermod.cpp @@ -148,58 +148,14 @@ void userLoop() { // Third row with mode name u8x8.setCursor(2, 2); - uint8_t qComma = 0; - bool insideQuotes = false; - uint8_t printedChars = 0; - char singleJsonSymbol; + char lineBuffer[17]; + extractModeName(knownMode, JSON_mode_names, lineBuffer, 16); + u8x8.print(lineBuffer); - // Find the mode name in JSON - for (size_t i = 0; i < strlen_P(JSON_mode_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_mode_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownMode)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } // Fourth row with palette name u8x8.setCursor(2, 3); - qComma = 0; - insideQuotes = false; - printedChars = 0; - // Looking for palette name in JSON. - for (size_t i = 0; i < strlen_P(JSON_palette_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_palette_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownPalette)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } + extractModeName(knownPalette, JSON_palette_names, lineBuffer, 16); + u8x8.print(lineBuffer); u8x8.setFont(u8x8_font_open_iconic_embedded_1x1); u8x8.drawGlyph(0, 0, 80); // wifi icon diff --git a/usermods/Enclosure_with_OLED_temp_ESP07/usermod_bme280.cpp b/usermods/Enclosure_with_OLED_temp_ESP07/usermod_bme280.cpp index c39c74d2..25bcf43a 100644 --- a/usermods/Enclosure_with_OLED_temp_ESP07/usermod_bme280.cpp +++ b/usermods/Enclosure_with_OLED_temp_ESP07/usermod_bme280.cpp @@ -191,58 +191,14 @@ void userLoop() { // Third row with mode name u8x8.setCursor(2, 2); - uint8_t qComma = 0; - bool insideQuotes = false; - uint8_t printedChars = 0; - char singleJsonSymbol; + char lineBuffer[17]; + extractModeName(knownMode, JSON_mode_names, lineBuffer, 16); + u8x8.print(lineBuffer); - // Find the mode name in JSON - for (size_t i = 0; i < strlen_P(JSON_mode_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_mode_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownMode)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } // Fourth row with palette name u8x8.setCursor(2, 3); - qComma = 0; - insideQuotes = false; - printedChars = 0; - // Looking for palette name in JSON. - for (size_t i = 0; i < strlen_P(JSON_palette_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_palette_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownPalette)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } + extractModeName(knownPalette, JSON_palette_names, lineBuffer, 16); + u8x8.print(lineBuffer); u8x8.setFont(u8x8_font_open_iconic_embedded_1x1); u8x8.drawGlyph(0, 0, 80); // wifi icon diff --git a/usermods/ST7789_display/ST7789_display.h b/usermods/ST7789_display/ST7789_display.h index b2ee8d5c..bfa2bbee 100644 --- a/usermods/ST7789_display/ST7789_display.h +++ b/usermods/ST7789_display/ST7789_display.h @@ -285,33 +285,9 @@ class St7789DisplayUsermod : public Usermod { // mode name tft.setTextColor(TFT_CYAN); tft.setCursor(0, 144); - uint8_t qComma = 0; - bool insideQuotes = false; - uint8_t printedChars = 0; - char singleJsonSymbol; - // Find the mode name in JSON - for (size_t i = 0; i < strlen_P(JSON_mode_names); i++) - { - singleJsonSymbol = pgm_read_byte_near(JSON_mode_names + i); - switch (singleJsonSymbol) - { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownMode)) - break; - tft.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > tftcharwidth - 1)) - break; - } + char lineBuffer[tftcharwidth+1]; + extractModeName(knownMode, JSON_mode_names, lineBuffer, tftcharwidth); + tft.print(lineBuffer); // palette name tft.setTextColor(TFT_YELLOW); diff --git a/usermods/TTGO-T-Display/usermod.cpp b/usermods/TTGO-T-Display/usermod.cpp index 75e90b1e..b0670f96 100644 --- a/usermods/TTGO-T-Display/usermod.cpp +++ b/usermods/TTGO-T-Display/usermod.cpp @@ -177,58 +177,15 @@ void userLoop() { // Third row with mode name tft.setCursor(1, 68); - uint8_t qComma = 0; - bool insideQuotes = false; - uint8_t printedChars = 0; - char singleJsonSymbol; - // Find the mode name in JSON - for (size_t i = 0; i < strlen_P(JSON_mode_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_mode_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownMode)) - break; - tft.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > tftcharwidth - 1)) - break; - } + char lineBuffer[tftcharwidth+1]; + extractModeName(knownMode, JSON_mode_names, lineBuffer, tftcharwidth); + tft.print(lineBuffer); + // Fourth row with palette name tft.setCursor(1, 90); - qComma = 0; - insideQuotes = false; - printedChars = 0; - // Looking for palette name in JSON. - for (size_t i = 0; i < strlen_P(JSON_palette_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_palette_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownPalette)) - break; - tft.print(singleJsonSymbol); - printedChars++; - } - // The following is modified from the code from the u8g2/u8g8 based code (knownPalette was knownMode) - if ((qComma > knownPalette) || (printedChars > tftcharwidth - 1)) - break; - } + extractModeName(knownPalette, JSON_palette_names, lineBuffer, tftcharwidth); + tft.print(lineBuffer); + // Fifth row with estimated mA usage tft.setCursor(1, 112); // Print estimated milliamp usage (must specify the LED type in LED prefs for this to be a reasonable estimate). diff --git a/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod.cpp b/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod.cpp index a93b20c9..f5c25fb5 100644 --- a/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod.cpp +++ b/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod.cpp @@ -185,58 +185,14 @@ void userLoop() { // Third row with mode name u8x8.setCursor(2, 2); - uint8_t qComma = 0; - bool insideQuotes = false; - uint8_t printedChars = 0; - char singleJsonSymbol; + char lineBuffer[17]; + extractModeName(knownMode, JSON_mode_names, lineBuffer, 16); + u8x8.print(lineBuffer); - // Find the mode name in JSON - for (size_t i = 0; i < strlen_P(JSON_mode_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_mode_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownMode)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } // Fourth row with palette name u8x8.setCursor(2, 3); - qComma = 0; - insideQuotes = false; - printedChars = 0; - // Looking for palette name in JSON. - for (size_t i = 0; i < strlen_P(JSON_palette_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_palette_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownPalette)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } + extractModeName(knownPalette, JSON_palette_names, lineBuffer, 16); + u8x8.print(lineBuffer); u8x8.setFont(u8x8_font_open_iconic_embedded_1x1); u8x8.drawGlyph(0, 0, 80); // wifi icon diff --git a/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod_bme280.cpp b/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod_bme280.cpp index 15ec58ad..50a2833b 100644 --- a/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod_bme280.cpp +++ b/usermods/Wemos_D1_mini+Wemos32_mini_shield/usermod_bme280.cpp @@ -191,58 +191,14 @@ void userLoop() { // Third row with mode name u8x8.setCursor(2, 2); - uint8_t qComma = 0; - bool insideQuotes = false; - uint8_t printedChars = 0; - char singleJsonSymbol; + char lineBuffer[17]; + extractModeName(knownMode, JSON_mode_names, lineBuffer, 16); + u8x8.print(lineBuffer); - // Find the mode name in JSON - for (size_t i = 0; i < strlen_P(JSON_mode_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_mode_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownMode)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } // Fourth row with palette name u8x8.setCursor(2, 3); - qComma = 0; - insideQuotes = false; - printedChars = 0; - // Looking for palette name in JSON. - for (size_t i = 0; i < strlen_P(JSON_palette_names); i++) { - singleJsonSymbol = pgm_read_byte_near(JSON_palette_names + i); - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownPalette)) - break; - u8x8.print(singleJsonSymbol); - printedChars++; - } - if ((qComma > knownMode) || (printedChars > u8x8.getCols() - 2)) - break; - } + extractModeName(knownPalette, JSON_palette_names, lineBuffer, 16); + u8x8.print(lineBuffer); u8x8.setFont(u8x8_font_open_iconic_embedded_1x1); u8x8.drawGlyph(0, 0, 80); // wifi icon diff --git a/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h b/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h index aaa96764..8c254e40 100644 --- a/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h +++ b/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h @@ -470,30 +470,8 @@ class FourLineDisplayUsermod : public Usermod { */ void showCurrentEffectOrPalette(int knownMode, const char *qstring, uint8_t row) { char lineBuffer[LINE_BUFFER_SIZE]; - uint8_t qComma = 0; - bool insideQuotes = false; - uint8_t printedChars = 0; - char singleJsonSymbol; - - // Find the mode name in JSON - for (size_t i = 0; i < strlen_P(qstring); i++) { - singleJsonSymbol = pgm_read_byte_near(qstring + i); - if (singleJsonSymbol == '\0') break; - switch (singleJsonSymbol) { - case '"': - insideQuotes = !insideQuotes; - break; - case '[': - case ']': - break; - case ',': - qComma++; - default: - if (!insideQuotes || (qComma != knownMode)) break; - lineBuffer[printedChars++] = singleJsonSymbol; - } - if ((qComma > knownMode) || (printedChars >= getCols()-2) || printedChars >= sizeof(lineBuffer)-2) break; - } + extractModeName(knownMode, qstring, lineBuffer, LINE_BUFFER_SIZE-1); + uint8_t printedChars = strlen(lineBuffer); for (;printedChars < getCols()-2 && printedChars < sizeof(lineBuffer)-2; printedChars++) lineBuffer[printedChars]=' '; lineBuffer[printedChars] = 0; drawString(2, row*lineHeight, lineBuffer); diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 3f4807f2..77eb2124 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -128,6 +128,8 @@ bool deserializeState(JsonObject root, byte callMode = CALL_MODE_DIRECT_CHANGE, void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool forPreset = false, bool segmentBounds = true); void serializeState(JsonObject root, bool forPreset = false, bool includeBri = true, bool segmentBounds = true); void serializeInfo(JsonObject root); +void serializeSRNames(JsonArray arr, const char *qstring); +void extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen); void serveJson(AsyncWebServerRequest* request); bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient = 0); diff --git a/wled00/json.cpp b/wled00/json.cpp index 72999d65..af6579ca 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -796,7 +796,9 @@ void serializeNodes(JsonObject root) } } -void serializeSRNames(JsonVariant arr, const char *qstring) { +// deserializes mode names string into JsonArray +// also removes WLED-SR extensions (@...) from deserialised names +void deserializeModeNames(JsonArray arr, const char *qstring) { String lineBuffer; bool insideQuotes = false; char singleJsonSymbol; @@ -827,6 +829,39 @@ void serializeSRNames(JsonVariant arr, const char *qstring) { } } +// extracts effect mode (or palette) name from names serialized string +// caller must provide large enough buffer! +void extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen) +{ + uint8_t qComma = 0; + bool insideQuotes = false; + uint8_t printedChars = 0; + char singleJsonSymbol; + + // Find the mode name in JSON + for (size_t i = 0; i < strlen_P(src); i++) { + singleJsonSymbol = pgm_read_byte_near(src + i); + if (singleJsonSymbol == '\0') break; + switch (singleJsonSymbol) { + case '"': + insideQuotes = !insideQuotes; + break; + case '[': + case ']': + break; + case ',': + qComma++; + default: + if (!insideQuotes || (qComma != mode)) break; + dest[printedChars++] = singleJsonSymbol; + } + if ((qComma > mode) || (printedChars >= maxLen)) break; + } + dest[printedChars] = '\0'; + char *p = strchr(dest,'@'); + if (p != nullptr) *p = '\0'; +} + void serveJson(AsyncWebServerRequest* request) { byte subJson = 0; @@ -879,7 +914,7 @@ void serveJson(AsyncWebServerRequest* request) { //doc[F("effects")] = serialized((const __FlashStringHelper*)JSON_mode_names); JsonArray effects = doc.createNestedArray(F("effects")); - serializeSRNames(effects, JSON_mode_names); // remove WLED-SR extensions from effect names + deserializeModeNames(effects, JSON_mode_names); // remove WLED-SR extensions from effect names doc[F("palettes")] = serialized((const __FlashStringHelper*)JSON_palette_names); } }