-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttonDynamic.hpp
More file actions
68 lines (59 loc) · 1.68 KB
/
buttonDynamic.hpp
File metadata and controls
68 lines (59 loc) · 1.68 KB
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
#ifndef BUTTON_DYNAMIC_HPP
#define BUTTON_DYNAMIC_HPP
// ----------------------------------------------------------------------------------------------------
/** ButtonDynamic uses it's own address as an "index".
*
* This saves at least 1 byte per instance in RAM, requires however additional flash and runtime. As
* for microcontrollers RAM is the most constrained entity, this design was chosen.
*
* It's usage is designed similar to the following:
*
* ButtonDynamic<...> buttons[3] = {
* {},
* {},
* {},
* };
*
* void buttonInitialize(void const * const instance)
* {
* switch (static_cast<ButtonDynamic<...> const *>(instance) - buttons)
* {
* case 0: return Button0::initialize();
* case 1: return Button1::initialize();
* case 2: return Button2::initialize();
* }
* // assert(numberOfButtons > index);
* }
*
**/
template<void (*initButtonFunction)(void const * const instance),
bool (*getButtonIsDown)(void const * const instance),
void (*deinitButtonFunction)(void const * const instance)>
class ButtonDynamic
{
public:
constexpr ButtonDynamic()
{
initButtonFunction(this);
}
~ButtonDynamic()
{
deinitButtonFunction(this);
}
/**
* @brief isDown - button is currently being pressed down.
*/
bool isDown() const
{
return getButtonIsDown(this);
}
/**
* @brief isUp - button is currently not being pressed down.
*/
bool isUp() const
{
return !isDown();
}
};
// ----------------------------------------------------------------------------------------------------
#endif // BUTTON_DYNAMIC_HPP