CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
CanvasAnim.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
16
17namespace cdc::ui::anim {
18
20enum : uint8_t {
33};
34
36enum : uint8_t {
38 FLAG_YOYO = 0x02,
41};
42
44enum : int8_t {
48};
49
51enum : uint8_t {
55};
56
58enum : uint8_t {
63};
64
66constexpr uint16_t REPEAT_FOREVER = 0xFFFF;
67
70struct Completion {
71 uint32_t doneActionId = 0;
72 uint32_t handle = 0;
73 uint32_t refId = 0;
74};
75
78public:
79 virtual ~AnimTarget() = default;
80 virtual bool getElemOffset(uint32_t elemId, int16_t* ox, int16_t* oy) = 0;
81 virtual bool setElemOffset(uint32_t elemId, int16_t ox, int16_t oy) = 0;
82 virtual bool setElemVisible(uint32_t elemId, bool visible) = 0;
83};
84
91int32_t ease(uint8_t curve, int32_t t);
92
95 uint32_t elemId = 0;
96 int16_t fromX = 0;
97 int16_t fromY = 0;
98 int16_t toX = 0;
99 int16_t toY = 0;
100 uint16_t durationMs = 0;
101 uint16_t delayMs = 0;
102 uint16_t repeat = 0;
104 uint8_t flags = 0;
105 uint32_t doneActionId = 0;
106 uint32_t startAfter = 0;
107};
108
118public:
119 static constexpr uint8_t MAX_TWEENS = 16;
120 static constexpr uint16_t MAX_DURATION_MS = 60000;
121
128 uint32_t start(const TweenConfig& cfg, uint32_t nowMs);
129
134 uint32_t blink(uint32_t elemId, uint16_t periodMs, uint16_t count,
135 uint32_t doneActionId, uint32_t nowMs);
136
138 bool cancel(uint32_t handle);
140 void cancelForElem(uint32_t elemId);
141 bool pause(uint32_t handle, bool paused, uint32_t nowMs);
143 int8_t state(uint32_t handle) const;
145 uint8_t activeCount() const;
147 void shiftTime(uint32_t deltaMs);
149 void reset();
150
159 bool advance(uint32_t nowMs, AnimTarget& target,
160 Completion* done, uint8_t doneCap, uint8_t* doneCount);
161
162private:
163 enum class Kind : uint8_t { Move, Blink };
164 enum class Phase : uint8_t { Free, Chained, Delayed, Running };
165
166 struct Slot {
167 uint32_t handle = 0;
168 uint32_t elemId = 0;
169 uint32_t doneActionId = 0;
170 uint32_t startAfter = 0; // predecessor handle while Chained
171 uint32_t startMs = 0; // run start (delay already applied)
172 int16_t fromX = 0, fromY = 0, toX = 0, toY = 0;
173 uint16_t durationMs = 0;
174 uint16_t delayMs = 0;
175 uint16_t remaining = 0; // remaining extra runs
176 uint16_t blinkCount = 0; // remaining blink phases (Kind::Blink)
177 uint32_t pausedAtMs = 0;
178 uint8_t easing = EASE_LINEAR;
179 uint8_t flags = 0;
180 int16_t lastX = 0, lastY = 0; // last offset written to the target
181 Kind kind = Kind::Move;
182 Phase phase = Phase::Free;
183 bool paused = false;
184 bool reversed = false; // current yoyo direction / blink phase
185 bool started = false; // FROM_CURRENT/SHOW_START applied
186 bool hasLast = false;
187 bool moved = false; // wrote a visible change this tick
188 };
189
190 Slot* findSlot(uint32_t handle);
191 const Slot* findSlot(uint32_t handle) const;
192 Slot* allocSlot();
193 void releaseChain(uint32_t handle);
194 void finishSlot(Slot& s, AnimTarget& target, uint32_t nowMs,
195 Completion* done, uint8_t doneCap, uint8_t* doneCount);
196 bool advanceMove(Slot& s, uint32_t nowMs, AnimTarget& target);
197 bool advanceBlink(Slot& s, uint32_t nowMs, AnimTarget& target);
198 bool writeOffset(Slot& s, AnimTarget& target, int16_t x, int16_t y);
199
200 Slot slots_[MAX_TWEENS];
201 uint32_t nextHandle_ = 1;
202};
203
213public:
214 static constexpr uint8_t MAX_SPRITES = 8;
215 static constexpr uint16_t MAX_FRAMES = 32;
216
218 void setArena(uint8_t* buf, uint32_t cap);
219 bool hasArena() const { return arena_ != nullptr; }
220
225 int32_t create(uint16_t w, uint16_t h, uint16_t frameCount,
226 const uint8_t* data, uint32_t len);
228 bool setMask(uint32_t handle, const uint8_t* mask, uint32_t len);
231 bool setFrameDurations(uint32_t handle, const uint16_t* ms, uint16_t count);
232 bool destroy(uint32_t handle);
233
234 bool setFlags(uint32_t handle, uint8_t flags);
236 bool setScale(uint32_t handle, uint8_t scale);
237 bool setFrame(uint32_t handle, uint16_t frame);
238 int32_t frame(uint32_t handle) const;
239
247 bool scroll(uint32_t handle, uint16_t windowW, uint16_t stepPx,
248 uint16_t gapPx, uint16_t frameMs, uint32_t nowMs);
249
250 bool play(uint32_t handle, uint8_t mode, uint16_t frameMs, uint16_t repeat,
251 uint32_t doneActionId, uint32_t nowMs);
252 bool stop(uint32_t handle);
256 void stopAll();
257 bool anyPlaying() const;
258 void shiftTime(uint32_t deltaMs);
259 void reset();
260
263 bool advance(uint32_t nowMs, Completion* done, uint8_t doneCap,
264 uint8_t* doneCount);
265
267 struct FrameView {
268 const uint8_t* data = nullptr;
269 const uint8_t* mask = nullptr;
270 uint16_t w = 0, h = 0;
271 uint8_t flags = 0;
272 uint8_t scale = 1;
273 uint16_t scrollX = 0;
274 uint16_t scrollWindow = 0;
275 uint16_t scrollGap = 0;
276 };
277 bool frameView(uint32_t handle, FrameView* out) const;
278
279private:
280 struct Slot {
281 uint32_t handle = 0; // 0 = free
282 uint32_t sheetOff = 0;
283 uint32_t maskOff = 0;
284 uint32_t durOff = 0;
285 uint32_t bytes = 0; // arena bytes at sheetOff (sheet only)
286 uint32_t doneActionId = 0;
287 uint32_t nextFrameAt = 0;
288 uint16_t w = 0, h = 0;
289 uint16_t frameCount = 0;
290 uint16_t curFrame = 0;
291 uint16_t frameMs = 0;
292 uint16_t remaining = 0; // remaining extra cycles
293 uint16_t scrollX = 0; // marquee position
294 uint16_t scrollWindow = 0; // marquee window width (0 = frames mode)
295 uint16_t scrollStep = 0; // px per beat
296 uint16_t scrollGap = 0; // blank px between wraps
297 uint8_t mode = SPRITE_ONCE;
298 uint8_t flags = 0;
299 uint8_t scale = 1;
300 bool playing = false;
301 bool hasMask = false;
302 bool hasDur = false;
303 int8_t dir = 1; // ping-pong direction
304 };
305
306 Slot* findSlot(uint32_t handle);
307 const Slot* findSlot(uint32_t handle) const;
308 uint32_t frameBytes(const Slot& s) const;
309 uint16_t frameDuration(const Slot& s) const;
310 int32_t arenaAlloc(uint32_t bytes);
311 void arenaFree(uint32_t off, uint32_t bytes);
312
313 Slot slots_[MAX_SPRITES];
314 uint8_t* arena_ = nullptr;
315 uint32_t arenaCap_ = 0;
316 uint32_t arenaUsed_ = 0;
317 uint32_t nextHandle_ = 1;
318};
319
320} // namespace cdc::ui::anim
uint8_t flags
Interface the animator drives; implemented by CanvasView on its elements.
Definition CanvasAnim.h:77
virtual bool setElemVisible(uint32_t elemId, bool visible)=0
virtual ~AnimTarget()=default
virtual bool getElemOffset(uint32_t elemId, int16_t *ox, int16_t *oy)=0
virtual bool setElemOffset(uint32_t elemId, int16_t ox, int16_t oy)=0
Fixed-slot tween engine animating element offsets (plus a blink convenience that toggles visibility).
Definition CanvasAnim.h:117
uint8_t activeCount() const
Number of live slots (delayed, chained, running or paused).
bool pause(uint32_t handle, bool paused, uint32_t nowMs)
static constexpr uint8_t MAX_TWEENS
Definition CanvasAnim.h:119
void reset()
Drop all slots without firing completions (teardown).
bool advance(uint32_t nowMs, AnimTarget &target, Completion *done, uint8_t doneCap, uint8_t *doneCount)
Advance all tweens to nowMs, applying offsets/visibility.
void cancelForElem(uint32_t elemId)
Silently cancel every tween bound to an element (element was removed).
uint32_t start(const TweenConfig &cfg, uint32_t nowMs)
Start an offset tween. A FROM_CURRENT start captures the live offset lazily on the first advance() af...
void shiftTime(uint32_t deltaMs)
Shift every time base forward (view was paused for deltaMs).
int8_t state(uint32_t handle) const
uint32_t blink(uint32_t elemId, uint16_t periodMs, uint16_t count, uint32_t doneActionId, uint32_t nowMs)
Start a visibility blink: count on/off cycles of periodMs per phase, ending visible....
bool cancel(uint32_t handle)
Cancel a tween and its chain (handle 0 = all). No completion fires.
static constexpr uint16_t MAX_DURATION_MS
Definition CanvasAnim.h:120
Fixed-slot store for multi-frame 1-bpp sprites with host-driven playback. Pixel data lives in an inje...
Definition CanvasAnim.h:212
static constexpr uint16_t MAX_FRAMES
Definition CanvasAnim.h:215
bool stop(uint32_t handle)
bool destroy(uint32_t handle)
bool setMask(uint32_t handle, const uint8_t *mask, uint32_t len)
Attach a mask plane (same sheet layout/size as the frame data).
bool advance(uint32_t nowMs, Completion *done, uint8_t doneCap, uint8_t *doneCount)
bool setFlags(uint32_t handle, uint8_t flags)
int32_t create(uint16_t w, uint16_t h, uint16_t frameCount, const uint8_t *data, uint32_t len)
Copy a frame sheet into the arena and allocate a sprite slot.
void setArena(uint8_t *buf, uint32_t cap)
Inject the pixel arena. Must happen before create(); resets the store.
bool play(uint32_t handle, uint8_t mode, uint16_t frameMs, uint16_t repeat, uint32_t doneActionId, uint32_t nowMs)
int32_t frame(uint32_t handle) const
Current frame or -1.
bool frameView(uint32_t handle, FrameView *out) const
bool scroll(uint32_t handle, uint16_t windowW, uint16_t stepPx, uint16_t gapPx, uint16_t frameMs, uint32_t nowMs)
Turn the sprite into a horizontal scroller (marquee backing).
static constexpr uint8_t MAX_SPRITES
Definition CanvasAnim.h:214
void shiftTime(uint32_t deltaMs)
bool setFrame(uint32_t handle, uint16_t frame)
bool setFrameDurations(uint32_t handle, const uint16_t *ms, uint16_t count)
bool setScale(uint32_t handle, uint8_t scale)
Integer upscale factor 1..4 applied when the sprite is drawn.
uint8_t curve
@ FLAG_SHOW_START
Show the element when the delay elapses.
Definition CanvasAnim.h:40
@ FLAG_HIDE_DONE
Hide the element on completion.
Definition CanvasAnim.h:39
@ FLAG_YOYO
Each repeat run alternates direction.
Definition CanvasAnim.h:38
@ FLAG_FROM_CURRENT
Ignore from_x/y, start at the live offset.
Definition CanvasAnim.h:37
constexpr uint16_t REPEAT_FOREVER
Repeat-forever sentinel shared by tweens and sprite playback.
Definition CanvasAnim.h:66
@ SPRITE_ONCE
Play to the last frame and hold it.
Definition CanvasAnim.h:52
@ SPRITE_FLAG_ROT_90
Rotate 90 deg clockwise (before flips).
Definition CanvasAnim.h:62
@ SPRITE_FLAG_OPAQUE
Unset data bits paint white.
Definition CanvasAnim.h:59
int32_t ease(uint8_t curve, int32_t t)
Apply an easing curve to a normalized 8.8 fixed-point time.
uint32_t refId
Element id (tween) or final frame (sprite).
Definition CanvasAnim.h:73
uint32_t handle
Tween handle or sprite handle.
Definition CanvasAnim.h:72
Replay accessors: geometry, flags and pixel pointers of a sprite.
Definition CanvasAnim.h:267
const uint8_t * data
Current frame pixels.
Definition CanvasAnim.h:268
const uint8_t * mask
Mask plane or null.
Definition CanvasAnim.h:269
uint16_t scrollGap
Blank gap between wraps.
Definition CanvasAnim.h:275
uint16_t scrollWindow
Window width; 0 = not scrolling.
Definition CanvasAnim.h:274
uint16_t scrollX
Scroll position (scrollWindow > 0).
Definition CanvasAnim.h:273
Tween start configuration (mirrors host_anim_t plus the blink shorthand).
Definition CanvasAnim.h:94
uint16_t repeat
Extra runs; REPEAT_FOREVER = endless.
Definition CanvasAnim.h:102
uint32_t startAfter
Predecessor tween handle; 0 = start now.
Definition CanvasAnim.h:106