|
| 1 | +// Weather animation is rendered procedurally based on a few parameters |
| 2 | +// (time of day, cloud cover, rainfall, etc.). Most of the inputs are NOT |
| 3 | +// real-world units...see comments for explanation of what's needed. |
| 4 | + |
| 5 | +// NeoPixel stuff ---------------------------------------------------------- |
| 6 | + |
| 7 | +#include <Adafruit_NeoPixel.h> |
| 8 | + |
| 9 | +#define NEOPIXEL_PIN 14 // NeoPixels are connected to this pin |
| 10 | +#define NUM_LEDS 16 // Number of NeoPixels |
| 11 | +#define FPS 50 // Animation frame rate (frames per second) |
| 12 | + |
| 13 | +Adafruit_NeoPixel leds(NUM_LEDS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); |
| 14 | + |
| 15 | +// Animation control stuff ------------------------------------------------- |
| 16 | + |
| 17 | +uint8_t renderBuf[NUM_LEDS][3], // Each frame of animation is assembled here |
| 18 | + alphaBuf[NUM_LEDS], // Alpha mask for compositing each layer |
| 19 | + rainBuf[NUM_LEDS], // Extra mask just for raindrop brightness |
| 20 | + rainCounter = 1, // Drop-to-drop countdown, in frames |
| 21 | + rainInterval = 0, // Drop-to-drop interval, frames (0=no rain) |
| 22 | + windSpeed = 0, // Per-frame cloud motion (see comments) |
| 23 | + cloudCover = 0; // Percent cloud cover |
| 24 | + |
| 25 | +uint16_t sunCenter = 0, // Position of 'sun' in 16-bit sky |
| 26 | + sunRadius = 8192, // Size of sun (same units) |
| 27 | + cloudOffset = 0, // Position of cloud bitmap 'seam' |
| 28 | + timeOfDay = 32768; // Fixed-point day/night value (see notes) |
| 29 | + |
| 30 | +uint8_t lightningBrightness = 0; |
| 31 | +uint8_t lightningIntensity = 0; |
| 32 | +uint8_t snowIntensity = 0; |
| 33 | + |
| 34 | +uint32_t cloudBits = 0; // Bitmask of clouds |
| 35 | +#if NUM_LEDS < 32 |
| 36 | + #define NUM_CLOUD_BITS NUM_LEDS |
| 37 | +#else |
| 38 | + #define NUM_CLOUD_BITS 32 |
| 39 | +#endif |
| 40 | + |
| 41 | +#define N_STARS (3 + (NUM_LEDS / 7)) |
| 42 | +struct star { |
| 43 | + uint8_t pos; |
| 44 | + uint8_t brightness; |
| 45 | +} star[N_STARS]; |
| 46 | + |
| 47 | +// Flake will "move," then "stop" when it hits the "ground," then fade. |
| 48 | +// Kinda like raindrops, but moving first. |
| 49 | +#define MAX_FLAKES (3 + (NUM_LEDS / 7)) |
| 50 | +struct flake { |
| 51 | + uint16_t pos; |
| 52 | + int16_t speed; |
| 53 | + uint8_t brightness; |
| 54 | + uint8_t time; |
| 55 | +} flake[MAX_FLAKES]; |
| 56 | +uint8_t nFlakes = 0; |
| 57 | + |
| 58 | +void randomFlake(void) { |
| 59 | + flake[nFlakes].pos = random(65536); |
| 60 | + uint8_t w = windSpeed; |
| 61 | + if(w < 20) w = 20; |
| 62 | + do { |
| 63 | + flake[nFlakes].speed = random(w / -4, (w * 5) / 4); |
| 64 | + } while(!flake[nFlakes].speed); |
| 65 | + flake[nFlakes].brightness = random(128, 255); |
| 66 | + flake[nFlakes].time = random(FPS, FPS * 2); // # frames until snowflake "touches ground" |
| 67 | + nFlakes++; |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +uint16_t lightningCounter = 0; |
| 72 | + |
| 73 | +extern const uint8_t gamma8[]; // Big table at end of this code |
| 74 | + |
| 75 | +// One-time initialization - clears NeoPixels & sets up some variables ----- |
| 76 | + |
| 77 | +void animSetup(void) { |
| 78 | + |
| 79 | + leds.begin(); |
| 80 | + leds.setBrightness(200); |
| 81 | + leds.clear(); // All NeoPixels off ASAP |
| 82 | + leds.show(); |
| 83 | + |
| 84 | + randomSeed(analogRead(A0)); |
| 85 | + |
| 86 | + memset(rainBuf, 0, sizeof(rainBuf)); // Clear rain buffer |
| 87 | + for(uint8_t i=0; i<N_STARS; i++) { // Initialize star positions |
| 88 | + star[i].pos = random(NUM_LEDS); // TO DO: make stars not overlap |
| 89 | + star[i].brightness = random(15, 45); |
| 90 | + } |
| 91 | + memset(flake, 0, sizeof(flake)); // Clear snowflakes |
| 92 | +} |
| 93 | + |
| 94 | +// Utility functions ------------------------------------------------------- |
| 95 | + |
| 96 | +// Set up animation based on some weather attributes like cloud cover, etc. |
| 97 | +void animConfig( |
| 98 | + uint16_t t, // Time of day in fixed-point 16-bit units, where 0=midnight, |
| 99 | + // 32768=noon, 65536=midnight. THIS DOES NOT CORRESPOND TO |
| 100 | + // ANY SORT OF REAL-WORLD UNITS LIKE SECONDS, nor does it |
| 101 | + // handle things like seasons or Daylight Saving Time, it's |
| 102 | + // just an "ish" approximation to give the sky animation some |
| 103 | + // vague context. The time of day should be polled from the |
| 104 | + // same source that's providing the weather data, DO NOT use |
| 105 | + // millis() or micros() to attempt to follow real time, as |
| 106 | + // the NeoPixel library is known to mangle these interrupt- |
| 107 | + // based functions. TIME OF DAY IS "ISH!" |
| 108 | + uint8_t c, // Cloud cover, as a percentage (0-100). |
| 109 | + uint8_t r, // Rainfall as a "strength" value (0-255) that doesn't really |
| 110 | + // correspond to anything except "none" to "max." |
| 111 | + uint8_t s, // Snowfall, similar "strength" value (0-255). |
| 112 | + uint8_t l, // Lightning, ditto. |
| 113 | + uint8_t w) { // Wind speed as a "strength" value (0-255) that also doesn't |
| 114 | + // correspond to anything real; this is the number of fixed- |
| 115 | + // point units that the clouds will move per frame. There are |
| 116 | + // 65536 units around the 'sky,' so a value of 255 will take |
| 117 | + // about 257 frames to make a full revolution of the LEDs, |
| 118 | + // which at 50 FPS would be a little over 5 seconds. |
| 119 | + |
| 120 | + timeOfDay = t; |
| 121 | + cloudCover = (c > 100) ? 100 : c; |
| 122 | + rainInterval = r ? map(r, 1, 255, 64, 1) : 0; |
| 123 | + windSpeed = w; |
| 124 | + lightningIntensity = l; |
| 125 | + snowIntensity = s; |
| 126 | + |
| 127 | + // Randomize cloud bitmask based on cloud cover percentage: |
| 128 | + cloudBits = 0; |
| 129 | + for(uint8_t i=0; i<NUM_CLOUD_BITS; i++) { |
| 130 | + cloudBits <<= 1; |
| 131 | + if(cloudCover > random(150)) cloudBits |= 1; |
| 132 | + } |
| 133 | + |
| 134 | + nFlakes = 0; |
| 135 | + memset(flake, 0, sizeof(flake)); |
| 136 | + if(s) { |
| 137 | + uint8_t n = 3 + (snowIntensity * (MAX_FLAKES - 2)) / 256; |
| 138 | + while(nFlakes < n) { |
| 139 | + randomFlake(); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// Interpolate between two 'packed' (32-bit) RGB colors. |
| 145 | +// Second argument is weighting (0-255) of second color. |
| 146 | +uint32_t colorInterp(uint32_t color1, uint32_t color2, uint8_t w) { |
| 147 | + uint8_t r1 = (color1 >> 16) & 0xFF, |
| 148 | + g1 = (color1 >> 8) & 0xFF, |
| 149 | + b1 = color1 & 0xFF, |
| 150 | + r2 = (color2 >> 16) & 0xFF, |
| 151 | + g2 = (color2 >> 8) & 0xFF, |
| 152 | + b2 = color2 & 0xFF; |
| 153 | + uint16_t w2 = (uint16_t)w + 1, // 1-256 |
| 154 | + w1 = 257 - w2; // 1-256 |
| 155 | + r1 = (r1 * w1 + r2 * w2) >> 8; |
| 156 | + g1 = (g1 * w1 + g2 * w2) >> 8; |
| 157 | + b1 = (b1 * w1 + b2 * w2) >> 8; |
| 158 | + return (((uint32_t)r1 << 16) | ((uint32_t)g1 << 8) | b1); |
| 159 | +} |
| 160 | + |
| 161 | +// Using alphaBuf as a mask, fill an RGB color atop renderBuf |
| 162 | +void overlay(uint8_t r, uint8_t g, uint8_t b) { |
| 163 | + uint16_t i, a1, a2; |
| 164 | + for(i=0; i<NUM_LEDS; i++) { |
| 165 | + a1 = alphaBuf[i] + 1; // 1-256 |
| 166 | + a2 = 257 - a1; // 1-256 |
| 167 | + renderBuf[i][0] = (r * a1 + renderBuf[i][0] * a2) >> 8; |
| 168 | + renderBuf[i][1] = (g * a1 + renderBuf[i][1] * a2) >> 8; |
| 169 | + renderBuf[i][2] = (b * a1 + renderBuf[i][2] * a2) >> 8; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// Same as above, for packed 32-bit RGB value |
| 174 | +void overlay(uint32_t color) { |
| 175 | + overlay((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF); |
| 176 | +} |
| 177 | + |
| 178 | +void waitForFrame(void) { |
| 179 | + static uint32_t timeOfLastFrame = 0L; |
| 180 | + uint32_t t; |
| 181 | + while(((t = millis()) - timeOfLastFrame) < (1000 / FPS)) yield(); |
| 182 | + timeOfLastFrame = t; |
| 183 | +} |
| 184 | + |
| 185 | +#define NIGHTSKYCLEAR 0x0a1923 |
| 186 | +#define DAYSKYCLEAR 0x28648c |
| 187 | +#define NIGHTSKYCLOUDBG 0x2c2425 |
| 188 | +#define DAYSKYCLOUDBG 0x5e6065 |
| 189 | +#define NIGHTSKYCLOUDFG 0x515159 |
| 190 | +#define DAYSKYCLOUDFG 0xc2c2c2 |
| 191 | +#define NIGHTSNOW 0xa6b1c0 |
| 192 | +#define DAYSNOW 0xffffff |
| 193 | +#define SUNCLEAR 0xffff60 |
| 194 | +#define SUNCLOUDY 0x7a7a61 |
| 195 | + |
| 196 | +void renderFrame(void) { |
| 197 | + // Display *prior* frame of data at start of function -- |
| 198 | + // this ensures uniform updates, as render time may vary. |
| 199 | + leds.show(); |
| 200 | + |
| 201 | + // Then begin processing next frame... |
| 202 | + |
| 203 | + int i; |
| 204 | + |
| 205 | + // tod: 0-64K, where 0 = midnight, 32K = noon, 64K = midnight |
| 206 | + // this is an artistic approximation and doesn't take seasons, |
| 207 | + // etc into consideration. if you need that, can fudge it into |
| 208 | + // tod rather than here. |
| 209 | + // Sunrise and sunset are two 90-minute periods centered around |
| 210 | + // 6am and 6pm (again, not factoring in seasons, daylight savings |
| 211 | + // time, etc.). Sky and other effects will interpolate between |
| 212 | + // day and night states for these two things. |
| 213 | + long y = timeOfDay; |
| 214 | + uint8_t dayWeight; |
| 215 | + if(y > 32767) y = 65536 - y; |
| 216 | + y = y * 256L / 4096 - 896; |
| 217 | + dayWeight = (y > 255) ? 255 : ((y < 0) ? 0 : y); // 0-255 night/day |
| 218 | + |
| 219 | + // Determine sky and cloud color based on % of cloud cover |
| 220 | + uint32_t |
| 221 | + clearSkyColor = colorInterp(NIGHTSKYCLEAR , DAYSKYCLEAR , dayWeight), |
| 222 | + cloudySkyColor = colorInterp(NIGHTSKYCLOUDBG, DAYSKYCLOUDBG, dayWeight), |
| 223 | + cloudColor = colorInterp(NIGHTSKYCLOUDFG, DAYSKYCLOUDFG, dayWeight), |
| 224 | + skyColor = colorInterp(clearSkyColor, cloudySkyColor, map(cloudCover, 30, 70, 0, 255)); |
| 225 | + |
| 226 | + for(i=0; i<NUM_LEDS; i++) { |
| 227 | + renderBuf[i][0] = skyColor >> 16; |
| 228 | + renderBuf[i][1] = skyColor >> 8; |
| 229 | + renderBuf[i][2] = skyColor; |
| 230 | + } |
| 231 | + |
| 232 | + // Stars |
| 233 | + if(dayWeight < 128) { // Dark? Or getting there? |
| 234 | + uint16_t nightWeight = 257 - dayWeight; |
| 235 | + memset(alphaBuf, 0, sizeof(alphaBuf)); |
| 236 | + for(i=0; i<N_STARS; i++) { |
| 237 | + alphaBuf[star[i].pos] = (nightWeight * random(star[i].brightness/2, star[i].brightness)) >> 8; |
| 238 | + } |
| 239 | + overlay(255, 255, 255); |
| 240 | + } else { |
| 241 | + sunRadius = map(dayWeight, 128, 255, 1, 8192); |
| 242 | + uint16_t x; |
| 243 | + int16_t px1, px2, sx1, sx2; |
| 244 | + |
| 245 | + // Clear alpha buffer, gonna render 'sun' there... |
| 246 | + memset(alphaBuf, 0, sizeof(alphaBuf)); |
| 247 | + |
| 248 | + uint32_t |
| 249 | + sunColor = colorInterp(SUNCLEAR, SUNCLOUDY, map(cloudCover, 30, 70, 0, 255)); |
| 250 | + |
| 251 | + // Figure overlap between sun and each pixel... |
| 252 | + // uint16_t left, right, dist1, dist2; |
| 253 | + for(i=0; i<NUM_LEDS; i++) { |
| 254 | + // Pixel coord in fixed-point space |
| 255 | + x = (i * 65536L) / NUM_LEDS; |
| 256 | + int16_t foo = sunCenter - x; // sun center in pixel space |
| 257 | + sx1 = foo - sunRadius; |
| 258 | + sx2 = foo + sunRadius; |
| 259 | + px1 = 0; |
| 260 | + px2 = 65536 / NUM_LEDS; |
| 261 | + if((sx1 >= px2) || (sx2 < 0)) continue; // No overlap |
| 262 | + else if((sx1 <= 0) && (sx2 >= px2)) alphaBuf[i] = 255; // Fully encompassed |
| 263 | + else { |
| 264 | + if(sx1 > 0) { |
| 265 | + if(sx2 < px2) { |
| 266 | + alphaBuf[i] = 255L * (sx2 - sx1) / (px2 - px1); |
| 267 | + } else { |
| 268 | + alphaBuf[i] = 255L * (px2 - sx1) / (px2 - px1); |
| 269 | + } |
| 270 | + } else { |
| 271 | + alphaBuf[i] = 255L * (sx2 - px1) / (px2 - px1); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + overlay(sunColor); // Composite sun atop sky |
| 277 | + } |
| 278 | + |
| 279 | + if(cloudBits) { |
| 280 | + // Clear alpha buffer, gonna render clouds there... |
| 281 | + memset(alphaBuf, 0, sizeof(alphaBuf)); |
| 282 | + uint16_t x, minor; |
| 283 | + uint8_t major, l, r; |
| 284 | + for(i=0; i<NUM_LEDS; i++) { |
| 285 | + x = (i * 65536L) / NUM_LEDS - cloudOffset; // Pixel coord in fixed-point space (0-65535) relative to clouds |
| 286 | + x = (x * (NUM_CLOUD_BITS * 256UL)) / 65536; // Scale to cloud pixel space |
| 287 | + major = x >> 8; // Left bit number (0 to NUM_CLOUD_BITS-1) |
| 288 | + minor = x & 0xFF; // Weight (0-255) of next bit over |
| 289 | + l = (cloudBits & (1 << major)) ? 220 : 0; // Left bit opacity |
| 290 | + if(++major >= NUM_CLOUD_BITS) major = 0; // Next bit over |
| 291 | + r = (cloudBits & (1 << major)) ? 220 : 0; // Right bit opacity |
| 292 | + alphaBuf[i] = ((l * (257 - minor)) + (r * (minor + 1))) >> 8; // Blend |
| 293 | + } |
| 294 | + |
| 295 | + uint32_t c = colorInterp(NIGHTSKYCLOUDFG, DAYSKYCLOUDFG, dayWeight); |
| 296 | + overlay(c); // Composite clouds atop sky |
| 297 | + } |
| 298 | + |
| 299 | + if(rainInterval) { |
| 300 | + memset(alphaBuf, 0, sizeof(alphaBuf)); |
| 301 | + for(i=0; i<NUM_LEDS; i++) { |
| 302 | + rainBuf[i] = (rainBuf[i] * (uint16_t)245) >> 8; |
| 303 | + } |
| 304 | + // Periodically, randomly, add a drop to rainBuf[] |
| 305 | + if(!--rainCounter) { |
| 306 | + i = random(NUM_LEDS); // Which spot? |
| 307 | + int16_t foo = rainBuf[i] + 255; |
| 308 | + if(foo > 255) foo = 255; |
| 309 | + rainBuf[i] = foo; |
| 310 | + uint8_t r4 = rainInterval / 4; |
| 311 | + if(r4 < 1) r4 = 1; |
| 312 | + rainCounter = random(r4, rainInterval); |
| 313 | + } |
| 314 | + memcpy(alphaBuf, rainBuf, sizeof(rainBuf)); |
| 315 | + overlay(130, 130, 150); |
| 316 | + } |
| 317 | + |
| 318 | + if(nFlakes) { |
| 319 | + uint16_t x, minor; |
| 320 | + uint8_t major, l, r; |
| 321 | + memset(alphaBuf, 0, sizeof(alphaBuf)); |
| 322 | + for(i=0; i<nFlakes; i++) { |
| 323 | + // Render flake here |
| 324 | + x = (flake[i].pos * (NUM_LEDS * 256UL)) / 65536; |
| 325 | + major = x >> 8; // Left pixel number (0 to NUM_LEDS-1) |
| 326 | + minor = x & 0xFF; // Weight (0-255) of next pixel over |
| 327 | + alphaBuf[major] = (alphaBuf[major] * (1 + minor)) + (flake[i].brightness * (257 - minor)) >> 8; |
| 328 | + if(++major >= NUM_LEDS) major = 0; |
| 329 | + alphaBuf[major] = (alphaBuf[major] * (257 - minor)) + (flake[i].brightness * (1 + minor)) >> 8; |
| 330 | + flake[i].pos += flake[i].speed; |
| 331 | + if(flake[i].time) { |
| 332 | + flake[i].time--; |
| 333 | + } else { |
| 334 | + flake[i].brightness = (flake[i].brightness * 253) >> 8; |
| 335 | + if(!flake[i].brightness) { |
| 336 | + memcpy(&flake[i], &flake[nFlakes-1], sizeof(struct flake)); // Move last flake to this pos. |
| 337 | + i--; // Flake moved, so don't increment |
| 338 | + nFlakes--; // Decrement number of flakes |
| 339 | + randomFlake(); // And add a new one in last pos. |
| 340 | + } |
| 341 | + } |
| 342 | + } |
| 343 | + overlay(255, 255, 255); |
| 344 | + } |
| 345 | + |
| 346 | + if(lightningBrightness) { |
| 347 | + for(i=0; i<NUM_LEDS; i++) alphaBuf[i] = lightningBrightness; |
| 348 | + overlay(255, 255, 255); |
| 349 | + lightningBrightness = (lightningBrightness * 220) >> 8; |
| 350 | + } |
| 351 | + if(lightningIntensity) { |
| 352 | + if(!random(50 + (255 - lightningIntensity) * 3)) { |
| 353 | + i = random(128, 256); |
| 354 | + if(i > lightningBrightness) lightningBrightness = i; |
| 355 | + } |
| 356 | + } |
| 357 | + |
| 358 | + sunCenter += 65536 / 30 / FPS; // 30 sec for 1 revolution |
| 359 | + cloudOffset -= windSpeed; |
| 360 | + |
| 361 | +// timeOfDay += 65536/60/FPS; // 1 min for day/night cycle |
| 362 | + |
| 363 | + // Convert RGB renderbuf to gamma-corrected LED-native color order: |
| 364 | + for(uint16_t i=0; i<NUM_LEDS; i++) { |
| 365 | + leds.setPixelColor(i, |
| 366 | + pgm_read_byte(&gamma8[renderBuf[i][0]]), |
| 367 | + pgm_read_byte(&gamma8[renderBuf[i][1]]), |
| 368 | + pgm_read_byte(&gamma8[renderBuf[i][2]])); |
| 369 | + } |
| 370 | + // DON'T call leds.show() here! That's done at start of function. |
| 371 | +} |
| 372 | + |
| 373 | +// Gamma correction improves appearance of midrange colors |
| 374 | +const uint8_t gamma8[] PROGMEM = { |
| 375 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 376 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, |
| 377 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, |
| 378 | + 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, |
| 379 | + 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, |
| 380 | + 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, |
| 381 | + 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, |
| 382 | + 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, |
| 383 | + 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, |
| 384 | + 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, |
| 385 | + 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, |
| 386 | + 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, |
| 387 | + 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, |
| 388 | + 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, |
| 389 | + 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, |
| 390 | + 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 }; |
| 391 | + |
| 392 | + |
0 commit comments