FastLED 3.10.10
Loading...
Searching...
No Matches
Chromancer.ino
Go to the documentation of this file.
1// @filter: (memory is large) and (board is not adafruit_feather_nrf52840_sense)
2// The Adafruit Feather nRF52840 Sense marks D1 invalid in its variant table (UART RX),
3// so the hardcoded pin-1 strip this sketch drives cannot be routed there. See #5300.
4
14
15/*
16 Original Source: https://github.com/ZackFreedman/Chromance
17 GaryWoo's Video: https://www.youtube.com/watch?v=-nSCtxa2Kp0
18 GaryWoo's LedMap: https://gist.github.com/Garywoo/b6cd1ea90cb5e17cc60b01ae68a2b770
19 GaryWoo's presets: https://gist.github.com/Garywoo/82fa67c6e1f9529dc16a01dd97d05d58
20 Chromance wall hexagon source (emotion controlled w/ EmotiBit)
21 Partially cribbed from the DotStar example
22 I smooshed in the ESP32 BasicOTA sketch, too
23
24 (C) Voidstar Lab 2021
25*/
26
27// FastLED.h must be included first to trigger precompiled headers for FastLED's build system
28#include "FastLED.h"
29
31#include "fl/log/log.h"
32#include "fl/stl/stdio.h"
33
34#include <FastLED.h>
35
36#include "fl/math/screenmap.h"
37#include "fl/math/math.h"
38#include "fl/stl/json.h"
39#include "fl/ui/ui.h"
40#include "fl/stl/map.h"
41
42#include "fl/stl/string.h"
43
44#include "./screenmap.json.h"
45#include "./mapping.h"
46#include "./ripple.h"
47#include "./detail.h"
48
49enum {
54};
55
56
57// Strips are different lengths because I am a dumb
58
59constexpr int lengths[] = {
60 154, // Black strip
61 168, // Green strip
62 84, // Red strip
63 154 // Blue strip
64};
65
66
67
68
69
70
71
72// non emscripten uses separate arrays for each strip. Eventually emscripten
73// should support this as well but right now we don't
76CRGB leds2[lengths[RedStrip]] = {}; // Red
79
80
81byte ledColors[40][14][3]; // LED buffer - each ripple writes to this, then we
82 // write this to the strips
83//float decay = 0.97; // Multiply all LED's by this amount each tick to create
84 // fancy fading tails
85
86fl::UISlider sliderDecay("decay", .97f, .8, 1.0, .01);
87
88// These ripples are endlessly reused so we don't need to do any memory
89// management
90#define numberOfRipples 30
92 Ripple(0), Ripple(1), Ripple(2), Ripple(3), Ripple(4), Ripple(5),
93 Ripple(6), Ripple(7), Ripple(8), Ripple(9), Ripple(10), Ripple(11),
94 Ripple(12), Ripple(13), Ripple(14), Ripple(15), Ripple(16), Ripple(17),
95 Ripple(18), Ripple(19), Ripple(20), Ripple(21), Ripple(22), Ripple(23),
96 Ripple(24), Ripple(25), Ripple(26), Ripple(27), Ripple(28), Ripple(29),
97};
98
99// Biometric detection and interpretation
100// IR (heartbeat) is used to fire outward ripples
101float lastIrReading; // When our heart pumps, reflected IR drops sharply
102float highestIrReading; // These vars let us detect this drop
103unsigned long
104 lastHeartbeat; // Track last heartbeat so we can detect noise/disconnections
105#define heartbeatLockout \
106 500 // Heartbeats that happen within this many milliseconds are ignored
107#define heartbeatDelta 300 // Drop in reflected IR that constitutes a heartbeat
108
109// Heartbeat color ripples are proportional to skin temperature
110#define lowTemperature 33.0 // Resting temperature
111#define highTemperature 37.0 // Really fired up
114 2.0; // Carries skin temperature from temperature callback to IR callback
115
116// EDA code was too unreliable and was cut.
117// TODO: Rebuild EDA code
118
119// Gyroscope is used to reject data if you're moving too much
120#define gyroAlpha 0.9 // Exponential smoothing constant
121#define gyroThreshold \
122 300 // Minimum angular velocity total (X+Y+Z) that disqualifies readings
124
125// If you don't have an EmotiBit or don't feel like wearing it, that's OK
126// We'll fire automatic pulses
127#define randomPulsesEnabled true // Fire random rainbow pulses from random nodes
128#define cubePulsesEnabled true // Draw cubes at random nodes
129fl::UICheckbox starburstPulsesEnabled("Starburst Pulses", true);
130fl::UICheckbox simulatedBiometricsEnabled("Simulated Biometrics", true);
131
132#define autoPulseTimeout \
133 5000 // If no heartbeat is received in this many ms, begin firing
134 // random/simulated pulses
135#define randomPulseTime 2000 // Fire a random pulse every (this many) ms
136unsigned long lastRandomPulse;
138
142#define autoPulseChangeTime 30000
144
145#define simulatedHeartbeatBaseTime \
146 600 // Fire a simulated heartbeat pulse after at least this many ms
147#define simulatedHeartbeatVariance \
148 200 // Add random jitter to simulated heartbeat
149#define simulatedEdaBaseTime 1000 // Same, but for inward EDA pulses
150#define simulatedEdaVariance 10000
152unsigned long nextSimulatedEda;
153
154// Helper function to check if a node is on the border
155bool isNodeOnBorder(byte node) {
156 for (int i = 0; i < numberOfBorderNodes; i++) {
157 if (node == borderNodes[i]) {
158 return true;
159 }
160 }
161 return false;
162}
163
164fl::UITitle title("Chromancer");
165fl::UIDescription description("Take 6 seconds to boot up. Chromancer is a wall-mounted hexagonal LED display that originally reacted to biometric data from an EmotiBit sensor. It visualizes your heartbeat, skin temperature, and movement in real-time. Chromancer also has a few built-in effects that can be triggered with the push of a button. Enjoy!");
166fl::UICheckbox allWhite("All White", false);
167
168fl::UIButton simulatedHeartbeat("Simulated Heartbeat");
169fl::UIButton triggerStarburst("Trigger Starburst");
177bool wasSpiralClicked = false;
178
179// Group related UI elements using fl::UIGroup template multi-argument constructor
183
184void setup() {
185 Serial.begin(115200);
186
187 Serial.println("*** LET'S GOOOOO ***");
188
189 Serial.println("JSON SCREENMAP");
190 Serial.println(JSON_SCREEN_MAP);
191
194
195 fl::printf("Parsed %d segment maps\n", int(segmentMaps.size()));
196 for (auto kv : segmentMaps) {
197 Serial.print(kv.first.c_str());
198 Serial.print(" ");
199 Serial.println(kv.second.getLength());
200 }
201
202
203 // fl::ScreenMap screenmaps[4];
204 fl::ScreenMap red, black, green, blue;
205 bool ok = true;
206
207 auto red_it = segmentMaps.find("red_segment");
208 ok = (red_it != segmentMaps.end()) && ok;
209 if (red_it != segmentMaps.end()) red = red_it->second;
210
211 auto black_it = segmentMaps.find("back_segment");
212 ok = (black_it != segmentMaps.end()) && ok;
213 if (black_it != segmentMaps.end()) black = black_it->second;
214
215 auto green_it = segmentMaps.find("green_segment");
216 ok = (green_it != segmentMaps.end()) && ok;
217 if (green_it != segmentMaps.end()) green = green_it->second;
218
219 auto blue_it = segmentMaps.find("blue_segment");
220 ok = (blue_it != segmentMaps.end()) && ok;
221 if (blue_it != segmentMaps.end()) blue = blue_it->second;
222 if (!ok) {
223 Serial.println("Failed to get all segment maps");
224 return;
225 }
226
227
228 CRGB* red_leds = leds[RedStrip];
229 CRGB* black_leds = leds[BlackStrip];
230 CRGB* green_leds = leds[GreenStrip];
231 CRGB* blue_leds = leds[BlueStrip];
232
233 FastLED.addLeds<WS2812, 2>(black_leds, lengths[BlackStrip]).setScreenMap(black);
234 FastLED.addLeds<WS2812, 3>(green_leds, lengths[GreenStrip]).setScreenMap(green);
235 FastLED.addLeds<WS2812, 1>(red_leds, lengths[RedStrip]).setScreenMap(red);
236 FastLED.addLeds<WS2812, 4>(blue_leds, lengths[BlueStrip]).setScreenMap(blue);
237
238 FastLED.show();
239}
240
241
242void loop() {
243 unsigned long benchmark = millis();
244 FL_UNUSED(benchmark);
245
246 // Fade all dots to create trails
247 for (int strip = 0; strip < 40; strip++) {
248 for (int led = 0; led < 14; led++) {
249 for (int i = 0; i < 3; i++) {
250 ledColors[strip][led][i] *= sliderDecay.value();
251 }
252 }
253 }
254
255 for (int i = 0; i < numberOfRipples; i++) {
256 ripples[i].advance(ledColors);
257 }
258
259 for (int segment = 0; segment < 40; segment++) {
260 for (int fromBottom = 0; fromBottom < 14; fromBottom++) {
261 int strip = ledAssignments[segment][0];
262 int led = fl::round(fmap(fromBottom, 0, 13, ledAssignments[segment][2],
263 ledAssignments[segment][1]));
264 leds[strip][led] = CRGB(ledColors[segment][fromBottom][0],
265 ledColors[segment][fromBottom][1],
266 ledColors[segment][fromBottom][2]);
267 }
268 }
269
270 if (allWhite) {
271 // for all strips
272 for (int i = 0; i < 4; i++) {
273 for (int j = 0; j < lengths[i]; j++) {
274 leds[i][j] = CRGB::White;
275 }
276 }
277 }
278
279 FastLED.show();
280
281
282 // Check if buttons were clicked
288
289 if (wasSpiralClicked) {
290 // Trigger spiral wave effect from center
291 unsigned int baseColor = random(0xFFFF);
292 byte centerNode = 15; // Center node
293
294 // Create 6 ripples in a spiral pattern
295 for (int i = 0; i < 6; i++) {
296 if (nodeConnections[centerNode][i] >= 0) {
297 for (int j = 0; j < numberOfRipples; j++) {
298 if (ripples[j].state == dead) {
299 ripples[j].start(
300 centerNode, i,
302 baseColor + (0xFFFF / 6) * i, 255, 255),
303 0.3 + (i * 0.1), // Varying speeds creates spiral effect
304 2000,
305 i % 2 ? alwaysTurnsLeft : alwaysTurnsRight); // Alternating turn directions
306 break;
307 }
308 }
309 }
310 }
311 lastHeartbeat = millis();
312 }
313
315 // Trigger immediate border wave effect
316 unsigned int baseColor = random(0xFFFF);
317
318 // Start ripples from each border node in sequence
319 for (int i = 0; i < numberOfBorderNodes; i++) {
320 byte node = borderNodes[i];
321 // Find an inward direction
322 for (int dir = 0; dir < 6; dir++) {
323 if (nodeConnections[node][dir] >= 0 &&
324 !isNodeOnBorder(nodeConnections[node][dir])) {
325 for (int j = 0; j < numberOfRipples; j++) {
326 if (ripples[j].state == dead) {
327 ripples[j].start(
328 node, dir,
330 baseColor + (0xFFFF / numberOfBorderNodes) * i,
331 255, 255),
332 .4, 2000, 0);
333 break;
334 }
335 }
336 break;
337 }
338 }
339 }
340 lastHeartbeat = millis();
341 }
342
344 // Trigger immediate rainbow cube effect
345 int node = cubeNodes[random(numberOfCubeNodes)];
346 unsigned int baseColor = random(0xFFFF);
347 byte behavior = random(2) ? alwaysTurnsLeft : alwaysTurnsRight;
348
349 for (int i = 0; i < 6; i++) {
350 if (nodeConnections[node][i] >= 0) {
351 for (int j = 0; j < numberOfRipples; j++) {
352 if (ripples[j].state == dead) {
353 ripples[j].start(
354 node, i,
356 baseColor + (0xFFFF / 6) * i, 255, 255),
357 .5, 2000, behavior);
358 break;
359 }
360 }
361 }
362 }
363 lastHeartbeat = millis();
364 }
365
367 // Trigger immediate starburst effect
368 unsigned int baseColor = random(0xFFFF);
369 byte behavior = random(2) ? alwaysTurnsLeft : alwaysTurnsRight;
370
371 for (int i = 0; i < 6; i++) {
372 for (int j = 0; j < numberOfRipples; j++) {
373 if (ripples[j].state == dead) {
374 ripples[j].start(
375 starburstNode, i,
377 baseColor + (0xFFFF / 6) * i, 255, 255),
378 .65, 1500, behavior);
379 break;
380 }
381 }
382 }
383 lastHeartbeat = millis();
384 }
385
387 // Trigger immediate heartbeat effect
388 for (int i = 0; i < 6; i++) {
389 for (int j = 0; j < numberOfRipples; j++) {
390 if (ripples[j].state == dead) {
391 ripples[j].start(15, i, 0xEE1111,
392 float(random(100)) / 100.0 * .1 + .4, 1000, 0);
393 break;
394 }
395 }
396 }
397 lastHeartbeat = millis();
398 }
399
400 if (millis() - lastHeartbeat >= autoPulseTimeout) {
401 // When biometric data is unavailable, visualize at random
403 millis() - lastRandomPulse >= randomPulseTime) {
404 unsigned int baseColor = random(0xFFFF);
405
406 if (currentAutoPulseType == 255 ||
409 byte possiblePulse = 255;
410 while (true) {
411 possiblePulse = random(3);
412
413 if (possiblePulse == currentAutoPulseType)
414 continue;
415
416 switch (possiblePulse) {
417 case 0:
419 continue;
420 break;
421
422 case 1:
424 continue;
425 break;
426
427 case 2:
429 continue;
430 break;
431
432 default:
433 continue;
434 }
435
436 currentAutoPulseType = possiblePulse;
437 lastAutoPulseChange = millis();
438 break;
439 }
440 }
441
442 switch (currentAutoPulseType) {
443 case 0: {
444 int node = 0;
445 bool foundStartingNode = false;
446 while (!foundStartingNode) {
447 node = random(25);
448 foundStartingNode = true;
449 for (int i = 0; i < numberOfBorderNodes; i++) {
450 // Don't fire a pulse on one of the outer nodes - it
451 // looks boring
452 if (node == borderNodes[i])
453 foundStartingNode = false;
454 }
455
456 if (node == lastAutoPulseNode)
457 foundStartingNode = false;
458 }
459
460 lastAutoPulseNode = node;
461
462 for (int i = 0; i < 6; i++) {
463 if (nodeConnections[node][i] >= 0) {
464 for (int j = 0; j < numberOfRipples; j++) {
465 if (ripples[j].state == dead) {
466 ripples[j].start(
467 node, i,
468 // strip0.ColorHSV(baseColor
469 // + (0xFFFF / 6) * i,
470 // 255, 255),
471 Adafruit_DotStar_ColorHSV(baseColor, 255,
472 255),
473 float(random(100)) / 100.0 * .2 + .5, 3000,
474 1);
475
476 break;
477 }
478 }
479 }
480 }
481 break;
482 }
483
484 case 1: {
485 int node = cubeNodes[random(numberOfCubeNodes)];
486
487 while (node == lastAutoPulseNode)
488 node = cubeNodes[random(numberOfCubeNodes)];
489
490 lastAutoPulseNode = node;
491
492 byte behavior = random(2) ? alwaysTurnsLeft : alwaysTurnsRight;
493
494 for (int i = 0; i < 6; i++) {
495 if (nodeConnections[node][i] >= 0) {
496 for (int j = 0; j < numberOfRipples; j++) {
497 if (ripples[j].state == dead) {
498 ripples[j].start(
499 node, i,
500 // strip0.ColorHSV(baseColor
501 // + (0xFFFF / 6) * i,
502 // 255, 255),
503 Adafruit_DotStar_ColorHSV(baseColor, 255,
504 255),
505 .5, 2000, behavior);
506
507 break;
508 }
509 }
510 }
511 }
512 break;
513 }
514
515 case 2: {
516 byte behavior = random(2) ? alwaysTurnsLeft : alwaysTurnsRight;
517
519
520 for (int i = 0; i < 6; i++) {
521 for (int j = 0; j < numberOfRipples; j++) {
522 if (ripples[j].state == dead) {
523 ripples[j].start(
524 starburstNode, i,
526 baseColor + (0xFFFF / 6) * i, 255, 255),
527 .65, 1500, behavior);
528
529 break;
530 }
531 }
532 }
533 break;
534 }
535
536 default:
537 break;
538 }
539 lastRandomPulse = millis();
540 }
541
543 // Simulated heartbeat
544 if (millis() >= nextSimulatedHeartbeat) {
545 for (int i = 0; i < 6; i++) {
546 for (int j = 0; j < numberOfRipples; j++) {
547 if (ripples[j].state == dead) {
548 ripples[j].start(
549 15, i, 0xEE1111,
550 float(random(100)) / 100.0 * .1 + .4, 1000, 0);
551
552 break;
553 }
554 }
555 }
556
559 }
560
561 // Simulated EDA ripples
562 if (millis() >= nextSimulatedEda) {
563 for (int i = 0; i < 10; i++) {
564 for (int j = 0; j < numberOfRipples; j++) {
565 if (ripples[j].state == dead) {
566 byte targetNode =
568 byte direction = 255;
569
570 while (direction == 255) {
571 direction = random(6);
572 if (nodeConnections[targetNode][direction] < 0)
573 direction = 255;
574 }
575
576 ripples[j].start(
577 targetNode, direction, 0x1111EE,
578 float(random(100)) / 100.0 * .5 + 2, 300, 2);
579
580 break;
581 }
582 }
583 }
584
586 random(simulatedEdaVariance);
587 }
588 }
589 }
590
591 // Serial.print("Benchmark: ");
592 // Serial.println(millis() - benchmark);
593}
CRGB leds[1]
fl::UIDescription description("Demo of the Animatrix effects. @author of fx is StefanPetrick")
byte currentAutoPulseType
#define simulatedHeartbeatBaseTime
fl::UIGroup automationControls("Automation", starburstPulsesEnabled, simulatedBiometricsEnabled)
CRGB leds2[lengths[RedStrip]]
bool isNodeOnBorder(byte node)
bool wasStarburstClicked
bool wasRainbowCubeClicked
byte lastAutoPulseNode
unsigned long nextSimulatedHeartbeat
fl::UIButton simulatedHeartbeat("Simulated Heartbeat")
fl::UITitle title("Chromancer")
byte ledColors[40][14][3]
#define highTemperature
fl::UIGroup displayControls("Display", sliderDecay, allWhite)
void setup()
CRGB leds3[lengths[BlueStrip]]
unsigned long lastAutoPulseChange
#define lowTemperature
unsigned long lastRandomPulse
#define simulatedEdaBaseTime
fl::UIGroup effectTriggers("Effect Triggers", simulatedHeartbeat, triggerStarburst, triggerRainbowCube, triggerBorderWave, triggerSpiral)
float lastKnownTemperature
fl::UIButton triggerBorderWave("Border Wave")
float lastIrReading
fl::UICheckbox starburstPulsesEnabled("Starburst Pulses", true)
#define simulatedEdaVariance
fl::UICheckbox allWhite("All White", false)
bool wasSpiralClicked
float gyroX
#define randomPulsesEnabled
CRGB leds1[lengths[GreenStrip]]
constexpr int lengths[]
unsigned long lastHeartbeat
fl::UIButton triggerSpiral("Spiral Wave")
#define autoPulseTimeout
bool wasHeartbeatClicked
fl::UICheckbox simulatedBiometricsEnabled("Simulated Biometrics", true)
@ GreenStrip
@ RedStrip
@ BlackStrip
@ BlueStrip
float highestIrReading
#define simulatedHeartbeatVariance
#define randomPulseTime
float gyroZ
bool wasBorderWaveClicked
#define numberOfRipples
fl::UISlider sliderDecay("decay",.97f,.8, 1.0,.01)
#define autoPulseChangeTime
unsigned long nextSimulatedEda
byte numberOfAutoPulseTypes
fl::UIButton triggerStarburst("Trigger Starburst")
float gyroY
Ripple ripples[numberOfRipples]
#define cubePulsesEnabled
fl::UIButton triggerRainbowCube("Rainbow Cube")
CRGB leds0[lengths[BlackStrip]]
void loop()
TestState state
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
static bool ParseJson(const char *jsonStrScreenMap, fl::flat_map< string, ScreenMap > *segmentMaps, string *err=nullptr) FL_NO_EXCEPT
iterator find(const Key &key) FL_NO_EXCEPT
Definition flat_map.h:156
size_type size() const FL_NO_EXCEPT
Definition flat_map.h:98
iterator end() FL_NO_EXCEPT
Definition flat_map.h:86
uint32_t Adafruit_DotStar_ColorHSV(uint16_t hue, uint8_t sat, uint8_t val)
Definition detail.h:6
fl::CRGB CRGB
Definition crgb.h:25
FastLED's Elegant JSON Library: fl::json
Centralized logging categories for FastLED hardware interfaces and subsystems.
int starburstNode
Definition mapping.h:156
int numberOfCubeNodes
Definition mapping.h:152
int nodeConnections[25][6]
Definition mapping.h:15
int ledAssignments[40][3]
Definition mapping.h:95
int numberOfBorderNodes
Definition mapping.h:147
int borderNodes[]
Definition mapping.h:148
int cubeNodes[]
Definition mapping.h:153
void printf(const char *format) FL_NO_EXCEPT
enable_if<!is_integral< T >::value, T >::type round(T value) FL_NO_EXCEPT
Definition math.h:327
@ dead
Definition ripple.h:17
@ alwaysTurnsRight
Definition ripple.h:27
@ alwaysTurnsLeft
Definition ripple.h:28
float fmap(float x, float in_min, float in_max, float out_min, float out_max)
Definition ripple.h:31
#define FL_UNUSED(x)
const char JSON_SCREEN_MAP[]
@ White
<div style='background:#FFFFFF;width:4em;height:4em;'></div>
Definition crgb.h:653
#define Serial
Definition serial.h:314
Aggregator header for the fl/ui/ family of per-element UI types.