Skip to content

Commit a52bfa0

Browse files
authored
Merge pull request #49 from nielsvandepas/master
Add parameterized callbacks
2 parents f78433c + 0e7d697 commit a52bfa0

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/OneButton.cpp

+50-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,29 @@ void OneButton::attachClick(callbackFunction newFunction)
8282
} // attachClick
8383

8484

85+
// save function for parameterized click event
86+
void OneButton::attachClick(parameterizedCallbackFunction newFunction, void* parameter)
87+
{
88+
_paramClickFunc = newFunction;
89+
_clickFuncParam = parameter;
90+
} // attachClick
91+
92+
8593
// save function for doubleClick event
8694
void OneButton::attachDoubleClick(callbackFunction newFunction)
8795
{
8896
_doubleClickFunc = newFunction;
8997
} // attachDoubleClick
9098

9199

100+
// save function for parameterized doubleClick event
101+
void OneButton::attachDoubleClick(parameterizedCallbackFunction newFunction, void* parameter)
102+
{
103+
_paramDoubleClickFunc = newFunction;
104+
_doubleClickFuncParam = parameter;
105+
} // attachDoubleClick
106+
107+
92108
// save function for press event
93109
// DEPRECATED, is replaced by attachLongPressStart, attachLongPressStop,
94110
// attachDuringLongPress,
@@ -103,18 +119,39 @@ void OneButton::attachLongPressStart(callbackFunction newFunction)
103119
_longPressStartFunc = newFunction;
104120
} // attachLongPressStart
105121

122+
// save function for parameterized longPressStart event
123+
void OneButton::attachLongPressStart(parameterizedCallbackFunction newFunction, void* parameter)
124+
{
125+
_paramLongPressStartFunc = newFunction;
126+
_longPressStartFuncParam = parameter;
127+
} // attachLongPressStart
128+
106129
// save function for longPressStop event
107130
void OneButton::attachLongPressStop(callbackFunction newFunction)
108131
{
109132
_longPressStopFunc = newFunction;
110133
} // attachLongPressStop
111134

135+
// save function for parameterized longPressStop event
136+
void OneButton::attachLongPressStop(parameterizedCallbackFunction newFunction, void* parameter)
137+
{
138+
_paramLongPressStopFunc = newFunction;
139+
_longPressStopFuncParam = parameter;
140+
} // attachLongPressStop
141+
112142
// save function for during longPress event
113143
void OneButton::attachDuringLongPress(callbackFunction newFunction)
114144
{
115145
_duringLongPressFunc = newFunction;
116146
} // attachDuringLongPress
117147

148+
// save function for parameterized during longPress event
149+
void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction, void* parameter)
150+
{
151+
_paramDuringLongPressFunc = newFunction;
152+
_duringLongPressFuncParam = parameter;
153+
} // attachDuringLongPress
154+
118155
// function to get the current long pressed state
119156
bool OneButton::isLongPressed(){
120157
return _isLongPressed;
@@ -178,8 +215,12 @@ void OneButton::tick(bool activeLevel)
178215
_pressFunc();
179216
if (_longPressStartFunc)
180217
_longPressStartFunc();
218+
if (_paramLongPressStartFunc)
219+
_paramLongPressStartFunc(_longPressStartFuncParam);
181220
if (_duringLongPressFunc)
182221
_duringLongPressFunc();
222+
if (_paramDuringLongPressFunc)
223+
_paramDuringLongPressFunc(_duringLongPressFuncParam);
183224
_state = 6; // step to state 6
184225
_stopTime = now; // remember stopping time
185226
} else {
@@ -188,11 +229,13 @@ void OneButton::tick(bool activeLevel)
188229

189230
} else if (_state == 2) {
190231
// waiting for menu pin being pressed the second time or timeout.
191-
if (_doubleClickFunc == NULL ||
232+
if ((_doubleClickFunc == NULL && _paramDoubleClickFunc == NULL) ||
192233
(unsigned long)(now - _startTime) > _clickTicks) {
193234
// this was only a single short click
194235
if (_clickFunc)
195236
_clickFunc();
237+
if (_paramClickFunc)
238+
_paramClickFunc(_clickFuncParam);
196239
_state = 0; // restart.
197240

198241
} else if ((activeLevel) &&
@@ -209,6 +252,8 @@ void OneButton::tick(bool activeLevel)
209252
// this was a 2 click sequence.
210253
if (_doubleClickFunc)
211254
_doubleClickFunc();
255+
if (_paramDoubleClickFunc)
256+
_paramDoubleClickFunc(_doubleClickFuncParam);
212257
_state = 0; // restart.
213258
_stopTime = now; // remember stopping time
214259
} // if
@@ -219,13 +264,17 @@ void OneButton::tick(bool activeLevel)
219264
_isLongPressed = false; // Keep track of long press state
220265
if (_longPressStopFunc)
221266
_longPressStopFunc();
267+
if (_paramLongPressStopFunc)
268+
_paramLongPressStopFunc(_longPressStopFuncParam);
222269
_state = 0; // restart.
223270
_stopTime = now; // remember stopping time
224271
} else {
225272
// button is being long pressed
226273
_isLongPressed = true; // Keep track of long press state
227274
if (_duringLongPressFunc)
228275
_duringLongPressFunc();
276+
if (_paramDuringLongPressFunc)
277+
_paramDuringLongPressFunc(_duringLongPressFuncParam);
229278
} // if
230279

231280
} // if

src/OneButton.h

+21
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
extern "C" {
3131
typedef void (*callbackFunction)(void);
32+
typedef void (*parameterizedCallbackFunction)(void*);
3233
}
3334

3435

@@ -54,13 +55,18 @@ class OneButton
5455
// attach functions that will be called when button was pressed in the
5556
// specified way.
5657
void attachClick(callbackFunction newFunction);
58+
void attachClick(parameterizedCallbackFunction newFunction, void* parameter);
5759
void attachDoubleClick(callbackFunction newFunction);
60+
void attachDoubleClick(parameterizedCallbackFunction newFunction, void* parameter);
5861
void attachPress(
5962
callbackFunction newFunction); // DEPRECATED, replaced by longPressStart,
6063
// longPressStop and duringLongPress
6164
void attachLongPressStart(callbackFunction newFunction);
65+
void attachLongPressStart(parameterizedCallbackFunction newFunction, void* parameter);
6266
void attachLongPressStop(callbackFunction newFunction);
67+
void attachLongPressStop(parameterizedCallbackFunction newFunction, void* parameter);
6368
void attachDuringLongPress(callbackFunction newFunction);
69+
void attachDuringLongPress(parameterizedCallbackFunction newFunction, void* parameter);
6470

6571
// ----- State machine functions -----
6672

@@ -95,11 +101,26 @@ class OneButton
95101

96102
// These variables will hold functions acting as event source.
97103
callbackFunction _clickFunc = NULL;
104+
parameterizedCallbackFunction _paramClickFunc = NULL;
105+
void* _clickFuncParam = NULL;
106+
98107
callbackFunction _doubleClickFunc = NULL;
108+
parameterizedCallbackFunction _paramDoubleClickFunc = NULL;
109+
void* _doubleClickFuncParam = NULL;
110+
99111
callbackFunction _pressFunc = NULL;
112+
100113
callbackFunction _longPressStartFunc = NULL;
114+
parameterizedCallbackFunction _paramLongPressStartFunc = NULL;
115+
void* _longPressStartFuncParam = NULL;
116+
101117
callbackFunction _longPressStopFunc = NULL;
118+
parameterizedCallbackFunction _paramLongPressStopFunc = NULL;
119+
void* _longPressStopFuncParam;
120+
102121
callbackFunction _duringLongPressFunc = NULL;
122+
parameterizedCallbackFunction _paramDuringLongPressFunc = NULL;
123+
void* _duringLongPressFuncParam = NULL;
103124

104125
// These variables that hold information across the upcoming tick calls.
105126
// They are initialized once on program start and are updated every time the

0 commit comments

Comments
 (0)