InputEvents v1.4.0
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();
142
144
153
159
160
162
176 bool hasChanged();
177
181 bool isEnabled();
182
191 bool isIdle();
193
194
196
211 void setNumIncrements(uint8_t numIncr=10);
212
218 void setNumNegativeIncrements(uint8_t numIncr=10);
219
225 void setNumPositiveIncrements(uint8_t numIncr=10);
227
229
240 void setStartValues();
241
249 void setCentreBoundary(uint16_t width=200);
257 void setOuterBoundary(uint16_t width=100);
259
260
262
271 void setRateLimit(uint16_t ms);
272
279 void enableAutoCalibrate(bool allow=true);
281
282
283protected:
296
297private:
298
299
300#ifndef FUNCTIONAL_SUPPORTED
301private:
302 static void analogXCallback(InputEventType et, EventAnalog &enc) {
303 EventJoystick *instance = static_cast<EventJoystick *>(enc.getOwner());
304 if (instance) instance->onInputXCallback(et, enc);
305 }
306
307 static void analogYCallback(InputEventType et, EventAnalog &enc) {
308 EventJoystick *instance = static_cast<EventJoystick *>(enc.getOwner());
309 if (instance) instance->onInputYCallback(et, enc);
310 }
311#endif
312
313
314};
315
316
317#endif
318
InputEventType
A list of all events that can be fired by InputEvents classes.
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:184
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:152
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:157
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 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