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

◆ drawTwinkles()

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

Definition at line 155 of file TwinkleFox.ino.

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