-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blinkenlights.cpp
99 lines (83 loc) · 1.96 KB
/
Blinkenlights.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Blinkenlights.h"
Blinkenlights::Blinkenlights(
byte top_pin,
byte bottom_pin,
byte flashlight_pin,
unsigned long interval
) {
_top_pin = top_pin;
_bottom_pin = bottom_pin;
_flashlight_pin = flashlight_pin;
_interval = interval;
_state = BLINKEN_OFF;
pinMode(_top_pin, OUTPUT);
pinMode(_bottom_pin, OUTPUT);
}
BlinkenState Blinkenlights::tick(boolean mastLowered)
{
switch (_state) {
case BLINKEN_OFF:
return whenOff(mastLowered);
break;
case BLINKEN_BLINK_OFF:
return whenBlinkOff(mastLowered);
break;
case BLINKEN_BLINK_ON:
return whenBlinkOn(mastLowered);
break;
case BLINKEN_FLASHLIGHT:
return whenFlashlight(mastLowered);
break;
}
}
BlinkenState Blinkenlights::whenOff(boolean mastLowered)
{
digitalWrite(_top_pin, LOW);
digitalWrite(_bottom_pin, LOW);
digitalWrite(_flashlight_pin, LOW);
if (mastLowered) {
_blinkAfter = millis() + _interval;
_state = BLINKEN_BLINK_OFF;
}
return _state;
}
BlinkenState Blinkenlights::whenBlinkOff(boolean mastLowered)
{
digitalWrite(_top_pin, LOW);
digitalWrite(_bottom_pin, HIGH);
digitalWrite(_flashlight_pin, LOW);
unsigned long now = millis();
if (now > _blinkAfter) {
_blinkAfter = now + _interval;
_state = BLINKEN_BLINK_ON;
}
if (!mastLowered) {
_state = BLINKEN_OFF;
}
return _state;
}
BlinkenState Blinkenlights::whenBlinkOn(boolean mastLowered)
{
digitalWrite(_top_pin, HIGH);
digitalWrite(_bottom_pin, HIGH);
digitalWrite(_flashlight_pin, LOW);
unsigned long now = millis();
if (now > _blinkAfter) {
_blinkAfter = now + _interval;
_state = BLINKEN_BLINK_OFF;
}
if (!mastLowered) {
_state = BLINKEN_OFF;
}
return _state;
}
BlinkenState Blinkenlights::whenFlashlight(boolean mastLowered)
{
digitalWrite(_top_pin, LOW);
digitalWrite(_bottom_pin, LOW);
digitalWrite(_flashlight_pin, HIGH);
if (!mastLowered) {
_state = BLINKEN_OFF;
}
return _state;
}