Move components to lib folder
This commit is contained in:
parent
ae3c20b59d
commit
b9b68f08cb
8 changed files with 1 additions and 0 deletions
76
lib/LED/LED.cpp
Normal file
76
lib/LED/LED.cpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
#ifndef LED_cpp
|
||||
#define LED_cpp
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "LEDOutput.cpp"
|
||||
|
||||
class LED {
|
||||
std::string _name;
|
||||
LEDOutput* _output;
|
||||
bool _on;
|
||||
unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd;
|
||||
float _brightness;
|
||||
|
||||
unsigned long getFadeDuration(){
|
||||
return _on ? _fadeDurationOn : _fadeDurationOff;
|
||||
}
|
||||
|
||||
unsigned long getRemainingFadeTime(){
|
||||
unsigned long now = millis();
|
||||
return _fadeEnd >= now ? constrain(_fadeEnd - now, 0, getFadeDuration()) : 0;
|
||||
}
|
||||
|
||||
float getMultiplier() {
|
||||
float value = getRemainingFadeTime() / (float)getFadeDuration();
|
||||
return _brightness - value;
|
||||
}
|
||||
|
||||
float getOutputMultiplier(){
|
||||
float value = getMultiplier();
|
||||
return _on ? value : _brightness - value;
|
||||
}
|
||||
|
||||
void reset(bool on){
|
||||
if (on == _on)
|
||||
return;
|
||||
float oldMultiplier = getMultiplier();
|
||||
_on = on;
|
||||
_fadeStart = millis();
|
||||
_fadeEnd = _fadeStart + (getFadeDuration() * oldMultiplier);
|
||||
}
|
||||
|
||||
public:
|
||||
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);
|
||||
}
|
||||
|
||||
void off(){
|
||||
reset(false);
|
||||
}
|
||||
|
||||
void toggle(){
|
||||
reset(!_on);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
_output->writeFraction(getOutputMultiplier());
|
||||
}
|
||||
|
||||
void setBrightness(float brightness){
|
||||
_brightness = brightness;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
33
lib/LED/LEDOutput.cpp
Normal file
33
lib/LED/LEDOutput.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#ifndef LEDOutput_cpp
|
||||
#define LEDOutput_cpp
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define LED_OUTPUT_FREQUENCY 490
|
||||
#define LED_OUTPUT_RESOLUTION 15
|
||||
#define LED_OUTPUT_PWM_RANGE 32767
|
||||
|
||||
class LEDOutput {
|
||||
unsigned int _channel;
|
||||
bool _invert;
|
||||
|
||||
public:
|
||||
LEDOutput(unsigned int channel, bool invert = false) {
|
||||
_channel = channel;
|
||||
_invert = invert;
|
||||
ledcSetup(channel, LED_OUTPUT_FREQUENCY, LED_OUTPUT_RESOLUTION);
|
||||
}
|
||||
|
||||
void attach(unsigned int pin){
|
||||
ledcAttachPin(pin, _channel);
|
||||
}
|
||||
|
||||
void writeFraction(float fraction){
|
||||
auto value = fraction * LED_OUTPUT_PWM_RANGE;
|
||||
if (_invert)
|
||||
value = LED_OUTPUT_PWM_RANGE - value;
|
||||
ledcWrite(_channel, value);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
48
lib/LED/LedManager.cpp
Normal file
48
lib/LED/LedManager.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#ifndef LedManager_cpp
|
||||
#define LedManager_cpp
|
||||
|
||||
#include "LED.cpp"
|
||||
#include "TimerManager.cpp"
|
||||
#include <map>
|
||||
#include <functional>
|
||||
|
||||
class LedManager {
|
||||
TimerManager *_timers;
|
||||
std::map<int, LED *> _leds;
|
||||
|
||||
public:
|
||||
LedManager(TimerManager *timers) {
|
||||
_timers = timers;
|
||||
}
|
||||
|
||||
void registerLEDs(int id, LED *leds) {
|
||||
_leds[id] = leds;
|
||||
_timers->add(id, [&]() { leds->off(); });
|
||||
}
|
||||
|
||||
void loop() {
|
||||
for (auto iterator = _leds.begin(); iterator != _leds.end(); ++iterator) {
|
||||
auto leds = iterator->second;
|
||||
leds->loop();
|
||||
}
|
||||
}
|
||||
|
||||
void setLedProperties(int id, bool on, float brightness, unsigned long timer) {
|
||||
if (!_leds.count(id))
|
||||
return;
|
||||
|
||||
auto led = _leds.find(id)->second;
|
||||
|
||||
led->setBrightness(brightness);
|
||||
|
||||
if (on)
|
||||
led->on();
|
||||
else
|
||||
led->off();
|
||||
|
||||
if (timer > 0)
|
||||
_timers->reset(id, timer);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue