-
Notifications
You must be signed in to change notification settings - Fork 5
/
eyeblink.cpp
412 lines (364 loc) · 11.1 KB
/
eyeblink.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* eyeblink.cpp
*
* Create a pair of blinking eyes that fade in, blink a few times, then
* fade away.
*
* The Eyeblink animation is designed so that many instances can run simultaneously
* on a single LED strip. And if other strip animation code also uses millis()
* scheduling and doesn't fiddle with the Neopixel pixels buffer, they could probably
* run alongside this code, as well.
*
* Suggestions and GitHub pull requests are welcome.
*
* @author Dougal Campbell <[email protected]>
* @see https://community.particle.io/t/halloween-blinky-eyes/16065
* @see http://dougal.gunters.org/
*/
#include "eyeblinks.h"
// Constructor
Eyeblink::Eyeblink(Adafruit_NeoPixel* s, uint16_t start, uint8_t sep, uint32_t col)
{
setStrip(s);
startPos = start;
eyeSep = sep;
color = col;
state = WAITING;
colorCurrent = 0x00000000;
fadeInTime = random(0, 4000) + 1000;
fadeOutTime = random(500, 4000);
blinksMin = 2;
blinksMax = 6;
blinkCount = random(blinksMin, blinksMax);
startEvent = millis();
nextEvent = startEvent + random(15000);
if (debuglevel >= LOG_INFO) {
Serial.println("Eyeblink init");
}
}
Eyeblink::Eyeblink(Adafruit_NeoPixel* s)
{
setStrip(s);
startPos = 0;
eyeSep = 1;
color = 0x00ffffff;
state = WAITING;
colorCurrent = 0x00000000;
fadeInTime = 0;
fadeOutTime = 0;
blinksMin = 0;
blinksMax = 5;
blinkCount = random(blinksMin, blinksMax);
startEvent = millis();
nextEvent = startEvent + random(15000);
if (debuglevel >= LOG_INFO) {
Serial.println("Eyeblink created");
}
}
/**
* Empty constructor
*
* Sets a few sane defaults, but gives you complete freedom to
* override them, of course. Dont forget to call setStrip(),
* setStartEvent(), and setNextEvent() in your main code.
*/
Eyeblink::Eyeblink()
{
state = WAITING;
startPos = 0;
eyeSep = 2;
color = 0x00ffffff;
colorCurrent = 0x00000000;
fadeInTime = 1000;
fadeOutTime = 1000;
blinksMin = 0;
blinksMax = 5;
blinkCount= random(blinksMin, blinksMax + 1);
startEvent = 0;
nextEvent = 0;
if (debuglevel >= LOG_INFO) {
Serial.println("Eyeblink init");
}
}
Eyeblink::~Eyeblink()
{
// Go dark on destruction
color = 0x00000000;
colorCurrent = color;
draw();
}
/**
* State machine. Wait for the nextEvent time, and change states.
*
* (blinkCount)
* +-----+
* v |
* WAITING -> FADEIN -> ON -> OFF
* ^ |
* | v
* +---------------FADEOUT
*
* @TODO * Put the more of the wait times into variables that can be configured
* at run-time. E.g., blink on/off time.
*
* * Smart automatic randomization/distribution of eyes along the strip.
*
* * Matrix support?
*/
void Eyeblink::step() {
uint32_t currentTime = millis();
switch (state) {
// "Baby, what time is it?"
case WAITING:
if (currentTime > nextEvent) {
// transition WAITING -> FADEIN
state = FADEIN;
startEvent = currentTime;
nextEvent = startEvent + fadeInTime;
if (debuglevel >= LOG_INFO) {
Serial.print("WAITING complete. Going to FADEIN in ");
Serial.print(nextEvent - startEvent);
Serial.println("ms...");
}
}
break;
case FADEIN:
if (currentTime > nextEvent) {
// transition FADEIN -> ON
state = ON;
colorCurrent = color;
startEvent = currentTime;
nextEvent = startEvent + random(1000, 4000);
if (debuglevel >= LOG_INFO) {
Serial.print("FADEIN complete. Going to ON in ");
Serial.print(nextEvent - startEvent);
Serial.println("ms...");
}
} else {
// We're in mid-fade:
// scale color according to current time, in relation to start/end time
colorCurrent = scaleColor(color, currentTime, startEvent, nextEvent);
}
break;
case ON:
if (currentTime > nextEvent) {
if (blinkCount >= 0) {
// blinking... transition ON -> OFF
state = OFF;
colorCurrent = 0x00000000; // black
startEvent = currentTime;
nextEvent = startEvent + random(50, 150);
if (debuglevel >= LOG_INFO) {
Serial.print("ON complete. Going to OFF in ");
Serial.print(nextEvent - startEvent);
Serial.println("ms...");
}
} else {
// fade out... transition ON -> FADEOUT
state = FADEOUT;
startEvent = currentTime;
nextEvent = startEvent + fadeOutTime;
}
}
break;
case OFF:
if (currentTime > nextEvent) {
// blinking... transition OFF -> ON
state = ON;
colorCurrent = color;
startEvent = currentTime;
nextEvent = startEvent + random(100, 1500);
--blinkCount; // reduce number of remaining blinks
if (debuglevel >= LOG_TRACE) {
Serial.print("OFF complete. Back to ON in ");
Serial.print(nextEvent - startEvent);
Serial.println("ms...");
}
}
break;
case FADEOUT:
if (currentTime > nextEvent) {
// transition FADEOUT -> WAITING
state = WAITING;
colorCurrent = 0x00000000; // black
nextEvent = currentTime + 10000 + random(50000) - random(4000) - random(500) - random(500);
startEvent = currentTime;
blinkCount = random(blinksMin, blinksMax+1);
if (debuglevel >= LOG_INFO) {
Serial.print("FADEOUT complete. Back to WAITING in ");
Serial.print(nextEvent - startEvent);
Serial.println("ms...");
}
} else {
// scale color down according to current time, in relation to start/end time
colorCurrent = scaleColor(color, currentTime, nextEvent, startEvent);
}
break;
default:
// THIS SHOULD NEVER HAPPEN! but reset, if it does...
reset();
if (debuglevel >= LOG_ERROR) {
Serial.println("Where am I?!? Unknown state encountered.");
}
}
}
void Eyeblink::draw() {
// Set our two eyes, according to the position, separation, and current color
strip->setPixelColor(startPos, colorCurrent);
strip->setPixelColor(startPos + eyeSep, colorCurrent);
// NOISY!
if (debuglevel >= LOG_TRACE) {
Serial.print("startPos: ");
Serial.print(startPos);
Serial.print(", eyeSep: ");
Serial.print(eyeSep);
Serial.print(", color: ");
Serial.println(color, HEX);
}
}
uint32_t Eyeblink::scaleColor(uint32_t color, uint32_t scale, uint32_t min, uint32_t max) {
/**
* Cheap way to adjust brightness of a color, based on a sliding value between
* a min and max.
*/
// bust out the color components
uint8_t r = color >> 16 & 0xff;
uint8_t g = color >> 8 & 0xff;
uint8_t b = color & 0xff;
uint8_t factor = map(scale, min, max, 0, 255);
// Scale the values
r = ((uint16_t) r * factor) / 255;
g = ((uint16_t) g * factor) / 255;
b = ((uint16_t) b * factor) / 255;
uint32_t clr = ((uint32_t) r << 16 | (uint32_t) g << 8 | (uint32_t) b);
// NOISY!
if (debuglevel >= LOG_TRACE) {
Serial.print(" scale: ");
Serial.print(scale);
Serial.print(" min: ");
Serial.print(min);
Serial.print(" max: ");
Serial.print(max);
Serial.print(" factor: ");
Serial.println(factor);
Serial.print(" original color: ");
Serial.println(color, HEX);
Serial.print(" scale: ");
Serial.println(factor);
Serial.print(" new color: ");
Serial.println(clr, HEX);
Serial.print(" rgb: ");
Serial.print(r, HEX);
Serial.print(", ");
Serial.print(g, HEX);
Serial.print(", ");
Serial.println(b, HEX);
}
return clr;
}
/**
* reset()
*
* Reset an instance
*/
void Eyeblink::reset() {
startEvent = millis();
nextEvent = startEvent + fadeInTime;
colorCurrent = color;
state = WAITING;
if (debuglevel >= LOG_INFO) {
Serial.print("RESET. Back to WAITING. Waiting for ");
Serial.print(fadeInTime);
Serial.println("ms...");
}
}
/**
* Getters
*/
uint32_t Eyeblink::getColor() {
return color;
}
// getColorCurrent is probably of limited usefulness. I'm just including
// it for the sake of completeness. And who knows...?
uint32_t Eyeblink::getColorCurrent() {
return colorCurrent;
}
uint16_t Eyeblink::getFadeInTime() {
return fadeInTime;
}
uint16_t Eyeblink::getFadeOutTime() {
return fadeOutTime;
}
uint8_t Eyeblink::getBlinksMin() {
return blinksMin;
}
uint8_t Eyeblink::getBlinksMax() {
return blinksMax;
}
uint8_t Eyeblink::getBlinkCount() {
return blinkCount;
}
uint16_t Eyeblink::getStartPos() {
return startPos;
}
uint8_t Eyeblink::getEyeSep() {
return eyeSep;
}
uint32_t Eyeblink::getStartEvent() {
return startEvent;
}
uint32_t Eyeblink::getNextEvent() {
return nextEvent;
}
uint8_t Eyeblink::getState() {
return state;
}
Adafruit_NeoPixel* Eyeblink::getStrip() {
return (Adafruit_NeoPixel *) strip;
}
/**
* Setters
*/
void Eyeblink::setColor(uint32_t newColor) {
color = newColor;
}
// setColorCurrent is probably of limited usefulness. I'm just including
// it for the sake of completeness. And who knows...?
void Eyeblink::setColorCurrent(uint32_t newColorCurrent) {
colorCurrent = newColorCurrent;
}
void Eyeblink::setFadeInTime(uint32_t newFadeInTime) {
fadeInTime = newFadeInTime;
}
void Eyeblink::setFadeOutTime(uint32_t newFadeOutTime) {
fadeOutTime = newFadeOutTime;
}
void Eyeblink::setBlinksMin(uint8_t newBlinksMin) {
blinksMin = newBlinksMin;
}
void Eyeblink::setBlinksMax(uint8_t newBlinksMax) {
blinksMax = newBlinksMax;
}
void Eyeblink::setBlinkCount(uint8_t newBlinkCount) {
blinkCount = newBlinkCount;
}
void Eyeblink::setStartPos(uint16_t newStartPos) {
startPos = newStartPos;
}
void Eyeblink::setEyeSep(uint8_t newEyeSep) {
eyeSep = newEyeSep;
}
void Eyeblink::setStartEvent(uint32_t newStartEvent) {
startEvent = newStartEvent;
}
void Eyeblink::setNextEvent(uint32_t newNextEvent) {
nextEvent = newNextEvent;
}
void Eyeblink::setState(uint8_t newState) {
state = newState;
}
void Eyeblink::setStrip(Adafruit_NeoPixel* newStrip) {
// Keep a reference to a NeoPixel strip
strip = newStrip;
}
// - fin -