-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushButton.cpp
46 lines (41 loc) · 947 Bytes
/
PushButton.cpp
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
#include "PushButton.h"
PushButton::PushButton(uint32_t pin, bool isPullUp,
bool internalPullUpActivated)
{
this->pin = pin;
this->isPullUp = isPullUp;
this->internalPullUpActivated = internalPullUpActivated;
lastTimeStateChanged = millis();
debounceDelay = 50;
}
void PushButton::init()
{
gpio_init(pin);
gpio_set_dir(pin, GPIO_IN);
if (isPullUp && internalPullUpActivated)
{
gpio_pull_up(pin);
}
state= gpio_get(pin);
}
void PushButton::readState()
{
unsigned long timeNow = millis();
if (timeNow - lastTimeStateChanged > debounceDelay) {
uint32_t newState = gpio_get(pin);
if (newState != state) {
state = newState;
lastTimeStateChanged = timeNow;
}
}
}
bool PushButton::isPressed()
{
readState();
if (isPullUp) {
return (state == 0);
}
else {
return (state == 1);
}
}