FastLED 3.10.6
Loading...
Searching...
No Matches

◆ drawTwinkles()

void drawTwinkles ( CRGBSet & L)
Examples
TwinkleFox.ino.

Definition at line 160 of file TwinkleFox.ino.

161{
162 // "PRNG16" is the pseudorandom number generator
163 // It MUST be reset to the same starting value each time
164 // this function is called, so that the sequence of 'random'
165 // numbers that it generates is (paradoxically) stable.
166 uint16_t PRNG16 = 11337;
167
168 uint32_t clock32 = millis();
169
170 // Set up the background color, "bg".
171 // if AUTO_SELECT_BACKGROUND_COLOR == 1, and the first two colors of
172 // the current palette are identical, then a deeply faded version of
173 // that color is used for the background color
174 CRGB bg;
175 if( (AUTO_SELECT_BACKGROUND_COLOR == 1) &&
176 (gCurrentPalette[0] == gCurrentPalette[1] )) {
177 bg = gCurrentPalette[0];
178 uint8_t bglight = bg.getAverageLight();
179 if( bglight > 64) {
180 bg.nscale8_video( 16); // very bright, so scale to 1/16th
181 } else if( bglight > 16) {
182 bg.nscale8_video( 64); // not that bright, so scale to 1/4th
183 } else {
184 bg.nscale8_video( 86); // dim, scale to 1/3rd.
185 }
186 } else {
187 bg = gBackgroundColor; // just use the explicitly defined background color
188 }
189
190 uint8_t backgroundBrightness = bg.getAverageLight();
191
192 for( CRGB& pixel: L) {
193 PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
194 uint16_t myclockoffset16= PRNG16; // use that number as clock offset
195 PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
196 // use that number as clock speed adjustment factor (in 8ths, from 8/8ths to 23/8ths)
197 uint8_t myspeedmultiplierQ5_3 = ((((PRNG16 & 0xFF)>>4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
198 uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
199 uint8_t myunique8 = PRNG16 >> 8; // get 'salt' value for this pixel
200
201 // We now have the adjusted 'clock' for this pixel, now we call
202 // the function that computes what color the pixel should be based
203 // on the "brightness = f( time )" idea.
204 CRGB c = computeOneTwinkle( myclock30, myunique8);
205
206 uint8_t cbright = c.getAverageLight();
207 int16_t deltabright = cbright - backgroundBrightness;
208 if( deltabright >= 32 || (!bg)) {
209 // If the new pixel is significantly brighter than the background color,
210 // use the new color.
211 pixel = c;
212 } else if( deltabright > 0 ) {
213 // If the new pixel is just slightly brighter than the background color,
214 // mix a blend of the new color and the background color
215 pixel = blend( bg, c, deltabright * 8);
216 } else {
217 // if the new pixel is not at all brighter than the background color,
218 // just use the background color.
219 pixel = bg;
220 }
221 }
222}
CRGBPalette16 gCurrentPalette
CRGB gBackgroundColor
CRGB computeOneTwinkle(uint32_t ms, uint8_t salt)
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
fl::CRGB CRGB
Definition crgb.h:25
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
unsigned char uint8_t
Definition stdint.h:208
fl::i16 int16_t
Definition stdint.h:214
fl::u32 uint32_t
Definition stdint.h:218
fl::u16 uint16_t
Definition stdint.h:213
FASTLED_FORCE_INLINE CRGB & nscale8_video(u8 scaledown) FL_NO_EXCEPT
Scale down a RGB to N/256ths of it's current brightness using "video" dimming rules.
Definition crgb.hpp:72
FASTLED_FORCE_INLINE u8 getAverageLight() const FL_NO_EXCEPT
Get the average of the R, G, and B values.
Definition crgb.hpp:152
#define AUTO_SELECT_BACKGROUND_COLOR
Definition twinklefox.h:99

References AUTO_SELECT_BACKGROUND_COLOR, blend(), computeOneTwinkle(), gBackgroundColor, gCurrentPalette, fl::CRGB::getAverageLight(), and fl::CRGB::nscale8_video().

Referenced by loop().

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