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