FastLED 3.9.3
Loading...
Searching...
No Matches
demoreel100.hpp
1#pragma once
2
3#include "FastLED.h"
4#include "fx/fx1d.h"
5#include "namespace.h"
6
7FASTLED_NAMESPACE_BEGIN
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_REF(DemoReel100);
21
22class DemoReel100 : public FxStrip {
23 public:
24 DemoReel100(uint16_t num_leds) : FxStrip(num_leds) {}
25
26 void lazyInit() override { start_time = millis(); }
27
28 void draw(DrawContext context) override {
29 CRGB *leds = context.leds;
30 if (leds == nullptr || mNumLeds == 0) {
31 return;
32 }
33
34 // Call the current pattern function once, updating the 'leds' array
35 runPattern(leds);
36
37 // do some periodic updates
39 hue++;
40 } // slowly cycle the "base color" through the rainbow
41 EVERY_N_SECONDS(10) { nextPattern(); } // change patterns periodically
42 }
43
44 const char *fxName(int) const override { return "DemoReel100"; }
45
46 private:
47 uint8_t current_pattern_number = 0;
48 uint8_t hue = 0;
49 unsigned long start_time = 0;
50
51 void nextPattern() {
52 // add one to the current pattern number, and wrap around at the end
53 current_pattern_number =
54 (current_pattern_number + 1) % 6; // 6 is the number of patterns
55 }
56
57 void runPattern(CRGB *leds) {
58 switch (current_pattern_number) {
59 case 0:
60 rainbow(leds);
61 break;
62 case 1:
63 rainbowWithGlitter(leds);
64 break;
65 case 2:
66 confetti(leds);
67 break;
68 case 3:
69 sinelon(leds);
70 break;
71 case 4:
72 juggle(leds);
73 break;
74 case 5:
75 bpm(leds);
76 break;
77 }
78 }
79
80 void rainbow(CRGB *leds) {
81 // FastLED's built-in rainbow generator
82 fill_rainbow(leds, mNumLeds, hue, 7);
83 }
84
85 void rainbowWithGlitter(CRGB *leds) {
86 // built-in FastLED rainbow, plus some random sparkly glitter
87 rainbow(leds);
88 addGlitter(80, leds);
89 }
90
91 void addGlitter(fract8 chanceOfGlitter, CRGB *leds) {
92 if (random8() < chanceOfGlitter) {
93 leds[random16(mNumLeds)] += CRGB::White;
94 }
95 }
96
97 void confetti(CRGB *leds) {
98 // random colored speckles that blink in and fade smoothly
99 fadeToBlackBy(leds, mNumLeds, 10);
100 int pos = random16(mNumLeds);
101 leds[pos] += CHSV(hue + random8(64), 200, 255);
102 }
103
104 void sinelon(CRGB *leds) {
105 // a colored dot sweeping back and forth, with fading trails
106 fadeToBlackBy(leds, mNumLeds, 20);
107 int pos = beatsin16(13, 0, mNumLeds - 1);
108 leds[pos] += CHSV(hue, 255, 192);
109 }
110
111 void bpm(CRGB *leds) {
112 // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
113 uint8_t BeatsPerMinute = 62;
115 uint8_t beat = beatsin8(BeatsPerMinute, 64, 255);
116 for (uint16_t i = 0; i < mNumLeds; i++) {
117 leds[i] =
118 ColorFromPalette(palette, hue + (i * 2), beat - hue + (i * 10));
119 }
120 }
121
122 void juggle(CRGB *leds) {
123 // eight colored dots, weaving in and out of sync with each other
124 fadeToBlackBy(leds, mNumLeds, 20);
125 uint8_t dothue = 0;
126 for (uint16_t i = 0; i < 8; i++) {
127 leds[beatsin16(i + 7, 0, mNumLeds - 1)] |= CHSV(dothue, 200, 255);
128 dothue += 32;
129 }
130 }
131};
132
133FASTLED_NAMESPACE_END
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 fx1d.h:12
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:962
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:980
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:30
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Get a color from a palette.
@ White
Definition crgb.h:615
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:1323
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1288
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:11
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39