InputEvents v1.4.0
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 idleFlagged = 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);
84
86
95 void setIdleTimeout(unsigned int timeoutMs=10000) { idleTimeout = timeoutMs; }
96
100 unsigned long msSinceLastEvent() { return millis() - lastEventMs; }
101
109 bool isIdle() { return (millis() - lastEventMs) > idleTimeout; }
110
116 void resetIdleTimer();
118
120
129 void blockEvent(InputEventType et);
130
136 void allowEvent(InputEventType et);
137
141 void blockAllEvents();
142
146 void allowAllEvents() { memset(excludedEvents, 0, sizeof(excludedEvents)); } // Reset the bitmask to 0
147
153
155
164 void setInputId(uint8_t id) { input_id = id; }
165
169 uint8_t getInputId() {return input_id; }
170
174 void setInputValue(uint8_t val) { input_value = val; }
175
179 uint8_t getInputValue() { return input_value; }
181
182
183protected:
184 bool callbackIsSet = false;
185
194
200 virtual void invoke(InputEventType et) = 0;
201
205 virtual void onEnabled();
209 virtual void onDisabled();
213 virtual void onIdle();
214
215
216
217
218private:
219 uint8_t excludedEvents[2] = {0};
220
221
223#ifndef FUNCTIONAL_SUPPORTED
224public:
225 void setOwner(void *own) { owner = own; }
226 void *getOwner() { return owner; }
227
228protected:
229 void *owner = nullptr;
230#endif
232
233
234};
235
236#endif
Contains InputEventType enums and some defines.
InputEventType
A list of all events that can be fired by InputEvents classes.
Definition: InputEvents.h:55
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:169
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:51
bool isEventAllowed(InputEventType et)
Returns true if the event is not blocked.
Definition: EventInputBase.cpp:57
uint8_t input_id
Input ID, not used internally.
Definition: EventInputBase.h:31
bool callbackIsSet
Required because in C/C++ callback has to be defined in derived classes... :-/.
Definition: EventInputBase.h:184
virtual void invoke(InputEventType et)=0
To be overriden by derived classes.
bool isInvokable(InputEventType et)
Definition: EventInputBase.cpp:63
void allowEvent(InputEventType et)
Allow a a previously blocked event tto fire.
Definition: EventInputBase.cpp:45
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:73
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:164
bool _enabled
Input enabled flag.
Definition: EventInputBase.h:33
void blockEvent(InputEventType et)
Stop an event from firing.
Definition: EventInputBase.cpp:39
uint8_t getInputValue()
Get the input value (for use by user, not used internally). Not unique, default is 0.
Definition: EventInputBase.h:179
void setInputValue(uint8_t val)
Set the input value (for use by user, not used internally). Not unique, default is 0.
Definition: EventInputBase.h:174
unsigned long msSinceLastEvent()
Returns the number of ms since any event was fired for this input.
Definition: EventInputBase.h:100
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().
bool idleFlagged
True if input is idle.
Definition: EventInputBase.h:34
unsigned long idleTimeout
The idle timeout in milliseconds.
Definition: EventInputBase.h:36
void allowAllEvents()
Clear all blocked events.
Definition: EventInputBase.h:146
void setIdleTimeout(unsigned int timeoutMs=10000)
Set the idle timeout in milliseconds (default 10000, 10 seconds)
Definition: EventInputBase.h:95
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:109
uint8_t input_value
Input value, not used internally.
Definition: EventInputBase.h:32