-
Notifications
You must be signed in to change notification settings - Fork 0
/
LedState.h
59 lines (54 loc) · 1.97 KB
/
LedState.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef LED_STATE_H
#define LED_STATE_H
#include "DurationFsm.h"
class ColorLedState: public DurationState, public Runnable {
public:
ColorLedState(byte greenValue, byte redValue, byte blueValue, unsigned long minDurationMs, unsigned long ledOnDurationMs, String name)
: DurationState(minDurationMs, name), greenValue(greenValue), redValue(redValue), blueValue(blueValue), ledOnDurationMs(ledOnDurationMs) {
}
ColorLedState(byte greenValue, byte redValue, byte blueValue, unsigned long minDurationMs, unsigned long ledOnDurationMs, String name, SuperState * const superState)
: DurationState(minDurationMs, name, superState), greenValue(greenValue), redValue(redValue), blueValue(blueValue), ledOnDurationMs(ledOnDurationMs) {
}
virtual void enter() {
reactivateLed();
}
virtual void exit() {
deactivateLed();
scheduler.removeCallbacks(this);
}
void reactivateLed() {
#ifndef COLOR_LED_INVERTED
analogWrite(COLOR_LED_GREEN_PIN, greenValue);
analogWrite(COLOR_LED_RED_PIN, redValue);
analogWrite(COLOR_LED_BLUE_PIN, blueValue);
#else
analogWrite(COLOR_LED_GREEN_PIN, 255 - greenValue);
analogWrite(COLOR_LED_RED_PIN, 255 - redValue);
analogWrite(COLOR_LED_BLUE_PIN, 255 - blueValue);
#endif
if (ledOnDurationMs != INFINITE_DURATION) {
scheduler.scheduleDelayed(this, ledOnDurationMs);
}
}
void deactivateLed() {
#ifndef COLOR_LED_INVERTED
analogWrite(COLOR_LED_GREEN_PIN, 0);
analogWrite(COLOR_LED_RED_PIN, 0);
analogWrite(COLOR_LED_BLUE_PIN, 0);
#else
analogWrite(COLOR_LED_GREEN_PIN, 255);
analogWrite(COLOR_LED_RED_PIN, 255);
analogWrite(COLOR_LED_BLUE_PIN, 255);
#endif
}
void run() {
deactivateLed();
}
bool isActive() {
return ledOnDurationMs == INFINITE_DURATION || scheduler.isScheduled(this);
}
private:
const byte greenValue, redValue, blueValue;
const unsigned long ledOnDurationMs;
};
#endif