This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
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

A class for reading and debouncing buttons and switches. More...

#include <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). More...
 
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 stableTime ()
 Return the time (in milliseconds) that the button has been stable for. More...
 

Static Public Member Functions

static const __FlashStringHelper * getName (State state)
 Return the name of the state as a string. More...
 

Private Attributes

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

Static Private Attributes

static bool invertState = false
 
constexpr static unsigned long debounceTime = BUTTON_DEBOUNCE_TIME
 Edit this in Settings.hpp. More...
 

Detailed Description

A class for reading and debouncing buttons and switches.

Examples
SysEx-Send-Receive.ino, and Toggle-LEDs.ino.

Definition at line 15 of file Button.hpp.

Member Enumeration Documentation

◆ State

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

Enumerator
Pressed 
Released 

< Input went from low to low (0,0)

Falling 

< Input went from high to high (1,1)

Rising 

< Input went from high to low (1,0)

Definition at line 50 of file Button.hpp.

Constructor & Destructor Documentation

◆ Button() [1/2]

Button::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 24 of file Button.hpp.

◆ Button() [2/2]

Button::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

◆ begin()

void Button::begin ( )

Initialize (enable the internal pull-up resistor).

Examples
SysEx-Send-Receive.ino.

Definition at line 9 of file Button.cpp.

◆ invert()

void Button::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 INDIVIDUAL_BUTTON_INVERT is not defined.
See also
INDIVIDUAL_BUTTON_INVERT

Definition at line 11 of file Button.cpp.

◆ update()

Button::State Button::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
Returns
The state of the button, either Button::PRESSED, Button::RELEASED, Button::FALLING or Button::RISING.
Examples
SysEx-Send-Receive.ino.

Definition at line 17 of file Button.cpp.

◆ getState()

Button::State Button::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.

◆ getName()

const __FlashStringHelper * Button::getName ( Button::State  state)
static

Return the name of the state as a string.

Definition at line 36 of file Button.cpp.

◆ stableTime()

unsigned long Button::stableTime ( )

Return the time (in milliseconds) that the button has been stable for.

Definition at line 46 of file Button.cpp.

Member Data Documentation

◆ pin

pin_t Button::pin
private

Definition at line 102 of file Button.hpp.

◆ prevInput

bool Button::prevInput = HIGH
private

Definition at line 104 of file Button.hpp.

◆ debouncedState

State Button::debouncedState = Released
private

Definition at line 105 of file Button.hpp.

◆ prevBounceTime

unsigned long Button::prevBounceTime = 0
private

Definition at line 106 of file Button.hpp.

◆ invertState

bool Button::invertState = false
staticprivate

Definition at line 111 of file Button.hpp.

◆ debounceTime

constexpr static unsigned long Button::debounceTime = BUTTON_DEBOUNCE_TIME
staticconstexprprivate

Edit this in Settings.hpp.

See also
BUTTON_DEBOUNCE_TIME

Definition at line 116 of file Button.hpp.


The documentation for this class was generated from the following files:
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:34
LOW
const uint8_t LOW
Definition: ExtendedInputOutput.hpp:35