ISF15ACP4 Driver 1.0.0
API reference
Loading...
Searching...
No Matches
isf15acp4_animator.ipp
Go to the documentation of this file.
1#ifndef ISF15ACP4_ANIMATOR_HEADER_INCLUDED
2#error "isf15acp4_animator.ipp must be included from isf15acp4_animator.hpp"
3#endif
4
5#include <cmath>
6
7namespace isf15acp4 {
8
9inline Animator::Animator(GraphicsContext& gfx, AnimationFrameFn fn, void* user_data,
10 uint32_t fps) noexcept
11 : gfx_(gfx), frame_fn_(fn), user_data_(user_data) {
12 SetFps(fps);
13}
14
15inline bool Animator::Tick(uint32_t elapsed_ms) noexcept {
16 elapsed_accum_ += elapsed_ms;
17 if (elapsed_accum_ < frame_ms_) {
18 return false;
19 }
20 elapsed_accum_ -= frame_ms_;
21 RenderNow();
22 return true;
23}
24
25inline void Animator::RenderNow() noexcept {
26 if (frame_fn_ != nullptr) {
28 }
29 ++frame_;
30}
31
32namespace animations {
33
34inline void ColorWheel(GraphicsContext& gfx, uint32_t frame, void* /*user_data*/) noexcept {
35 const uint8_t w = gfx.Width();
36 const uint8_t h = gfx.Height();
37 const uint8_t cx = static_cast<uint8_t>(w / 2);
38 const uint8_t cy = static_cast<uint8_t>(h / 2);
39 const float phase = static_cast<float>(frame) * 0.08f;
40
41 for (uint8_t y = 0; y < h; ++y) {
42 for (uint8_t x = 0; x < w; ++x) {
43 const float dx = static_cast<float>(x - cx);
44 const float dy = static_cast<float>(y - cy);
45 const float angle = std::atan2(dy, dx) + phase;
46 const uint8_t r = static_cast<uint8_t>((std::sin(angle) * 0.5f + 0.5f) * 255.0f);
47 const uint8_t g =
48 static_cast<uint8_t>((std::sin(angle + 2.094f) * 0.5f + 0.5f) * 255.0f);
49 const uint8_t b =
50 static_cast<uint8_t>((std::sin(angle + 4.188f) * 0.5f + 0.5f) * 255.0f);
51 gfx.SetPixel(x, y, MakeColor565(r, g, b));
52 }
53 }
54 gfx.DrawString(2, 2, "WHEEL", colors::White, colors::Black);
55}
56
57inline void Plasma(GraphicsContext& gfx, uint32_t frame, void* /*user_data*/) noexcept {
58 const uint8_t w = gfx.Width();
59 const uint8_t h = gfx.Height();
60 for (uint8_t y = 0; y < h; ++y) {
61 for (uint8_t x = 0; x < w; ++x) {
62 const float v = std::sin((x + frame) * 0.15f) + std::sin((y + frame) * 0.21f) +
63 std::sin((x + y + frame) * 0.11f);
64 const uint8_t c = static_cast<uint8_t>((v + 3.0f) / 6.0f * 255.0f);
65 gfx.SetPixel(x, y, MakeColor565(c, c / 2, 255 - c));
66 }
67 }
68 gfx.DrawString(2, h - 9, "PLASMA", colors::Cyan, colors::Black);
69}
70
72 const char* text;
73};
74
75inline void ScrollingText(GraphicsContext& gfx, uint32_t frame, void* user_data) noexcept {
76 auto* state = static_cast<ScrollTextState*>(user_data);
77 gfx.Clear(colors::Black);
78 gfx.DrawString(2, 2, "SmartDisplay", colors::White);
79 const int offset = static_cast<int>(frame % 120);
80 gfx.DrawString(static_cast<uint8_t>(gfx.Width() - offset), static_cast<uint8_t>(gfx.Height() / 2),
81 state != nullptr ? state->text : "ISF15ACP4", colors::Orange);
82}
83
85 bool pressed;
86};
87
88inline void PulseButton(GraphicsContext& gfx, uint32_t frame, void* user_data) noexcept {
89 auto* state = static_cast<PulseButtonState*>(user_data);
90 const bool pressed = state != nullptr && state->pressed;
91 gfx.Clear(pressed ? MakeColor565(0, 48, 0) : colors::Black);
92 gfx.DrawRect({0, 0, static_cast<uint8_t>(gfx.Width() - 1), static_cast<uint8_t>(gfx.Height() - 1)},
93 pressed ? colors::Green : colors::Gray);
94
95 const uint8_t pulse = static_cast<uint8_t>((frame % 20) + (pressed ? 20 : 0));
96 gfx.FillRect({40, 24, static_cast<uint8_t>(40 + pulse / 4), 40},
97 pressed ? colors::Green : colors::Blue);
98 gfx.DrawString(18, 48, pressed ? "ACTIVE" : "IDLE", colors::White);
99}
100
101} // namespace animations
102
103} // namespace isf15acp4
AnimationFrameFn frame_fn_
Definition isf15acp4_animator.hpp:39
uint32_t frame_
Definition isf15acp4_animator.hpp:41
void RenderNow() noexcept
Force immediate frame render.
Definition isf15acp4_animator.ipp:25
GraphicsContext & gfx_
Definition isf15acp4_animator.hpp:38
bool Tick(uint32_t elapsed_ms) noexcept
Advance animation clock and invoke frame callback when due.
Definition isf15acp4_animator.ipp:15
Animator(GraphicsContext &gfx, AnimationFrameFn fn, void *user_data=nullptr, uint32_t fps=30) noexcept
Definition isf15acp4_animator.ipp:9
void * user_data_
Definition isf15acp4_animator.hpp:40
Owns the active graphics backend selected at construction time.
Definition isf15acp4_graphics_context.hpp:42
void Plasma(GraphicsContext &gfx, uint32_t frame, void *user_data) noexcept
Definition isf15acp4_animator.ipp:57
void PulseButton(GraphicsContext &gfx, uint32_t frame, void *user_data) noexcept
Definition isf15acp4_animator.ipp:88
void ScrollingText(GraphicsContext &gfx, uint32_t frame, void *user_data) noexcept
Definition isf15acp4_animator.ipp:75
void ColorWheel(GraphicsContext &gfx, uint32_t frame, void *user_data) noexcept
Definition isf15acp4_animator.ipp:34
constexpr Color565 Black
Definition isf15acp4_types.hpp:116
constexpr Color565 White
Definition isf15acp4_types.hpp:117
constexpr Color565 Cyan
Definition isf15acp4_types.hpp:122
constexpr Color565 Gray
Definition isf15acp4_types.hpp:125
constexpr Color565 Green
Definition isf15acp4_types.hpp:119
constexpr Color565 Blue
Definition isf15acp4_types.hpp:120
constexpr Color565 Orange
Definition isf15acp4_types.hpp:124
Definition isf15acp4.hpp:19
void(*)(GraphicsContext &gfx, uint32_t frame, void *user_data) noexcept AnimationFrameFn
Callback that renders one animation frame into a graphics context.
Definition isf15acp4_animator.hpp:16
constexpr Color565 MakeColor565(uint8_t r, uint8_t g, uint8_t b) noexcept
Build an RGB565 color from 8-bit RGB channels.
Definition isf15acp4_types.hpp:110
Definition isf15acp4_animator.ipp:84
bool pressed
Definition isf15acp4_animator.ipp:85
Definition isf15acp4_animator.ipp:71
const char * text
Definition isf15acp4_animator.ipp:72