-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebounceSwitch.h
50 lines (37 loc) · 1.38 KB
/
DebounceSwitch.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
/*
DebounceSwitch - a class for de-bouncing a mechanical switch
The parameters are the GPIO pin #, a flag that's true if using an internal pullup
resistor, a pair of callback functions that get called when the pin goes either high
or low, and a debounce delay in ms.
The initPin() command configures the input pin, and returns the inital state of
the switch. It must be called first.
The readPin() command returns the current state (de-bounced), and calls
the corresponding callback function whenever the state changes.
Visit https://github.com/bdubs-astro/DebounceSwitch for more details.
*/
#ifndef DebounceSwitch_h
#define DebounceSwitch_h
#include "Arduino.h"
class DebounceSwitch {
public:
DebounceSwitch(const int _pin, bool _intPullup, void (*_loCallback)(void), void (*_hiCallback)(void)):
intPullup(_intPullup), pin(_pin), loCallback(_loCallback), hiCallback(_hiCallback) {
// configure input pin
if (intPullup)
pinMode(pin, INPUT_PULLUP); // use internal pullup resistor
else
pinMode(pin, INPUT);
}
bool initPin();
bool readPin(int debounceDelay);
private:
bool intPullup;
int pin;
void (*loCallback)(void);
void (*hiCallback)(void);
bool reading;
bool currState;
bool prevState;
unsigned long timePrev;
};
#endif