AbleButtons V0.4.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
TestAbleButton.ino
Go to the documentation of this file.
1/**
2 * @file TestAbleButton.ino Test the Arduino Button Library Extension (ABLE).
3 * This program will periodically output "Looking OK..." to the Serial port. As
4 * you press buttons connected to pins 2 and 3 it will assert the button
5 * settings in the callback fuctions and in the main loop.
6 *
7 * The TestAbleButton program is split (by design) into multiple files to ensure
8 * AbleButtons works in multi-module programs. A common Config.h header file
9 * declares common elements shared across these modules.
10 *
11 * @copyright Copyright (c) 2022 John Scott
12 */
13#include "Config.h"
14#include "Checks.h"
15#include "Utils.h"
16
17#if TESTABLE_CALLBACK
18#include "Callback.h"
21#else
22Button btnA(BUTTON_A_PIN); ///< Button A
23Button btnB(BUTTON_B_PIN); ///< Button B
24#endif
25
26/// Array of buttons for ButtonList.
28 &btnA,
29 &btnB
30};
31ButtonList btnList(btns); ///< List of buttons.
32bool led = false; ///< State of the builtin LED.
33
34/**
35 * Setup the PushBtn example. Called once to initialise everything.
36 */
37void setup() {
38 Serial.begin(115200);
39 Serial.println(F("\nTestAbleButton: AbleButtons Test Harness.\n"));
40
41 pinMode(LED_BUILTIN, OUTPUT);
42
43# if TESTABLE_CLASS >= TESTABLE_BUTTON
44 Button::setHeldTime(2500);
45 assert(Button::heldTime() == 2500);
46 Button::setIdleTime(5000);
47 assert(Button::idleTime() == 5000);
48#endif
49
50# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
51 Button::setClickTime(750);
52 assert(Button::clickTime() == 750);
53# endif
54
55 btnList.begin(); // Begin buttons in the list (sets pin mode etc).
56
57 // Serial << "State: " << btnB.currState_
58 // << "; Prev: " <<btnB.prevState_
59 // << "; msStart: " << btnB.millisStart_
60 // << "; msPrev: " << btnB.prevMillis_
61 // << "; Count: " << btnB.stateCount_
62 // << "; ms: " << millis()
63 // << endl;
64
65 for(int i = 0; i < NUM_BUTTONS; ++i) {
66 checkButtonSetup(btns[i]); // Check each button setup OK.
67 }
68 checkButtonListIntegrity(); // Check ButtonList setup OK.
69}
70
71/**
72 * Control TestAbleButton. Called repeatedly in a loop.
73 */
74void loop() {
75 // static uint8_t currState = btnB.currState_;
76 // static uint8_t prevState = btnB.prevState_;
77 // static unsigned long millisStart = btnB.millisStart_;
78 // static unsigned long prevStart = btnB.prevMillis_;
79 // static uint8_t stateCount = btnB.stateCount_;
80 static unsigned long ms = millis(); ///< Timer to preiodically print OK message.
81 bool resetSingleClicked = false;
82 bool resetDoubleClicked = false;
83
84 if(millis() - ms > 5000) {
85 Serial.println(F("Looking OK..."));
86 ms = millis();
87 }
88
89 btnList.handle(); // Handle features of the buttons (debouncing, clicks & callbacks).
90
91 // if (currState != btnB.currState_
92 // || prevState != btnB.prevState_
93 // || millisStart != btnB.millisStart_
94 // || prevStart != btnB.prevMillis_
95 // || stateCount != btnB.stateCount_
96 // || (btnB.stateCount_ && (millis() - btnB.millisStart_) >= btnB.clickTime_)) {
97 // Serial << "State: " << btnB.currState_
98 // << "; Prev: " <<btnB.prevState_
99 // << "; msStart: " << btnB.millisStart_
100 // << "; msPrev: " << btnB.prevMillis_
101 // << "; Count: " << btnB.stateCount_
102 // << "; Delta: " << millis() - btnB.millisStart_
103 // << "; Time: " << btnB.clickTime_
104 // << endl;
105
106 // currState = btnB.currState_;
107 // prevState = btnB.prevState_;
108 // millisStart = btnB.millisStart_;
109 // prevStart = btnB.prevMillis_;
110 // stateCount = btnB.stateCount_;
111 // }
112
113#if TESTABLE_CLASS < TESTABLE_CLICKER
114 led = btnList.anyPressed(); // Display pressed activity to LED.
115#else
116 if(btnList.anyClicked()) { // Display clicked activity to LED.
117 led = !led;
118 }
119#endif
120 digitalWrite(LED_BUILTIN, led); // Update the LED.
121
122 checkButtonListIntegrity(); // Check ButtonList::any* and ButtonList::all* states...
123
124 for(int i = 0; i < NUM_BUTTONS; ++i) {
125 Button *btn = btns[i];
126 ButtonState &state(btnState[i]);
127
129
130# if TESTABLE_CALLBACK
131 // Check state when button pressed event.
132 if(state.wasPressed) {
134 state.wasPressed = false; // Clear most recent callback signal.
135 }
136
137 // Check state when button released event.
138 if(state.wasReleased) {
140 state.wasReleased = false;
141 }
142
143 // Check state when button held event.
144 if(state.wasHeld) {
146 state.wasHeld = false;
147 }
148
149 // Check state when button idle event.
150 if(state.wasIdle) {
152 state.wasIdle = false;
153 }
154
155 // Check state when button idle event.
156 if(state.wasIdle) {
158 state.wasIdle = false;
159 }
160
161# if TESTABLE_CLASS >= TESTABLE_CLICKER
162 // Check state when button clicked event.
163 if(state.wasClicked) {
165 state.wasClicked = false;
166 }
167# endif
168# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
169 // Check state when button single-clicked event.
170 if(state.wasSingleClicked) {
172 state.wasSingleClicked = false;
173 }
174
175 if(state.resetSingleClicked) {
176 resetSingleClicked = true;
177 state.resetSingleClicked = false;
178 }
179
180 // Check state when button double-clicked event.
181 if(state.wasDoubleClicked) {
183 state.wasDoubleClicked = false;
184 }
185
186 if(state.resetDoubleClicked) {
187 resetDoubleClicked = true;
188 state.resetDoubleClicked = false;
189 }
190# endif
191# endif
192
195 }
196
197 // Reset clicks/double-clicks and check.
198# if TESTABLE_CLASS >= TESTABLE_CLICKER
200 assert(btnList.anyClicked() == false);
202# endif
203
204# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
205 if(resetSingleClicked) {
207 assert(btnList.anySingleClicked() == false);
208 }
209
210 if(resetDoubleClicked) {
212 assert(btnList.anyDoubleClicked() == false);
213 }
215# endif
216}
Button btn(BUTTON_PIN)
The button to check.
#define BUTTON_B_PIN
Connect button between this pin and ground.
#define BUTTON_A_PIN
Connect button between this pin and ground.
void onEvent(Button::CALLBACK_EVENT event, uint8_t id)
Callback function for button events.
Definition: Callback.cpp:20
Declarations for Callback module.
void checkButtonJustSingleClicked(Button *btn)
Check button that has just been single-clicked.
Definition: Checks.cpp:167
void checkButtonJustClicked(Button *btn)
Check button that has just been clicked.
Definition: Checks.cpp:142
void checkButtonListIntegrity()
Check integrity of ButtonList invariants (always hold true).
Definition: Checks.cpp:286
void checkButtonJustReleased(Button *btn)
Check button that has just been released.
Definition: Checks.cpp:67
void checkButtonJustHeld(Button *btn)
Check button that has just been held.
Definition: Checks.cpp:92
void checkButtonIntegrity(Button *btn, ButtonState &state)
Check integrity of Button invariants (always hold true).
Definition: Checks.cpp:202
void checkButtonJustIdle(Button *btn)
Check button that has just become or remains idle.
Definition: Checks.cpp:117
void checkButtonSetup(Button *btn)
Check the state of an individual button just setup.
Definition: Checks.cpp:17
void displayButtonChanges(int index)
Display button changes to Serial.
Definition: Checks.cpp:347
struct ButtonState btnState[NUM_BUTTONS]
Previous state for each button.
Definition: Checks.cpp:9
void checkButtonJustDoubleClicked(Button *btn)
Check button that has just been double-clicked.
Definition: Checks.cpp:177
void checkButtonJustPressed(Button *btn)
Check state of a pressed button just pressed.
Definition: Checks.cpp:42
Declarations for the Checks module.
Declarations to select which buttons we use.
#define NUM_BUTTONS
The number of buttons to connect for testing.
Definition: Config.h:13
Button * btns[NUM_BUTTONS]
Array of buttons for ButtonList.
ButtonList btnList(btns)
List of buttons.
void setup()
Setup the PushBtn example.
Button btnB(BUTTON_B_PIN, onEvent)
Button B.
bool led
State of the builtin LED.
Button btnA(BUTTON_A_PIN, onEvent)
Button A.
void loop()
Control TestAbleButton.
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
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 resetDoubleClicked()
Reset double-clicked state of all buttons, returning true if any were double-clicked.
Definition: ButtonList.h:90
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
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 anySingleClicked() const
Determine if any of the buttons have been single-clicked.
Definition: ButtonList.h:319
void begin()
Initialise all the buttons.
Definition: ButtonList.h:53