-
Notifications
You must be signed in to change notification settings - Fork 2
/
StateList.cpp
89 lines (79 loc) · 2.24 KB
/
StateList.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
#include "StateList.h"
#include "Arduino.h"
LED_CONTROLLER_NAMESPACE_USING
#define BROKEN_BLINK_INTERVAL 1000
Color StateList::colorPassed(0x00FF00);
Color StateList::colorFailed(0xFF0000);
StateList::StateList() : numKnownStates(0), blinkOn(false), colorsChanged(true),
brokenBlinkInterval(BROKEN_BLINK_INTERVAL),
colorBroken(), colorBrokenDim(),
colorPassedDim(colorPassed), colorFailedDim(colorFailed),
historyScale(1.0)
{}
void StateList::parseStates(const char* stateString) {
colorsChanged = true;
numKnownStates = 0;
int i = 0;
while(stateString[i] != '\0') {
if (numKnownStates >= STRIP_LENGTH) {
Serial.print("StateList ran out of space, trauncating: "
"more than ");
Serial.print(STRIP_LENGTH);
Serial.print(" states (characters) in \"");
Serial.print(stateString);
Serial.println("\".");
return;
}
bool isFirst = i == 0;
switch(stateString[i]) {
case STATE_CHAR_PASSED:
stateColors[numKnownStates] = isFirst ?
&colorPassed : &colorPassedDim;
break;
case STATE_CHAR_FAILED:
stateColors[numKnownStates] = isFirst ?
&colorFailed : &colorFailedDim;
break;
case STATE_CHAR_BROKEN:
stateColors[numKnownStates] = isFirst ?
&colorBroken : &colorBrokenDim;
break;
default:
Serial.print("Invalid state (character) \"");
Serial.print(stateString[i]);
Serial.print("\" at index ");
Serial.print(i);
Serial.print(" of state string \"");
Serial.print(stateString);
Serial.println("\", trauncating.");
return;
}
i++;
numKnownStates++;
}
}
void StateList::setHistoryScale(float scale) {
if (scale != historyScale) {
colorsChanged = true;
historyScale = scale;
colorPassedDim = colorPassed.scaled(historyScale);
colorFailedDim = colorFailed.scaled(historyScale);
}
}
bool StateList::update() {
bool updated = colorsChanged;
colorsChanged = false;
if (brokenBlinkInterval.update()) {
brokenBlinkInterval.clearExpired();
updated = true;
blinkOn = !blinkOn;
colorBroken.setCombinedValue(blinkOn ? 0xFF0000 : 0x440000);
colorBrokenDim = colorBroken.scaled(historyScale);
}
return updated;
}
void StateList::apply(Color* stripColors) {
for(int i = 0; i < numKnownStates && i < STRIP_LENGTH; i++) {
stripColors[i].add(*stateColors[i]);
}
}