diff --git a/src/JsonLightControl.cpp b/src/JsonLightControl.cpp index d744aa9..d81f9fb 100644 --- a/src/JsonLightControl.cpp +++ b/src/JsonLightControl.cpp @@ -3,10 +3,10 @@ #include class JsonLightControl{ - std::map _leds; + std::map _leds; public: - void registerLEDs(const char *name, LED *leds){ + void registerLEDs(std::string name, LED *leds){ _leds[name] = leds; } @@ -14,18 +14,23 @@ public: if (input.length() <= 0) return; - Serial.println(input.c_str()); - - DynamicJsonDocument document(256); + StaticJsonDocument<256> document; deserializeJson(document, input.c_str()); - auto lights = document["lights"]; - Serial.println(lights.size()); - auto on = document["on"].as(); + auto lights = document.as(); + for (int i = 0; i < lights.size(); i++) { - auto name = lights[i].as(); + auto name = lights[i]["name"].as(); + if (!_leds.count(name)) + continue; + auto led = _leds.find(name)->second; + + auto brightness = lights[i]["brightness"].as(); + led->setBrightness(brightness); + + auto on = lights[i]["on"].as(); if (on) led->on(); else diff --git a/src/LED.cpp b/src/LED.cpp index fdf721d..adde2b7 100644 --- a/src/LED.cpp +++ b/src/LED.cpp @@ -8,6 +8,7 @@ class LED{ LEDOutput* _output; bool _on; unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd; + float _brightness; unsigned long getFadeDuration(){ return _on ? _fadeDurationOn : _fadeDurationOff; @@ -20,12 +21,12 @@ class LED{ float getMultiplier() { float value = getRemainingFadeTime() / (float)getFadeDuration(); - return 1.0f - value; + return _brightness - value; } float getOutputMultiplier(){ float value = getMultiplier(); - return _on ? value : 1.0f - value; + return _on ? value : _brightness - value; } void reset(bool on){ @@ -42,6 +43,7 @@ public: _output = output; _fadeDurationOn = fadeDurationOn; _fadeDurationOff = fadeDurationOff; + _brightness = 1.0f; } void on(){ @@ -59,6 +61,10 @@ public: void loop() { _output->writeFraction(getOutputMultiplier()); } + + void setBrightness(float brightness){ + _brightness = brightness; + } }; #endif