Arduino Helpers master
Utility library for Arduino
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
MultiPurposeButton Class Reference

#include <AH/Hardware/MultiPurposeButton.hpp>

Detailed Description

Class for detecting short/long button presses and double clicks.

Examples
3.MultiPurposeButton.ino.

Definition at line 12 of file MultiPurposeButton.hpp.

+ Collaboration diagram for MultiPurposeButton:

Public Types

enum  Event {
  None , PressStart , ShortPressRelease , LongPress ,
  LongPressRelease , MultiPress , MultiPressDone
}
 

Public Member Functions

 MultiPurposeButton (pin_t pin)
 
void begin ()
 
Event update ()
 Read the button state and return the Event (if any). More...
 
uint8_t getMultiPressCount () const
 Get the number of times the button was pressed in quick succession (after MultiPressDone), this is the final count. More...
 
uint8_t getCurrentMultiPressCount () const
 Get the number of times the button was pressed in quick succession, while the button is being pressed (before MultiPressDone). More...
 
unsigned long getLongPressDelay () const
 Get the number of milliseconds after which a press is considered a long press. More...
 
void setLongPressDelay (unsigned long longPressDelay)
 Set the number of milliseconds after which a press is considered a long press. More...
 
unsigned long getMultiPressDelay () const
 Get the number of milliseconds between multipresses. More...
 
void setMultiPressDelay (unsigned long multiPressDelay)
 Set the number of milliseconds between multipresses. More...
 
unsigned long previousBounceTime () const
 
unsigned long stableTime () const
 
unsigned long stableTime (unsigned long now) const
 
unsigned long getPressedTime () const
 Get the number of milliseconds the button has been pressed for. More...
 
unsigned long getLongPressedTime () const
 Get the number of milliseconds the button has been pressed for. More...
 
Button::State getButtonState () const
 
void invert ()
 

Static Public Member Functions

static FlashString_t getName (Event ev)
 Return the name of the event as a string. More...
 

Protected Attributes

Button button
 
unsigned long longPressDelay = 1000
 
unsigned long multiPressDelay = 400
 
bool longPress = false
 
uint8_t multiPressCountNew = 0
 
uint8_t multiPressCount = 0
 

Member Enumeration Documentation

◆ Event

enum Event
Enumerator
None 

Nothing changed.

PressStart 

The button was just pressed.

ShortPressRelease 

The button was released after a short press.

LongPress 

The button has been pressed for some time.

LongPressRelease 

The button was released after a long press.

MultiPress 

The button was pressed in quick succession of the previous release.

MultiPressDone 

The button has been released for long enough to rule out another MultiPress.

Definition at line 16 of file MultiPurposeButton.hpp.

Constructor & Destructor Documentation

◆ MultiPurposeButton()

MultiPurposeButton ( pin_t  pin)
inline

Definition at line 14 of file MultiPurposeButton.hpp.

Member Function Documentation

◆ begin()

void begin ( )
inline
See also
Button::begin()
Examples
3.MultiPurposeButton.ino.

Definition at line 29 of file MultiPurposeButton.hpp.

◆ update()

Event update ( )
inline

Read the button state and return the Event (if any).

Definition at line 32 of file MultiPurposeButton.hpp.

◆ getMultiPressCount()

uint8_t getMultiPressCount ( ) const
inline

Get the number of times the button was pressed in quick succession (after MultiPressDone), this is the final count.

Definition at line 65 of file MultiPurposeButton.hpp.

◆ getCurrentMultiPressCount()

uint8_t getCurrentMultiPressCount ( ) const
inline

Get the number of times the button was pressed in quick succession, while the button is being pressed (before MultiPressDone).

The count could still increase if the user keeps on pressing the button.

Definition at line 69 of file MultiPurposeButton.hpp.

◆ getLongPressDelay()

unsigned long getLongPressDelay ( ) const
inline

Get the number of milliseconds after which a press is considered a long press.

Definition at line 73 of file MultiPurposeButton.hpp.

◆ setLongPressDelay()

void setLongPressDelay ( unsigned long  longPressDelay)
inline

Set the number of milliseconds after which a press is considered a long press.

Definition at line 76 of file MultiPurposeButton.hpp.

◆ getMultiPressDelay()

unsigned long getMultiPressDelay ( ) const
inline

Get the number of milliseconds between multipresses.

Definition at line 81 of file MultiPurposeButton.hpp.

◆ setMultiPressDelay()

void setMultiPressDelay ( unsigned long  multiPressDelay)
inline

Set the number of milliseconds between multipresses.

Definition at line 83 of file MultiPurposeButton.hpp.

◆ previousBounceTime()

unsigned long previousBounceTime ( ) const
inline
See also
Button::previousBounceTime() const

Definition at line 88 of file MultiPurposeButton.hpp.

◆ stableTime() [1/2]

unsigned long stableTime ( ) const
inline
See also
Button::stableTime() const

Definition at line 92 of file MultiPurposeButton.hpp.

◆ stableTime() [2/2]

unsigned long stableTime ( unsigned long  now) const
inline

◆ getPressedTime()

unsigned long getPressedTime ( ) const
inline

Get the number of milliseconds the button has been pressed for.

Returns 0 if the button is not currently pressed.

Definition at line 100 of file MultiPurposeButton.hpp.

◆ getLongPressedTime()

unsigned long getLongPressedTime ( ) const
inline

Get the number of milliseconds the button has been pressed for.

Returns 0 if the button is not currently pressed or if the current press is not a long press (yet).

Definition at line 106 of file MultiPurposeButton.hpp.

◆ getName()

static FlashString_t getName ( Event  ev)
inlinestatic

Return the name of the event as a string.

Definition at line 111 of file MultiPurposeButton.hpp.

◆ getButtonState()

Button::State getButtonState ( ) const
inline
See also
Button::getState() const

Definition at line 114 of file MultiPurposeButton.hpp.

◆ invert()

void invert ( )
inline
See also
Button::invert()

Definition at line 116 of file MultiPurposeButton.hpp.

Member Data Documentation

◆ button

Button button
protected

Definition at line 119 of file MultiPurposeButton.hpp.

◆ longPressDelay

unsigned long longPressDelay = 1000
protected

Definition at line 120 of file MultiPurposeButton.hpp.

◆ multiPressDelay

unsigned long multiPressDelay = 400
protected

Definition at line 121 of file MultiPurposeButton.hpp.

◆ longPress

bool longPress = false
protected

Definition at line 122 of file MultiPurposeButton.hpp.

◆ multiPressCountNew

uint8_t multiPressCountNew = 0
protected

Definition at line 123 of file MultiPurposeButton.hpp.

◆ multiPressCount

uint8_t multiPressCount = 0
protected

Definition at line 124 of file MultiPurposeButton.hpp.


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