AbleButtons V0.4.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
able::CallbackButton< Button > Class Template Reference

Callback button template to call a function if the button is pressed. More...

#include <CallbackButton.h>

+ Inheritance diagram for able::CallbackButton< Button >:

Public Types

enum  CALLBACK_EVENT {
  BEGIN_EVENT , PRESSED_EVENT , RELEASED_EVENT , HELD_EVENT ,
  IDLE_EVENT , SINGLE_CLICKED_EVENT , DOUBLE_CLICKED_EVENT
}
 Button event codes. More...
 
enum  { BUTTON_PRESSED = Circuit::BUTTON_PRESSED , BUTTON_RELEASED = Circuit::BUTTON_RELEASED }
 

Public Member Functions

 CallbackButton (uint8_t pin, void(*callbackFn)(enum CALLBACK_EVENT, uint8_t)=0, uint8_t id=Pin::nextId())
 Create a callback button on the specified pin. More...
 
void begin ()
 Initialise the button. More...
 
void handle ()
 Handle the button. More...
 
void setCallback (void(*callbackFn)(CALLBACK_EVENT, uint8_t))
 Set a (new) callback function. More...
 
uint8_t id () const
 Return the id of the button. More...
 
bool resetClicked ()
 Reset the clicked state of the button, returning what is was. More...
 
bool resetSingleClicked ()
 Reset the clicked state of the button, returning what is was. More...
 
bool resetDoubleClicked ()
 Reset the double-clicked state of the button, returning what is was. More...
 
bool isPressed () const
 Determine if the button is currently pressed. More...
 
bool isHeld () const
 Determine if the button is currently held down. More...
 
bool isIdle () const
 Determine if the button is currently idle (unpressed for a "long" time). More...
 
bool isClicked () const
 Determine if the button is clicked. More...
 
bool isSingleClicked () const
 Determine if the button is exclusively single-clicked. More...
 
bool isDoubleClicked () const
 Determine if the button is double-clicked. More...
 

Protected Member Functions

void doCallback (CALLBACK_EVENT event)
 
void readPin ()
 Read the pin directly. More...
 
int clicks (uint8_t pressed, uint8_t released)
 Return the number of clicks. More...
 

Static Protected Member Functions

static uint8_t nextId ()
 Return the next auto-assigned button identifier. More...
 

Protected Attributes

void(* callbackFn_ )(enum CALLBACK_EVENT, uint8_t)
 Callback function. More...
 
uint8_t id_
 Identifier for the button passed to callback functions. More...
 
CALLBACK_EVENT lastEvent_
 Indicates the last event sent. More...
 
uint8_t pin_
 The Arduino pin connected to the button. More...
 
uint8_t currState_
 The reading of the pin. More...
 

Static Protected Attributes

static uint8_t autoId_ = 0
 Auto-assigned button identifier. More...
 

Detailed Description

template<typename Button>
class able::CallbackButton< Button >

Callback button template to call a function if the button is pressed.

Parameters
ButtonThe base button class for Callback. Defaults to the Button template class.

Definition at line 18 of file CallbackButton.h.

Member Enumeration Documentation

◆ anonymous enum

template<typename Circuit , typename Pin >
anonymous enum
inherited

Definition at line 27 of file Button.h.

◆ CALLBACK_EVENT

template<typename Button >
enum able::CallbackButton::CALLBACK_EVENT

Button event codes.

When a callback function is called, the first argument is an event code below.

Enumerator
BEGIN_EVENT 

The button's begin() method has completed.

PRESSED_EVENT 

The button has been pressed.

RELEASED_EVENT 

The button has been released.

HELD_EVENT 

The button has been held down for a while.

IDLE_EVENT 

The button has been idle (untouched) for a while.

SINGLE_CLICKED_EVENT 

The button has been clicked (pressed+released).

DOUBLE_CLICKED_EVENT 

The button has been double-clicked.

Definition at line 24 of file CallbackButton.h.

Constructor & Destructor Documentation

◆ CallbackButton()

template<typename Button >
able::CallbackButton< Button >::CallbackButton ( uint8_t  pin,
void(*)(enum CALLBACK_EVENT, uint8_t)  callbackFn = 0,
uint8_t  id = Pin::nextId() 
)
inline

Create a callback button on the specified pin.

Parameters
pinThe pin connected to the button.
callbackFnThe function to call when the button is pressed.
idCallback identifier for the button (default auto-assigned).

Definition at line 46 of file CallbackButton.h.

Member Function Documentation

◆ begin()

template<typename Button >
void able::CallbackButton< Button >::begin ( )
inline

Initialise the button.

Called from setup() of an Arduino program.

Definition at line 66 of file CallbackButton.h.

◆ clicks()

int able::Pin::clicks ( uint8_t  pressed,
uint8_t  released 
)
inlineprotectedinherited

Return the number of clicks.

Parameters
pressedThe pressed state of a button.
releasedThe released state of a button.
Returns
Always no clicks (0) as clicks not supported by default.

Definition at line 83 of file Pins.h.

◆ doCallback()

template<typename Button >
void able::CallbackButton< Button >::doCallback ( CALLBACK_EVENT  event)
inlineprotected

Definition at line 101 of file CallbackButton.h.

◆ handle()

template<typename Button >
void able::CallbackButton< Button >::handle ( )
inline

Handle the button.

Called from loop() of an Arduino program.

Definition at line 74 of file CallbackButton.h.

◆ id()

template<typename Button >
uint8_t able::CallbackButton< Button >::id ( ) const
inline

Return the id of the button.

Returns
The id of the button (auto-assigned or set when created).

Definition at line 116 of file CallbackButton.h.

◆ isClicked()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::isClicked ( ) const
inlineinherited

Determine if the button is clicked.

Clicks are registered as a press then release. If the ClickerPin (or subclass) is used, the button returns the click state, otherwise the compile will fail with errors.

Returns
True if clicked else false.

Definition at line 156 of file Button.h.

◆ isDoubleClicked()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::isDoubleClicked ( ) const
inlineinherited

Determine if the button is double-clicked.

Double-clicks are registered as two clicks within the double-click time. If the DoubleClickPin is used, the button returns the double-click state, otherwise the compile will fail with errors.

Returns
True if double-clicked else false.

Definition at line 182 of file Button.h.

◆ isHeld()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::isHeld ( ) const
inlineinherited

Determine if the button is currently held down.

Returns
True if held, else false.

Definition at line 136 of file Button.h.

◆ isIdle()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::isIdle ( ) const
inlineinherited

Determine if the button is currently idle (unpressed for a "long" time).

Returns
True if idle, else false.

Definition at line 145 of file Button.h.

◆ isPressed()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::isPressed ( ) const
inlineinherited

Determine if the button is currently pressed.

Returns
True if pressed, else false.

Definition at line 127 of file Button.h.

◆ isSingleClicked()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::isSingleClicked ( ) const
inlineinherited

Determine if the button is exclusively single-clicked.

Clicks are registered as a press then release. Single-clicks wait until any double-click time has passed to ensure it's exclusively a click and not the first click in a double-click sequence. If the DoubleClickerPin is used, the button returns the single-click state, otherwise the compile will fail with errors.

Returns
True if exclusively clicked else false.

Definition at line 170 of file Button.h.

◆ nextId()

static uint8_t able::Pin::nextId ( )
inlinestaticprotectedinherited

Return the next auto-assigned button identifier.

Returns
The next auto-assigned button identifier. Each call increases the auto-assigned identifier.

Definition at line 29 of file Pins.h.

◆ readPin()

void able::Pin::readPin ( )
inlineprotectedinherited

Read the pin directly.

In order to save memory, virtual functions are not used (which can consume almost 1K of memory to deal with). Runtime polymorphism is not required, so avoiding virtual functions saves memory.

Definition at line 66 of file Pins.h.

◆ resetClicked()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::resetClicked ( )
inlineinherited

Reset the clicked state of the button, returning what is was.

This allows the click state to be effectively read once so that a clicked state only triggers something once, when checked. For example toggling something on/off when the button is clicked. For buttons that don't support clicking, the compile will fail with errors.

Returns
True if the button was clicked, else false.

Definition at line 80 of file Button.h.

◆ resetDoubleClicked()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::resetDoubleClicked ( )
inlineinherited

Reset the double-clicked state of the button, returning what is was.

This allows the double-click state to be effectively read once so that a double-clicked state only triggers something once, when checked. For example toggling something on/off when the button is double- clicked. For buttons that don't support double-clicking, the compile will fail with errors.

Returns
True if the button was double-clicked, else false.

Definition at line 111 of file Button.h.

◆ resetSingleClicked()

template<typename Circuit , typename Pin >
bool able::Button< Circuit, Pin >::resetSingleClicked ( )
inlineinherited

Reset the clicked state of the button, returning what is was.

This allows the click state to be effectively read once so that a clicked state only triggers something once, when checked. For example toggling something on/off when the button is clicked. For buttons that don't support clicking, the compile will fail with errors.

Returns
True if the button was clicked, else false.

Definition at line 95 of file Button.h.

◆ setCallback()

template<typename Button >
void able::CallbackButton< Button >::setCallback ( void(*)(CALLBACK_EVENT, uint8_t)  callbackFn)
inline

Set a (new) callback function.

Parameters
callbackFnThe function to call for a button event. Use 0 to clear the callback function.

Definition at line 96 of file CallbackButton.h.

Member Data Documentation

◆ autoId_

uint8_t able::Pin::autoId_ = 0
staticprotectedinherited

Auto-assigned button identifier.

Definition at line 91 of file Pins.h.

◆ callbackFn_

template<typename Button >
void(* able::CallbackButton< Button >::callbackFn_) (enum CALLBACK_EVENT, uint8_t)
protected

Callback function.

Definition at line 124 of file CallbackButton.h.

◆ currState_

uint8_t able::Pin::currState_
protectedinherited

The reading of the pin.

Definition at line 94 of file Pins.h.

◆ id_

template<typename Button >
uint8_t able::CallbackButton< Button >::id_
protected

Identifier for the button passed to callback functions.

Definition at line 125 of file CallbackButton.h.

◆ lastEvent_

template<typename Button >
CALLBACK_EVENT able::CallbackButton< Button >::lastEvent_
protected

Indicates the last event sent.

Definition at line 126 of file CallbackButton.h.

◆ pin_

uint8_t able::Pin::pin_
protectedinherited

The Arduino pin connected to the button.

Definition at line 93 of file Pins.h.


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