FastLED 3.10.8
Loading...
Searching...
No Matches
noisepalette.cpp.hpp
Go to the documentation of this file.
1#include "fl/stl/stdint.h"
3
4#ifndef FASTLED_INTERNAL
5#define FASTLED_INTERNAL
6#endif
7
8#include "fl/system/fastled.h"
9#include "fl/stl/memory.h"
10#include "fl/math/xymap.h"
11#include "fl/fx/fx2d.h"
12#include "fl/math/random8.h"
13#include "noise.h"
14
16#include "fl/stl/noexcept.h"
17
18namespace fl {
19
21 FL_NO_EXCEPT : Fx2d(xyMap), speed(0), scale(0), colorLoop(1), mFps(fps) {
22 // currentPalette = PartyColors_p;
23 FL_STATIC_ASSERT(sizeof(currentPalette) == sizeof(CRGBPalette16),
24 "Palette size mismatch");
26 width = xyMap.getWidth();
27 height = xyMap.getHeight();
28
29 // Initialize our coordinates to some random values
30 mX = random16();
31 mY = random16();
32 mZ = random16();
33
35
36 // Allocate memory for the noise array using vector
37 noise.resize(width * height);
38}
39
41 currentPaletteIndex = paletteIndex % 12; // Ensure the index wraps around
42 switch (currentPaletteIndex) {
43 case 0:
45 speed = 20;
46 scale = 30;
47 colorLoop = 1;
48 break;
49 case 1:
51 speed = 10;
52 scale = 50;
53 colorLoop = 1;
54 break;
55 case 2:
57 speed = 20;
58 scale = 30;
59 colorLoop = 1;
60 break;
61 case 3:
63 speed = 8;
64 scale = 120;
65 colorLoop = 0;
66 break;
67 case 4:
69 speed = 4;
70 scale = 30;
71 colorLoop = 0;
72 break;
73 case 5:
75 speed = 8;
76 scale = 50;
77 colorLoop = 0;
78 break;
79 case 6:
81 speed = 20;
82 scale = 90;
83 colorLoop = 0;
84 break;
85 case 7:
87 speed = 20;
88 scale = 30;
89 colorLoop = 1;
90 break;
91 case 8:
92 case 9:
93 case 10:
95 speed = 20 + (currentPaletteIndex - 8) * 5;
96 scale = 20 + (currentPaletteIndex - 8) * 5;
97 colorLoop = 1;
98 break;
99 case 11:
101 speed = 2;
102 scale = 20;
103 colorLoop = 1;
104 break;
105 default:
106 break;
107 }
108}
109
111 static u8 ihue = 0; // okay static in header
112
113 for (u16 i = 0; i < width; i++) {
114 for (u16 j = 0; j < height; j++) {
115 // We use the value at the (i,j) coordinate in the noise
116 // array for our brightness, and the flipped value from (j,i)
117 // for our pixel's index into the color palette.
118
119 u8 index = noise[i * height + j];
120 u8 bri = noise[j * width + i];
121
122 // if this palette is a 'loop', add a slowly-changing base value
123 if (colorLoop) {
124 index += ihue;
125 }
126
127 // brighten up, as the color palette itself often contains the
128 // light/dark dynamic range desired
129 if (bri > 127) {
130 bri = 255;
131 } else {
132 bri = dim8_raw(bri * 2);
133 }
134
136 leds[XY(i, j)] = color;
137 }
138 }
139
140 ihue += 1;
141}
142
144 // If we're running at a low "speed", some 8-bit artifacts become
145 // visible from frame-to-frame. In order to reduce this, we can do some
146 // fast data-smoothing. The amount of data smoothing we're doing depends
147 // on "speed".
148 u8 dataSmoothing = 0;
149 if (speed < 50) {
150 dataSmoothing = 200 - (speed * 4);
151 }
152
153 for (u16 i = 0; i < width; i++) {
154 int ioffset = scale * i;
155 for (u16 j = 0; j < height; j++) {
156 int joffset = scale * j;
157
158 u8 data = inoise8(mX + ioffset, mY + joffset, mZ);
159
160 // The range of the inoise8 function is roughly 16-238.
161 // These two operations expand those values out to roughly
162 // 0..255 You can comment them out if you want the raw noise
163 // data.
164 data = qsub8(data, 16);
165 data = qadd8(data, scale8(data, 39));
166
167 if (dataSmoothing) {
168 u8 olddata = noise[i * height + j];
169 u8 newdata = scale8(olddata, dataSmoothing) +
170 scale8(data, 256 - dataSmoothing);
171 data = newdata;
172 }
173
174 noise[i * height + j] = data;
175 }
176 }
177
178 mZ += speed;
179
180 // apply slow drift to X and Y, just for visual variation.
181 mX += speed / 8;
182 mY -= speed / 16;
183}
184
186 while (true) {
187 u8 new_idx = random8() % 12;
188 if (new_idx == currentPaletteIndex) {
189 continue;
190 }
191 currentPaletteIndex = new_idx;
193 return currentPaletteIndex;
194 }
195}
196
197} // namespace fl
CRGB leds[1]
void SetupPurpleAndGreenPalette()
void SetupBlackAndWhiteStripedPalette()
fl::UISlider scale("Scale", 4,.1, 4,.1)
uint16_t speed
Definition Noise.ino:72
u16 xyMap(u16 x, u16 y) const FL_NO_EXCEPT
Definition fx2d.h:21
Fx2d(const XYMap &xyMap) FL_NO_EXCEPT
Definition fx2d.h:20
CRGBPalette16 currentPalette
void setPalettePreset(int paletteIndex) FL_NO_EXCEPT
void mapNoiseToLEDsUsingPalette(fl::span< CRGB > leds) FL_NO_EXCEPT
void SetupRandomPalette() FL_NO_EXCEPT
void fillnoise8() FL_NO_EXCEPT
fl::vector_psram< u8 > noise
NoisePalette(XYMap xyMap, float fps=60.f) FL_NO_EXCEPT
u8 changeToRandomPalette() FL_NO_EXCEPT
uint8_t colorLoop
Definition curr.h:244
Fast, efficient random number generators specifically designed for high-performance LED programming.
Internal FastLED header for implementation files.
fl::u8 inoise8(fl::u16 x, fl::u16 y, fl::u16 z) FL_NO_EXCEPT
8-Bit, fixed point implementation of Perlin's noise.
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 fl::u16 random16() FL_NO_EXCEPT
Generate a 16-bit random number.
Definition random8.h:63
LIB8STATIC fl::u8 random8() FL_NO_EXCEPT
Generate an 8-bit random number.
Definition random8.h:53
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
fl::u16 XY(fl::u16 x, fl::u16 y)
Definition Luminova.h:28
CRGB ColorFromPalette(const CRGBPalette16 &pal, fl::u8 index, fl::u8 brightness, TBlendType blendType) FL_NO_EXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
Demonstrates how to mix noise generation with color palettes on a 2D LED matrix.
#define FL_STATIC_ASSERT(...)
Portable compile-time assertion wrapper.
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38