forked from kost/dcled
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimations.h
162 lines (147 loc) · 5.18 KB
/
animations.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// dcled-hidapi - userland driver for the Dream Cheeky LED Message Board
// Copyright 2018 Jahn Fuchs <[email protected]>
// Distributed under the MIT License. See accompanying LICENSE file.
#pragma once
#include "fonts.h"
#include "screen.h"
namespace dcled {
enum class ScrollSpeed : uint16_t {
Fast = 50 , Medium = 100, Slow = 150
};
class Animation
{
public:
virtual ~Animation() = default;
/// Updates the screen with the next animation step if the end is not reached and returns
/// the time in ms that the updated screen should be shown. Returns 0 if the end of the
/// animation was reached.
virtual uint32_t step(Screen&) { return 0; }
/// Resets the animation to the beginning.
virtual void reset() = 0;
};
class NestedAnimation : public Animation
{
public:
NestedAnimation(std::unique_ptr<Animation> a) : animation_(std::move(a)) {}
virtual ~NestedAnimation() override = default;
virtual uint32_t step(Screen& s) override;
virtual void reset() override;
private:
Screen s_;
std::unique_ptr<Animation> animation_;
bool first_step_ = true;
};
class InvertAnimation : public NestedAnimation
{
public:
InvertAnimation(std::unique_ptr<Animation> a) : NestedAnimation(std::move(a)) {}
virtual ~InvertAnimation() override = default;
virtual uint32_t step(Screen& s) override;
};
class FlipAnimation : public NestedAnimation
{
public:
FlipAnimation(std::unique_ptr<Animation> a, Screen::Flip direction = Screen::Flip::Horizontal)
: NestedAnimation(std::move(a)), flip_direction_(direction) {}
virtual ~FlipAnimation() override = default;
virtual uint32_t step(Screen& s) override;
private:
const Screen::Flip flip_direction_ = Screen::Flip::Horizontal;
};
/// Shows a digital clock in either 24 or 12 hour format, for a given time or forever.
class ClockAnimation : public Animation
{
public:
enum class Mode { H24 = 24, H12 = 12 };
ClockAnimation( uint32_t display_time_s = 0, bool blinking_colon = true, Mode mode = Mode::H24 );
virtual ~ClockAnimation() override = default;
virtual uint32_t step(Screen&) override;
virtual void reset() override;
private:
uint32_t display_time_s_ = 0; // 0: indefinitely
uint64_t time_left_ms_ = 0;
bool blinking_colon_ = true;
bool colon_show_state_ = true;
Mode mode_ = Mode::H24;
};
/// Shows a screen for the given time.
class ShowScreenAnimation : public Animation
{
public:
ShowScreenAnimation(const Screen& s, uint32_t time_ms = 1000);
virtual ~ShowScreenAnimation() override = default;
virtual uint32_t step(Screen&) override;
virtual void reset() override { done_ = false; }
private:
Screen s_;
uint32_t time_ms_ = 1000;
bool done_ = false;
};
class InvertScreenAnimation : public Animation
{
public:
InvertScreenAnimation(uint32_t time_ms = 0) : time_ms_(time_ms) {}
virtual ~InvertScreenAnimation() override = default;
virtual uint32_t step(Screen&) override;
virtual void reset() override { done_ = false; }
private:
uint32_t time_ms_ = 0;
bool done_ = false;
};
class FlipScreenAnimation : public Animation
{
public:
FlipScreenAnimation(Screen::Flip direction = Screen::Flip::Horizontal, uint32_t time_ms = 0)
: direction_(direction), time_ms_(time_ms) {}
virtual ~FlipScreenAnimation() override = default;
virtual uint32_t step(Screen&) override;
virtual void reset() override { done_ = false; }
private:
Screen::Flip direction_ = Screen::Flip::Horizontal;
uint32_t time_ms_ = 0;
bool done_ = false;
};
class NilAnimation : public Animation
{
public:
NilAnimation(uint32_t time_ms = 1000) : time_ms_(time_ms) {}
virtual ~NilAnimation() override = default;
virtual uint32_t step(Screen&) override;
virtual void reset() override { done_ = false; }
private:
uint32_t time_ms_ = 1000;
bool done_ = false;
};
class SetRectAnimation : public Animation
{
public:
SetRectAnimation(bool on, uint32_t x, uint32_t y, uint32_t w = 1, uint32_t h = 1, uint32_t time_ms = 0)
: on_(on), time_ms_(time_ms), x_(x), y_(y), w_(w), h_(h) {}
virtual ~SetRectAnimation() override = default;
virtual uint32_t step(Screen&) override;
virtual void reset() override { done_ = false; }
private:
bool on_ = true;
uint32_t time_ms_ = 0;
uint32_t x_ = 0, y_ = 0, w_ = 1, h_ = 1;
bool done_ = false;
};
/// Scrolling test animation with a given text, scrollspeed and font.
class TextAnimation : public Animation
{
public:
TextAnimation(const std::string& text, const font::Font& font);
TextAnimation(const std::string& text, uint32_t speed, const font::Font& font = font::Default);
TextAnimation(const std::string& text, ScrollSpeed speed = ScrollSpeed::Medium,
const font::Font& font = font::Default);
virtual ~TextAnimation() override = default;
virtual uint32_t step(Screen&) override;
virtual void reset() override;
private:
const font::Font& font_;
const std::string text_;
uint32_t scrollspeed_ = static_cast<uint32_t>(ScrollSpeed::Medium);
uint32_t cur_char_ = 0;
uint8_t cur_char_pix_ = 0;
};
}