Manueller Modus: einzelne Pixel per API setzen (/api/pixels)

- PUT /api/pixels: alle 125 Pixel, Index x + 5*y + 25*z
- PATCH /api/pixels: optional fill, dann einzelne Pixel mit x, y, z
- GET /api/pixels: aktuelles Bild
- Schreiben schaltet den manuellen Modus ein: Farben unverändert (nur die
  Strombegrenzung dimmt), kein Autowechsel, keine Klatsch-Blitze; ein
  Effektstart oder {"manual": false} in /api/state beendet ihn
- Nicht im Flash gespeichert, damit Skripte viele Bilder schicken können
- App zeigt den manuellen Modus in der Statuszeile

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mikeandClaude Opus 5.5 committed 2026-11-17 13:56:23 +01:00
1 parent ff1fdeaf4f
commit ffac7d6ff2
8 files changed
+193 -8

No files matched your search

+81 -1
View File
@@ -8,6 +8,10 @@
// GET /api/categories alle Kategorien: [{"id": "flash", "name": "Flash"}, ...]
// GET /api/effects alle Effekte: [{"id": "gewitter", "name": "Gewitter", "category": "natur"}, ...]
// GET /api/weather zuletzt abgerufenes Wetter am eingestellten Ort
// GET /api/pixels Bild des manuellen Modus: {"manual": true, "pixels": ["#rrggbb", ...]}
// PUT /api/pixels alle 125 Pixel: {"pixels": ["#ff0000", ...]}, Index x + 5*y + 25*z
// PATCH /api/pixels einzelne Pixel: {"fill": "#000000", "pixels": [{"x": 0, "y": 0, "z": 0, "color": "#ff0000"}]}
// Beide schalten den manuellen Modus ein (Effekt wählen beendet ihn).
// POST /api/specials/{id} Spezialeffekt auslösen: blitz, explosion, regenbogen
// POST /api/lightning Blitz auslösen (wie /api/specials/blitz)
//
@@ -46,6 +50,7 @@ void sendState() {
doc["auto"] = autoAdvance;
doc["allCategories"] = autoAll;
doc["playFavorites"] = playFavorites;
doc["manual"] = manualMode;
doc["brightness"] = brightness;
JsonArray favs = doc["favorites"].to<JsonArray>();
for (uint8_t k = 0; k < numFavorites(); k++) favs.add(effectId(favorite(k)));
@@ -121,7 +126,7 @@ void updateState() {
JsonVariant effect = body["effect"], autoOn = body["auto"], all = body["allCategories"], level = body["brightness"];
JsonVariant playFav = body["playFavorites"], favs = body["favorites"], lamp = body["lampColor"], text = body["text"];
JsonVariant place = body["place"], lat = body["latitude"], lon = body["longitude"];
JsonVariant place = body["place"], lat = body["latitude"], lon = body["longitude"], manual = body["manual"];
int index = -1;
if (!effect.isNull()) {
if (!effect.is<const char *>()) return sendError(400, "effect muss eine Effekt-id sein, siehe /api/effects");
@@ -133,6 +138,7 @@ void updateState() {
if (!level.isNull() && !(level.is<int>() && level.as<int>() >= 0 && level.as<int>() <= 255))
return sendError(400, "brightness muss eine ganze Zahl von 0 bis 255 sein");
if (!playFav.isNull() && !playFav.is<bool>()) return sendError(400, "playFavorites muss true oder false sein");
if (!manual.isNull() && !manual.is<bool>()) return sendError(400, "manual muss true oder false sein");
uint8_t favList[MAX_FAVORITES], favCount = 0;
if (!favs.isNull()) {
if (!favs.is<JsonArray>()) return sendError(400, "favorites muss eine Liste von Effekt-ids sein");
@@ -169,10 +175,81 @@ void updateState() {
if (!lamp.isNull()) lampColor = lampNew;
if (!text.isNull()) tickerText = text.as<const char *>();
if (!place.isNull()) setLocation(place.as<const char *>(), lat.as<float>(), lon.as<float>());
if (!manual.isNull()) setManual(manual.as<bool>());
settingsChanged();
sendState();
}
void sendPixels() {
JsonDocument doc;
doc["manual"] = manualMode;
JsonArray pixels = doc["pixels"].to<JsonArray>();
char hex[8];
for (uint8_t z = 0; z < NUM_LAYERS; z++)
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) {
const CRGB &c = manualFrame[z][y][x];
snprintf(hex, sizeof(hex), "#%02x%02x%02x", c.r, c.g, c.b);
pixels.add(hex);
}
sendJson(200, doc);
}
// PUT (full): alle 125 Pixel als Liste von Farben. PATCH: erst "fill", dann
// einzelne Pixel mit x, y, z. Wie bei /api/state erst alles prüfen, dann übernehmen.
// Absichtlich nicht im Flash gespeichert: ein Skript darf viele Bilder pro Sekunde schicken.
void updatePixels(bool full) {
if (!server.hasArg("plain")) return sendError(415, "Body als JSON mit Content-Type: application/json senden");
JsonDocument body;
if (deserializeJson(body, server.arg("plain")) || !body.is<JsonObject>())
return sendError(400, "JSON-Objekt erwartet");
JsonVariant pixels = body["pixels"], fill = body["fill"];
CRGB fillColor;
if (!fill.isNull() && !(fill.is<const char *>() && parseColor(fill.as<const char *>(), fillColor)))
return sendError(400, "fill muss eine Farbe wie \"#000000\" sein");
static CRGB frame[NUM_LEDS]; // neues Bild (PUT) bzw. neue Pixelfarben (PATCH)
static uint8_t where[NUM_LEDS][3];
uint8_t count = 0;
if (full) {
if (!pixels.is<JsonArray>() || pixels.size() != NUM_LEDS)
return sendError(400, "pixels muss eine Liste mit 125 Farben sein, Index x + 5*y + 25*z");
for (JsonVariant p : pixels.as<JsonArray>()) {
if (!(p.is<const char *>() && parseColor(p.as<const char *>(), frame[count])))
return sendError(400, "pixels: jede Farbe wie \"#ff0000\"");
count++;
}
} else {
if (pixels.isNull() && fill.isNull()) return sendError(400, "pixels oder fill angeben");
if (!pixels.isNull()) {
if (!pixels.is<JsonArray>()) return sendError(400, "pixels muss eine Liste von {x, y, z, color} sein");
if (pixels.size() > NUM_LEDS) return sendError(400, "höchstens 125 Pixel pro Anfrage");
for (JsonVariant p : pixels.as<JsonArray>()) {
JsonVariant x = p["x"], y = p["y"], z = p["z"], color = p["color"];
if (!(x.is<int>() && y.is<int>() && z.is<int>() && x.as<int>() >= 0 && y.as<int>() >= 0 && z.as<int>() >= 0 &&
x.as<int>() < CUBE_SIZE && y.as<int>() < CUBE_SIZE && z.as<int>() < NUM_LAYERS))
return sendError(400, "pixels: x, y und z müssen ganze Zahlen von 0 bis 4 sein");
if (!(color.is<const char *>() && parseColor(color.as<const char *>(), frame[count])))
return sendError(400, "pixels: color muss eine Farbe wie \"#ff0000\" sein");
where[count][0] = x.as<int>();
where[count][1] = y.as<int>();
where[count][2] = z.as<int>();
count++;
}
}
}
if (full) {
memcpy(manualFrame, frame, sizeof(manualFrame)); // gleiche Reihenfolge: [z][y][x]
} else {
if (!manualMode) fill_solid(&manualFrame[0][0][0], NUM_LEDS, CRGB::Black); // neu: leeres Bild
if (!fill.isNull()) fill_solid(&manualFrame[0][0][0], NUM_LEDS, fillColor);
for (uint8_t k = 0; k < count; k++) manualFrame[where[k][2]][where[k][1]][where[k][0]] = frame[k];
}
setManual(true);
server.send(204);
}
void webBegin() {
WiFi.setHostname("cube");
WiFi.mode(WIFI_STA);
@@ -186,6 +263,9 @@ void webBegin() {
server.on("/api/effects", HTTP_GET, sendEffects);
server.on("/api/categories", HTTP_GET, sendCategories);
server.on("/api/weather", HTTP_GET, sendWeather);
server.on("/api/pixels", HTTP_GET, sendPixels);
server.on("/api/pixels", HTTP_PUT, [] { updatePixels(true); });
server.on("/api/pixels", HTTP_PATCH, [] { updatePixels(false); });
server.on("/api/lightning", HTTP_POST, [] {
triggerLightning(millis());
server.send(204);