InputEvents
v1.5.2
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
src
EventInputBase.h
1
/*
2
*
3
* GPLv2 Licence https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
4
*
5
* Copyright (c) 2024 Philip Fletcher <philip.fletcher@stutchbury.com>
6
*
7
*/
8
9
#ifndef EVENT_INPUT_BASE_H
10
#define EVENT_INPUT_BASE_H
11
12
#include <Arduino.h>
13
14
#include "
InputEvents.h
"
15
16
#ifdef FUNCTIONAL_SUPPORTED
17
#include <functional>
18
#endif
19
20
26
class
EventInputBase
{
27
28
29
protected
:
30
31
uint8_t
input_id
= 0;
32
uint8_t
input_value
= 0;
33
bool
_enabled
=
true
;
34
bool
idleFired
=
true
;
35
unsigned
long
lastEventMs
= millis();
36
unsigned
long
idleTimeout
= 10000;
37
38
39
public
:
40
49
virtual
void
begin
() = 0;
50
54
virtual
void
unsetCallback
();
55
59
bool
isCallbackSet
() {
return
callbackIsSet
; }
60
66
void
update
();
67
75
bool
isEnabled
() {
return
_enabled
; }
76
82
void
enable
(
bool
e =
true
);
83
90
void
resetState
();
91
93
95
104
void
setIdleTimeout
(
unsigned
int
timeoutMs=10000) {
idleTimeout
= timeoutMs; }
105
109
unsigned
long
msSinceLastEvent
() {
return
millis() -
lastEventMs
; }
110
118
bool
isIdle
() {
return
(millis() -
lastEventMs
) >
idleTimeout
; }
119
125
void
resetIdleTimer
();
127
129
138
void
blockEvent
(
InputEventType
et);
139
145
void
allowEvent
(
InputEventType
et);
146
150
void
blockAllEvents
();
151
155
void
allowAllEvents
() { memset(excludedEvents, 0,
sizeof
(excludedEvents)); }
// Reset the bitmask to 0
156
160
bool
isEventAllowed
(
InputEventType
et);
162
164
173
void
setInputId
(uint8_t
id
) {
input_id
= id; }
174
178
uint8_t
getInputId
() {
return
input_id
; }
179
183
void
setInputValue
(uint8_t val) {
input_value
= val; }
184
188
uint8_t
getInputValue
() {
return
input_value
; }
190
191
192
protected
:
193
bool
callbackIsSet
=
false
;
194
202
bool
isInvokable
(
InputEventType
et);
203
209
virtual
void
invoke
(
InputEventType
et) = 0;
210
214
virtual
void
onEnabled
();
218
virtual
void
onDisabled
();
222
virtual
void
onIdle
();
223
224
225
226
227
private
:
228
//uint8_t excludedEvents[4] = {0};
229
uint8_t excludedEvents[(
static_cast<
uint8_t
>
(
InputEventType::COUNT
) + 7) / 8] = {0};
230
232
#ifndef FUNCTIONAL_SUPPORTED
233
public
:
234
void
setOwner(
void
*own) { owner = own; }
235
void
*getOwner() {
return
owner; }
236
237
protected
:
238
void
*owner =
nullptr
;
239
#endif
241
242
243
};
244
245
#endif
InputEvents.h
Contains InputEventType enums and some defines.
InputEventType
InputEventType
The size of the InputEventType enum.
Definition:
InputEvents.h:55
InputEventType::COUNT
@ COUNT
Not used - replaces NUM_EVENT_TYPE_ENUMS.
EventInputBase
The common base for InputEvents input classes.
Definition:
EventInputBase.h:26
EventInputBase::getInputId
uint8_t getInputId()
Get the input ID (for use by user, not used internally). Not unique, default is 0.
Definition:
EventInputBase.h:178
EventInputBase::resetIdleTimer
void resetIdleTimer()
Reset the idle timer. The IDLE event will fire setIdleTimeout ms after this is called.
Definition:
EventInputBase.cpp:33
EventInputBase::lastEventMs
unsigned long lastEventMs
number of milliseconds since the last event
Definition:
EventInputBase.h:35
EventInputBase::blockAllEvents
void blockAllEvents()
Stop all events from firing - usually used in conjunction with allowEvent()
Definition:
EventInputBase.cpp:58
EventInputBase::isEventAllowed
bool isEventAllowed(InputEventType et)
Returns true if the event is not blocked.
Definition:
EventInputBase.cpp:65
EventInputBase::input_id
uint8_t input_id
Input ID, not used internally.
Definition:
EventInputBase.h:31
EventInputBase::resetState
void resetState()
Resets the 'user' state of the input: silently enables if disabled, clears any blocked events,...
Definition:
EventInputBase.cpp:38
EventInputBase::callbackIsSet
bool callbackIsSet
Required because in C/C++ callback has to be defined in derived classes... :-/.
Definition:
EventInputBase.h:193
EventInputBase::invoke
virtual void invoke(InputEventType et)=0
To be overriden by derived classes.
EventInputBase::isInvokable
bool isInvokable(InputEventType et)
Definition:
EventInputBase.cpp:71
EventInputBase::allowEvent
void allowEvent(InputEventType et)
Allow a a previously blocked event tto fire.
Definition:
EventInputBase.cpp:52
EventInputBase::unsetCallback
virtual void unsetCallback()
Unset the callback. Must be overriden in derived class & then base method called.
Definition:
EventInputBase.cpp:11
EventInputBase::enable
void enable(bool e=true)
Enable or disable an input.
Definition:
EventInputBase.cpp:81
EventInputBase::isCallbackSet
bool isCallbackSet()
Returns true if the callback is set.
Definition:
EventInputBase.h:59
EventInputBase::isEnabled
bool isEnabled()
Returns true if input is enabled.
Definition:
EventInputBase.h:75
EventInputBase::onEnabled
virtual void onEnabled()
Can be ovrriden by derived classes but base method must be called.
Definition:
EventInputBase.cpp:26
EventInputBase::setInputId
void setInputId(uint8_t id)
Set the input ID (for use by user, not used internally). Not unique, default is 0.
Definition:
EventInputBase.h:173
EventInputBase::_enabled
bool _enabled
Input enabled flag.
Definition:
EventInputBase.h:33
EventInputBase::blockEvent
void blockEvent(InputEventType et)
Stop an event from firing.
Definition:
EventInputBase.cpp:46
EventInputBase::getInputValue
uint8_t getInputValue()
Get the input value (for use by user, not used internally). Not unique, default is 0.
Definition:
EventInputBase.h:188
EventInputBase::setInputValue
void setInputValue(uint8_t val)
Set the input value (for use by user, not used internally). Not unique, default is 0.
Definition:
EventInputBase.h:183
EventInputBase::msSinceLastEvent
unsigned long msSinceLastEvent()
Returns the number of ms since any event was fired for this input.
Definition:
EventInputBase.h:109
EventInputBase::onIdle
virtual void onIdle()
Can be ovrriden by derived classes but base method must be called.
Definition:
EventInputBase.cpp:30
EventInputBase::update
void update()
Update the state of the input.
Definition:
EventInputBase.cpp:18
EventInputBase::begin
virtual void begin()=0
Must be called from setup().
EventInputBase::idleTimeout
unsigned long idleTimeout
The idle timeout in milliseconds.
Definition:
EventInputBase.h:36
EventInputBase::allowAllEvents
void allowAllEvents()
Clear all blocked events.
Definition:
EventInputBase.h:155
EventInputBase::setIdleTimeout
void setIdleTimeout(unsigned int timeoutMs=10000)
Set the idle timeout in milliseconds (default 10000, 10 seconds)
Definition:
EventInputBase.h:104
EventInputBase::onDisabled
virtual void onDisabled()
Can be ovrriden by derived classes but base method must be called.
Definition:
EventInputBase.cpp:28
EventInputBase::isIdle
bool isIdle()
Return true if no activity for longer than setIdleTimeout - irrespective of whether the idle (or chan...
Definition:
EventInputBase.h:118
EventInputBase::input_value
uint8_t input_value
Input value, not used internally.
Definition:
EventInputBase.h:32
EventInputBase::idleFired
bool idleFired
True if input IDLE event has fired.
Definition:
EventInputBase.h:34
Generated by
1.9.4