From b1953681501c8b382b17e3da2b093332fca2a620 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sat, 18 Nov 2023 14:47:45 +0100 Subject: [PATCH] Hotfix for individual LED addressing --- src/lib/webserver.cpp | 81 ++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/src/lib/webserver.cpp b/src/lib/webserver.cpp index 9064547..588be10 100644 --- a/src/lib/webserver.cpp +++ b/src/lib/webserver.cpp @@ -713,57 +713,52 @@ void onApiLightsSetColor(AsyncWebServerRequest *request) void onApiLightsSetJson(AsyncWebServerRequest *request, JsonVariant &json) { - if (request->method() == HTTP_PATCH) { + JsonArray lights = json.as(); - JsonArray lights = json.as(); + if (lights.size() != pixels.numPixels()) + { + Serial.printf("Invalid values for LED set %d\n", lights.size()); + request->send(400); + return; + } - if (lights.size() != pixels.numPixels()) + for (uint i = 0; i < pixels.numPixels(); i++) + { + unsigned int red, green, blue; + + if (lights[i].containsKey("red") && lights[i].containsKey("green") && lights[i].containsKey("blue")) { - Serial.printf("Invalid values for LED set %d\n", lights.size()); + + red = lights[i]["red"].as(); + green = lights[i]["green"].as(); + blue = lights[i]["blue"].as(); + } + else if (lights[i].containsKey("hex")) + { + if (!sscanf(lights[i]["hex"].as().c_str(), "#%02X%02X%02X", &red, &green, &blue) == 3) + { + Serial.printf("Invalid hex for LED %d\n", i); + request->send(400); + return; + } + } + else + { + Serial.printf("No valid color for LED %d\n", i); request->send(400); return; } - for (uint i = 0; i < pixels.numPixels(); i++) - { - unsigned int red, green, blue; - - if (lights[i].containsKey("red") && lights[i].containsKey("green") && lights[i].containsKey("blue")) - { - - red = lights[i]["red"].as(); - green = lights[i]["green"].as(); - blue = lights[i]["blue"].as(); - } - else if (lights[i].containsKey("hex")) - { - if (!sscanf(lights[i]["hex"].as().c_str(), "#%02X%02X%02X", &red, &green, &blue) == 3) - { - Serial.printf("Invalid hex for LED %d\n", i); - request->send(400); - return; - } - } - else - { - Serial.printf("No valid color for LED %d\n", i); - request->send(400); - return; - } - - pixels.setPixelColor((pixels.numPixels() - i - 1), pixels.Color( - red, - green, - blue)); - } - - pixels.show(); - saveLedState(); - - request->send(200); - } else { - request->send(404); + pixels.setPixelColor((pixels.numPixels() - i - 1), pixels.Color( + red, + green, + blue)); } + + pixels.show(); + saveLedState(); + + request->send(200); } void onIndex(AsyncWebServerRequest *request) { request->send(LittleFS, "/index.html", String(), false); }