Add a timer to turn off after a period of time. Move the LED name to reduce string literals.
This commit is contained in:
parent
59ec76b52f
commit
c3e91d269e
5 changed files with 93 additions and 12 deletions
|
@ -1,13 +1,19 @@
|
|||
#include "LED.cpp"
|
||||
#include "TimerManager.cpp"
|
||||
#include <map>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonLightControl{
|
||||
std::map<std::string, LED*> _leds;
|
||||
TimerManager *_timers;
|
||||
|
||||
public:
|
||||
void registerLEDs(std::string name, LED *leds){
|
||||
_leds[name] = leds;
|
||||
JsonLightControl(TimerManager *timers){
|
||||
_timers = timers;
|
||||
}
|
||||
|
||||
void registerLEDs(LED *leds){
|
||||
_leds[leds->getName()] = leds;
|
||||
}
|
||||
|
||||
void action(std::string input){
|
||||
|
@ -21,20 +27,26 @@ public:
|
|||
|
||||
for (int i = 0; i < lights.size(); i++)
|
||||
{
|
||||
auto name = lights[i]["name"].as<std::string>();
|
||||
auto light = lights[i];
|
||||
|
||||
auto name = light["name"].as<std::string>();
|
||||
if (!_leds.count(name))
|
||||
continue;
|
||||
|
||||
auto led = _leds.find(name)->second;
|
||||
|
||||
auto brightness = lights[i]["brightness"].as<float>();
|
||||
auto brightness = light["brightness"].as<float>();
|
||||
led->setBrightness(brightness);
|
||||
|
||||
auto on = lights[i]["on"].as<bool>();
|
||||
auto on = light["on"].as<bool>();
|
||||
if (on)
|
||||
led->on();
|
||||
else
|
||||
led->off();
|
||||
|
||||
auto timer = light["timer"].as<unsigned long>();
|
||||
if (timer > 0)
|
||||
_timers->reset(name, timer);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
10
src/LED.cpp
10
src/LED.cpp
|
@ -4,7 +4,8 @@
|
|||
#include <Arduino.h>
|
||||
#include "LEDOutput.cpp"
|
||||
|
||||
class LED{
|
||||
class LED {
|
||||
std::string _name;
|
||||
LEDOutput* _output;
|
||||
bool _on;
|
||||
unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd;
|
||||
|
@ -39,13 +40,18 @@ class LED{
|
|||
}
|
||||
|
||||
public:
|
||||
LED(LEDOutput* output, unsigned long fadeDurationOn, unsigned long fadeDurationOff) {
|
||||
LED(std::string name, LEDOutput* output, unsigned long fadeDurationOn, unsigned long fadeDurationOff) {
|
||||
_name = name;
|
||||
_output = output;
|
||||
_fadeDurationOn = fadeDurationOn;
|
||||
_fadeDurationOff = fadeDurationOff;
|
||||
_brightness = 1.0f;
|
||||
}
|
||||
|
||||
std::string getName(){
|
||||
return _name;
|
||||
}
|
||||
|
||||
void on(){
|
||||
reset(true);
|
||||
}
|
||||
|
|
32
src/Timer.cpp
Normal file
32
src/Timer.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
class Timer{
|
||||
private:
|
||||
unsigned long _interval, _lastTick;
|
||||
void (*_callback)(void);
|
||||
bool _running;
|
||||
|
||||
public:
|
||||
Timer(void (*callback)(void)){
|
||||
_callback = callback;
|
||||
_running = false;
|
||||
}
|
||||
|
||||
void reset(unsigned long interval){
|
||||
_interval = interval;
|
||||
_lastTick = millis();
|
||||
_running = true;
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if (!_running)
|
||||
return;
|
||||
|
||||
unsigned long tick = millis();
|
||||
|
||||
if (tick - _lastTick >= _interval){
|
||||
_callback();
|
||||
_running = false;
|
||||
}
|
||||
}
|
||||
};
|
26
src/TimerManager.cpp
Normal file
26
src/TimerManager.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <map>
|
||||
#include "Timer.cpp"
|
||||
|
||||
class TimerManager{
|
||||
std::map<std::string, Timer*> _timers;
|
||||
|
||||
public:
|
||||
|
||||
void add(std::string name, void (*callback)(void)){
|
||||
_timers[name] = new Timer(callback);
|
||||
}
|
||||
|
||||
void reset(std::string name, unsigned long interval){
|
||||
if (!_timers.count(name))
|
||||
return;
|
||||
_timers.find(name)->second->reset(interval);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
for(auto iterator = _timers.begin(); iterator != _timers.end(); ++iterator)
|
||||
{
|
||||
auto timer = iterator->second;
|
||||
timer->loop();
|
||||
}
|
||||
}
|
||||
};
|
15
src/main.cpp
15
src/main.cpp
|
@ -7,11 +7,12 @@
|
|||
#define FADE_OUT_DURATION 2000
|
||||
|
||||
LEDOutput _seatingLedOutput(0, true);
|
||||
LED _seatingLeds(&_seatingLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||
LED _seatingLeds("seating", &_seatingLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||
LEDOutput _kitchenLedOutput(1 , true);
|
||||
LED _kitchenLeds(&_kitchenLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||
LED _kitchenLeds("kitchen", &_kitchenLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
|
||||
|
||||
JsonLightControl _jsonLightControl;
|
||||
TimerManager _timers;
|
||||
JsonLightControl _jsonLightControl(&_timers);
|
||||
BluetoothLEDCallback _btCallback(&_jsonLightControl);
|
||||
BluetoothService _btService("Van Lights", &_btCallback);
|
||||
|
||||
|
@ -24,14 +25,18 @@ void setup() {
|
|||
_kitchenLedOutput.attach(18);
|
||||
_kitchenLedOutput.attach(23);
|
||||
|
||||
_jsonLightControl.registerLEDs("seating", &_seatingLeds);
|
||||
_jsonLightControl.registerLEDs("kitchen", &_kitchenLeds);
|
||||
_timers.add(_seatingLeds.getName(), [](){ _seatingLeds.off(); });
|
||||
_timers.add(_kitchenLeds.getName(), [](){ _kitchenLeds.off(); });
|
||||
|
||||
_jsonLightControl.registerLEDs(&_seatingLeds);
|
||||
_jsonLightControl.registerLEDs(&_kitchenLeds);
|
||||
|
||||
_btService.init();
|
||||
_btService.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
_timers.loop();
|
||||
_seatingLeds.loop();
|
||||
_kitchenLeds.loop();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue