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

◆ drawTwinkles()

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

Definition at line 147 of file TwinkleFox.ino.

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

References AUTO_SELECT_BACKGROUND_COLOR, 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: