Instrument Neutral Distributed Interface INDI 2.2.5
joystick.h
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2013-2026 Jasem Mutlaq. All rights reserved.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18
19 The full GNU General Public License is included in this distribution in the
20 file called LICENSE.
21*******************************************************************************/
22
23#pragma once
24
25#include "defaultdevice.h"
26#include "indipropertynumber.h"
27#include "indipropertyswitch.h"
28#include "indipropertytext.h"
29
30#include <memory>
31#include <string>
32#include <vector>
33#include <cmath>
34
35class JoyStickDriver;
36
48{
49 public:
50 JoyStick();
51
52 virtual bool initProperties() override;
53 virtual bool updateProperties() override;
54 virtual void ISGetProperties(const char *dev) override;
55 virtual bool ISSnoopDevice(XMLEle *root) override;
56 virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
57 virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
58 virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
59
60 static void joystickHelper(int joystick_n, double mag, double angle);
61 static void axisHelper(int axis_n, int value);
62 static void buttonHelper(int button_n, int value);
63
64 protected:
65 // Generic indi device entries
66 virtual bool Connect() override;
67 virtual bool Disconnect() override;
68 virtual const char *getDefaultName() override;
69
70 bool saveConfigItems(FILE *fp) override;
71
72 void setupParams();
73
74 void joystickEvent(int joystick_n, double mag, double angle);
75 void axisEvent(int axis_n, int value);
76 void buttonEvent(int button_n, int value);
77
78 // One PropertyNumber per joystick (each has 2 elements: Magnitude + Angle)
79 std::vector<INDI::PropertyNumber> JoyStickNP;
80
81 // Axis raw values and dead zones (sized to number of axes at connect time)
84
85 // EMA smoothing factor α ∈ (0, 1]: 1 = no filter, lower = more smoothing
87
88 // Buttons
90
91 // Device port
93
94 // Joystick information (name, version, counts)
96
97 // ── Calibration ──────────────────────────────────────────────────────────
108
121
136
137 std::unique_ptr<JoyStickDriver> driver;
138
139 // Per-axis EMA state (allocated at connect time)
140 std::vector<double> m_axisEMA;
141
142 // Circular EMA state for joystick angle (cos/sin components to handle 0°/360° wraparound)
143 std::vector<double> m_joystickAngleCosEMA;
144 std::vector<double> m_joystickAngleSinEMA;
145
146 private:
147 // ── Calibration helpers ───────────────────────────────────────────────────
148
150 bool m_calibrating {false};
151
153 std::vector<int> m_axisCalCenter;
154 std::vector<int> m_axisCalMin;
155 std::vector<int> m_axisCalMax;
156
158 std::string m_calibrationFile;
159
169 bool loadCalibrationData();
170
179 bool saveCalibrationData();
180
185 void applyCalibrationToDriver();
186
191 void applyReverseToDriver();
192};
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
The JoyStickDriver class provides basic functionality to read events from supported game pads under L...
The JoyStick class provides an INDI driver that displays event data from game pads....
Definition joystick.h:48
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
Definition joystick.cpp:218
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
Definition joystick.cpp:430
INDI::PropertyNumber FilterNP
Definition joystick.h:86
static void buttonHelper(int button_n, int value)
Definition joystick.cpp:532
virtual bool Disconnect() override
Disconnect from device.
Definition joystick.cpp:85
std::vector< double > m_axisEMA
Definition joystick.h:140
std::unique_ptr< JoyStickDriver > driver
Definition joystick.h:137
INDI::PropertyNumber AxisCalibrationNP
AxisCalibrationNP Per-axis calibration values (IP_RW).
Definition joystick.h:120
static void joystickHelper(int joystick_n, double mag, double angle)
Definition joystick.cpp:527
bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
Definition joystick.cpp:662
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition joystick.cpp:387
virtual const char * getDefaultName() override
Definition joystick.cpp:59
INDI::PropertyText JoystickInfoTP
Definition joystick.h:95
INDI::PropertySwitch ButtonSP
Definition joystick.h:89
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
Definition joystick.cpp:349
static void axisHelper(int axis_n, int value)
Definition joystick.cpp:537
std::vector< INDI::PropertyNumber > JoyStickNP
Definition joystick.h:79
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition joystick.cpp:265
virtual bool ISSnoopDevice(XMLEle *root) override
Process a snoop event from INDI server. This function is called when a snooped property is updated in...
Definition joystick.cpp:357
INDI::PropertyText PortTP
Definition joystick.h:92
void joystickEvent(int joystick_n, double mag, double angle)
Definition joystick.cpp:546
INDI::PropertySwitch CalibrationSP
CalibrationSP Start / End the calibration sweep.
Definition joystick.h:107
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
Definition joystick.cpp:366
INDI::PropertySwitch AxisReverseSP
AxisReverseSP Per-axis polarity reversal toggles (IP_RW).
Definition joystick.h:135
virtual bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition joystick.cpp:64
void axisEvent(int axis_n, int value)
Definition joystick.cpp:595
INDI::PropertyNumber DeadZoneNP
Definition joystick.h:83
INDI::PropertyNumber AxisNP
Definition joystick.h:82
void setupParams()
Definition joystick.cpp:95
std::vector< double > m_joystickAngleSinEMA
Definition joystick.h:144
std::vector< double > m_joystickAngleCosEMA
Definition joystick.h:143
void buttonEvent(int button_n, int value)
Definition joystick.cpp:643
ISState
Switch state.
Definition indiapi.h:148