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