AbleButtons V0.2.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
CheckButton.cpp
Go to the documentation of this file.
1/**
2 * @file CheckButton.cpp Check state of a button.
3 *
4 * @copyright Copyright (c) 2022 John Scott
5 */
6#include "Checks.h"
7#include "Utils.h"
8
9struct ButtonState btnState[NUM_BUTTONS]; ///< Previous state for each button.
10
11/**
12 * Check the state of an individual button just setup.
13 *
14 * @param btn The button to check.
15 */
17 // Given the button, when first setup, then it...
18 assert(btn->isPressed() == false); // ...is not pressed.
19 assert(btn->isHeld() == false); // ...is not held.
20 assert(btn->isIdle() == false); // ...is not idle.
21
22# if TESTABLE_CLASS >= TESTABLE_CLICKER
23 assert(btn->isClicked() == false); // ...is not clicked
24# endif
25
26# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
27 assert(btn->isDoubleClicked() == false); // ...is not double-clicked.
28# endif
29}
30
31
32/**
33 * Check state of a pressed button just pressed.
34 *
35 * @param btn The button to check.
36 */
38 // Given a button, when just pressed, then it...
39 assert(btn->isPressed() == true); // ...must be pressed.
40 assert(btn->isHeld() == false); // ...cannot be held.
41 assert(btn->isIdle() == false); // ...cannot be idle.
42
43# if TESTABLE_CLASS >= TESTABLE_CLICKER
44 assert(btn->isClicked() == false); // ...cannot be clicked.
45# endif
46
47# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
48 assert(btn->isDoubleClicked() == false); // ...cannot be double-clicked.
49# endif
50}
51
52
53/**
54 * Check button that has just been released.
55 *
56 * @param btn The button to check.
57 */
59 // Given a button, when just released, then it...
60 assert(btn->isPressed() == false); // ...cannot be pressed.
61 assert(btn->isHeld() == false); // ...cannot be held.
62 assert(btn->isIdle() == false); // ...cannot be idle.
63
64# if TESTABLE_CLASS >= TESTABLE_CLICKER
65 assert(btn->isClicked() == true); // ...must be clicked.
66# endif
67
68# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
69 // assert(btn->isDoubleClicked() == true); // ...*might* be double-clicked.
70# endif
71}
72
73
74/**
75 * Check button that has just been held.
76 *
77 * @param btn The button to check.
78 */
80 // Given a button, when it is held, then it...
81 assert(btn->isPressed() == true); // ...must be pressed.
82 assert(btn->isHeld() == true); // ...must be held.
83 assert(btn->isIdle() == false); // ...cannot be idle.
84
85# if TESTABLE_CLASS >= TESTABLE_CLICKER
86 assert(btn->isClicked() == false); // ...cannot be clicked.
87# endif
88
89# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
90 assert(btn->isDoubleClicked() == false); // ...cannot be double-clicked.
91# endif
92}
93
94
95/**
96 * Check button that has just become or remains idle.
97 *
98 * @param btn The button to check.
99 */
101 // Given a button, when it is idle, then it...
102 assert(btn->isPressed() == false); // ...cannot be pressed.
103 assert(btn->isHeld() == false); // ...cannot be held.
104 assert(btn->isIdle() == true); // ...will be idle.
105
106# if TESTABLE_CLASS >= TESTABLE_CLICKER
107 assert(btn->isClicked() == false); // ...cannot be clicked.
108# endif
109
110# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
111 assert(btn->isDoubleClicked() == false); // ...cannot be double-clicked.
112# endif
113}
114
115
116/**
117 * Check button that has just been clicked.
118 *
119 * @param btn The button to check.
120 */
122 // Given a button, when it just becomes clicked, then it...
123 assert(btn->isPressed() == false); // ...cannot be pressed.
124 assert(btn->isHeld() == false); // ...cannot be held.
125 assert(btn->isIdle() == false); // ...cannot be idle.
126
127# if TESTABLE_CLASS >= TESTABLE_CLICKER
128 assert(btn->isClicked() == true); // ...must be clicked.
129# endif
130
131# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
132 // assert(btn->isDoubleClicked() == false); // ...*might* be double-clicked.
133# endif
134}
135
136/**
137 * Check button that has just been double-clicked.
138 *
139 * @param btn The button to check.
140 */
142 // Given a button, when it just becomes double-clicked, then it...
143 assert(btn->isPressed() == false); // ...cannot be pressed.
144 assert(btn->isHeld() == false); // ...cannot be held.
145 assert(btn->isIdle() == false); // ...cannot be idle.
146
147# if TESTABLE_CLASS >= TESTABLE_CLICKER
148 assert(btn->isClicked() == true); // ...must be clicked.
149# endif
150
151# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
152 assert(btn->isDoubleClicked() == true); // ...will be double-clicked.
153# endif
154}
155
156/**
157 * Check integrity of Button invariants (always hold true).
158 *
159 * @param btn The button to check.
160 * @param state The tracked state of the button for comparison.
161 */
163 // Basic checks...
164 if(btn->isPressed()) {
165 // Given a pressed button...
166 if(!state.isPressed) {
167 // ...when it is just pressed, then...
169 } else {
170 // ...when it is still pressed, then it...
171 // assert(btn->isHeld() == false); // ...*might* be held.
172 assert(btn->isIdle() == false); // ...cannot be idle.
173
174# if TESTABLE_CLASS >= TESTABLE_CLICKER
175 assert(btn->isClicked() == false); // ...cannot be clicked.
176# endif
177
178# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
179 assert(btn->isDoubleClicked() == false); // ...cannot double-clicked.
180# endif
181 }
182 } else {
183 // Given an unpressed button...
184 if(state.isPressed) {
185 // ...when it is just released, then...
187 } else {
188 // ...when it is still released, then it...
189 assert(btn->isHeld() == false); // ...cannot be held.
190 // assert(btn->isIdle() == false); // ...*might* be idle.
191
192# if TESTABLE_CLASS >= TESTABLE_CLICKER
193 assert(btn->isClicked() == false); // ...cannot be clicked.
194# endif
195
196# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
197 assert(btn->isDoubleClicked() == false); // ...cannot double-clicked.
198# endif
199 }
200 }
201
202 if(!state.isHeld && btn->isHeld()) {
203 // Given a button that has just become held, then...
205 }
206
207 if(!state.isIdle && btn->isIdle()) {
208 // Given a button that has just become idle, then...
210 }
211
212 // Clickable checks...
213# if TESTABLE_CLASS >= TESTABLE_CLICKER
214 if(!state.isClicked && btn->isClicked()) {
215 // Given a button that has just been clicked, then...
217 }
218# endif
219
220 // Double clicker checks...
221# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
222 if(!state.isDoubleClicked && btn->isDoubleClicked()) {
223 // given a button that has just been double-clicked, then...
225 }
226# endif
227}
Button btn(BUTTON_PIN)
The button to check.
void checkButtonJustClicked(Button *btn)
Check button that has just been clicked.
void checkButtonJustReleased(Button *btn)
Check button that has just been released.
Definition: CheckButton.cpp:58
void checkButtonJustHeld(Button *btn)
Check button that has just been held.
Definition: CheckButton.cpp:79
void checkButtonIntegrity(Button *btn, ButtonState &state)
Check integrity of Button invariants (always hold true).
void checkButtonJustIdle(Button *btn)
Check button that has just become or remains idle.
void checkButtonSetup(Button *btn)
Check the state of an individual button just setup.
Definition: CheckButton.cpp:16
struct ButtonState btnState[NUM_BUTTONS]
Previous state for each button.
Definition: CheckButton.cpp:9
void checkButtonJustDoubleClicked(Button *btn)
Check button that has just been double-clicked.
void checkButtonJustPressed(Button *btn)
Check state of a pressed button just pressed.
Definition: CheckButton.cpp:37
Declarations for the Checks module.
#define NUM_BUTTONS
The number of buttons to connect for testing.
Definition: Config.h:13
Utility function declarations.
#define assert(e)
Macro to assert using FlashStringHelper to reduce memory usage.
Definition: Utils.h:30
Core Button class.
Definition: Button.h:22
bool isClicked() const
Determine if the button is clicked.
Definition: Button.h:132
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 isDoubleClicked() const
Determine if the button is double-clicked.
Definition: Button.h:144
bool isPressed() const
Determine if the button is currently pressed.
Definition: Button.h:103
Structure for remembering the state of each button.
Definition: Checks.h:13
bool isDoubleClicked
Button double-clicked.
Definition: Checks.h:21
bool isIdle
Button is idle.
Definition: Checks.h:16
bool isClicked
Button clicked (presed+released).
Definition: Checks.h:18
bool isPressed
Button is pressed.
Definition: Checks.h:14
bool isHeld
Button is held.
Definition: Checks.h:15