Initial commit
This commit is contained in:
commit
8c50498c7d
10 changed files with 312 additions and 0 deletions
60
src/LED.cpp
Normal file
60
src/LED.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include <Arduino.h>
|
||||
#include "LEDOutput.cpp"
|
||||
|
||||
class LED{
|
||||
LEDOutput* _output;
|
||||
bool _on;
|
||||
unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd;
|
||||
|
||||
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 1.0f - value;
|
||||
}
|
||||
|
||||
float getOutputMultiplier(){
|
||||
float value = getMultiplier();
|
||||
return _on ? value : 1.0f - value;
|
||||
}
|
||||
|
||||
void reset(bool on){
|
||||
if (on == _on)
|
||||
return;
|
||||
float oldMultiplier = getMultiplier();
|
||||
_on = on;
|
||||
_fadeStart = millis();
|
||||
_fadeEnd = _fadeStart + (getFadeDuration() * oldMultiplier);
|
||||
}
|
||||
|
||||
public:
|
||||
LED(LEDOutput* output, unsigned long fadeDurationOn, unsigned long fadeDurationOff) {
|
||||
_output = output;
|
||||
_fadeDurationOn = fadeDurationOn;
|
||||
_fadeDurationOff = fadeDurationOff;
|
||||
}
|
||||
|
||||
void on(){
|
||||
reset(true);
|
||||
}
|
||||
|
||||
void off(){
|
||||
reset(false);
|
||||
}
|
||||
|
||||
void toggle(){
|
||||
reset(!_on);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float multiplier = getOutputMultiplier();
|
||||
_output->writeFraction(1 - multiplier);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue