-
Notifications
You must be signed in to change notification settings - Fork 10
/
gurdybutton.cpp
47 lines (36 loc) · 1.47 KB
/
gurdybutton.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
47
#include "gurdybutton.h"
/// @brief Constructor. This class handles simple push-on, release-off buttons.
/// @param my_pin The digital pin this button is connected to
/// @param interval The debounce interval for this button
/// @details This class assumes the button is wired up as an active-low button. It applies the internal pullup resistor.
GurdyButton::GurdyButton(int my_pin, int interval) {
pinMode(my_pin, INPUT_PULLUP);
bounce_obj = new Bounce(my_pin, interval);
being_pressed = false;
};
/// @brief Polls the button and updates its state.
/// @details This should be run every loop() even if the results are not used.
void GurdyButton::update() {
bounce_obj->update();
// If button was pressed or released this cycle, record that.
if (bounce_obj->fallingEdge()) {
being_pressed = true;
} else if (bounce_obj->risingEdge()) {
being_pressed = false;
};
};
/// @brief Reports if the button is being pressed this update() cycle.
/// @return True if button is actve, false otherwise.
bool GurdyButton::beingPressed() {
return being_pressed;
};
/// @brief Reports if the button was pressed down this update() cycle.
/// @return True if button was just pressed down, false otherwise.
bool GurdyButton::wasPressed() {
return bounce_obj->fallingEdge();
};
/// @brief Reports if the button was released this update() cycle.
/// @return True if button was just released, false otherwise.
bool GurdyButton::wasReleased() {
return bounce_obj->risingEdge();
};