AbleButtons V0.4.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
Callback.cpp
Go to the documentation of this file.
1/**
2 * @file Callback.cpp Definition of the callback functiosn. These functions
3 * perform some assertions on the state of the button list and the individual
4 * button pressed when a button is pressed. They also set button state flags
5 * so the main program can verify button states after hendling the events.
6 *
7 * @copyright Copyright (c) 2022 John Scott
8 */
9#include "Callback.h"
10#include "Checks.h"
11#include "Utils.h"
12
13#if TESTABLE_CALLBACK
14 /**
15 * Callback function for button events.
16 *
17 * @param event The event that has occured.
18 * @param id The identifier of the button generating the callback.
19 */
20 void onEvent(Button::CALLBACK_EVENT event, uint8_t id) {
21 assert(id > 0);
22 Button *btn = btnList.button(id); assert(btn == btns[id - 1]);
23 ButtonState &state(btnState[id - 1]);
24
25 if(event == Button::BEGIN_EVENT) {
26 Serial << F("Event BEGIN_EVENT (") << event << F(") for id ") << id << endl;
27
28 // Assert begin() called once for a button.
29 assert(btnState[id - 1].wasStarted == false);
30 btnState[id - 1].wasStarted = true; // Set begin for the buttin id.
31 } else if(event == Button::PRESSED_EVENT) {
32 Serial << F("Event PRESSED_EVENT (") << event << F(") for id ") << id << endl;
33 for(size_t i = 0; i < NUM_BUTTONS; ++i) {
34 assert(btnState[i].wasStarted);
35 }
36
38 state.wasPressed = true;
39 } else if(event == Button::RELEASED_EVENT) {
40 Serial << F("Event RELEASED_EVENT (") << event << F(") for id ") << id << endl;
41
42 for(size_t i = 0; i < NUM_BUTTONS; ++i) {
43 assert(btnState[i].wasStarted);
44 }
45
47 state.wasReleased = true;
48 } else if(event == Button::HELD_EVENT) {
49 Serial << F("Event HELD_EVENT (") << event << F(") for id ") << id << endl;
50
51 for(size_t i = 0; i < sizeof(NUM_BUTTONS); ++i) {
52 assert(btnState[i].wasStarted);
53 }
54
56 state.wasHeld = true;
57 } else if(event == Button::IDLE_EVENT) {
58 Serial << F("Event IDLE_EVENT (") << event << F(") for id ") << id << endl;
59
60 for(size_t i = 0; i < sizeof(NUM_BUTTONS); ++i) {
61 assert(btnState[i].wasStarted);
62 }
63
65 state.wasIdle = true;
66 } else if(event == Button::SINGLE_CLICKED_EVENT) {
67 Serial << F("Event SINGLE_CLICKED_EVENT (") << event << F(") for id ") << id << endl;
68
69 for(size_t i = 0; i < sizeof(NUM_BUTTONS); ++i) {
70 assert(btnState[i].wasStarted);
71 }
72
74# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
75 state.wasSingleClicked = true;
76 state.resetSingleClicked = true;
77# endif
78 } else if(event == Button::DOUBLE_CLICKED_EVENT) {
79 Serial << F("Event DOUBLE_CLICKED_EVENT (") << event << F(") for id ") << id << endl;
80
81 for(size_t i = 0; i < sizeof(NUM_BUTTONS); ++i) {
82 assert(btnState[i].wasStarted);
83 }
84
86# if TESTABLE_CLASS >= TESTABLE_DOUBLECLICKER
87 state.wasDoubleClicked = true;
88 state.resetDoubleClicked = true;
89# endif
90 } else {
91 Serial << F("ERROR: Event UNKNOWN (") << event << F(") for id ") << id << endl;
92 }
93 }
94#endif
Button btn(BUTTON_PIN)
The button to check.
Button * btns[]
Array of buttons for ButtonList.
ButtonList btnList(btns)
List of button to control together.
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 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 checkButtonJustIdle(Button *btn)
Check button that has just become or remains idle.
Definition: Checks.cpp:117
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.
#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
endl
Special type to allow Serial << endl to print a line and flush the stream.
Definition: Utils.h:45
Core Button class.
Definition: Button.h:22
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