Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
AH
Hardware
Button.cpp
Go to the documentation of this file.
1
#include "
Button.hpp
"
2
3
BEGIN_AH_NAMESPACE
4
5
Button::Button
(
pin_t
pin
) :
pin
(
pin
) {}
6
7
void
Button::begin
() {
ExtIO::pinMode
(
pin
,
INPUT_PULLUP
); }
8
9
void
Button::invert
() {
state
.invert =
true
; }
10
11
Button::State
Button::update
() {
12
// Read pin state and current time
13
bool
input =
ExtIO::digitalRead
(
pin
) ^
state
.invert;
14
unsigned
long
now = millis();
15
// Check if enough time has elapsed after last bounce
16
if
(
state
.bouncing)
17
state
.bouncing = now -
state
.prevBounceTime <=
debounceTime
;
18
// Shift the debounced state one bit to the left, either appending the
19
// new input state if not bouncing, or repeat the old state if bouncing
20
bool
prevState =
state
.debounced & 0b01;
21
bool
newState =
state
.bouncing ? prevState : input;
22
state
.debounced = (prevState << 1) | newState;
23
// Check if the input changed state (button pressed, released or bouncing)
24
if
(input !=
state
.prevInput) {
25
state
.bouncing =
true
;
26
state
.prevInput = input;
27
state
.prevBounceTime = now;
28
}
29
return
getState
();
30
}
31
32
Button::State
Button::getState
()
const
{
33
return
static_cast<
State
>
(
state
.debounced);
34
}
35
36
FlashString_t
Button::getName
(
Button::State
state
) {
37
switch
(
state
) {
38
case
Button::Pressed
:
return
F(
"Pressed"
);
39
case
Button::Released
:
return
F(
"Released"
);
40
case
Button::Falling
:
return
F(
"Falling"
);
41
case
Button::Rising
:
return
F(
"Rising"
);
42
default
:
return
F(
"<invalid>"
);
// Keeps the compiler happy
43
}
44
}
45
46
unsigned
long
Button::previousBounceTime
()
const
{
47
return
state
.prevBounceTime;
48
}
49
50
unsigned
long
Button::stableTime
(
unsigned
long
now)
const
{
51
return
now -
previousBounceTime
();
52
}
53
54
unsigned
long
Button::stableTime
()
const
{
return
stableTime
(millis()); }
55
56
void
Button::setDebounceTime
(
unsigned
long
debounceTime
) {
57
Button::debounceTime
=
debounceTime
;
58
}
59
60
unsigned
long
Button::getDebounceTime
() {
return
Button::debounceTime
; }
61
62
unsigned
long
Button::debounceTime
=
BUTTON_DEBOUNCE_TIME
;
63
64
END_AH_NAMESPACE
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition
AH/Settings/NamespaceSettings.hpp:14
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition
AH/Settings/NamespaceSettings.hpp:11
INPUT_PULLUP
constexpr PinMode_t INPUT_PULLUP
Definition
Arduino-Hardware-Types.hpp:66
FlashString_t
std::remove_reference< decltype(*F(""))>::type * FlashString_t
Definition
Arduino-Wrapper.h:18
Button.hpp
AH::Button::previousBounceTime
unsigned long previousBounceTime() const
Return the time point (in milliseconds) when the button last bounced.
Definition
Button.cpp:46
AH::Button::setDebounceTime
static void setDebounceTime(unsigned long debounceTime=BUTTON_DEBOUNCE_TIME)
Set the debounce time for all Buttons.
Definition
Button.cpp:56
AH::Button::update
State update()
Read the button and return its new state.
Definition
Button.cpp:11
AH::Button::State
State
An enumeration of the different states a button can be in.
Definition
Button.hpp:47
AH::Button::Pressed
@ Pressed
Input went from low to low (0,0).
Definition
Button.hpp:48
AH::Button::Rising
@ Rising
Input went from low to high (0,1).
Definition
Button.hpp:51
AH::Button::Falling
@ Falling
Input went from high to low (1,0).
Definition
Button.hpp:50
AH::Button::Released
@ Released
Input went from high to high (1,1).
Definition
Button.hpp:49
AH::Button::state
struct AH::Button::InternalState state
AH::Button::invert
void invert()
Invert the input state of this button (button pressed is HIGH instead of LOW).
Definition
Button.cpp:9
AH::Button::getName
static FlashString_t getName(State state)
Return the name of the state as a string.
Definition
Button.cpp:36
AH::Button::getState
State getState() const
Get the state of the button, without updating it.
Definition
Button.cpp:32
AH::Button::begin
void begin()
Initialize (enable the internal pull-up resistor).
Definition
Button.cpp:7
AH::Button::pin
pin_t pin
Definition
Button.hpp:124
AH::Button::getDebounceTime
static unsigned long getDebounceTime()
Get the debounce time.
Definition
Button.cpp:60
AH::Button::stableTime
unsigned long stableTime() const
Return the time (in milliseconds) that the button has been stable for.
Definition
Button.cpp:54
AH::Button::Button
Button()
Construct a new Button object.
Definition
Button.hpp:24
AH::Button::debounceTime
static unsigned long debounceTime
Edit this in Settings.hpp.
Definition
Button.hpp:139
AH::ExtIO::pinMode
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
Definition
ExtendedInputOutput.cpp:32
AH::ExtIO::digitalRead
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition
ExtendedInputOutput.cpp:54
AH::BUTTON_DEBOUNCE_TIME
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition
AH/Settings/Settings.hpp:76
AH::ExtIO::pin_t
Type for storing pin numbers of Extended Input/Output elements.
Definition
Hardware-Types.hpp:25
Generated by
1.17.0