-
Notifications
You must be signed in to change notification settings - Fork 20
/
OneButton.h
107 lines (82 loc) · 3.64 KB
/
OneButton.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
// -----
// 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
// -----
#ifndef OneButton_h
#define OneButton_h
#include "Arduino.h"
// ----- Callback function types -----
extern "C" {
typedef void (*callbackFunction)(void);
}
class OneButton
{
public:
// ----- Constructor -----
OneButton(int pin, int active);
OneButton();
OneButton(bool (*callback)(void), bool);
// ----- Set runtime parameters -----
// set up defaults
void init();
// set button pin
void setPin(int pin);
// set if button is on pseudo pin
void setPseudo(bool (*callback)(void));
// set if button is active when low
void setActiveLow(int activeLow);
// 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 attachDoubleClick(callbackFunction newFunction);
void attachPress(callbackFunction newFunction); // DEPRECATED, replaced by longPressStart, longPressStop and duringLongPress
void attachLongPressStart(callbackFunction newFunction);
void attachLongPressStop(callbackFunction newFunction);
void attachDuringLongPress(callbackFunction newFunction);
// ----- State machine functions -----
// call this function every some milliseconds for handling button events.
void tick(void);
bool isLongPressed();
// ----- public debounce functions ------
void setDebounceDelay(int delay);
private:
int _pin; // hardware pin number.
int _clickTicks; // number of ticks that have to pass by before a click is detected
int _pressTicks; // number of ticks that have to pass by before a lonn button press is detected
int _buttonReleased;
int _buttonPressed;
bool _isLongPressed;
// These variables will hold functions acting as event source.
callbackFunction _clickFunc;
callbackFunction _doubleClickFunc;
callbackFunction _pressFunc;
callbackFunction _longPressStartFunc;
callbackFunction _longPressStopFunc;
callbackFunction _duringLongPressFunc;
// 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;
unsigned long _startTime; // will be set in state 1
// if we're reading a button from a pseudo (not a direct digial pin)
// then _pseudo is a function that returns a true/false button state
bool (*_pseudo)(void);
// Debounce variables and functions
// Help prevent bouncing button states from being read as double clicks
int _db_buttonState; // the current reading from the input pin
int _db_lastButtonState; // the previous reading from the input pin
unsigned long _db_lastDebounceTime; // the last time the output pin was toggled
unsigned long _db_debounceDelay; // the debounce time; increase if the output flickers
boolean debounce(boolean reading); // method to debounce the digital reading, call every loop
};
#endif