InputEvents v1.5.2
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
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
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
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
192protected:
193 bool callbackIsSet = false;
194
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
227private:
228 //uint8_t excludedEvents[4] = {0};
229 uint8_t excludedEvents[(static_cast<uint8_t>(InputEventType::COUNT) + 7) / 8] = {0};
230
232#ifndef FUNCTIONAL_SUPPORTED
233public:
234 void setOwner(void *own) { owner = own; }
235 void *getOwner() { return owner; }
236
237protected:
238 void *owner = nullptr;
239#endif
241
242
243};
244
245#endif
Contains InputEventType enums and some defines.
InputEventType
The size of the InputEventType enum.
Definition: InputEvents.h:55
@ COUNT
Not used - replaces NUM_EVENT_TYPE_ENUMS.
The common base for InputEvents input classes.
Definition: EventInputBase.h:26
uint8_t getInputId()
Get the input ID (for use by user, not used internally). Not unique, default is 0.
Definition: EventInputBase.h:178
void resetIdleTimer()
Reset the idle timer. The IDLE event will fire setIdleTimeout ms after this is called.
Definition: EventInputBase.cpp:33
unsigned long lastEventMs
number of milliseconds since the last event
Definition: EventInputBase.h:35
void blockAllEvents()
Stop all events from firing - usually used in conjunction with allowEvent()
Definition: EventInputBase.cpp:58
bool isEventAllowed(InputEventType et)
Returns true if the event is not blocked.
Definition: EventInputBase.cpp:65
uint8_t input_id
Input ID, not used internally.
Definition: EventInputBase.h:31
void resetState()
Resets the 'user' state of the input: silently enables if disabled, clears any blocked events,...
Definition: EventInputBase.cpp:38
bool callbackIsSet
Required because in C/C++ callback has to be defined in derived classes... :-/.
Definition: EventInputBase.h:193
virtual void invoke(InputEventType et)=0
To be overriden by derived classes.
bool isInvokable(InputEventType et)
Definition: EventInputBase.cpp:71
void allowEvent(InputEventType et)
Allow a a previously blocked event tto fire.
Definition: EventInputBase.cpp:52
virtual void unsetCallback()
Unset the callback. Must be overriden in derived class & then base method called.
Definition: EventInputBase.cpp:11
void enable(bool e=true)
Enable or disable an input.
Definition: EventInputBase.cpp:81
bool isCallbackSet()
Returns true if the callback is set.
Definition: EventInputBase.h:59
bool isEnabled()
Returns true if input is enabled.
Definition: EventInputBase.h:75
virtual void onEnabled()
Can be ovrriden by derived classes but base method must be called.
Definition: EventInputBase.cpp:26
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
bool _enabled
Input enabled flag.
Definition: EventInputBase.h:33
void blockEvent(InputEventType et)
Stop an event from firing.
Definition: EventInputBase.cpp:46
uint8_t getInputValue()
Get the input value (for use by user, not used internally). Not unique, default is 0.
Definition: EventInputBase.h:188
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
unsigned long msSinceLastEvent()
Returns the number of ms since any event was fired for this input.
Definition: EventInputBase.h:109
virtual void onIdle()
Can be ovrriden by derived classes but base method must be called.
Definition: EventInputBase.cpp:30
void update()
Update the state of the input.
Definition: EventInputBase.cpp:18
virtual void begin()=0
Must be called from setup().
unsigned long idleTimeout
The idle timeout in milliseconds.
Definition: EventInputBase.h:36
void allowAllEvents()
Clear all blocked events.
Definition: EventInputBase.h:155
void setIdleTimeout(unsigned int timeoutMs=10000)
Set the idle timeout in milliseconds (default 10000, 10 seconds)
Definition: EventInputBase.h:104
virtual void onDisabled()
Can be ovrriden by derived classes but base method must be called.
Definition: EventInputBase.cpp:28
bool isIdle()
Return true if no activity for longer than setIdleTimeout - irrespective of whether the idle (or chan...
Definition: EventInputBase.h:118
uint8_t input_value
Input value, not used internally.
Definition: EventInputBase.h:32
bool idleFired
True if input IDLE event has fired.
Definition: EventInputBase.h:34