AbleButtons V0.2.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
CallbackButton.h
Go to the documentation of this file.
1/**
2 * @file CallbackButton.h Definitions for handling callback functions. When a
3 * button is pressed or released, a callback function can called.
4 *
5 * @copyright Copyright (c) 2022 John Scott.
6 */
7#pragma once
8#include "Button.h"
9
10namespace able {
11 /**
12 * Callback button template to call a function if the button is pressed.
13 *
14 * @param Button The base button class for Callback. Defaults to the Button
15 * template class.
16 */
17 template <typename Button>
18 class CallbackButton: public Button {
19 public:
20 /**
21 * Button event codes. When a callback function is called, the first
22 * argument is an event code below.
23 */
25 BEGIN_EVENT, ///< The button's begin() method has completed.
26 PRESSED_EVENT, ///< The button has been pressed.
27 RELEASED_EVENT, ///< The button has been released.
28 HELD_EVENT, ///< The button has been held down for a while.
29 IDLE_EVENT ///< The button has been idle (untouched) for a while.
30 };
31
32 public:
33 //
34 // Creators...
35 //
36
37 /**
38 * Create a callback button on the specified pin.
39 *
40 * @param pin The pin connected to the button.
41 * @param callbackFn The function to call when the button is pressed.
42 * @param id Callback identifier for the button (default auto-assigned).
43 */
44 inline CallbackButton(uint8_t pin,
45 void(*callbackFn)(enum CALLBACK_EVENT, uint8_t) = 0,
46 uint8_t id = Pin::nextId())
47 :Button(pin), callbackFn_(callbackFn), id_(id) {}
48
49 private:
50 //
51 // Copying and assignment (not supported)...
52 //
53 CallbackButton(const CallbackButton &cpy) = delete; ///< Copying is not supported.
54 CallbackButton &operator=(const CallbackButton &) = delete; ///< Assigning is not supported.
55
56 public:
57 //
58 // Modifiers...
59 //
60
61 /**
62 * Initialise the button. Called from setup() of an Arduino program.
63 */
64 void begin() {
66 if(callbackFn_) {
68 }
69 }
70
71 /**
72 * Handle the button. Called from loop() of an Arduino program.
73 */
74 void handle() {
75 uint8_t currState = this->currState_;
77 if(currState != this->currState_) {
78 longEvent_ = false;
79 if(callbackFn_) {
80 if(this->isPressed()) {
82 } else {
84 }
85 }
86 } else if(!longEvent_ && callbackFn_) {
87 if(this->isHeld()) {
88 longEvent_ = true;
90 } else if(this->isIdle()) {
91 longEvent_ = true;
93 }
94 }
95 }
96
97 /**
98 * Set a (new) callback function.
99 *
100 * @param callbackFn The function to call for a button event. Use 0 to
101 * clear the callback function.
102 */
103 inline void setCallback(void(*callbackFn)(CALLBACK_EVENT, uint8_t)) {
104 callbackFn_ = callbackFn;
105 }
106
107 public:
108 //
109 // Accessors...
110 //
111
112 /**
113 * Return the id of the button.
114 *
115 * @return The id of the button (auto-assigned or set when created).
116 */
117 inline uint8_t id() const {
118 return id_;
119 }
120
121 protected:
122 //
123 // Data...
124 //
125 void (*callbackFn_)(enum CALLBACK_EVENT, uint8_t); ///< Callback function.
126 uint8_t id_; ///< Identifier for the button passed to callback functions.
127 bool longEvent_; ///< Indicates the held/idle event has been sent.
128 };
129}
Definition of the core Able Button template class.
Core Button class.
Definition: Button.h:22
void begin()
Initialise the button.
Definition: Button.h:51
void handle()
Handle the button.
Definition: Button.h:58
bool isHeld() const
Determine if the button is currently held down.
Definition: Button.h:112
bool isIdle() const
Determine if the button is currently idle (unpressed for a "long" time).
Definition: Button.h:121
bool isPressed() const
Determine if the button is currently pressed.
Definition: Button.h:103
Callback button template to call a function if the button is pressed.
uint8_t id_
Identifier for the button passed to callback functions.
CALLBACK_EVENT
Button event codes.
@ PRESSED_EVENT
The button has been pressed.
@ IDLE_EVENT
The button has been idle (untouched) for a while.
@ RELEASED_EVENT
The button has been released.
@ BEGIN_EVENT
The button's begin() method has completed.
@ HELD_EVENT
The button has been held down for a while.
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.
uint8_t id() const
Return the id of the button.
void(* callbackFn_)(enum CALLBACK_EVENT, uint8_t)
Callback function.
bool longEvent_
Indicates the held/idle event has been sent.
void handle()
Handle the button.
void setCallback(void(*callbackFn)(CALLBACK_EVENT, uint8_t))
Set a (new) callback function.
void begin()
Initialise the button.
uint8_t currState_
The reading of the pin.
Definition: Pins.h:73
static uint8_t nextId()
Return the next auto-assigned button identifier.
Definition: Pins.h:29