Repository navigation
Expand file tree
/
Copy pathOneButton.h
More file actions
133 lines (106 loc) · 4.67 KB
/
Copy pathOneButton.h
File metadata and controls
133 lines (106 loc) · 4.67 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
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
// -----
// OneButton.h - Library for detecting button clicks, doubleclicks and long
// press pattern on a single button. This class is implemented for use with the
// Arduino environment. Copyright (c) by Matthias Hertel,
// http://www.mathertel.de This work is licensed under a BSD style license. See
// http://www.mathertel.de/License.aspx More information on:
// http://www.mathertel.de/Arduino
// -----
// 02.10.2010 created by Matthias Hertel
// 21.04.2011 transformed into a library
// 01.12.2011 include file changed to work with the Arduino 1.0 environment
// 23.03.2014 Enhanced long press functionalities by adding longPressStart and
// longPressStop callbacks
// 21.09.2015 A simple way for debounce detection added.
// 14.05.2017 Debouncing improvements.
// 25.06.2018 Optional third parameter for deactivating pullup.
// 26.09.2018 Anatoli Arkhipenko: Included solution to use library with other
// sources of input.
// 26.09.2018 Initialization moved into class declaration.
// 26.09.2018 Jay M Ericsson: compiler warnings removed.
// -----
#ifndef OneButton_h
#define OneButton_h
#include "Arduino.h"
// ----- Callback function types -----
extern "C" {
typedef void (*callbackFunction)(void);
typedef void (*parameterizedCallbackFunction)(void*);
}
class OneButton
{
public:
// ----- Constructor -----
OneButton();
OneButton(int pin, int active, bool pullupActive = true);
// ----- Set runtime parameters -----
// set # millisec after safe click is assumed.
void setDebounceTicks(int ticks);
// set # millisec after single click is assumed.
void setClickTicks(int ticks);
// set # millisec after press is assumed.
void setPressTicks(int ticks);
// attach functions that will be called when button was pressed in the
// specified way.
void attachClick(callbackFunction newFunction);
void attachClick(parameterizedCallbackFunction newFunction, void* parameter);
void attachDoubleClick(callbackFunction newFunction);
void attachDoubleClick(parameterizedCallbackFunction newFunction, void* parameter);
void attachPress(
callbackFunction newFunction); // DEPRECATED, replaced by longPressStart,
// longPressStop and duringLongPress
void attachLongPressStart(callbackFunction newFunction);
void attachLongPressStart(parameterizedCallbackFunction newFunction, void* parameter);
void attachLongPressStop(callbackFunction newFunction);
void attachLongPressStop(parameterizedCallbackFunction newFunction, void* parameter);
void attachDuringLongPress(callbackFunction newFunction);
void attachDuringLongPress(parameterizedCallbackFunction newFunction, void* parameter);
// ----- State machine functions -----
/**
* @brief Call this function every some milliseconds for checking the input
* level at the initialized digital pin.
*/
void tick(void);
/**
* @brief Call this function every time the input level has changed.
* Using this function no digital input pin is checked because the current
* level is given by the parameter.
*/
void tick(bool level);
bool isLongPressed();
int getPressedTicks();
void reset(void);
private:
int _pin; // hardware pin number.
unsigned int _debounceTicks = 50; // number of ticks for debounce times.
unsigned int _clickTicks = 600; // number of ticks that have to pass by
// before a click is detected.
unsigned int _pressTicks = 1000; // number of ticks that have to pass by
// before a long button press is detected
int _buttonPressed;
bool _isLongPressed = false;
// These variables will hold functions acting as event source.
callbackFunction _clickFunc = NULL;
parameterizedCallbackFunction _paramClickFunc = NULL;
void* _clickFuncParam = NULL;
callbackFunction _doubleClickFunc = NULL;
parameterizedCallbackFunction _paramDoubleClickFunc = NULL;
void* _doubleClickFuncParam = NULL;
callbackFunction _pressFunc = NULL;
callbackFunction _longPressStartFunc = NULL;
parameterizedCallbackFunction _paramLongPressStartFunc = NULL;
void* _longPressStartFuncParam = NULL;
callbackFunction _longPressStopFunc = NULL;
parameterizedCallbackFunction _paramLongPressStopFunc = NULL;
void* _longPressStopFuncParam;
callbackFunction _duringLongPressFunc = NULL;
parameterizedCallbackFunction _paramDuringLongPressFunc = NULL;
void* _duringLongPressFuncParam = NULL;
// These variables that hold information across the upcoming tick calls.
// They are initialized once on program start and are updated every time the
// tick function is called.
int _state = 0;
unsigned long _startTime; // will be set in state 1
unsigned long _stopTime; // will be set in state 2
};
#endif