InputEvents v1.5.2
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
EventJoystick.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
10#ifndef EVENT_JOYSTICK_H
11#define EVENT_JOYSTICK_H
12
13#include "Arduino.h"
14#include "EventAnalog.h"
15
16
29
30protected:
31
32 #if defined(FUNCTIONAL_SUPPORTED)
36 typedef std::function<void(InputEventType et, EventJoystick &ie)> CallbackFunction;
37 #else
42 #endif
43
48
54 void invoke(InputEventType et) override;
55
56 void onEnabled() override;
57 void onDisabled() override;
58 void onIdle() override { /* Do nothing. Fire idle callback from either x or y but only if both are idle from onInputCallback() */ }
59
60public:
61
63
76 EventJoystick(byte analogX, byte analogY, uint8_t adcBits=10);
78
79
80
81
83
95 void begin();
96
104 callbackIsSet = true;
105 }
106
116 #if defined(FUNCTIONAL_SUPPORTED)
117 // Method to set callback with instance and class method
118 template <typename T>
119 void setCallback(T* instance, void (T::*method)(InputEventType, EventJoystick&)) {
120 // Wrap the method call in a lambda
121 callbackFunction = [instance, method](InputEventType et, EventJoystick &ie) {
122 (instance->*method)(et, ie); // Call the member function on the instance
123 };
124 callbackIsSet = true;
125 }
126 #endif
127
133 void unsetCallback() override;
134
140 void update();
141
148 void resetState();
149
151
153
162
168
169
171
185 bool hasChanged();
186
190 bool isEnabled();
191
200 bool isIdle();
202
203
205
220 void setNumIncrements(uint8_t numIncr=10);
221
227 void setNumNegativeIncrements(uint8_t numIncr=10);
228
234 void setNumPositiveIncrements(uint8_t numIncr=10);
236
238
249 void setStartValues();
250
258 void setCentreBoundary(uint16_t width=200);
266 void setOuterBoundary(uint16_t width=100);
268
269
271
280 void setRateLimit(uint16_t ms);
281
288 void enableAutoCalibrate(bool allow=true);
290
291
292protected:
305
306private:
307
308
309#ifndef FUNCTIONAL_SUPPORTED
310private:
311 static void analogXCallback(InputEventType et, EventAnalog &enc) {
312 EventJoystick *instance = static_cast<EventJoystick *>(enc.getOwner());
313 if (instance) instance->onInputXCallback(et, enc);
314 }
315
316 static void analogYCallback(InputEventType et, EventAnalog &enc) {
317 EventJoystick *instance = static_cast<EventJoystick *>(enc.getOwner());
318 if (instance) instance->onInputYCallback(et, enc);
319 }
320#endif
321
322
323};
324
325
326#endif
327
InputEventType
The size of the InputEventType enum.
Definition: InputEvents.h:55
The EventAnalog class is for analog inputs - slice an analog range into configurable number of increm...
Definition: EventAnalog.h:30
The common base for InputEvents input classes.
Definition: EventInputBase.h:26
bool callbackIsSet
Required because in C/C++ callback has to be defined in derived classes... :-/.
Definition: EventInputBase.h:193
The EventJoystick class contains two EventAnalog inputs configured as a joystick.
Definition: EventJoystick.h:28
EventAnalog x
The EventAnalog for the X axis.
Definition: EventJoystick.h:161
void begin()
Initialise the EventButton.
Definition: EventJoystick.cpp:26
void enableAutoCalibrate(bool allow=true)
If enableAutoCalibrate is set to true (the default), will do auto calibration, setting the minValue a...
Definition: EventJoystick.cpp:109
void onDisabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventJoystick.cpp:138
CallbackFunction callbackFunction
The callback function member.
Definition: EventJoystick.h:47
void setNumIncrements(uint8_t numIncr=10)
Split the analog range into this number of slices. The default is 10.
Definition: EventJoystick.cpp:77
std::function< void(InputEventType et, EventJoystick &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventJoystick.h:36
void onEnabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventJoystick.cpp:132
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventJoystick.cpp:60
void setOuterBoundary(uint16_t width=100)
Used to allow maximum increments to be reached 'on the diagonal'.
Definition: EventJoystick.cpp:97
EventJoystick(byte analogX, byte analogY, uint8_t adcBits=10)
Construct anEventJoystick input.
Definition: EventJoystick.cpp:13
void onInputYCallback(InputEventType et, EventInputBase &ie)
Definition: EventJoystick.cpp:52
bool isIdle()
Return true if no activity on both EventAnalog axis for longer than setIdleTimeout() - irrespective o...
Definition: EventJoystick.cpp:115
void setNumNegativeIncrements(uint8_t numIncr=10)
Normally increments are set with setNumIncrements() but you can also set the negative and positive si...
Definition: EventJoystick.cpp:82
EventAnalog y
The EventAnalog for the Y axis.
Definition: EventJoystick.h:166
void onInputCallback(InputEventType et, EventInputBase &ie)
Definition: EventJoystick.cpp:34
void setCallback(T *instance, void(T::*method)(InputEventType, EventJoystick &))
Set the Callback function to a class method.
Definition: EventJoystick.h:119
void setCentreBoundary(uint16_t width=200)
Used to create a central 'dead zone' on the joystick.
Definition: EventJoystick.cpp:92
bool isEnabled()
Returns true if enabled (or if either of the EventAnalg axis are enabled)
Definition: EventJoystick.cpp:107
void setRateLimit(uint16_t ms)
Limit the rate at which events are fired.
Definition: EventJoystick.cpp:120
void setStartValues()
Will set the X and Y start values as the current position.
Definition: EventJoystick.cpp:71
void setNumPositiveIncrements(uint8_t numIncr=10)
Normally increments are set with setNumIncrements() but you can also set the negative and positive si...
Definition: EventJoystick.cpp:87
void update()
Update the state from both X and Y pin inputs.
Definition: EventJoystick.cpp:65
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventJoystick.h:102
void resetState()
Resets the state of the input: silently enables if disabled, clears any blocked events,...
Definition: EventJoystick.cpp:144
void onIdle() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventJoystick.h:58
bool hasChanged()
Returns true of the position of either EventAnalog axis has changed since previous update()
Definition: EventJoystick.cpp:102
void invoke(InputEventType et) override
Override of the EventInputBase::invoke() virtual method.
Definition: EventJoystick.cpp:126
void onInputXCallback(InputEventType et, EventInputBase &ie)
Definition: EventJoystick.cpp:44