FastLED 3.9.7
Loading...
Searching...
No Matches
demoreel100.hpp
1#pragma once
2
3#include "FastLED.h"
4#include "fx/fx1d.h"
5#include "fl/namespace.h"
6
7namespace fl {
8
9
10
11// FastLED "100-lines-of-code" demo reel, showing just a few
12// of the kinds of animation patterns you can quickly and easily
13// compose using FastLED.
14//
15// This example also shows one easy way to define multiple
16// animations patterns and have them automatically rotate.
17//
18// -Mark Kriegsman, December 2014
19
20FASTLED_SMART_PTR(DemoReel100);
21
22class DemoReel100 : public Fx1d {
23 public:
24 DemoReel100(uint16_t num_leds) : Fx1d(num_leds) {}
25
26 void draw(DrawContext context) override {
27 CRGB *leds = context.leds;
28 if (leds == nullptr || mNumLeds == 0) {
29 return;
30 }
31 if (start_time == 0) {
32 start_time = millis();
33 }
34
35 // Call the current pattern function once, updating the 'leds' array
36 runPattern(leds);
37
38 // do some periodic updates
40 hue++;
41 } // slowly cycle the "base color" through the rainbow
42 EVERY_N_SECONDS(10) { nextPattern(); } // change patterns periodically
43 }
44
45 fl::Str fxName() const override { return "DemoReel100"; }
46
47 private:
48 uint8_t current_pattern_number = 0;
49 uint8_t hue = 0;
50 unsigned long start_time = 0;
51
52 void nextPattern() {
53 // add one to the current pattern number, and wrap around at the end
54 current_pattern_number =
55 (current_pattern_number + 1) % 6; // 6 is the number of patterns
56 }
57
58 void runPattern(CRGB *leds) {
59 switch (current_pattern_number) {
60 case 0:
61 rainbow(leds);
62 break;
63 case 1:
64 rainbowWithGlitter(leds);
65 break;
66 case 2:
67 confetti(leds);
68 break;
69 case 3:
70 sinelon(leds);
71 break;
72 case 4:
73 juggle(leds);
74 break;
75 case 5:
76 bpm(leds);
77 break;
78 }
79 }
80
81 void rainbow(CRGB *leds) {
82 // FastLED's built-in rainbow generator
83 fill_rainbow(leds, mNumLeds, hue, 7);
84 }
85
86 void rainbowWithGlitter(CRGB *leds) {
87 // built-in FastLED rainbow, plus some random sparkly glitter
88 rainbow(leds);
89 addGlitter(80, leds);
90 }
91
92 void addGlitter(fract8 chanceOfGlitter, CRGB *leds) {
93 if (random8() < chanceOfGlitter) {
94 leds[random16(mNumLeds)] += CRGB::White;
95 }
96 }
97
98 void confetti(CRGB *leds) {
99 // random colored speckles that blink in and fade smoothly
100 fadeToBlackBy(leds, mNumLeds, 10);
101 int pos = random16(mNumLeds);
102 leds[pos] += CHSV(hue + random8(64), 200, 255);
103 }
104
105 void sinelon(CRGB *leds) {
106 // a colored dot sweeping back and forth, with fading trails
107 fadeToBlackBy(leds, mNumLeds, 20);
108 int pos = beatsin16(13, 0, mNumLeds - 1);
109 leds[pos] += CHSV(hue, 255, 192);
110 }
111
112 void bpm(CRGB *leds) {
113 // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
114 uint8_t BeatsPerMinute = 62;
116 uint8_t beat = beatsin8(BeatsPerMinute, 64, 255);
117 for (uint16_t i = 0; i < mNumLeds; i++) {
118 leds[i] =
119 ColorFromPalette(palette, hue + (i * 2), beat - hue + (i * 10));
120 }
121 }
122
123 void juggle(CRGB *leds) {
124 // eight colored dots, weaving in and out of sync with each other
125 fadeToBlackBy(leds, mNumLeds, 20);
126 uint8_t dothue = 0;
127 for (uint16_t i = 0; i < 8; i++) {
128 leds[beatsin16(i + 7, 0, mNumLeds - 1)] |= CHSV(dothue, 200, 255);
129 dothue += 32;
130 }
131 }
132};
133
134} // namespace fl
central include file for FastLED, defines the CFastLED class/object
RGB color palette with 16 discrete values.
Definition colorutils.h:997
void draw(DrawContext context) override
Definition str.h:336
LIB8STATIC uint16_t beatsin16(accum88 beats_per_minute, uint16_t lowest=0, uint16_t highest=65535, uint32_t timebase=0, uint16_t phase_offset=0)
Generates a 16-bit sine wave at a given BPM that oscillates within a given range.
Definition lib8tion.h:957
LIB8STATIC uint8_t beatsin8(accum88 beats_per_minute, uint8_t lowest=0, uint8_t highest=255, uint32_t timebase=0, uint8_t phase_offset=0)
Generates an 8-bit sine wave at a given BPM that oscillates within a given range.
Definition lib8tion.h:975
void fadeToBlackBy(CRGB *leds, uint16_t num_leds, uint8_t fadeBy)
Reduce the brightness of an array of pixels all at once.
void fill_rainbow(struct CRGB *targetArray, int numToFill, uint8_t initialhue, uint8_t deltahue)
Fill a range of LEDs with a rainbow of colors.
uint8_t fract8
ANSI: unsigned short _Fract.
Definition types.h:36
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Get a color from a palette.
const TProgmemRGBPalette16 PartyColors_p
HSV color ramp: blue, purple, pink, red, orange, yellow (and back).
LIB8STATIC uint16_t random16()
Generate a 16-bit random number.
Definition random8.h:50
LIB8STATIC uint8_t random8()
Generate an 8-bit random number.
Definition random8.h:40
#define EVERY_N_MILLISECONDS(N)
Alias for EVERY_N_MILLIS.
Definition lib8tion.h:1318
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1283
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
@ White
Definition crgb.h:632