-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAction.h
executable file
·86 lines (65 loc) · 2.41 KB
/
Action.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
/*
This item execute an action when selected.
*/
#include <Arduino.h>
#include "MenuItem.h"
#ifndef Action_h
#define Action_h
template <class T> class ParamAction : public MenuItem {
public:
typedef void(*ActionCallback)(T);
ParamAction(MenuItem* parent, const char* text, ActionCallback callback, T data): MenuItem(parent, text) {
this->callback = callback;
this->data = data;
}
ParamAction(MenuItem* parent, const FlashString* text, ActionCallback callback, T data): MenuItem(parent, text) {
this->callback = callback;
this->data = data;
}
char getTypeId() { return 'a'; }
// MenuItem fields
// When activated from parent menu, trigger the callback and don't take control.
bool activate() {
if (this->callback)
this->callback(this->data);
return 0;
}
void deactivate() {};
// These three methods do nothing. Since Action doesn't take control, they are never called.
void doNext() { }
void doPrev() { }
MenuItem* action() { return NULL; }
protected:
// callback pointer
ActionCallback callback;
T data;
};
class Action : public MenuItem {
public:
typedef void(*ActionCallback)(void);
Action(MenuItem* parent, const char* text, ActionCallback callback): MenuItem(parent, text) {
setCallback(callback);
}
Action(MenuItem* parent, const FlashString* text, ActionCallback callback): MenuItem(parent, text) {
setCallback(callback);
}
char getTypeId() { return 'a'; }
// Set the callback to execute.
void setCallback(ActionCallback callback) { this->callback = callback; }
// MenuItem fields
// When activated from parent menu, trigger the callback and don't take control.
bool activate() {
if (this->callback)
this->callback();
return 0;
}
void deactivate() {};
// These three methods do nothing. Since Action doesn't take control, they are never called.
void doNext() { }
void doPrev() { }
MenuItem* action() { return NULL; }
protected:
// callback pointer
ActionCallback callback;
};
#endif