Control Surface new-input
MIDI Control Surface library for Arduino
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | List of all members
Button Class Reference

#include <AH/Hardware/Button.hpp>

Detailed Description

A class for reading and debouncing buttons and switches.

Examples
Custom-MIDI-Output-Element-Bankable.ino, and Custom-MIDI-Output-Element.ino.

Definition at line 18 of file Button.hpp.

+ Collaboration diagram for Button:

Public Types

enum  State { Pressed = 0b00 , Released = 0b11 , Falling = 0b10 , Rising = 0b01 }
 An enumeration of the different states a button can be in. More...
 

Public Member Functions

 Button ()
 Construct a new Button object. More...
 
 Button (pin_t pin)
 Construct a new Button object. More...
 
void begin ()
 Initialize (enable the internal pull-up resistor).
 
AH_INDIVIDUAL_BUTTON_INVERT_STATIC void invert ()
 Invert the state of all buttons, or of this specific button (button pressed is HIGH instead of LOW). More...
 
State update ()
 Read the button and return its new state. More...
 
State getState () const
 Get the state of the button, without updating it. More...
 
unsigned long previousBounceTime () const
 Return the time point (in milliseconds) when the button last bounced.
 
unsigned long stableTime (unsigned long now) const
 Return the time (in milliseconds) that the button has been stable for, compared to the given time point.
 
unsigned long stableTime () const
 Return the time (in milliseconds) that the button has been stable for.
 

Static Public Member Functions

static FlashString_t getName (State state)
 Return the name of the state as a string.
 
static void setDebounceTime (unsigned long debounceTime=BUTTON_DEBOUNCE_TIME)
 Set the debounce time for all Buttons. More...
 
static unsigned long getDebounceTime ()
 Get the debounce time. More...
 

Private Attributes

pin_t pin
 
bool prevInput = HIGH
 
State debouncedState = Released
 
unsigned long prevBounceTime = 0
 
bool invertState = false
 

Static Private Attributes

static unsigned long debounceTime = BUTTON_DEBOUNCE_TIME
 Edit this in Settings.hpp. More...
 

Member Enumeration Documentation

◆ State

enum State

An enumeration of the different states a button can be in.

Enumerator
Pressed 

Input went from low to low (0,0)

Released 

Input went from high to high (1,1)

Falling 

Input went from high to low (1,0)

Rising 

Input went from low to high (0,1)

Definition at line 53 of file Button.hpp.

Constructor & Destructor Documentation

◆ Button() [1/2]

Button ( )
inline

Construct a new Button object.

This constructor should not be used.
It is just a way to easily create arrays of buttons, and initializing them later.

Definition at line 27 of file Button.hpp.

◆ Button() [2/2]

Button ( pin_t  pin)

Construct a new Button object.

Parameters
pinThe digital pin to read from. The internal pull-up resistor will be enabled when begin is called.

Definition at line 7 of file Button.cpp.

Member Function Documentation

◆ invert()

void invert ( )

Invert the state of all buttons, or of this specific button (button pressed is HIGH instead of LOW).

Note
This affects all Button objects if AH_INDIVIDUAL_BUTTON_INVERT is not defined.
See also
AH_INDIVIDUAL_BUTTON_INVERT

Definition at line 11 of file Button.cpp.

◆ update()

Button::State update ( )

Read the button and return its new state.

The button is debounced, the debounce time can be set in Settings.hpp: BUTTON_DEBOUNCE_TIME.

Debounce time: ├────┤
Raw input:
HIGH ──────┐ ┌──────┐ ┌─┐ ┌─┐ ┌──────┐ ┌────────
LOW └──────┘ └─┘ └──────┘ └─┘ └─┘
├────┤ ├────┤ ├─┼─┼────┤ ├─┼─┼────┤ ├─┼────┤
Debounced output:
HIGH ──────┐ ┌──────┐ ┌──────────┐ ┌───
LOW └──────┘ └──────────┘ └──────┘
States:
HIGH ────────────────┐ ┌─────────────────
LOW └──────────────────┘
RELEASED FALLING PRESSED RISING
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
Returns
The state of the button, either Button::PRESSED, Button::RELEASED, Button::FALLING or Button::RISING.

Definition at line 17 of file Button.cpp.

◆ getState()

Button::State getState ( ) const

Get the state of the button, without updating it.

Returns the same value as the last call to update.

Returns
The state of the button, either Button::PRESSED, Button::RELEASED, Button::FALLING or Button::RISING.

Definition at line 34 of file Button.cpp.

◆ setDebounceTime()

void setDebounceTime ( unsigned long  debounceTime = BUTTON_DEBOUNCE_TIME)
static

Set the debounce time for all Buttons.

Note
This function affects all Button objects.
Parameters
debounceTimeThe new debounce time in milliseconds.

Definition at line 54 of file Button.cpp.

◆ getDebounceTime()

unsigned long getDebounceTime ( )
static

Get the debounce time.

Returns
The debounce time in milliseconds.
See also
setDebounceTime

Definition at line 58 of file Button.cpp.

Member Data Documentation

◆ debounceTime

unsigned long debounceTime = BUTTON_DEBOUNCE_TIME
staticprivate

Edit this in Settings.hpp.

See also
BUTTON_DEBOUNCE_TIME

Definition at line 144 of file Button.hpp.


The documentation for this class was generated from the following files: