Skip to content

Commit

Permalink
✨ feat: handle missing data
Browse files Browse the repository at this point in the history
- Fix brightness bug in ws2812fx library
- Handle missing data in HTTP POST request
  • Loading branch information
Cp0204 committed Jul 13, 2024
1 parent c77f931 commit 307235c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions cubefx/cubefx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void showEffect() {
ws2812fx.start();
}
ws2812fx.setSpeed(map(255 - effectSpeed, 0, 255, 0, 5000)); // 在 ws2812fx 速度代表帧 ms 延迟,越大越慢
ws2812fx.setBrightness(lightness);
ws2812fx.setBrightness(constrain(lightness + 1, 0, 255)); // ws2812fx的不明bug,当亮度=0时似乎=255
ws2812fx.setColor(colors[0]);
switch (effectId) {
case 0:
Expand Down Expand Up @@ -253,12 +253,14 @@ void handleHttpPost() {
httpServer.send(400, "text/plain", "Bad Request");
} else {
isLightOn = doc.containsKey("on") ? (doc["on"] == 0 ? 0 : 1) : 1;
effectId = doc["id"];
effectSpeed = constrain(doc["speed"], 0, 255);
lightness = constrain(doc["lightness"], 0, 254) + 1;
JsonArray data = doc["data"];
for (int i = 0; i < NUM_PIXELS; i++) {
colors[i] = compatibleColor(data[i]);
effectId = doc.containsKey("id") ? doc["id"] : effectId;
effectSpeed = doc.containsKey("speed") ? constrain(doc["speed"], 0, 255) : effectSpeed;
lightness = doc.containsKey("lightness") ? constrain(doc["lightness"], 0, 255) : lightness;
if (doc.containsKey("data")) {
JsonArray data = doc["data"];
for (int i = 0; i < NUM_PIXELS; i++) {
colors[i] = compatibleColor(data[i]);
}
}
showEffect();
saveToEEPROM();
Expand Down Expand Up @@ -366,7 +368,7 @@ uint16_t waterDropEffect(void) {
}
}

// 星空效果,随机闪缩,然后渐暗
// 星空效果,随机闪烁,然后渐暗
uint16_t starEffect(void) {
WS2812FX::Segment* seg = ws2812fx.getSegment();
int seglen = seg->stop - seg->start + 1;
Expand Down

0 comments on commit 307235c

Please sign in to comment.