AbleButtons V0.2.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 public:
101 //
102 // Accessors...
103 //
104
105 /**
106 * For CallbackButtons (which each have an id), return a pointer to the
107 * first button matching the id (or a null pointer if the id is not
108 * found). When using the button, always check if the return value is
109 * non-zero.
110 *
111 * Button *btn = btnList.button(id);
112 * if(btn) {
113 * // Use btn...
114 * } else {
115 * // Id not found in list...
116 * }
117 *
118 * @param id The identifier of the button to find in the list.
119 *
120 * @return The first id-matching button, or a null pointer.
121 */
122 Button *button(uint8_t id) const {
123 Button *rc = 0;
124 for(size_t i = 0; i < len_; ++i) {
125 if(buttons_[i]->id() == id ) {
126 rc = buttons_[i];
127 break;
128 }
129 }
130 return rc;
131 }
132
133 /**
134 * Determine if all of the buttons are currently pressed.
135 *
136 * @return True if all pressed, else false.
137 */
138 bool allPressed() const {
139 bool rc = true;
140 for(size_t i = 0; i < len_; ++i) {
141 if(!buttons_[i]->isPressed()) {
142 rc = false;
143 }
144 }
145 return rc;
146 }
147
148 /**
149 * Determine if any of the buttons are currently pressed.
150 *
151 * @return True if any pressed, else false.
152 */
153 bool anyPressed() const {
154 bool rc = false;
155 for(size_t i = 0; i < len_; ++i) {
156 if(buttons_[i]->isPressed()) {
157 rc = true;
158 }
159 }
160 return rc;
161 }
162
163 /**
164 * Determine if all of the buttons are currently held.
165 *
166 * @return True if all held, else false.
167 */
168 bool allHeld() const {
169 bool rc = true;
170 for(size_t i = 0; i < len_; ++i) {
171 if(!buttons_[i]->isHeld()) {
172 rc = false;
173 }
174 }
175 return rc;
176 }
177
178 /**
179 * Determine if any of the buttons are currently held.
180 *
181 * @return True if any held, else false.
182 */
183 bool anyHeld() const {
184 bool rc = false;
185 for(size_t i = 0; i < len_; ++i) {
186 if(buttons_[i]->isHeld()) {
187 rc = true;
188 }
189 }
190 return rc;
191 }
192
193 /**
194 * Determine if all of the buttons are currently idle.
195 *
196 * @return True if all idle, else false.
197 */
198 bool allIdle() const {
199 bool rc = true;
200 for(size_t i = 0; i < len_; ++i) {
201 if(!buttons_[i]->isIdle()) {
202 rc = false;
203 }
204 }
205 return rc;
206 }
207
208 /**
209 * Determine if any of the buttons are currently idle.
210 *
211 * @return True if any idle, else false.
212 */
213 bool anyIdle() const {
214 bool rc = false;
215 for(size_t i = 0; i < len_; ++i) {
216 if(buttons_[i]->isIdle()) {
217 rc = true;
218 }
219 }
220 return rc;
221 }
222
223 /**
224 * Determine if all of the buttons have been clicked.
225 *
226 * @return True if all pressed, else false.
227 */
228 bool allClicked() const {
229 bool rc = true;
230 for(size_t i = 0; i < len_; ++i) {
231 if(!buttons_[i]->isClicked()) {
232 rc = false;
233 }
234 }
235 return rc;
236 }
237
238 /**
239 * Determine if all of the buttons have been clicked.
240 *
241 * @return True if any clicked, else false.
242 */
243 bool anyClicked() const {
244 bool rc = false;
245 for(size_t i = 0; i < len_; ++i) {
246 if(buttons_[i]->isClicked()) {
247 rc = true;
248 }
249 }
250 return rc;
251 }
252
253 /**
254 * Determine if all of the buttons have been double-clicked.
255 *
256 * @return True if all double-clicked, else false.
257 */
258 bool allDoubleClicked() const {
259 bool rc = true;
260 for(size_t i = 0; i < len_; ++i) {
261 if(!buttons_[i]->isDoubleClicked()) {
262 rc = false;
263 }
264 }
265 return rc;
266 }
267
268 /**
269 * Determine if any of the buttons have been double-clicked.
270 *
271 * @return True if any double-clicked, else false.
272 */
273 bool anyDoubleClicked() const {
274 bool rc = false;
275 for(size_t i = 0; i < len_; ++i) {
276 if(buttons_[i]->isDoubleClicked()) {
277 rc = true;
278 }
279 }
280 return rc;
281 }
282
283 private:
284 //
285 // Data...
286 //
287 Button **buttons_; ///< The array of buttons to manage together.
288 size_t len_; ///< The length o the button array.
289 };
290}
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
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 allDoubleClicked() const
Determine if all of the buttons have been double-clicked.
Definition: ButtonList.h:258
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:138
bool anyHeld() const
Determine if any of the buttons are currently held.
Definition: ButtonList.h:183
bool allHeld() const
Determine if all of the buttons are currently held.
Definition: ButtonList.h:168
bool anyDoubleClicked() const
Determine if any of the buttons have been double-clicked.
Definition: ButtonList.h:273
bool anyClicked() const
Determine if all of the buttons have been clicked.
Definition: ButtonList.h:243
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:122
bool anyPressed() const
Determine if any of the buttons are currently pressed.
Definition: ButtonList.h:153
bool resetClicked()
Reset clicked state of all buttons, returning true if any were clicked.
Definition: ButtonList.h:74
bool anyIdle() const
Determine if any of the buttons are currently idle.
Definition: ButtonList.h:213
bool allIdle() const
Determine if all of the buttons are currently idle.
Definition: ButtonList.h:198
bool allClicked() const
Determine if all of the buttons have been clicked.
Definition: ButtonList.h:228
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