-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultidesktop.h
125 lines (109 loc) · 2.26 KB
/
multidesktop.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
#ifndef MULTI_DESKTOP_H
#define MULTI_DESKTOP_H
extern "C" {
typedef void (*func)(void);
}
/**
@brief Desktop class
@param show_ - callback function when this Desktop is active
@param cb - callback functions for all N buttons. i function called when user press i button.
*/
template <int8_t N> //keys
class Desktop
{
public:
Desktop<N>(func show_, func cb[])
: show(show_)
, buttons_callbacks(cb)
, next(0)
{ }
/**
@brief callback function when this Desktop is active
*/
func show;
/**
@brief link to next Desktop in MultiDesktop configuration
*/
Desktop *next;
/**
@brief callback functions for all N buttons. i function called when user press i button.
*/
func *buttons_callbacks;
private:
};
/**
@brief MultiDesktop class
You can change Desktops for show different info and call different
functions in each of them by pressing same buttons.
@param N - number of keys you want to use except 'next desktop' key.
@param next_code - key code for go to next Desktop
@param codes - N key codes you want to press
*/
template <int8_t N>
class MultiDesktop
{
public:
MultiDesktop<N>(int8_t next_code, int8_t codes[])
: current(0)
, first(0)
, key_codes(codes)
, next_desktop_code(next_code)
{}
/**
@brief add_desktop add new Desktop. Desktops are 1-direction looped list.
@param new_d - link to new Desktop
*/
void add_desktop(Desktop<N> *new_d)
{
if (!first)
{
first = new_d;
current = new_d;
}
else
{
Desktop<N> *d = first;
while (d->next && d->next != first)
d = d->next;
d->next = new_d;
new_d->next = first;
}
}
/**
@brief call this function when user press button. call show() to redraw screen.
@param code - button code
*/
void button_pressed(int8_t code)
{
if (next_desktop_code == code)
{
if (current->next)
current = current->next;
}
else
{
for (int8_t i=0; i < N; i++)
{
if (key_codes[i] == code)
{
if (current->buttons_callbacks[i])
current->buttons_callbacks[i]();
}
}
}
}
/**
@brief call show function of current desktop
*/
void show()
{
if (current->show)
current->show();
}
private:
Desktop<N> *current;
Desktop<N> *first;
int8_t next_desktop_code;
int8_t *key_codes;
};
#endif