Control Surface  1.1.1
MIDI Control Surface library for Arduino
Button.cpp
Go to the documentation of this file.
1 #include "Button.hpp"
2 
3 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
4 
6 
7 using namespace ExtIO;
8 
9 Button::Button(pin_t pin) : pin(pin) {}
10 
11 void Button::begin() { ExtIO::pinMode(pin, INPUT_PULLUP); }
12 
13 void Button::invert() { invertState = true; }
14 
15 #ifndef AH_INDIVIDUAL_BUTTON_INVERT
16 bool Button::invertState = false;
17 #endif
18 
19 Button::State Button::update() {
20  // read the button state and invert it if "invertState" is true
21  bool input = ExtIO::digitalRead(pin) ^ invertState;
22  bool prevState = debouncedState & 0b01;
23  unsigned long now = millis();
24  if (now - prevBounceTime > debounceTime) { // wait for state to stabilize
25  debouncedState = static_cast<State>((prevState << 1) | input);
26  } else {
27  debouncedState = static_cast<State>((prevState << 1) | prevState);
28  }
29  if (input != prevInput) { // Button is pressed, released or bounces
30  prevBounceTime = now;
31  prevInput = input;
32  }
33  return debouncedState;
34 }
35 
36 Button::State Button::getState() const { return debouncedState; }
37 
38 const __FlashStringHelper *Button::getName(Button::State state) {
39  switch (state) {
40  case Button::Pressed: return F("Pressed");
41  case Button::Released: return F("Released");
42  case Button::Falling: return F("Falling");
43  case Button::Rising: return F("Rising");
44  default: return F("<invalid>"); // Keeps the compiler happy
45  }
46 }
47 
48 unsigned long Button::previousBounceTime() const { return prevBounceTime; }
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 
65 
AH::ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
INPUT_PULLUP
const uint8_t INPUT_PULLUP
Definition: ExtendedInputOutput.hpp:51
Button.hpp
AH::ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:60
AH::Button::State
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:53
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::BUTTON_DEBOUNCE_TIME
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition: AH/Settings/Settings.hpp:74
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10