Handle automatic triggering of ripples at random intervals
| 321 | |
| 322 | // Handle automatic triggering of ripples at random intervals |
| 323 | void processAutoTrigger(uint32_t now) { |
| 324 | // Static variable to remember when the next auto-trigger should happen |
| 325 | static uint32_t nextTrigger = 0; |
| 326 | |
| 327 | // Calculate time until next trigger |
| 328 | uint32_t trigger_delta = nextTrigger - now; |
| 329 | |
| 330 | // Handle timer overflow (happens after ~49 days of continuous running) |
| 331 | if (trigger_delta > 10000) { |
| 332 | // If the delta is suspiciously large, we probably rolled over |
| 333 | trigger_delta = 0; |
| 334 | } |
| 335 | |
| 336 | // Only proceed if auto-trigger is enabled |
| 337 | if (autoTrigger) { |
| 338 | // Check if it's time for the next trigger |
| 339 | if (now >= nextTrigger) { |
| 340 | // Create a ripple |
| 341 | triggerRipple(); |
| 342 | |
| 343 | // Calculate the next trigger time based on the speed slider |
| 344 | // Invert the speed value so higher slider = faster triggers |
| 345 | float speed = 1.0f - triggerSpeed.value(); |
| 346 | |
| 347 | // Calculate min and max random intervals |
| 348 | // Higher speed = shorter intervals between triggers |
| 349 | uint32_t min_rand = 400 * speed; // Minimum interval (milliseconds) |
| 350 | uint32_t max_rand = 2000 * speed; // Maximum interval (milliseconds) |
| 351 | |
| 352 | // Ensure min is actually less than max (handles edge cases) |
| 353 | uint32_t min = fl::min(min_rand, max_rand); |
| 354 | uint32_t max = fl::max(min_rand, max_rand); |
| 355 | |
| 356 | // Ensure min and max aren't equal (would cause random() to crash) |
| 357 | if (min == max) { |
| 358 | max += 1; |
| 359 | } |
| 360 | |
| 361 | // Schedule the next trigger at a random time in the future |
| 362 | nextTrigger = now + random(min, max); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void wavefx_setup() { |
| 368 | // Create a screen map for visualization in the FastLED web compiler |
no test coverage detected