FastLED 3.9.7
Loading...
Searching...
No Matches
noisepalette.cpp
1
2
3
4#include <stdint.h>
5
6#include "FastLED.h"
7#include "fx/fx2d.h"
8#include "lib8tion/random8.h"
9#include "noise.h"
10#include "fl/ptr.h"
11#include "fl/xymap.h"
12
13#include "noisepalette.h"
14
15namespace fl {
16
17NoisePalette::NoisePalette(XYMap xyMap, float fps)
18 : Fx2d(xyMap), speed(0), scale(0), colorLoop(1), mFps(fps) {
19 // currentPalette = PartyColors_p;
20 static_assert(sizeof(currentPalette) == sizeof(CRGBPalette16),
21 "Palette size mismatch");
22 currentPalette = PartyColors_p;
23 width = xyMap.getWidth();
24 height = xyMap.getHeight();
25
26 // Initialize our coordinates to some random values
27 mX = random16();
28 mY = random16();
29 mZ = random16();
30
31 setPalettePreset(0);
32
33 // Allocate memory for the noise array using scoped_ptr
34 noise = scoped_ptr<uint8_t>(new uint8_t[width * height]);
35}
36
37void NoisePalette::setPalettePreset(int paletteIndex) {
38 currentPaletteIndex = paletteIndex % 12; // Ensure the index wraps around
39 switch (currentPaletteIndex) {
40 case 0:
41 currentPalette = RainbowColors_p;
42 speed = 20;
43 scale = 30;
44 colorLoop = 1;
45 break;
46 case 1:
47 SetupPurpleAndGreenPalette();
48 speed = 10;
49 scale = 50;
50 colorLoop = 1;
51 break;
52 case 2:
53 SetupBlackAndWhiteStripedPalette();
54 speed = 20;
55 scale = 30;
56 colorLoop = 1;
57 break;
58 case 3:
59 currentPalette = ForestColors_p;
60 speed = 8;
61 scale = 120;
62 colorLoop = 0;
63 break;
64 case 4:
65 currentPalette = CloudColors_p;
66 speed = 4;
67 scale = 30;
68 colorLoop = 0;
69 break;
70 case 5:
71 currentPalette = LavaColors_p;
72 speed = 8;
73 scale = 50;
74 colorLoop = 0;
75 break;
76 case 6:
77 currentPalette = OceanColors_p;
78 speed = 20;
79 scale = 90;
80 colorLoop = 0;
81 break;
82 case 7:
83 currentPalette = PartyColors_p;
84 speed = 20;
85 scale = 30;
86 colorLoop = 1;
87 break;
88 case 8:
89 case 9:
90 case 10:
91 SetupRandomPalette();
92 speed = 20 + (currentPaletteIndex - 8) * 5;
93 scale = 20 + (currentPaletteIndex - 8) * 5;
94 colorLoop = 1;
95 break;
96 case 11:
97 currentPalette = RainbowStripeColors_p;
98 speed = 2;
99 scale = 20;
100 colorLoop = 1;
101 break;
102 }
103}
104
105void NoisePalette::mapNoiseToLEDsUsingPalette(CRGB *leds) {
106 static uint8_t ihue = 0;
107
108 for (uint16_t i = 0; i < width; i++) {
109 for (uint16_t j = 0; j < height; j++) {
110 // We use the value at the (i,j) coordinate in the noise
111 // array for our brightness, and the flipped value from (j,i)
112 // for our pixel's index into the color palette.
113
114 uint8_t index = noise.get()[i * height + j];
115 uint8_t bri = noise.get()[j * width + i];
116
117 // if this palette is a 'loop', add a slowly-changing base value
118 if (colorLoop) {
119 index += ihue;
120 }
121
122 // brighten up, as the color palette itself often contains the
123 // light/dark dynamic range desired
124 if (bri > 127) {
125 bri = 255;
126 } else {
127 bri = dim8_raw(bri * 2);
128 }
129
130 CRGB color = ColorFromPalette(currentPalette, index, bri);
131 leds[XY(i, j)] = color;
132 }
133 }
134
135 ihue += 1;
136}
137
138void NoisePalette::fillnoise8() {
139 // If we're running at a low "speed", some 8-bit artifacts become
140 // visible from frame-to-frame. In order to reduce this, we can do some
141 // fast data-smoothing. The amount of data smoothing we're doing depends
142 // on "speed".
143 uint8_t dataSmoothing = 0;
144 if (speed < 50) {
145 dataSmoothing = 200 - (speed * 4);
146 }
147
148 for (uint16_t i = 0; i < width; i++) {
149 int ioffset = scale * i;
150 for (uint16_t j = 0; j < height; j++) {
151 int joffset = scale * j;
152
153 uint8_t data = inoise8(mX + ioffset, mY + joffset, mZ);
154
155 // The range of the inoise8 function is roughly 16-238.
156 // These two operations expand those values out to roughly
157 // 0..255 You can comment them out if you want the raw noise
158 // data.
159 data = qsub8(data, 16);
160 data = qadd8(data, scale8(data, 39));
161
162 if (dataSmoothing) {
163 uint8_t olddata = noise.get()[i * height + j];
164 uint8_t newdata = scale8(olddata, dataSmoothing) +
165 scale8(data, 256 - dataSmoothing);
166 data = newdata;
167 }
168
169 noise.get()[i * height + j] = data;
170 }
171 }
172
173 mZ += speed;
174
175 // apply slow drift to X and Y, just for visual variation.
176 mX += speed / 8;
177 mY -= speed / 16;
178}
179
180uint8_t NoisePalette::changeToRandomPalette() {
181 while (true) {
182 uint8_t new_idx = random8() % 12;
183 if (new_idx == currentPaletteIndex) {
184 continue;
185 }
186 currentPaletteIndex = new_idx;
187 setPalettePreset(currentPaletteIndex);
188 return currentPaletteIndex;
189 }
190}
191
192
193
194
195
196} // namespace fl
central include file for FastLED, defines the CFastLED class/object
RGB color palette with 16 discrete values.
Definition colorutils.h:997
LIB8STATIC uint8_t dim8_raw(uint8_t x)
Adjust a scaling value for dimming.
Definition scale8.h:703
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
Add one byte to another, saturating at 0xFF.
Definition math8.h:31
LIB8STATIC_ALWAYS_INLINE uint8_t qsub8(uint8_t i, uint8_t j)
Subtract one byte from another, saturating at 0x00.
Definition math8.h:103
uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z)
8-Bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:512
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Get a color from a palette.
const TProgmemRGBPalette16 RainbowStripeColors_p
HSV Rainbow colors with alternatating stripes of black.
const TProgmemRGBPalette16 OceanColors_p
Ocean colors, blues and whites.
const TProgmemRGBPalette16 CloudColors_p
Cloudy color palette.
const TProgmemRGBPalette16 ForestColors_p
Forest colors, greens.
const TProgmemRGBPalette16 LavaColors_p
Lava color palette.
const TProgmemRGBPalette16 PartyColors_p
HSV color ramp: blue, purple, pink, red, orange, yellow (and back).
const TProgmemRGBPalette16 RainbowColors_p
HSV Rainbow.
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
LIB8STATIC_ALWAYS_INLINE uint8_t scale8(uint8_t i, fract8 scale)
Scale one byte by a second one, which is treated as the numerator of a fraction whose denominator is ...
Definition scale8.h:34
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Functions to generate and fill arrays with noise.
Demonstrates how to mix noise generation with color palettes on a 2D LED matrix.
Fast, efficient random number generators specifically designed for high-performance LED programming.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54