| 24 | // WLED State Management |
| 25 | |
| 26 | void WLED::setState(const fl::json& wledState) { |
| 27 | if (!wledState.has_value()) { |
| 28 | FL_WARN("WLED: setState called with invalid JSON"); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Extract "on" field (bool) - optional, keep existing if missing |
| 33 | if (wledState.contains("on") && wledState["on"].is_bool()) { |
| 34 | bool newOn = wledState["on"] | mWledOn; |
| 35 | if (newOn != mWledOn) { |
| 36 | mWledOn = newOn; |
| 37 | FL_DBG("WLED: on=" << (mWledOn ? "true" : "false")); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Extract "bri" field (0-255) - optional, keep existing if missing |
| 42 | if (wledState.contains("bri")) { |
| 43 | if (wledState["bri"].is_int()) { |
| 44 | i64 briInt = wledState["bri"] | static_cast<i64>(mWledBri); |
| 45 | // Clamp to valid range 0-255 |
| 46 | if (briInt < 0) { |
| 47 | FL_WARN("WLED: brightness " << briInt << " out of range, clamping to 0"); |
| 48 | briInt = 0; |
| 49 | } else if (briInt > 255) { |
| 50 | FL_WARN("WLED: brightness " << briInt << " out of range, clamping to 255"); |
| 51 | briInt = 255; |
| 52 | } |
| 53 | u8 newBri = static_cast<u8>(briInt); |
| 54 | if (newBri != mWledBri) { |
| 55 | mWledBri = newBri; |
| 56 | FL_DBG("WLED: bri=" << static_cast<int>(mWledBri)); |
| 57 | } |
| 58 | } else { |
| 59 | FL_WARN("WLED: 'bri' field has invalid type (expected int)"); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Extract "transition" field (0-65535) - optional, keep existing if missing |
| 64 | if (wledState.contains("transition")) { |
| 65 | if (wledState["transition"].is_int()) { |
| 66 | i64 transInt = wledState["transition"] | static_cast<i64>(mTransition); |
| 67 | // Clamp to valid range 0-65535 |
| 68 | if (transInt < 0) { |
| 69 | FL_WARN("WLED: transition " << transInt << " out of range, clamping to 0"); |
| 70 | transInt = 0; |
| 71 | } else if (transInt > 65535) { |
| 72 | FL_WARN("WLED: transition " << transInt << " out of range, clamping to 65535"); |
| 73 | transInt = 65535; |
| 74 | } |
| 75 | u16 newTransition = static_cast<u16>(transInt); |
| 76 | if (newTransition != mTransition) { |
| 77 | mTransition = newTransition; |
| 78 | FL_DBG("WLED: transition=" << mTransition); |
| 79 | } |
| 80 | } else { |
| 81 | FL_WARN("WLED: 'transition' field has invalid type (expected int)"); |
| 82 | } |
| 83 | } |