AbleButtons V0.4.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
ButtonList.h
Go to the documentation of this file.
1/**
2 * @file ButtonList.h Definitions of the ButtonList templae class. The button
3 * list allows an array of buttons to be controlled via a single begin() and
4 * handle() pair of methods. Useful for programs that use multiple buttons.
5 *
6 * @copyright Copyright (c) 2022 John Scott.
7 */
8#pragma once
9#include "Button.h"
10
11namespace able {
12 /**
13 * Template for a list of buttons of the same type. Allows a set of buttons
14 * to be controlled together. NB: You cannot mix different button types in the
15 * same button list. Use separate lists to manage collections of pulldown,
16 * and pull-up resistors, buttons and clickers) s in the same list (use
17 * separate lists).
18 */
19 template <typename Button>
20 class ButtonList {
21 public:
22 //
23 // Creators...
24 //
25
26 /**
27 * Create a list of buttons. Makes beginning and handling of a set of
28 * buttons easier.
29 *
30 * @param buttons The array of buttons to manage together.
31 */
32 template <size_t n>
33 inline ButtonList(Button *(&buttons)[n]): ButtonList(buttons, n) {}
34
35 /**
36 * Create a list of buttons. Makes beginning and handling of a set of
37 * buttons easier.
38 *
39 * @param buttons The array of buttons to manage together.
40 * @param len The number of buttons in the list.
41 */
42 inline ButtonList(Button **buttons, size_t len)
43 : buttons_(buttons), len_(len) {}
44
45 public:
46 //
47 // Modifiers...
48 //
49
50 /**
51 * Initialise all the buttons. Called from setup() of an Arduino program.
52 */
53 inline void begin() {
54 for(size_t i = 0; i < len_; ++i) {
55 buttons_[i]->begin();
56 }
57 }
58
59 /**
60 * Handle all the buttons. Called in the loop() of an Arduino program to
61 * monitor all button states and dispatch any callback events if required.
62 */
63 void handle() {
64 for(size_t i = 0; i < len_; ++i) {
65 buttons_[i]->handle();
66 }
67 }
68
69 /**
70 * Reset clicked state of all buttons, returning true if any were clicked.
71 *
72 * @return True if any clicked, else false.
73 */
74 bool resetClicked() {
75 bool rc = false;
76 for(size_t i = 0; i < len_; ++i) {
77 if(buttons_[i]->resetClicked()) {
78 rc = true;
79 }
80 }
81 return rc;
82 }
83
84 /**
85 * Reset double-clicked state of all buttons, returning true if any were
86 * double-clicked.
87 *
88 * @return True if any double-clicked, else false.
89 */
91 bool rc = false;
92 for(size_t i = 0; i < len_; ++i) {
93 if(buttons_[i]->resetDoubleClicked()) {
94 rc = true;
95 }
96 }
97 return rc;
98 }
99
100 /**
101 * Reset single-clicked state of all buttons, returning true if any were
102 * single-clicked.
103 *
104 * @return True if any single-clicked, else false.
105 */
107 bool rc = false;
108 for(size_t i = 0; i < len_; ++i) {
109 if(buttons_[i]->resetSingleClicked()) {
110 rc = true;
111 }
112 }
113 return rc;
114 }
115
116 public:
117 //
118 // Accessors...
119 //
120
121 /**
122 * For CallbackButtons (which each have an id), return a pointer to the
123 * first button matching the id (or a null pointer if the id is not
124 * found). When using the button, always check if the return value is
125 * non-zero.
126 *
127 * Button *btn = btnList.button(id);
128 * if(btn) {
129 * // Use btn...
130 * } else {
131 * // Id not found in list...
132 * }
133 *
134 * @param id The identifier of the button to find in the list.
135 *
136 * @return The first id-matching button, or a null pointer.
137 */
138 Button *button(uint8_t id) const {
139 Button *rc = 0;
140 for(size_t i = 0; i < len_; ++i) {
141 if(buttons_[i]->id() == id ) {
142 rc = buttons_[i];
143 break;
144 }
145 }
146 return rc;
147 }
148
149 /**
150 * Determine if all of the buttons are currently pressed.
151 *
152 * @return True if all pressed, else false.
153 */
154 bool allPressed() const {
155 bool rc = true;
156 for(size_t i = 0; i < len_; ++i) {
157 if(!buttons_[i]->isPressed()) {
158 rc = false;
159 }
160 }
161 return rc;
162 }
163
164 /**
165 * Determine if any of the buttons are currently pressed.
166 *
167 * @return True if any pressed, else false.
168 */
169 bool anyPressed() const {
170 bool rc = false;
171 for(size_t i = 0; i < len_; ++i) {
172 if(buttons_[i]->isPressed()) {
173 rc = true;
174 }
175 }
176 return rc;
177 }
178
179 /**
180 * Determine if all of the buttons are currently held.
181 *
182 * @return True if all held, else false.
183 */
184 bool allHeld() const {
185 bool rc = true;
186 for(size_t i = 0; i < len_; ++i) {
187 if(!buttons_[i]->isHeld()) {
188 rc = false;
189 }
190 }
191 return rc;
192 }
193
194 /**
195 * Determine if any of the buttons are currently held.
196 *
197 * @return True if any held, else false.
198 */
199 bool anyHeld() const {
200 bool rc = false;
201 for(size_t i = 0; i < len_; ++i) {
202 if(buttons_[i]->isHeld()) {
203 rc = true;
204 }
205 }
206 return rc;
207 }
208
209 /**
210 * Determine if all of the buttons are currently idle.
211 *
212 * @return True if all idle, else false.
213 */
214 bool allIdle() const {
215 bool rc = true;
216 for(size_t i = 0; i < len_; ++i) {
217 if(!buttons_[i]->isIdle()) {
218 rc = false;
219 }
220 }
221 return rc;
222 }
223
224 /**
225 * Determine if any of the buttons are currently idle.
226 *
227 * @return True if any idle, else false.
228 */
229 bool anyIdle() const {
230 bool rc = false;
231 for(size_t i = 0; i < len_; ++i) {
232 if(buttons_[i]->isIdle()) {
233 rc = true;
234 }
235 }
236 return rc;
237 }
238
239 /**
240 * Determine if all of the buttons have been clicked.
241 *
242 * @return True if all pressed, else false.
243 */
244 bool allClicked() const {
245 bool rc = true;
246 for(size_t i = 0; i < len_; ++i) {
247 if(!buttons_[i]->isClicked()) {
248 rc = false;
249 }
250 }
251 return rc;
252 }
253
254 /**
255 * Determine if all of the buttons have been clicked.
256 *
257 * @return True if any clicked, else false.
258 */
259 bool anyClicked() const {
260 bool rc = false;
261 for(size_t i = 0; i < len_; ++i) {
262 if(buttons_[i]->isClicked()) {
263 rc = true;
264 }
265 }
266 return rc;
267 }
268
269 /**
270 * Determine if all of the buttons have been double-clicked.
271 *
272 * @return True if all double-clicked, else false.
273 */
274 bool allDoubleClicked() const {
275 bool rc = true;
276 for(size_t i = 0; i < len_; ++i) {
277 if(!buttons_[i]->isDoubleClicked()) {
278 rc = false;
279 }
280 }
281 return rc;
282 }
283
284 /**
285 * Determine if any of the buttons have been double-clicked.
286 *
287 * @return True if any double-clicked, else false.
288 */
289 bool anyDoubleClicked() const {
290 bool rc = false;
291 for(size_t i = 0; i < len_; ++i) {
292 if(buttons_[i]->isDoubleClicked()) {
293 rc = true;
294 }
295 }
296 return rc;
297 }
298
299 /**
300 * Determine if all of the buttons have been single-clicked.
301 *
302 * @return True if all single-clicked, else false.
303 */
304 bool allSingleClicked() const {
305 bool rc = true;
306 for(size_t i = 0; i < len_; ++i) {
307 if(!buttons_[i]->isSingleClicked()) {
308 rc = false;
309 }
310 }
311 return rc;
312 }
313
314 /**
315 * Determine if any of the buttons have been single-clicked.
316 *
317 * @return True if any single-clicked, else false.
318 */
319 bool anySingleClicked() const {
320 bool rc = false;
321 for(size_t i = 0; i < len_; ++i) {
322 if(buttons_[i]->isSingleClicked()) {
323 rc = true;
324 }
325 }
326 return rc;
327 }
328
329 private:
330 //
331 // Data...
332 //
333 Button **buttons_; ///< The array of buttons to manage together.
334 size_t len_; ///< The length o the button array.
335 };
336}
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
Template for a list of buttons of the same type.
Definition: ButtonList.h:20
void handle()
Handle all the buttons.
Definition: ButtonList.h:63
bool allSingleClicked() const
Determine if all of the buttons have been single-clicked.
Definition: ButtonList.h:304
bool allDoubleClicked() const
Determine if all of the buttons have been double-clicked.
Definition: ButtonList.h:274
bool resetDoubleClicked()
Reset double-clicked state of all buttons, returning true if any were double-clicked.
Definition: ButtonList.h:90
bool allPressed() const
Determine if all of the buttons are currently pressed.
Definition: ButtonList.h:154
bool anyHeld() const
Determine if any of the buttons are currently held.
Definition: ButtonList.h:199
bool allHeld() const
Determine if all of the buttons are currently held.
Definition: ButtonList.h:184
bool anyDoubleClicked() const
Determine if any of the buttons have been double-clicked.
Definition: ButtonList.h:289
bool anyClicked() const
Determine if all of the buttons have been clicked.
Definition: ButtonList.h:259
Button * button(uint8_t id) const
For CallbackButtons (which each have an id), return a pointer to the first button matching the id (or...
Definition: ButtonList.h:138
bool anyPressed() const
Determine if any of the buttons are currently pressed.
Definition: ButtonList.h:169
bool resetClicked()
Reset clicked state of all buttons, returning true if any were clicked.
Definition: ButtonList.h:74
bool resetSingleClicked()
Reset single-clicked state of all buttons, returning true if any were single-clicked.
Definition: ButtonList.h:106
bool anyIdle() const
Determine if any of the buttons are currently idle.
Definition: ButtonList.h:229
bool anySingleClicked() const
Determine if any of the buttons have been single-clicked.
Definition: ButtonList.h:319
bool allIdle() const
Determine if all of the buttons are currently idle.
Definition: ButtonList.h:214
bool allClicked() const
Determine if all of the buttons have been clicked.
Definition: ButtonList.h:244
ButtonList(Button **buttons, size_t len)
Create a list of buttons.
Definition: ButtonList.h:42
void begin()
Initialise all the buttons.
Definition: ButtonList.h:53
ButtonList(Button *(&buttons)[n])
Create a list of buttons.
Definition: ButtonList.h:33