-
Notifications
You must be signed in to change notification settings - Fork 0
/
statecontroller.cpp
194 lines (178 loc) · 6.05 KB
/
statecontroller.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "statecontroller.h"
#include <Arduino.h>
typedef unsigned long ms_time_t; ///< Millisecond time type
StateController::StateController(int buttonPin, StatusAbstract *statusLed, OutputAbstract *lights) :
_buttonPin(buttonPin),
_statusLed(statusLed),
_lights(lights)
{
// Set up button
pinMode(_buttonPin, INPUT);
digitalWrite(_buttonPin, HIGH);
// Initialize in idle state
setState(States::State::Idle);
}
States::State StateController::getState()
{
return _currentState;
}
void StateController::processEvents()
{
// Check button status
if (buttonHeld()) {
if (!_buttonActive) {
// Mark button as being held down
_buttonStartHold = millis();
_buttonActive = true;
_buttonReleased = false;
}
if (buttonHeldDuration() >= MS_BUTTON_HELD_LONG && _buttonEventTriggered == false) {
// Button has been held longer than LONG duration, fire event
buttonTriggered();
_buttonEventTriggered = true;
}
} else {
// Button not held, or held longer than maximum duration
if (_buttonActive && !_buttonReleased) {
// Fire button if not already triggered
if (_buttonEventTriggered) {
// Button already triggered, just reset event
_buttonEventTriggered = false;
} else {
// Trigger button
buttonTriggered();
}
_buttonReleased = true;
}
_buttonActive = false;
}
}
void StateController::markLinked()
{
if (getState() != States::State::Linked) {
setState(States::State::Linked);
}
}
bool StateController::buttonHeld()
{
// Check if button is active/pressed
return (digitalRead(_buttonPin) == LOW);
}
ms_time_t StateController::buttonHeldDuration()
{
if (!_buttonActive) {
return 0;
}
// Calculate how long button has been held
ms_time_t duration = millis() - _buttonStartHold;
// Clip to 0, no negative values
return (duration > 0 ? duration : 0);
}
void StateController::buttonTriggered()
{
if (buttonHeldDuration() >= MS_BUTTON_HELD_LONG) {
// Turn off lights if held
setState(States::State::DisplayBlack);
} else if (buttonHeldDuration() >= MS_BUTTON_HELD_SHORT) {
// Increment to next state
switch (getState()) {
case States::State::Idle:
case States::State::Linked:
case States::State::DisplayBlack:
// Treat idle, linked to computer, and black as the same state
setState(States::State::DisplayIdentify);
break;
case States::State::DisplayIdentify:
setState(States::State::DisplayDarkOrange);
break;
case States::State::DisplayDarkOrange:
setState(States::State::DisplayOrange);
break;
case States::State::DisplayOrange:
setState(States::State::DisplayAmbient);
break;
case States::State::DisplayAmbient:
setState(States::State::DisplayWhite);
break;
case States::State::DisplayWhite:
setState(States::State::DisplayIdentify);
break;
}
}
}
void StateController::setState(States::State newState)
{
if (newState == getState() && _setOnce == true) {
// State already set and startup (setOnce) passed
return;
}
// Track number of LEDs for later
int numLEDs = _lights->getNumLEDs();
// Apply new state
switch (newState) {
case States::State::Idle:
case States::State::Linked:
// Do nothing
break;
case States::State::DisplayBlack:
// Turn lights off
_statusLed->setBusy(true);
_lights->fillColor(sCRGB::Black, 255);
_lights->show();
_statusLed->setBusy(false);
break;
case States::State::DisplayIdentify:
// Display ends and middle on black
_statusLed->setBusy(true);
// Fill with black
_lights->fillColor(sCRGB::Black, 255);
// Start LEDs
_lights->setColor(0, _lights->getCorrectedColor(sCRGB::Red));
_lights->setColor(1, _lights->getCorrectedColor(sCRGB::Yellow));
// Middle LEDs
_lights->setColor((numLEDs / 2) - 2, _lights->getCorrectedColor(sCRGB::Yellow));
_lights->setColor((numLEDs / 2) - 1, _lights->getCorrectedColor(sCRGB::Purple));
_lights->setColor((numLEDs / 2) + 0, _lights->getCorrectedColor(sCRGB::Blue));
_lights->setColor((numLEDs / 2) + 1, _lights->getCorrectedColor(sCRGB::Yellow));
// Ending LEDs
_lights->setColor(numLEDs - 2, _lights->getCorrectedColor(sCRGB::Yellow));
_lights->setColor(numLEDs - 1, _lights->getCorrectedColor(sCRGB::Green));
_lights->show();
_statusLed->setBusy(false);
break;
case States::State::DisplayDarkOrange:
// Fill with a dark orange color
_statusLed->setBusy(true);
_lights->fillColor(_lights->getCorrectedColor(sCRGB::OrangeRed), 30);
_lights->show();
_statusLed->setBusy(false);
break;
case States::State::DisplayOrange:
// Fill with a bright orange color
_statusLed->setBusy(true);
_lights->fillColor(_lights->getCorrectedColor(sCRGB::OrangeRed), 255);
_lights->show();
_statusLed->setBusy(false);
break;
case States::State::DisplayAmbient:
// Fill with a color loosely approximating normal lighting
_statusLed->setBusy(true);
// Matches Actinic's "ambient" color in Color.cs
_lights->fillColor(sCRGB(255, 100, 30), 255);
_lights->show();
_statusLed->setBusy(false);
break;
case States::State::DisplayWhite:
// Fill with maximum color
_statusLed->setBusy(true);
_lights->fillColor(sCRGB::White, 255);
_lights->show();
_statusLed->setBusy(false);
break;
}
// Update the status LED
_statusLed->setState(newState);
// Store the new state
_currentState = newState;
_setOnce = true;
}