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>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
class JsonLightControl{
|
class JsonLightControl{
|
||||||
std::map<const char*, LED*> _leds;
|
std::map<std::string, LED*> _leds;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void registerLEDs(const char *name, LED *leds){
|
void registerLEDs(std::string name, LED *leds){
|
||||||
_leds[name] = leds;
|
_leds[name] = leds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,18 +14,23 @@ public:
|
||||||
if (input.length() <= 0)
|
if (input.length() <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Serial.println(input.c_str());
|
StaticJsonDocument<256> document;
|
||||||
|
|
||||||
DynamicJsonDocument document(256);
|
|
||||||
deserializeJson(document, input.c_str());
|
deserializeJson(document, input.c_str());
|
||||||
|
|
||||||
auto lights = document["lights"];
|
auto lights = document.as<JsonArray>();
|
||||||
Serial.println(lights.size());
|
|
||||||
auto on = document["on"].as<bool>();
|
|
||||||
for (int i = 0; i < lights.size(); i++)
|
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 led = _leds.find(name)->second;
|
||||||
|
|
||||||
|
auto brightness = lights[i]["brightness"].as<float>();
|
||||||
|
led->setBrightness(brightness);
|
||||||
|
|
||||||
|
auto on = lights[i]["on"].as<bool>();
|
||||||
if (on)
|
if (on)
|
||||||
led->on();
|
led->on();
|
||||||
else
|
else
|
||||||
|
|
10
src/LED.cpp
10
src/LED.cpp
|
@ -8,6 +8,7 @@ class LED{
|
||||||
LEDOutput* _output;
|
LEDOutput* _output;
|
||||||
bool _on;
|
bool _on;
|
||||||
unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd;
|
unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd;
|
||||||
|
float _brightness;
|
||||||
|
|
||||||
unsigned long getFadeDuration(){
|
unsigned long getFadeDuration(){
|
||||||
return _on ? _fadeDurationOn : _fadeDurationOff;
|
return _on ? _fadeDurationOn : _fadeDurationOff;
|
||||||
|
@ -20,12 +21,12 @@ class LED{
|
||||||
|
|
||||||
float getMultiplier() {
|
float getMultiplier() {
|
||||||
float value = getRemainingFadeTime() / (float)getFadeDuration();
|
float value = getRemainingFadeTime() / (float)getFadeDuration();
|
||||||
return 1.0f - value;
|
return _brightness - value;
|
||||||
}
|
}
|
||||||
|
|
||||||
float getOutputMultiplier(){
|
float getOutputMultiplier(){
|
||||||
float value = getMultiplier();
|
float value = getMultiplier();
|
||||||
return _on ? value : 1.0f - value;
|
return _on ? value : _brightness - value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset(bool on){
|
void reset(bool on){
|
||||||
|
@ -42,6 +43,7 @@ public:
|
||||||
_output = output;
|
_output = output;
|
||||||
_fadeDurationOn = fadeDurationOn;
|
_fadeDurationOn = fadeDurationOn;
|
||||||
_fadeDurationOff = fadeDurationOff;
|
_fadeDurationOff = fadeDurationOff;
|
||||||
|
_brightness = 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void on(){
|
void on(){
|
||||||
|
@ -59,6 +61,10 @@ public:
|
||||||
void loop() {
|
void loop() {
|
||||||
_output->writeFraction(getOutputMultiplier());
|
_output->writeFraction(getOutputMultiplier());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setBrightness(float brightness){
|
||||||
|
_brightness = brightness;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue