Control brightness through JSON payload
This commit is contained in:
parent
44b91c86f2
commit
59ec76b52f
2 changed files with 22 additions and 11 deletions
|
@ -3,10 +3,10 @@
|
|||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonLightControl{
|
||||
std::map<const char*, LED*> _leds;
|
||||
std::map<std::string, LED*> _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<bool>();
|
||||
auto lights = document.as<JsonArray>();
|
||||
|
||||
for (int i = 0; i < lights.size(); i++)
|
||||
{
|
||||
auto name = lights[i].as<const char*>();
|
||||
auto name = lights[i]["name"].as<std::string>();
|
||||
if (!_leds.count(name))
|
||||
continue;
|
||||
|
||||
auto led = _leds.find(name)->second;
|
||||
|
||||
auto brightness = lights[i]["brightness"].as<float>();
|
||||
led->setBrightness(brightness);
|
||||
|
||||
auto on = lights[i]["on"].as<bool>();
|
||||
if (on)
|
||||
led->on();
|
||||
else
|
||||
|
|
10
src/LED.cpp
10
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue