This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
Button.cpp
Go to the documentation of this file.
1 #include "Button.hpp"
2 
4 
5 using namespace ExtIO;
6 
7 Button::Button(pin_t pin) : pin(pin) {}
8 
10 
11 void Button::invert() { invertState = true; }
12 
13 #ifndef INDIVIDUAL_BUTTON_INVERT
14 bool Button::invertState = false;
15 #endif
16 
18  // read the button state and invert it if "invertState" is true
19  bool input = ExtIO::digitalRead(pin) ^ invertState;
20  bool prevState = debouncedState & 0b01;
21  unsigned long now = millis();
22  if (now - prevBounceTime > debounceTime) { // wait for state to stabilize
23  debouncedState = static_cast<State>((prevState << 1) | input);
24  } else {
25  debouncedState = static_cast<State>((prevState << 1) | prevState);
26  }
27  if (input != prevInput) { // Button is pressed, released or bounces
28  prevBounceTime = now;
29  prevInput = input;
30  }
31  return debouncedState;
32 }
33 
35 
36 const __FlashStringHelper *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::stableTime() { return millis() - prevBounceTime; }
47 
INPUT_PULLUP
const uint8_t INPUT_PULLUP
Definition: ExtendedInputOutput.hpp:39
Button::Rising
< Input went from high to low (1,0)
Definition: Button.hpp:54
Button::invert
INDIVIDUAL_BUTTON_INVERT_STATIC void invert()
Invert the state of all buttons, or of this specific button (button pressed is HIGH instead of LOW).
Definition: Button.cpp:11
Button.hpp
Button::Falling
< Input went from high to high (1,1)
Definition: Button.hpp:53
Button::debouncedState
State debouncedState
Definition: Button.hpp:105
ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:34
pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Def.hpp:17
Button::pin
pin_t pin
Definition: Button.hpp:102
Button::prevBounceTime
unsigned long prevBounceTime
Definition: Button.hpp:106
Button::stableTime
unsigned long stableTime()
Return the time (in milliseconds) that the button has been stable for.
Definition: Button.cpp:46
Button::update
State update()
Read the button and return its new state.
Definition: Button.cpp:17
Button::prevInput
bool prevInput
Definition: Button.hpp:104
Button::getState
State getState() const
Get the state of the button, without updating it.
Definition: Button.cpp:34
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
ExtIO
A namespace with alternatives to the standard Arduino IO functions that can be used with extended IO ...
Definition: ExtendedInputOutput.cpp:8
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:56
Button::begin
void begin()
Initialize (enable the internal pull-up resistor).
Definition: Button.cpp:9
Button::debounceTime
constexpr static unsigned long debounceTime
Edit this in Settings.hpp.
Definition: Button.hpp:116
Button::State
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:50
Button::Button
Button()
Construct a new Button object.
Definition: Button.hpp:24
Button::Pressed
Definition: Button.hpp:51
Button::getName
static const __FlashStringHelper * getName(State state)
Return the name of the state as a string.
Definition: Button.cpp:36
Button::Released
< Input went from low to low (0,0)
Definition: Button.hpp:52
Button::invertState
static bool invertState
Definition: Button.hpp:111
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23