forked from BananaNeil/FlowtoysConnectBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonManager.h
161 lines (136 loc) · 4.18 KB
/
ButtonManager.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
#pragma once
#include "CommandProvider.h"
//2 buttons wired on pin 22 and 23 (hardware pull up), no need to debounce, just a digital read
#if VERSION == 1
#define NUM_BUTTONS 2
#elif VERSION == 2
#define NUM_BUTTONS 3
#endif
#define LONGPRESS_TIME 500 //more than 500ms is long press
#define VERYLONGPRESS_TIME 1500
#define SHORTPRESS_TIME 500 //less than 500ms is short press
#define MULTIPRESS_TIME 300 //each new press shorter than 500ms after the previous one will increase the multiclick
class ButtonManager
{
public:
ButtonManager()
{
setEventCallbacks(&ButtonManager::buttonPressDefaultEvent,
&ButtonManager::buttonDefaultEvent,
&ButtonManager::buttonDefaultEvent,
&ButtonManager::buttonDefaultEvent,
&ButtonManager::buttonMultiPressDefaultEvent
);
}
~ButtonManager() {}
#if VERSION == 1
const int buttonPins[NUM_BUTTONS] {23,22};
#elif VERSION == 2
const int buttonPins[NUM_BUTTONS] {36,39,34};
#endif
bool pressed[NUM_BUTTONS];
bool longPress[NUM_BUTTONS];
bool veryLongPress[NUM_BUTTONS];
long timeAtPress[NUM_BUTTONS];
int multipressCount[NUM_BUTTONS];
void init()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
pinMode(buttonPins[i], INPUT);
pressed[i] = false;
longPress[NUM_BUTTONS] = false;
veryLongPress[NUM_BUTTONS] = false;
timeAtPress[NUM_BUTTONS] = millis();
multipressCount[NUM_BUTTONS] = false;
}
}
void update()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
int val = !digitalRead(buttonPins[i]);
setButton(i, val);
}
}
void setButton(int id, bool value)
{
if (pressed[id] != value) //button state changed
{
pressed[id] = value;
longPress[id] = false;
veryLongPress[id] = false;
if (pressed[id])
{
timeAtPress[id] = millis();
handlePressed(id, true);
multipressCount[id] ++;
if (multipressCount[id] > 1) handleMultiPress(id, multipressCount[id]);
} else
{
if (millis() < timeAtPress[id] + SHORTPRESS_TIME) handleShortPress(id);
handlePressed(id, false);
}
} else
{
if (pressed[id])
{
if (!longPress[id] && millis() > timeAtPress[id] + LONGPRESS_TIME)
{
longPress[id] = true;
handleLongPress(id);
}
if (!veryLongPress[id] && millis() > timeAtPress[id] + VERYLONGPRESS_TIME)
{
veryLongPress[id] = true;
handleVeryLongPress(id);
}
}
if (millis() > timeAtPress[id] + MULTIPRESS_TIME) multipressCount[id] = 0;
}
}
// EVENT HANDLING / COMMAND SENDNG
void handlePressed(int id, bool value)
{
onButtonPress(id, value);
}
void handleShortPress(int id)
{
onButtonShortPress(id);
}
void handleLongPress(int id)
{
onButtonLongPress(id);
}
void handleVeryLongPress(int id)
{
onButtonVeryLongPress(id);
}
void handleMultiPress(int id, int count)
{
onButtonMultiPress(id, count);
}
typedef void(*buttonValueEvent)(int,bool);
void (*onButtonPress) (int, bool);
typedef void(*buttonEvent)(int);
void (*onButtonShortPress) (int);
void (*onButtonLongPress) (int);
void (*onButtonVeryLongPress) (int);
typedef void(*multiPressEvent)(int,int);
void (*onButtonMultiPress) (int, int);
void setEventCallbacks (buttonValueEvent pressFunc,
buttonEvent shortPressFunc,
buttonEvent longPressFunc,
buttonEvent veryLongPressFunc,
multiPressEvent multiPressFunc
) {
onButtonPress = pressFunc;
onButtonShortPress = shortPressFunc;
onButtonLongPress = longPressFunc;
onButtonVeryLongPress = veryLongPressFunc;
onButtonMultiPress = multiPressFunc;
}
static void buttonPressDefaultEvent(int id, bool value){}
static void buttonDefaultEvent(int id){}
static void buttonMultiPressDefaultEvent(int id, int count){}
};