2 * @file animationQueueDemo.ino
3 * @brief Demonstrates queued animations, seamless state handoff, and wait().
5 * This example demonstrates:
6 * - Capturing four animations in a fixed-storage queue
7 * - Applying a hard time limit to a queued animation
8 * - Switching compatible animations without resetting their block positions
9 * - Stopping block emission on a timer and draining active blocks
10 * - Adding a non-blocking pause with wait()
13 * - Supported driver with compatible library (SBK_MAX72xx or SBK_HT16K33)
14 * - Bar meter display or LED array wired to the selected driver
17 * Samuel Barabé (Smart Builds & Kits)
25// -----------------------------------------------------------------------------
26// SBK BarDrive Configuration Flags
27// -----------------------------------------------------------------------------
28#define SBK_BARDRIVE_WITH_ANIM // Enable preset animations and queue controls.
30// This example uses four entries. The library default is already four.
31// If you change the global queue capacity, keep it at four or greater here.
32// #define SBK_BARDRIVE_QUEUE_CAPACITY 4
34// -----------------------------------------------------------------------------
35// SELECT YOUR DRIVER SETUP
36// Enable one driver configuration and comment out the other two.
37// -----------------------------------------------------------------------------
39/* === [A] MAX7219/MAX7221 via SOFTWARE SPI (any 3 digital pins) === */
40#define DIN_PIN A4 // Software SPI Data In
41#define CLK_PIN A5 // Software SPI Clock
42#define CS_PIN A3 // Chip Select / LOAD
43#include <SBK_MAX72xxSoft.h>
44SBK_MAX72xxSoft driver(DIN_PIN, CLK_PIN, CS_PIN, 1);
45#include <SBK_BarDrive.h>
46SBK_BarDrive<SBK_MAX72xxSoft> bar(
47 &driver, 0, MatrixPreset::SBK_BarMeter_SK28);
49/* === [B] MAX7219/MAX7221 via HARDWARE SPI (dedicated MCU SPI pins) === */
50// #define CS_PIN A3 // Chip Select / LOAD
51// #include <SBK_MAX72xxHard.h>
52// SBK_MAX72xxHard driver(CS_PIN, 1);
53// #include <SBK_BarDrive.h>
54// SBK_BarDrive<SBK_MAX72xxHard> bar(
55// &driver, 0, MatrixPreset::SBK_BarMeter_SK28);
57/* === [C] HT16K33 via I2C === */
58// #include <SBK_HT16K33.h>
59// const uint8_t NUM_DEV = 1;
60// const uint8_t DEV0_IDX = 0;
61// const uint8_t DEV0_ADD = 0x70;
62// const uint8_t DEV0_NUM_ROWS = 8; // 20-SOP = 8, 24-SOP = 12, 28-SOP = 16
63// SBK_HT16K33 driver(NUM_DEV);
64// #include <SBK_BarDrive.h>
65// SBK_BarDrive<SBK_HT16K33> bar(
66// &driver, 0, MatrixPreset::SBK_BarMeter_SK28);
69 * The default setup uses the SBK BarMeter SK28 28-segment preset.
70 * Change the MatrixPreset or constructor to match your display and wiring.
73void startAnimationQueue()
76 * enqueue() captures the animation and its modifiers in the next queue slot.
77 * The captured animations do not start until startQueue() is called.
79 * Queue index 0: run one complete fill-up/fill-down bounce.
80 * Queue index 1: emit colliding blocks for a maximum of three seconds.
81 * Queue index 2: reverse the in-flight blocks, emit outward for three
82 * seconds, then drain all remaining blocks naturally.
83 * Queue index 3: preserve the empty display for 1000 ms without blocking.
86 * - loop()/noLoop() configure an animation before it is captured.
87 * - forTime() may be used before enqueue(), or immediately afterward to
88 * modify the most recently queued entry, as shown for queue index 1.
89 * - enqueueReverseAnim() captures a reversed copy of the preceding entry
90 * while preserving its trackers and active block positions.
91 * - stopBlockEmissionAfter() may also follow an enqueue operation. It stops
92 * new blocks at the deadline, disables looping, and allows active blocks
93 * to leave before advancing the queue.
94 * - wait() is always non-looping, so noLoop() is unnecessary for waits.
96 * wait() is different from Arduino delay(): loop() and animations.update()
97 * continue to run, so the rest of the application remains responsive.
99 bar.animations().stop()
100 .bounceFillUpIntv(35, 35).noLoop().enqueue()
101 .collidingBlocks(45, 4, 2, 0).enqueue().forTime(3000)
102 .enqueueReverseAnim().stopBlockEmissionAfter(3000)
103 .wait(1000).enqueue()
104 .loopQueue().startQueue();
109#ifdef SBK_HT16K33_IS_DEFINED
110 // HT16K33 setup is required only when configuration [C] is enabled.
111 driver.setAddress(DEV0_IDX, DEV0_ADD);
112 driver.setDriverRows(DEV0_IDX, DEV0_NUM_ROWS);
116 driver.setBrightness(0, 10);
117 bar.setDirection(BarDirection::FORWARD);
118 startAnimationQueue();
123 // update() advances timing and queue transitions; show() sends pixels to the driver.
124 bar.animations().update();
127 // loopQueue() automatically replays the sequence after the final wait.