-
Notifications
You must be signed in to change notification settings - Fork 1
/
LightingEvents.h
executable file
·319 lines (256 loc) · 8.69 KB
/
LightingEvents.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#pragma once
#include <Adafruit_NeoPixel.h>
#ifndef ARRAYSIZE
#define ARRAYSIZE(x) (sizeof(x)/sizeof(*x))
#endif
// LightingEvent
//
// Base class for things like turn signals, braking, backing up, etc.
// All derived classes must implement the abstract base Draw() function.
// All derived methods MUST call their base class implementations properly
// in order for the timers to work, etc.
//
// In short, you create a derived class and then call Begin() when the
// event starts (like braking), and Update keeps track of the currentr
// state. Draw() actually renders the current state of the effect to
// the light strip.
class LightingEvent
{
unsigned long _eventStart;
bool _active;
protected:
Adafruit_NeoPixel * _pStrip;
public:
LightingEvent(Adafruit_NeoPixel * pStrip)
{
_pStrip = pStrip;
_eventStart = 0;
_active = false;
}
float TimeElapsedTotal() // Total time event has been running in fractional seconds
{
return (millis() - _eventStart) / 1000.0f;
}
bool GetActive()
{
return _active;
}
virtual void Begin()
{
_active = true;
_eventStart = millis();
};
virtual void End()
{
_active = false;
for (int i = 0; i < NUMBER_USED_PIXELS; i++)
_pStrip->setPixelColor(i, COLOR_BLACK);
_pStrip->show();
};
virtual void Draw() = 0;
};
class BackupEvent : public LightingEvent
{
const float BLOOM_TIME = 0.25f;
public:
BackupEvent(Adafruit_NeoPixel * pStrip) : LightingEvent(pStrip)
{
}
virtual void Draw() override
{
if (false == GetActive())
return;
// The backup light illuminates the whole strip in white. It quickly "blooms"
// out from the center to fill the strip.
float fPercentComplete = min(TimeElapsedTotal() / BLOOM_TIME, 1.0f);
int cLEDs = NUMBER_USED_PIXELS * fPercentComplete;
int iFirst = (NUMBER_USED_PIXELS / 2) - (cLEDs / 2);
int iLast = (NUMBER_USED_PIXELS / 2) + (cLEDs / 2);
for (int i = 0; i < NUMBER_USED_PIXELS; i++)
{
if (i < iFirst || i > iLast)
_pStrip->setPixelColor(i, COLOR_BLACK);
else
_pStrip->setPixelColor(i, COLOR_WHITE);
}
_pStrip->show();
}
};
class BrakingEvent : public LightingEvent
{
const float BRAKE_STROBE_DURATION = 0.5f;
const float BLOOM_START_SIZE = 0.10;
const float BLOOM_TIME = 0.50;
public:
BrakingEvent(Adafruit_NeoPixel * pStrip) : LightingEvent(pStrip)
{
}
// BrakingEvent::Draw
//
// The strobe flash happens too fast to wait for the loop pump system, so we do a full 50ms
// cycle of it here (30 on, 20 off). That way it is crisp and accurate but we still never
// block the system for more than 50ms, which is sort of a limit I've set
virtual void Draw() override
{
if (false == GetActive())
return;
if (TimeElapsedTotal() < BRAKE_STROBE_DURATION)
{
float timeElapsed = TimeElapsedTotal();
float pctComplete = min(1.0f, (timeElapsed / BLOOM_TIME) + BLOOM_START_SIZE);
float unusedEachEnd = (1.0f - pctComplete) * NUMBER_USED_PIXELS / 2;
for (uint16_t i = unusedEachEnd; i < NUMBER_USED_PIXELS - unusedEachEnd; i++)
_pStrip->setPixelColor(i, COLOR_RED);
_pStrip->show();
delay(30);
timeElapsed = TimeElapsedTotal();
pctComplete = min(1.0f, (timeElapsed / BLOOM_TIME) + BLOOM_START_SIZE);
unusedEachEnd = (1.0f - pctComplete) * NUMBER_USED_PIXELS / 2;
for (uint16_t i = unusedEachEnd; i < NUMBER_USED_PIXELS - unusedEachEnd; i++)
_pStrip->setPixelColor(i, COLOR_DARK_RED);
_pStrip->show();
delay(20);
return;
}
for (uint16_t i = 0; i < NUMBER_USED_PIXELS; i++)
_pStrip->setPixelColor(i, COLOR_RED);
_pStrip->show();
}
};
// SignalEvent
//
// Handles let turns, right turtns, and standard hazards (simply both signals at once)
class SignalEvent : public LightingEvent
{
public:
enum SIGNAL_STYLE
{
INVALID = 0,
LEFT_TURN,
RIGHT_TURN,
HAZARD
};
private:
const float SequentialBloomStart = 0.00f;
const float SequentialBloomTime = 0.50f;
const float SequentialHoldStart = SequentialBloomStart + SequentialBloomTime;
const float SequentialHoldTime = 0.25f;
const float SequentialFadeStart = SequentialHoldStart + SequentialHoldTime;
const float SequentialFadeTime = 0.125f;
const float SequentialOffStart = SequentialFadeStart + SequentialFadeTime;
const float SequentialOffTime = 0.25f;
const float SequentialCycleTime = SequentialOffStart + SequentialOffTime;
// SetTurnLED
//
// Depending on which way the signal is turning, light up it's LED on the correct
// end of the light strip
void SetTurnLED(int i, uint32_t color)
{
if (_style == LEFT_TURN || _style == HAZARD)
_pStrip->setPixelColor(i, color);
if (_style == RIGHT_TURN || _style == HAZARD)
_pStrip->setPixelColor(NUMBER_USED_PIXELS - 1 - i, color);
}
SIGNAL_STYLE _style;
public:
SignalEvent(Adafruit_NeoPixel * pStrip)
: LightingEvent(pStrip)
{
}
SignalEvent(Adafruit_NeoPixel * pStrip, SIGNAL_STYLE style)
: LightingEvent(pStrip),
_style(style)
{
}
virtual void Draw() override
{
if (false == GetActive())
return;
float fCyclePosition = fmod(TimeElapsedTotal(), SequentialCycleTime);
if (fCyclePosition > SequentialOffStart)
{
for (int i = 0; i < NUMBER_TURN_PIXELS; i++)
SetTurnLED(i, COLOR_BLACK);
}
else if (fCyclePosition > SequentialFadeStart)
{
fCyclePosition -= SequentialFadeStart;
float pctComplete = fCyclePosition / SequentialFadeTime;
int cPixelsLit = NUMBER_TURN_PIXELS - (NUMBER_TURN_PIXELS * pctComplete);
for (int i = 0; i < NUMBER_TURN_PIXELS; i++)
{
if (i < cPixelsLit)
SetTurnLED(i, COLOR_AMBER);
else
SetTurnLED(i, COLOR_BLACK);
}
}
else if (fCyclePosition > SequentialHoldStart)
{
for (int i = 0; i < NUMBER_TURN_PIXELS; i++)
SetTurnLED(i, COLOR_AMBER);
}
else
{
assert(fCyclePosition <= SequentialBloomTime);
float pctComplete = fCyclePosition / SequentialBloomTime;
int cPixelsLit = (NUMBER_TURN_PIXELS * pctComplete);
for (int i = 0; i < NUMBER_TURN_PIXELS; i++)
{
if (i >= (NUMBER_TURN_PIXELS - cPixelsLit))
SetTurnLED(i, COLOR_AMBER);
else
SetTurnLED(i, COLOR_BLACK);
}
}
_pStrip->show();
}
};
// PoliceLightBarState
//
// The police bar breaks the light strip into 8 sections, and then alternates patterns based on a table
struct PoliceLightBarState
{
uint32_t sectionColor[8];
uint32_t duration;
};
class PoliceLightBar : public LightingEvent
{
static const PoliceLightBarState _PoliceBarStates1[11]; // Sadly we must spec arraysize because its a forward declaration
public:
PoliceLightBar(Adafruit_NeoPixel * pStrip)
: LightingEvent(pStrip)
{
}
virtual void Draw() override
{
if (false == GetActive())
return;
const size_t sectionSize = NUMBER_USED_PIXELS / 8;
for (size_t row = 0; row < ARRAYSIZE(_PoliceBarStates1); row++)
{
for (uint16_t i = 0; i < NUMBER_USED_PIXELS; i++)
{
int iSection = i / sectionSize;
const uint32_t color = (_PoliceBarStates1[row].sectionColor[iSection]);
_pStrip->setPixelColor(i, color);
}
_pStrip->show();
delay(_PoliceBarStates1[row].duration);
}
}
};
const PoliceLightBarState PoliceLightBar::_PoliceBarStates1[] =
{
{ { COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED }, 200 },
{ { COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE }, 200 },
{ { COLOR_WHITE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED }, 20 },
{ { COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_WHITE }, 20 },
{ { COLOR_BLUE, COLOR_WHITE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED }, 20 },
{ { COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_WHITE, COLOR_RED }, 20 },
{ { COLOR_BLUE, COLOR_BLUE, COLOR_WHITE, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED }, 20 },
{ { COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_WHITE, COLOR_RED, COLOR_RED }, 20 },
{ { COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_WHITE, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED }, 20 },
{ { COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_WHITE, COLOR_BLUE, COLOR_RED, COLOR_RED }, 20 },
{ { COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE, COLOR_RED, COLOR_RED, COLOR_BLUE, COLOR_BLUE }, 200 },
};