-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneopixel.h
424 lines (376 loc) · 12.1 KB
/
neopixel.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
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
413
414
415
416
417
418
419
420
421
422
423
424
// this file has the code to setup and run the webserver.
// it seperated out to make the main code easier to read
// class: https://learn.adafruit.com/multi-tasking-the-arduino-part-3
// derived from: https://gist.github.com/oesterle/69e4ebb0a5591bbf586564ee1dc72b58
#ifndef neopixel_h
#define neopixel_h
#include <Adafruit_NeoPixel.h>
#include <ArduinoJson.h>
// Adafruit Feather M4 Express uses pin 8 for its built-in NeoPixel.
#define NEOPIXEL_PIN 8
// Pattern types supported: (remember to update the ActivePatternName function when assing a new pattern)
enum pattern { OFF, ON, RAINBOW_CYCLE, COLOR_WIPE, FADE };
// what to do when the pattern completes (remember to update the RepeatPatternName function)
enum repeatPattern { STOP, TOGGLE, REVERSE, FORWARD};
// NeoPattern Class - derived from the Adafruit_NeoPixel class
class NeoPatterns : public Adafruit_NeoPixel
{
public:
// Member Variables:
pattern ActivePattern; // which pattern is running
bool Forward; // direction to run the pattern false=reverse
repeatPattern Repeat; // what to do when the pattern complete
unsigned long Interval; // milliseconds between updates
unsigned long lastUpdate; // last update of position
uint32_t Color1, Color2; // What colors are in use
uint16_t TotalSteps; // total number of steps in the pattern
uint16_t Index; // current step within the pattern
uint8_t MaxBright=3; // maxBright is from 0 to 255; low numbers are dimmer.
void (*OnComplete)(); // Callback on completion of pattern
// Constructor - calls base-class constructor to initialize strip
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
:Adafruit_NeoPixel(pixels, pin, type)
{
OnComplete = callback;
}
// Update the pattern
void Update()
{
if((millis() - lastUpdate) > Interval) // time to update
{
lastUpdate = millis();
switch(ActivePattern)
{
case OFF:
OffUpdate();
break;
case ON:
OnUpdate();
break;
case RAINBOW_CYCLE:
RainbowCycleUpdate();
break;
case COLOR_WIPE:
ColorWipeUpdate();
break;
case FADE:
FadeUpdate();
break;
default:
break;
}
}
}
// Increment the Index and reset at the end
void Increment()
{
if (Forward)
{
Index++;
if (Index >= TotalSteps)
{
//to avoid bouncing back and forth, do not reset the index if in toggle mode
if (Repeat != TOGGLE) { Index = 1; }
if (OnComplete != NULL)
{
OnComplete(); // call the comlpetion callback
}
} else if (Index <= 0) {
Index = TotalSteps-1;
}
}
else // Direction == REVERSE
{
Index--;
if (Index <= 1)
{
//to avoid bouncing back and forth, do not reset the index if in toggle mode
if (Repeat != TOGGLE) { Index = TotalSteps-1; }
if (OnComplete != NULL)
{
OnComplete(); // call the comlpetion callback
}
} else if (Index >= TotalSteps) {
Index = 1;
}
}
}
// Flip pattern direction
void ToggleDirection()
{
Forward =!Forward;
}
// enforce pattern direction
void SetDirection(bool dir)
{
Forward = dir;
}
//return the name of the enum currently active
String ActivePatternName(){
switch(ActivePattern)
{
case OFF:
return "OFF";
break;
case ON:
return "ON";
break;
case RAINBOW_CYCLE:
return "RAINBOW_CYCLE";
break;
case COLOR_WIPE:
return "COLOR_WIPE";
break;
case FADE:
return "FADE";
break;
default:
break;
}
}
//return the name of the repeat enum
String RepeatPatternName(){
switch(Repeat)
{
case STOP:
return "STOP";
break;
case TOGGLE:
return "TOGGLE";
break;
case REVERSE:
return "REVERSE";
break;
case FORWARD:
return "FORWARD";
break;
default:
break;
}
}
//return the enum matching the passed name
pattern ActivePatternId(const char *ActivePatternName){
if (!strcmp(ActivePatternName, "OFF")) { return OFF;
} else if (!strcmp(ActivePatternName, "ON")) { return ON;
} else if (!strcmp(ActivePatternName, "RAINBOW_CYCLE")) { return RAINBOW_CYCLE;
} else if (!strcmp(ActivePatternName, "COLOR_WIPE")) { return COLOR_WIPE;
} else if (!strcmp(ActivePatternName, "FADE")) { return FADE;
}
}
//return the enum matching the passed repeat name
repeatPattern RepeatPatternID(const char *RepeatPatternName){
if (!strcmp(RepeatPatternName, "STOP")) { return STOP;
} else if (!strcmp(RepeatPatternName, "TOGGLE")) { return TOGGLE;
} else if (!strcmp(RepeatPatternName, "REVERSE")) { return REVERSE;
} else if (!strcmp(RepeatPatternName, "FORWARD")) { return FORWARD;
}
}
//expose the Color function
uint32_t xColor(uint8_t rd, uint8_t gr, uint8_t bl) { return Color(rd, gr, bl); }
//a function to update all the variables
void Update(const char *ActivePatternName, uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, bool dir, const char *RepeatPatternName){
ActivePattern = ActivePatternId(ActivePatternName);
Color1 = color1;
Color2 = color2;
Interval = interval;
TotalSteps = steps;
Index = 0;
Forward = dir ;
Repeat = RepeatPatternID(RepeatPatternName);
}
// Initialize for turning OFF the LED
void Off()
{
ActivePattern = OFF;
fill(Color(0, 0, 0)); //set all Pixels to off/black
show(); //this sets to color above (black)
show(); //the second turns off the LEDs
}
// Update the OFF Pattern
void OffUpdate()
{
//ensure that the pattern is applied even if initialize function is skipped over
// but only write once
if (Index == 0) {
fill(Color(0, 0, 0)); //set all Pixels to off/black
show(); //this sets to color above (black)
show(); //the second turns off the LEDs
Index++; //is now nono-zero
}
Increment();
}
// Initialize for turning ON the LED
void On(uint32_t color)
{
Color1 = color;
ActivePattern = ON;
fill(Color1); //set all Pixels to primary color
show();
}
// Update the ON Pattern
void OnUpdate()
{
//ensure that the pattern is applied even if initialize function is skipped over
// but only write once
if (Index == 0) {
fill(Color1); //set all Pixels to primary color
show();
}
Increment();
}
// Initialize for a RainbowCycle
void RainbowCycle(uint8_t interval, bool frwrd = true)
{
ActivePattern = RAINBOW_CYCLE;
Interval = interval;
TotalSteps = 255;
Index = 0;
Forward - frwrd;
}
// Update the Rainbow Cycle Pattern
void RainbowCycleUpdate()
{
for(int i=0; i< numPixels(); i++)
{
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
}
show();
Increment();
}
// Initialize for a ColorWipe
void ColorWipe(uint32_t color, uint8_t interval, bool frwrd = true)
{
ActivePattern = COLOR_WIPE;
Interval = interval;
TotalSteps = numPixels();
Color1 = color;
Index = 0;
Forward = frwrd;
}
// Update the Color Wipe Pattern
void ColorWipeUpdate()
{
setPixelColor(Index, Color1);
show();
Increment();
}
// Initialize for a Fade
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, bool frwrd = true)
{
ActivePattern = FADE;
Interval = interval;
TotalSteps = steps;
Color1 = color1;
Color2 = color2;
Index = 0;
Forward = frwrd;
}
// Update the Fade Pattern
void FadeUpdate()
{
// Calculate linear interpolation between Color1 and Color2
// Optimise order of operations to minimize truncation error
uint8_t red;
uint8_t green;
uint8_t blue;
red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
fill(Color(red, green, blue));
show();
Increment();
}
// Returns the Red component of a 32-bit color
uint8_t Red(uint32_t color)
{
return (color >> 16) & 0xFF;
}
// Returns the Green component of a 32-bit color
uint8_t Green(uint32_t color)
{
return (color >> 8) & 0xFF;
}
// Returns the Blue component of a 32-bit color
uint8_t Blue(uint32_t color)
{
return color & 0xFF;
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
uint16_t r = 0, g = 0, b = 0;
if(WheelPos < 85) {
r = 255 - WheelPos * 3;
b = WheelPos * 3;
} else if(WheelPos < 170) {
WheelPos -= 85;
g = WheelPos * 3;
b = 255 - WheelPos * 3;
} else {
WheelPos -= 170;
r = WheelPos * 3;
g = 255 - WheelPos * 3;
}
r = r * MaxBright / 255;
g = g * MaxBright / 255;
b = b * MaxBright / 255;
return Color(r, g, b);
}
//return all settings as a JSON string
String json() {
char hex_string[20] = "#";
StaticJsonDocument<255> neopixelsJsonDoc;
neopixelsJsonDoc["NeoPixel"]["Pattern"] = ActivePatternName();
sprintf(hex_string+1, "%06X", Color1); //convert number to hex
neopixelsJsonDoc["NeoPixel"]["Color1"] = hex_string;
sprintf(hex_string+1, "%06X", Color2); //convert number to hex
neopixelsJsonDoc["NeoPixel"]["Color2"] = hex_string;
neopixelsJsonDoc["NeoPixel"]["Interval"] = Interval;
neopixelsJsonDoc["NeoPixel"]["Steps"] = TotalSteps;
neopixelsJsonDoc["NeoPixel"]["Forward"] = Forward;
neopixelsJsonDoc["NeoPixel"]["Repeat"] = RepeatPatternName();
String outJson;
serializeJson(neopixelsJsonDoc, outJson);
// Serial.print(F("neopixel JSON: ")); Serial.println(outJson);
return outJson;
}
};
void neopixelComplete();
NeoPatterns neopixelLED(1, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800, &neopixelComplete);
void neopixelComplete() {
//if no changes are declared here, the existing pattern repeats
switch(neopixelLED.Repeat)
{
case STOP:
neopixelLED.Off();
break;
case TOGGLE:
neopixelLED.ToggleDirection();;
break;
case REVERSE:
neopixelLED.SetDirection(false);
break;
case FORWARD:
neopixelLED.SetDirection(true);
break;
default:
break;
}
}
void neopixel_setup() {
neopixelLED.begin();
// neopixelLED.RainbowCycle(25);
// neopixelLED.Fade(0x0F0000, 0x00000F, 6, 255);
neopixelLED.Update(
"RAINBOW_CYCLE", //ActivePattern
0x1F0000, //Color1
0x00001F, //Color2
255, //TotalSteps
16, //Itnterval
true, //Forward
"TOGGLE" //Repeat
);
}
#endif //neopixel_h