AbleButtons V0.4.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 SINGLE_CLICKED_EVENT, ///< The button has been clicked (pressed+released).
31 DOUBLE_CLICKED_EVENT ///< The button has been double-clicked.
32 };
33
34 public:
35 //
36 // Creators...
37 //
38
39 /**
40 * Create a callback button on the specified pin.
41 *
42 * @param pin The pin connected to the button.
43 * @param callbackFn The function to call when the button is pressed.
44 * @param id Callback identifier for the button (default auto-assigned).
45 */
46 inline CallbackButton(uint8_t pin,
47 void(*callbackFn)(enum CALLBACK_EVENT, uint8_t) = 0,
48 uint8_t id = Pin::nextId())
49 :Button(pin), callbackFn_(callbackFn), id_(id) {}
50
51 private:
52 //
53 // Copying and assignment (not supported)...
54 //
55 CallbackButton(const CallbackButton &cpy) = delete; ///< Copying is not supported.
56 CallbackButton &operator=(const CallbackButton &) = delete; ///< Assigning is not supported.
57
58 public:
59 //
60 // Modifiers...
61 //
62
63 /**
64 * Initialise the button. Called from setup() of an Arduino program.
65 */
66 void begin() {
68 doCallback(BEGIN_EVENT);
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 if(this->isPressed()) {
79 doCallback(PRESSED_EVENT);
80 } else {
81 doCallback(RELEASED_EVENT);
82 }
83 } else if(lastEvent_ != HELD_EVENT && this->isHeld()) {
84 doCallback(HELD_EVENT);
85 } else if(lastEvent_ != IDLE_EVENT && this->isIdle()) {
86 doCallback(IDLE_EVENT);
87 }
88 }
89
90 /**
91 * Set a (new) callback function.
92 *
93 * @param callbackFn The function to call for a button event. Use 0 to
94 * clear the callback function.
95 */
96 inline void setCallback(void(*callbackFn)(CALLBACK_EVENT, uint8_t)) {
97 callbackFn_ = callbackFn;
98 }
99
100 protected:
101 inline void doCallback(CALLBACK_EVENT event) {
102 lastEvent_ = event;
103 if(callbackFn_) callbackFn_(event, id_);
104 }
105
106 public:
107 //
108 // Accessors...
109 //
110
111 /**
112 * Return the id of the button.
113 *
114 * @return The id of the button (auto-assigned or set when created).
115 */
116 inline uint8_t id() const {
117 return id_;
118 }
119
120 protected:
121 //
122 // Data...
123 //
124 void (*callbackFn_)(enum CALLBACK_EVENT, uint8_t); ///< Callback function.
125 uint8_t id_; ///< Identifier for the button passed to callback functions.
126 CALLBACK_EVENT lastEvent_; ///< Indicates the last event sent.
127 };
128
129 /**
130 * Callback clicker button template to call a function if the button is pressed.
131 *
132 * @param Button The base button class for Callback. Defaults to the Button
133 * template class.
134 */
135 template <typename Button>
137 public:
138 //
139 // Creators...
140 //
141
142 /**
143 * Create a callback button on the specified pin.
144 *
145 * @param pin The pin connected to the button.
146 * @param callbackFn The function to call when the button is pressed.
147 * @param id Callback identifier for the button (default auto-assigned).
148 */
149 inline CallbackClickerButton(uint8_t pin,
150 void(*callbackFn)(enum Button::CALLBACK_EVENT, uint8_t) = 0,
151 uint8_t id = Pin::nextId())
152 :Button(pin, callbackFn, id) {}
153
154 private:
155 //
156 // Copying and assignment (not supported)...
157 //
158 CallbackClickerButton(const CallbackClickerButton &cpy) = delete; ///< Copying is not supported.
159 CallbackClickerButton &operator=(const CallbackClickerButton &) = delete; ///< Assigning is not supported.
160
161 public:
162 //
163 // Modifiers...
164 //
165
166 /**
167 * Handle the button. Called from loop() of an Arduino program.
168 */
169 void handle() {
171 if(this->lastEvent_ == Button::RELEASED_EVENT) {
172 switch(Button::clicks(Button::BUTTON_PRESSED, Button::BUTTON_RELEASED)){
173 case 1:
174 doCallback(Button::SINGLE_CLICKED_EVENT);
175 break;
176
177 case 2:
178 doCallback(Button::DOUBLE_CLICKED_EVENT);
179 break;
180
181 default: // No clicks.
182 break;
183 }
184 }
185 }
186 };
187}
Definition of the core Able Button template class.
Core Button class.
Definition: Button.h:22
void begin()
Initialise the button.
Definition: Button.h:60
void handle()
Handle the button.
Definition: Button.h:67
bool isHeld() const
Determine if the button is currently held down.
Definition: Button.h:136
bool isIdle() const
Determine if the button is currently idle (unpressed for a "long" time).
Definition: Button.h:145
bool isPressed() const
Determine if the button is currently pressed.
Definition: Button.h:127
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.
@ DOUBLE_CLICKED_EVENT
The button has been double-clicked.
@ 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.
@ SINGLE_CLICKED_EVENT
The button has been clicked (pressed+released).
CALLBACK_EVENT lastEvent_
Indicates the last event sent.
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.
void handle()
Handle the button.
void setCallback(void(*callbackFn)(CALLBACK_EVENT, uint8_t))
Set a (new) callback function.
void begin()
Initialise the button.
Callback clicker button template to call a function if the button is pressed.
CallbackClickerButton(uint8_t pin, void(*callbackFn)(enum Button::CALLBACK_EVENT, uint8_t)=0, uint8_t id=Pin::nextId())
Create a callback button on the specified pin.
void handle()
Handle the button.
uint8_t currState_
The reading of the pin.
Definition: Pins.h:94
static uint8_t nextId()
Return the next auto-assigned button identifier.
Definition: Pins.h:29
int clicks(uint8_t pressed, uint8_t released)
Return the number of clicks.
Definition: Pins.h:83