AbleButtons
V0.2.0
Lightweight button library for Arduino.
Loading...
Searching...
No Matches
CallbackButton.h
Go to the documentation of this file.
1
/**
2
* @file CallbackButton.h Definitions for handling callback functions. When a
3
* button is pressed or released, a callback function can called.
4
*
5
* @copyright Copyright (c) 2022 John Scott.
6
*/
7
#pragma once
8
#include "
Button.h
"
9
10
namespace
able {
11
/**
12
* Callback button template to call a function if the button is pressed.
13
*
14
* @param Button The base button class for Callback. Defaults to the Button
15
* template class.
16
*/
17
template
<
typename
Button>
18
class
CallbackButton
:
public
Button
{
19
public
:
20
/**
21
* Button event codes. When a callback function is called, the first
22
* argument is an event code below.
23
*/
24
enum
CALLBACK_EVENT
{
25
BEGIN_EVENT
,
///< The button's begin() method has completed.
26
PRESSED_EVENT
,
///< The button has been pressed.
27
RELEASED_EVENT
,
///< The button has been released.
28
HELD_EVENT
,
///< The button has been held down for a while.
29
IDLE_EVENT
///< The button has been idle (untouched) for a while.
30
};
31
32
public
:
33
//
34
// Creators...
35
//
36
37
/**
38
* Create a callback button on the specified pin.
39
*
40
* @param pin The pin connected to the button.
41
* @param callbackFn The function to call when the button is pressed.
42
* @param id Callback identifier for the button (default auto-assigned).
43
*/
44
inline
CallbackButton
(uint8_t pin,
45
void
(*callbackFn)(
enum
CALLBACK_EVENT
, uint8_t) = 0,
46
uint8_t
id
=
Pin::nextId
())
47
:
Button
(pin),
callbackFn_
(callbackFn),
id_
(
id
) {}
48
49
private
:
50
//
51
// Copying and assignment (not supported)...
52
//
53
CallbackButton
(
const
CallbackButton
&cpy) =
delete
;
///< Copying is not supported.
54
CallbackButton
&operator=(
const
CallbackButton
&) =
delete
;
///< Assigning is not supported.
55
56
public
:
57
//
58
// Modifiers...
59
//
60
61
/**
62
* Initialise the button. Called from setup() of an Arduino program.
63
*/
64
void
begin
() {
65
Button::begin
();
66
if
(
callbackFn_
) {
67
callbackFn_
(
BEGIN_EVENT
,
id_
);
68
}
69
}
70
71
/**
72
* Handle the button. Called from loop() of an Arduino program.
73
*/
74
void
handle
() {
75
uint8_t currState = this->
currState_
;
76
Button::handle
();
77
if
(currState != this->
currState_
) {
78
longEvent_
=
false
;
79
if
(
callbackFn_
) {
80
if
(this->
isPressed
()) {
81
callbackFn_
(
PRESSED_EVENT
,
id_
);
82
}
else
{
83
callbackFn_
(
RELEASED_EVENT
,
id_
);
84
}
85
}
86
}
else
if
(!
longEvent_
&&
callbackFn_
) {
87
if
(this->
isHeld
()) {
88
longEvent_
=
true
;
89
callbackFn_
(
HELD_EVENT
,
id_
);
90
}
else
if
(this->
isIdle
()) {
91
longEvent_
=
true
;
92
callbackFn_
(
IDLE_EVENT
,
id_
);
93
}
94
}
95
}
96
97
/**
98
* Set a (new) callback function.
99
*
100
* @param callbackFn The function to call for a button event. Use 0 to
101
* clear the callback function.
102
*/
103
inline
void
setCallback
(
void
(*callbackFn)(
CALLBACK_EVENT
, uint8_t)) {
104
callbackFn_
= callbackFn;
105
}
106
107
public
:
108
//
109
// Accessors...
110
//
111
112
/**
113
* Return the id of the button.
114
*
115
* @return The id of the button (auto-assigned or set when created).
116
*/
117
inline
uint8_t
id
()
const
{
118
return
id_
;
119
}
120
121
protected
:
122
//
123
// Data...
124
//
125
void (*
callbackFn_
)(
enum
CALLBACK_EVENT
, uint8_t);
///< Callback function.
126
uint8_t
id_
;
///< Identifier for the button passed to callback functions.
127
bool
longEvent_
;
///< Indicates the held/idle event has been sent.
128
};
129
}
Button.h
Definition of the core Able Button template class.
able::Button
Core Button class.
Definition:
Button.h:22
able::Button::begin
void begin()
Initialise the button.
Definition:
Button.h:51
able::Button::handle
void handle()
Handle the button.
Definition:
Button.h:58
able::Button::isHeld
bool isHeld() const
Determine if the button is currently held down.
Definition:
Button.h:112
able::Button::isIdle
bool isIdle() const
Determine if the button is currently idle (unpressed for a "long" time).
Definition:
Button.h:121
able::Button::isPressed
bool isPressed() const
Determine if the button is currently pressed.
Definition:
Button.h:103
able::CallbackButton
Callback button template to call a function if the button is pressed.
Definition:
CallbackButton.h:18
able::CallbackButton::id_
uint8_t id_
Identifier for the button passed to callback functions.
Definition:
CallbackButton.h:126
able::CallbackButton::CALLBACK_EVENT
CALLBACK_EVENT
Button event codes.
Definition:
CallbackButton.h:24
able::CallbackButton::PRESSED_EVENT
@ PRESSED_EVENT
The button has been pressed.
Definition:
CallbackButton.h:26
able::CallbackButton::IDLE_EVENT
@ IDLE_EVENT
The button has been idle (untouched) for a while.
Definition:
CallbackButton.h:29
able::CallbackButton::RELEASED_EVENT
@ RELEASED_EVENT
The button has been released.
Definition:
CallbackButton.h:27
able::CallbackButton::BEGIN_EVENT
@ BEGIN_EVENT
The button's begin() method has completed.
Definition:
CallbackButton.h:25
able::CallbackButton::HELD_EVENT
@ HELD_EVENT
The button has been held down for a while.
Definition:
CallbackButton.h:28
able::CallbackButton::CallbackButton
CallbackButton(uint8_t pin, void(*callbackFn)(enum CALLBACK_EVENT, uint8_t)=0, uint8_t id=Pin::nextId())
Create a callback button on the specified pin.
Definition:
CallbackButton.h:44
able::CallbackButton::id
uint8_t id() const
Return the id of the button.
Definition:
CallbackButton.h:117
able::CallbackButton::callbackFn_
void(* callbackFn_)(enum CALLBACK_EVENT, uint8_t)
Callback function.
Definition:
CallbackButton.h:125
able::CallbackButton::longEvent_
bool longEvent_
Indicates the held/idle event has been sent.
Definition:
CallbackButton.h:127
able::CallbackButton::handle
void handle()
Handle the button.
Definition:
CallbackButton.h:74
able::CallbackButton::setCallback
void setCallback(void(*callbackFn)(CALLBACK_EVENT, uint8_t))
Set a (new) callback function.
Definition:
CallbackButton.h:103
able::CallbackButton::begin
void begin()
Initialise the button.
Definition:
CallbackButton.h:64
able::Pin::currState_
uint8_t currState_
The reading of the pin.
Definition:
Pins.h:73
able::Pin::nextId
static uint8_t nextId()
Return the next auto-assigned button identifier.
Definition:
Pins.h:29
AbleButtons
src
CallbackButton.h
Generated by
1.9.5