FastLED 3.9.15
Loading...
Searching...
No Matches

◆ getPaletteIndex()

uint8_t getPaletteIndex ( uint32_t millis32,
int width,
int max_width,
int height,
int max_height,
uint32_t y_speed )

Definition at line 112 of file FireCylinder.h.

113 {
114 // This function calculates which color to use from our palette for each LED
115
116 // Get the scale factor from the UI slider
117 uint16_t scale = scaleXY.as<uint16_t>();
118
119 // Convert width position to an angle (0-255 represents 0-360 degrees)
120 // This maps our flat coordinate to a position on a cylinder
121 float xf = (float)width / (float)max_width; // Normalized position (0.0 to 1.0)
122 uint8_t x = (uint8_t)(xf * 255); // Convert to 0-255 range for trig functions
123
124 // Calculate the sine and cosine of this angle to get 3D coordinates on the cylinder
125 uint32_t cosx = cos8(x); // cos8 returns a value 0-255 representing cosine
126 uint32_t sinx = sin8(x); // sin8 returns a value 0-255 representing sine
127
128 // Apply scaling to the sine/cosine values
129 // This controls how "wide" the noise pattern is around the cylinder
130 float trig_scale = scale * scaleX.value();
131 cosx *= trig_scale;
132 sinx *= trig_scale;
133
134 // Calculate Y coordinate (vertical position) with speed offset for movement
135 uint32_t y = height * scale + y_speed;
136
137 // Calculate Z coordinate (time dimension) - controls how the pattern changes over time
138 uint16_t z = millis32 / invSpeedZ.as<uint16_t>();
139
140 // Generate 16-bit Perlin noise using our 4D coordinates (x,y,z,t)
141 // The << 8 shifts values left by 8 bits (multiplies by 256) to use the full 16-bit range
142 // The last parameter (0) could be replaced with another time variable for more variation
143 uint16_t noise16 = inoise16(cosx << 8, sinx << 8, y << 8, 0);
144
145 // Convert 16-bit noise to 8-bit by taking the high byte
146 uint8_t noise_val = noise16 >> 8;
147
148 // Calculate how much to subtract based on vertical position (height)
149 // This creates the fade-out effect from bottom to top
150 // The formula maps height from 0 to max_height-1 to a value from 255 to 0
151 int8_t subtraction_factor = abs8(height - (max_height - 1)) * 255 /
152 (max_height - 1);
153
154 // Subtract the factor from the noise value (with underflow protection)
155 // qsub8 is a "saturating subtraction" - it won't go below 0
156 return qsub8(noise_val, subtraction_factor);
157}
uint32_t z[NUM_LAYERS]
Definition Fire2023.ino:84
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
UISlider scaleXY("Scale", 8, 1, 100, 1)
UISlider invSpeedZ("Inverse SpeedZ", 20, 1, 100, 1)
UISlider scaleX("ScaleX",.3, 0.1, 3,.01)
int y_speed
uint16_t scale
Definition funky.cpp:83
LIB8STATIC_ALWAYS_INLINE int8_t abs8(int8_t i)
Take the absolute value of a signed 8-bit uint8_t.
Definition math8.h:500
LIB8STATIC_ALWAYS_INLINE uint8_t qsub8(uint8_t i, uint8_t j)
Subtract one byte from another, saturating at 0x00.
Definition math8.h:103
uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z, uint32_t t)
16-bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:440
LIB8STATIC uint8_t cos8(uint8_t theta)
Fast 8-bit approximation of cos(x).
Definition trig8.h:271
#define sin8
Platform-independent alias of the fast sin implementation.
Definition trig8.h:221

References abs8(), cos8(), inoise16(), invSpeedZ(), qsub8(), scale, scaleX(), scaleXY(), sin8, x, y, y_speed, and z.

Referenced by loop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: