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

◆ drawTwinkles()

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

Definition at line 157 of file TwinkleFox.ino.

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

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

Referenced by loop().

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