-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.cpp
427 lines (330 loc) · 11 KB
/
main.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "FastLED.h"
#include "FX.h"
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 IRAM_ATTR myRedWhiteBluePalette_p;
#include "palettes.h"
//#define NUM_LEDS 512
#define NUM_LEDS 40
#define DATA_PIN_1 13
#define DATA_PIN_2 18
#define BRIGHTNESS 80
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
#define N_COLORS 17
static const CRGB colors[N_COLORS] = {
CRGB::Red,
CRGB::Green,
CRGB::Blue,
CRGB::White,
CRGB::AliceBlue,
CRGB::ForestGreen,
CRGB::Lavender,
CRGB::MistyRose,
CRGB::DarkOrchid,
CRGB::DarkOrange,
CRGB::Black,
CRGB::Teal,
CRGB::Violet,
CRGB::Lime,
CRGB::Chartreuse,
CRGB::BlueViolet,
CRGB::Aqua
};
static const char *colors_names[N_COLORS] {
"Red",
"Green",
"Blue",
"White",
"aliceblue",
"ForestGreen",
"Lavender",
"MistyRose",
"DarkOrchid",
"DarkOrange",
"Black",
"Teal",
"Violet",
"Lime",
"Chartreuse",
"BlueViolet",
"Aqua"
};
extern "C" {
void app_main();
}
/* test using the FX unit
**
*/
static void blinkWithFx_allpatterns(void *pvParameters) {
uint16_t mode = FX_MODE_STATIC;
WS2812FX ws2812fx;
ws2812fx.init(NUM_LEDS, leds1, false); // type was configured before
ws2812fx.setBrightness(255);
ws2812fx.setMode(0 /*segid*/, mode);
// microseconds
uint64_t mode_change_time = esp_timer_get_time();
while (true) {
if ((mode_change_time + 10000000L) < esp_timer_get_time() ) {
mode += 1;
mode %= MODE_COUNT;
mode_change_time = esp_timer_get_time();
ws2812fx.setMode(0 /*segid*/, mode);
printf(" changed mode to %d\n", mode);
}
ws2812fx.service();
vTaskDelay(10 / portTICK_PERIOD_MS); /*10ms*/
}
};
/* test specific patterns so we know FX is working right
**
*/
typedef struct {
const char *name;
int mode;
int secs; // secs to test it
uint32_t color;
int speed;
} testModes_t;
static const testModes_t testModes[] = {
{ "color wipe: all leds after each other up. Then off. Repeat. RED", FX_MODE_COLOR_WIPE, 5, 0xFF0000, 1000 },
{ "color wipe: all leds after each other up. Then off. Repeat. RGREE", FX_MODE_COLOR_WIPE, 5, 0x00FF00, 1000 },
{ "color wipe: all leds after each other up. Then off. Repeat. Blu", FX_MODE_COLOR_WIPE, 5, 0x0000FF, 1000 },
{ "chase rainbow: Color running on white.", FX_MODE_CHASE_RAINBOW, 10, 0xffffff, 200 },
{ "breath, on white.", FX_MODE_BREATH, 5, 0xffffff, 100 },
{ "breath, on red.", FX_MODE_BREATH, 5, 0xff0000, 100 },
{ "what is twinkefox? on red?", FX_MODE_TWINKLEFOX, 20, 0xff0000, 2000 },
};
#define TEST_MODES_N ( sizeof(testModes) / sizeof(testModes_t))
static void blinkWithFx_test(void *pvParameters) {
WS2812FX ws2812fx;
WS2812FX::Segment *segments = ws2812fx.getSegments();
ws2812fx.init(NUM_LEDS, leds1, false); // type was configured before
ws2812fx.setBrightness(255);
int test_id = 0;
printf(" start mode: %s\n",testModes[test_id].name);
ws2812fx.setMode(0 /*segid*/, testModes[test_id].mode);
segments[0].colors[0] = testModes[test_id].color;
segments[0].speed = testModes[test_id].speed;
uint64_t nextMode = esp_timer_get_time() + (testModes[test_id].secs * 1000000L );
while (true) {
uint64_t now = esp_timer_get_time();
if (nextMode < now ) {
test_id = (test_id +1) % TEST_MODES_N;
nextMode = esp_timer_get_time() + (testModes[test_id].secs * 1000000L );
ws2812fx.setMode(0 /*segid*/, testModes[test_id].mode);
segments[0].colors[0] = testModes[test_id].color;
segments[0].speed = testModes[test_id].speed;
printf(" changed mode to: %s\n",testModes[test_id].name);
}
ws2812fx.service();
vTaskDelay(10 / portTICK_PERIOD_MS); /*10ms*/
}
};
/*
** chase sequences are good for testing correctness, because you can see
** that the colors are correct, and you can see cases where the wrong pixel is lit.
*/
#define CHASE_DELAY 200
void blinkLeds_chase2(void *pvParameters) {
while(true) {
for (int ci = 0; ci < N_COLORS; ci++) {
CRGB color = colors[ci];
printf(" chase: *** color %s ***\n",colors_names[ci]);
// set strings to black first
fill_solid(leds1, NUM_LEDS, CRGB::Black);
fill_solid(leds2, NUM_LEDS, CRGB::Black);
FastLED.show();
int prev;
// forward
printf(" chase: forward\n");
prev = -1;
for (int i = 0; i < NUM_LEDS; i++) {
if (prev >= 0) {
leds2[prev] = leds1[prev] = CRGB::Black;
}
leds2[i] = leds1[i] = color;
prev = i;
FastLED.show();
delay(CHASE_DELAY);
}
printf(" chase: backward\n");
prev = -1;
for (int i = NUM_LEDS-1; i >= 0; i--) {
if (prev >= 0) {
leds2[prev] = leds1[prev] = CRGB::Black;
}
leds2[i] = leds1[i] = color;
prev = i;
FastLED.show();
delay(CHASE_DELAY);
}
// two at a time
printf(" chase: twofer\n");
prev = -1;
for (int i = 0; i < NUM_LEDS; i += 2) {
if (prev >= 0) {
leds2[prev] = leds1[prev] = CRGB::Black;
leds2[prev+1] = leds1[prev+1] = CRGB::Black;
}
leds2[i] = leds1[i] = color;
leds2[i+1] = leds1[i+1] = color;
prev = i;
FastLED.show();
delay(CHASE_DELAY);
}
} // for all colors
} // while true
}
void ChangePalettePeriodically(){
uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 99;
if( lastSecond != secondHand) {
lastSecond = secondHand;
if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
}
}
void blinkLeds_interesting(void *pvParameters){
while(1){
printf("blink leds\n");
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
for( int i = 0; i < NUM_LEDS; i++) {
leds1[i] = ColorFromPalette( currentPalette, startIndex, 64, currentBlending);
leds2[i] = ColorFromPalette( currentPalette, startIndex, 64, currentBlending);
startIndex += 3;
}
printf("show leds\n");
FastLED.show();
delay(400);
};
};
// Going to use the ESP timer system to attempt to get a frame rate.
// According to the documentation, this is a fairly high priority,
// and one should attempt to do minimal work - such as dispatching a message to a queue.
// at first, let's try just blasting pixels on it.
// Target frames per second
#define FASTFADE_FPS 30
typedef struct {
CHSV color;
} fastfade_t;
static void _fastfade_cb(void *param){
fastfade_t *ff = (fastfade_t *)param;
ff->color.hue++;
if (ff->color.hue % 10 == 0) {
printf("fast hsv fade h: %d s: %d v: %d\n",ff->color.hue,ff->color.s, ff->color.v);
}
fill_solid(leds1,NUM_LEDS,ff->color);
fill_solid(leds2,NUM_LEDS,ff->color);
FastLED.show();
};
static void fastfade(void *pvParameters){
fastfade_t ff_t = {
.color = CHSV(0/*hue*/,255/*sat*/,255/*value*/)
};
esp_timer_create_args_t timer_create_args = {
.callback = _fastfade_cb,
.arg = (void *) &ff_t,
.dispatch_method = ESP_TIMER_TASK,
.name = "fastfade_timer"
};
esp_timer_handle_t timer_h;
esp_timer_create(&timer_create_args, &timer_h);
esp_timer_start_periodic(timer_h, 1000000L / FASTFADE_FPS );
// suck- just trying this
while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS);
};
}
void blinkLeds_simple(void *pvParameters){
while(1){
for (int j=0;j<N_COLORS;j++) {
printf("blink leds\n");
for (int i=0;i<NUM_LEDS;i++) {
leds1[i] = colors[j];
leds2[i] = colors[j];
}
FastLED.show();
delay(1000);
};
}
};
#define N_COLORS_CHASE 7
CRGB colors_chase[N_COLORS_CHASE] = {
CRGB::AliceBlue,
CRGB::Lavender,
CRGB::DarkOrange,
CRGB::Red,
CRGB::Green,
CRGB::Blue,
CRGB::White,
};
void blinkLeds_chase(void *pvParameters) {
int pos = 0;
int led_color = 0;
while(1){
printf("chase leds\n");
// do it the dumb way - blank the leds
for (int i=0;i<NUM_LEDS;i++) {
leds1[i] = CRGB::Black;
leds2[i] = CRGB::Black;
}
// set the one LED to the right color
leds1[pos] = leds2[pos] = colors_chase[led_color];
pos = (pos + 1) % NUM_LEDS;
// use a new color
if (pos == 0) {
led_color = (led_color + 1) % N_COLORS_CHASE ;
}
uint64_t start = esp_timer_get_time();
FastLED.show();
uint64_t end = esp_timer_get_time();
printf("Show Time: %" PRIu64 "\n",end-start);
delay(200);
};
}
void app_main() {
printf(" entering app main, call add leds\n");
// the WS2811 family uses the RMT driver
FastLED.addLeds<LED_TYPE, DATA_PIN_1>(leds1, NUM_LEDS);
FastLED.addLeds<LED_TYPE, DATA_PIN_2>(leds2, NUM_LEDS);
// this is a good test because it uses the GPIO ports, these are 4 wire not 3 wire
//FastLED.addLeds<APA102, 13, 15>(leds, NUM_LEDS);
printf(" set max power\n");
// I have a 2A power supply, although it's 12v
FastLED.setMaxPowerInVoltsAndMilliamps(12,2000);
// change the task below to one of the functions above to try different patterns
printf("create task for led blinking\n");
//xTaskCreatePinnedToCore(&blinkLeds_simple, "blinkLeds", 4000, NULL, 5, NULL, 0);
//xTaskCreatePinnedToCore(&fastfade, "blinkLeds", 4000, NULL, 5, NULL, 0);
//xTaskCreatePinnedToCore(&blinkWithFx_allpatterns, "blinkLeds", 4000, NULL, 5, NULL, 0);
xTaskCreatePinnedToCore(&blinkWithFx_test, "blinkLeds", 4000, NULL, 5, NULL, 0);
//xTaskCreatePinnedToCore(&blinkLeds_chase, "blinkLeds", 4000, NULL, 5, NULL, 0);
//xTaskCreatePinnedToCore(&blinkLeds_chase2, "blinkLeds", 4000, NULL, 5, NULL, 0);
}