AntiMicroX
joysensor.h
Go to the documentation of this file.
1 /* antimicrox Gamepad to KB+M event mapper
2  * Copyright (C) 2022 Max Maisel <max.maisel@posteo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <QHash>
21 #include <QObject>
22 #include <QTimer>
23 
24 #include "joysensordirection.h"
25 #include "joysensortype.h"
26 #include "pt1filter.h"
27 
28 class SetJoystick;
29 class JoySensorButton;
30 class QXmlStreamReader;
31 class QXmlStreamWriter;
32 
39 class JoySensor : public QObject
40 {
41  Q_OBJECT
42 
43  public:
44  explicit JoySensor(JoySensorType type, int originset, SetJoystick *parent_set, QObject *parent);
45  virtual ~JoySensor();
46 
47  void joyEvent(float *values, bool ignoresets = false);
48  void queuePendingEvent(float *values, bool ignoresets = false);
49  void activatePendingEvent();
50  bool hasPendingEvent() const;
51  void clearPendingEvent();
52 
53  void copyAssignments(JoySensor *dest_sensor);
54  bool hasSlotsAssigned() const;
55 
56  QString getPartialName(bool forceFullFormat = false, bool displayNames = false) const;
57  QString getSensorName() const;
58 
59  JoySensorType getType() const;
61  double getDeadZone() const;
62  double getDiagonalRange() const;
63  double getMaxZone() const;
64  unsigned int getSensorDelay() const;
65  virtual float getXCoordinate() const = 0;
66  virtual float getYCoordinate() const = 0;
67  virtual float getZCoordinate() const = 0;
68  virtual QString sensorTypeName() const = 0;
69 
70  bool inDeadZone(float *values) const;
71  double getDistanceFromDeadZone() const;
72  double getDistanceFromDeadZone(double x, double y, double z) const;
73  double calculateXDistanceFromDeadZone() const;
74  double calculateXDistanceFromDeadZone(double x, double y, double z) const;
75  double calculateYDistanceFromDeadZone() const;
76  double calculateYDistanceFromDeadZone(double x, double y, double z) const;
77  double calculateZDistanceFromDeadZone() const;
78  double calculateZDistanceFromDeadZone(double x, double y, double z) const;
79  double calculateDistance() const;
80  double calculateDistance(double x, double y, double z) const;
81  double calculatePitch() const;
82  double calculatePitch(double x, double y, double z) const;
83  double calculateRoll() const;
84  double calculateRoll(double x, double y, double z) const;
85  double calculateDirectionalDistance(JoySensorDirection direction) const;
86 
87  static double radToDeg(double value);
88  static double degToRad(double value);
89 
90  bool isCalibrated() const;
91  void resetCalibration();
92  virtual void getCalibration(double *offsetX, double *offsetY, double *offsetZ) const = 0;
93  virtual void setCalibration(double offsetX, double offsetY, double offsetZ) = 0;
94 
95  QHash<JoySensorDirection, JoySensorButton *> *getButtons();
97 
98  bool isDefault() const;
99  void readConfig(QXmlStreamReader *xml);
100  void writeConfig(QXmlStreamWriter *xml) const;
101 
102  SetJoystick *getParentSet() const;
103 
104  signals:
105  void moved(float xaxis, float yaxis, float zaxis);
106  void active(float xaxis, float yaxis, float zaxis);
107  void released(float xaxis, float yaxis, float zaxis);
108  void deadZoneChanged(double value);
109  void diagonalRangeChanged(double value);
110  void maxZoneChanged(double value);
111  void sensorDelayChanged(int value);
112  void sensorNameChanged();
113  void propertyUpdated();
114 
115  public slots:
116  virtual void reset();
117  void setDeadZone(double value);
118  void setMaxZone(double value);
119  void setDiagonalRange(double value);
120  void setSensorDelay(unsigned int value);
121  void setSensorName(QString tempName);
123 
124  private slots:
125  void delayTimerExpired();
126 
127  protected:
128  void resetButtons();
129  virtual void populateButtons() = 0;
131  virtual void applyCalibration() = 0;
132  void determineSensorEvent(JoySensorButton **eventbutton) const;
133  void createDeskEvent(JoySensorDirection direction, bool ignoresets = false);
134 
136  double m_dead_zone;
138  double m_max_zone;
139  unsigned int m_sensor_delay;
140 
141  bool m_active;
142  static const size_t ACTIVE_BUTTON_COUNT = 3;
144 
145  float m_current_value[3];
146  float m_pending_value[3];
151 
153  QString m_sensor_name;
155 
158  QHash<JoySensorDirection, JoySensorButton *> m_buttons;
159 };
JoySensor::m_pending_value
float m_pending_value[3]
Definition: joysensor.h:146
JoySensor::calculateDistance
double calculateDistance() const
Get the vector length of the sensor.
Definition: joysensor.cpp:408
JoySensor::setSensorDelay
void setSensorDelay(unsigned int value)
Sets the sensor input delaqy to the given value.
Definition: joysensor.cpp:649
JoySensor::joyEvent
void joyEvent(float *values, bool ignoresets=false)
Main sensor mapping function. When activated, it generates a "moved" QT event which updates various p...
Definition: joysensor.cpp:53
pt1filter.h
JoySensor::moved
void moved(float xaxis, float yaxis, float zaxis)
JoySensor::resetButtons
void resetButtons()
Reset all the properties of the sensor direction buttons.
Definition: joysensor.cpp:785
JoySensor::createDeskEvent
void createDeskEvent(JoySensorDirection direction, bool ignoresets=false)
Find the position of the three sensor axes, deactivate no longer used sensor direction button and the...
Definition: joysensor.cpp:825
JoySensor::setDeadZone
void setDeadZone(double value)
Sets the dead zone of the sensor to the given value.
Definition: joysensor.cpp:599
JoySensor::getZCoordinate
virtual float getZCoordinate() const =0
GlobalVariables::JoySensor::DEFAULTSENSORDELAY
static const unsigned int DEFAULTSENSORDELAY
Definition: globalvariables.h:235
JoySensor::isDefault
bool isDefault() const
Checks if all sensor settings and button mappings are the their default values. This is used during X...
Definition: joysensor.cpp:557
SENSOR_FWD
@ SENSOR_FWD
Definition: joysensordirection.h:34
JoySensor::isCalibrated
bool isCalibrated() const
Check if the sensor is calibrated.
Definition: joysensor.cpp:530
JoySensor::queuePendingEvent
void queuePendingEvent(float *values, bool ignoresets=false)
Queues next movement event from InputDaemon.
Definition: joysensor.cpp:121
JoySensor::calculateDirectionalDistance
double calculateDirectionalDistance(JoySensorDirection direction) const
Used to calculate the distance value that should be used by the JoyButton in the given direction.
Definition: joysensor.cpp:488
JoySensor::m_pending_event
bool m_pending_event
Definition: joysensor.h:149
JoySensor::calculateYDistanceFromDeadZone
double calculateYDistanceFromDeadZone() const
Get current Y distance of the sensor past the assigned dead zone.
Definition: joysensor.cpp:345
JoySensor::deadZoneChanged
void deadZoneChanged(double value)
joysensor.h
JoySensor::getDistanceFromDeadZone
double getDistanceFromDeadZone() const
Get current radial distance of the sensor past the assigned dead zone.
Definition: joysensor.cpp:289
JoySensor::m_pending_ignore_sets
bool m_pending_ignore_sets
Definition: joysensor.h:150
joybuttonxml.h
JoySensor::m_calibrated
bool m_calibrated
Definition: joysensor.h:147
JoySensor::delayTimerExpired
void delayTimerExpired()
Slot called when m_delay_timer has timed out. The method will call createDeskEvent.
Definition: joysensor.cpp:780
JoySensor::clearPendingEvent
void clearPendingEvent()
Clears a previously queued event.
Definition: joysensor.cpp:157
JoySensor::~JoySensor
virtual ~JoySensor()
Definition: joysensor.cpp:43
SetJoystick
A set of mapped events which can by switched by a controller event. Contains controller input objects...
Definition: setjoystick.h:39
JoySensor::m_dead_zone
double m_dead_zone
Definition: joysensor.h:136
JoySensor::sensorTypeName
virtual QString sensorTypeName() const =0
JoySensor::calculatePitch
double calculatePitch() const
Calculate the pitch angle (in degrees) corresponding to the current position of controller.
Definition: joysensor.cpp:425
JoySensor::getDiagonalRange
double getDiagonalRange() const
Get the assigned diagonal range value.
Definition: joysensor.cpp:264
SENSOR_LEFT
@ SENSOR_LEFT
Definition: joysensordirection.h:30
JoyButton::copyAssignments
void copyAssignments(JoyButton *destButton)
Copy assignments and properties from one button to another. Used for set copying.
Definition: joybutton.cpp:4083
JoySensor::m_calibration_value
double m_calibration_value[3]
Definition: joysensor.h:148
JoySensor::sensorDelayChanged
void sensorDelayChanged(int value)
GlobalVariables::JoySensor::DEFAULTDIAGONALRANGE
static const int DEFAULTDIAGONALRANGE
Definition: globalvariables.h:234
JoySensor::setCalibration
virtual void setCalibration(double offsetX, double offsetY, double offsetZ)=0
JoySensorType
JoySensorType
Definition: joysensortype.h:23
JoySensor::reset
virtual void reset()
Resets internal variables back to default.
Definition: joysensor.cpp:578
JoyButton::joyEvent
void joyEvent(bool pressed, bool ignoresets=false)
Activates mapped slots and generates QT events which highlight pressed controller buttons.
Definition: joybutton.cpp:176
JoySensor::calculateZDistanceFromDeadZone
double calculateZDistanceFromDeadZone() const
Get current Z distance of the sensor past the assigned dead zone.
Definition: joysensor.cpp:377
JoySensor::released
void released(float xaxis, float yaxis, float zaxis)
JoySensor::setSensorName
void setSensorName(QString tempName)
Sets the name of this sensor.
Definition: joysensor.cpp:663
JoySensor::radToDeg
static double radToDeg(double value)
Utility function which converts a given value from radians to degree.
Definition: joysensor.cpp:519
SENSOR_CENTERED
@ SENSOR_CENTERED
Definition: joysensordirection.h:29
JoySensor::m_active_button
JoySensorButton * m_active_button[ACTIVE_BUTTON_COUNT]
Definition: joysensor.h:143
JoySensor::hasPendingEvent
bool hasPendingEvent() const
Checks if an event is queued.
Definition: joysensor.cpp:152
JoySensor::populateButtons
virtual void populateButtons()=0
JoySensor::hasSlotsAssigned
bool hasSlotsAssigned() const
Check if any direction is mapped to a keyboard or mouse event.
Definition: joysensor.cpp:202
JoySensor::calculateXDistanceFromDeadZone
double calculateXDistanceFromDeadZone() const
Get current X distance of the sensor past the assigned dead zone.
Definition: joysensor.cpp:313
JoySensor::m_max_zone
double m_max_zone
Definition: joysensor.h:138
JoySensor::calculateRoll
double calculateRoll() const
Calculate the roll angle (in degrees) corresponding to the current position of controller.
Definition: joysensor.cpp:455
JoySensor::diagonalRangeChanged
void diagonalRangeChanged(double value)
JoySensor::activatePendingEvent
void activatePendingEvent()
Activates previously queued movement event This is called by InputDevice.
Definition: joysensor.cpp:138
GlobalVariables::JoySensorButton::xmlName
static const QString xmlName
Definition: globalvariables.h:273
SENSOR_DOWN
@ SENSOR_DOWN
Definition: joysensordirection.h:33
JoySensor::determineSensorEvent
void determineSensorEvent(JoySensorButton **eventbutton) const
Set buttons for current sensor direction zone.
Definition: joysensor.cpp:800
JoySensor::readConfig
void readConfig(QXmlStreamReader *xml)
Take a XML stream and set the sensor and direction button properties according to the values containe...
Definition: joysensor.cpp:682
JoySensorButton
Represents a sensor mapping in a SetJoystick.
Definition: joysensorbutton.h:30
JoySensor::setMaxZone
void setMaxZone(double value)
Sets the maximum zone of the sensor to the given value.
Definition: joysensor.cpp:614
JoySensorButton::getDirection
JoySensorDirection getDirection() const
returns the direction of this button.
Definition: joysensorbutton.cpp:157
JoySensor::resetCalibration
void resetCalibration()
Resets the calibration of the sensor back to uncalibrated state.
Definition: joysensor.cpp:535
JoySensor::getCalibration
virtual void getCalibration(double *offsetX, double *offsetY, double *offsetZ) const =0
inputdevice.h
JoySensor::m_current_direction
JoySensorDirection m_current_direction
Definition: joysensor.h:156
JoySensor::sensorNameChanged
void sensorNameChanged()
JoySensor::getType
JoySensorType getType() const
Returns the sensor type.
Definition: joysensor.cpp:247
JoySensor::m_active
bool m_active
Definition: joysensor.h:141
JoySensor::m_type
JoySensorType m_type
Definition: joysensor.h:135
JoySensor::calculateSensorDirection
virtual JoySensorDirection calculateSensorDirection()=0
JoySensor::m_originset
int m_originset
Definition: joysensor.h:152
JoyButtonXml::writeConfig
virtual void writeConfig(QXmlStreamWriter *xml)
Definition: joybuttonxml.cpp:342
joysensortype.h
JoySensor::m_parent_set
SetJoystick * m_parent_set
Definition: joysensor.h:157
JoySensor::getCurrentDirection
JoySensorDirection getCurrentDirection() const
Returns the current sensor direction.
Definition: joysensor.cpp:252
JoySensor::ACTIVE_BUTTON_COUNT
static const size_t ACTIVE_BUTTON_COUNT
Definition: joysensor.h:142
JoySensor::applyCalibration
virtual void applyCalibration()=0
GlobalVariables::JoySensor::GYRO_MAX
static const double GYRO_MAX
Definition: globalvariables.h:232
GlobalVariables::JoySensor::ACCEL_MAX
static const double ACCEL_MAX
Definition: globalvariables.h:230
JoySensor::m_sensor_name
QString m_sensor_name
Definition: joysensor.h:153
SENSOR_UP
@ SENSOR_UP
Definition: joysensordirection.h:32
JoySensorDirection
JoySensorDirection
A bitfield style enum which encodes all possible three dimensional sensor directions....
Definition: joysensordirection.h:27
JoySensor::getMaxZone
double getMaxZone() const
Get the assigned max zone value.
Definition: joysensor.cpp:270
JoySensor::writeConfig
void writeConfig(QXmlStreamWriter *xml) const
Write the status of the properties of a sensor and direction buttons to an XML stream.
Definition: joysensor.cpp:738
JoySensor::getXCoordinate
virtual float getXCoordinate() const =0
JoySensor
Represents one sensor in a SetJoystick and its connections to other parts of the application....
Definition: joysensor.h:39
SENSOR_RIGHT
@ SENSOR_RIGHT
Definition: joysensordirection.h:31
JoySensor::m_buttons
QHash< JoySensorDirection, JoySensorButton * > m_buttons
Definition: joysensor.h:158
JoySensor::m_delay_timer
QTimer m_delay_timer
Definition: joysensor.h:154
joysensorbutton.h
joysensordirection.h
JoySensor::m_current_value
float m_current_value[3]
Definition: joysensor.h:145
JoySensor::getSensorName
QString getSensorName() const
Returns the sensor name.
Definition: joysensor.cpp:242
InputDevice::profileEdited
void profileEdited()
Definition: inputdevice.cpp:1207
JoySensor::getSensorDelay
unsigned int getSensorDelay() const
Get the assigned input delay.
Definition: joysensor.cpp:276
JoySensor::JoySensor
JoySensor(JoySensorType type, int originset, SetJoystick *parent_set, QObject *parent)
Definition: joysensor.cpp:29
JoySensor::getDirectionButton
JoySensorButton * getDirectionButton(JoySensorDirection direction)
Get a pointer to the sensor direction button for the desired direction.
Definition: joysensor.cpp:550
JoySensor::m_diagonal_range
double m_diagonal_range
Definition: joysensor.h:137
JoySensor::copyAssignments
void copyAssignments(JoySensor *dest_sensor)
Copy slots from all sensor buttons and properties from a sensor onto another.
Definition: joysensor.cpp:168
SENSOR_BWD
@ SENSOR_BWD
Definition: joysensordirection.h:35
JoySensor::getParentSet
SetJoystick * getParentSet() const
Get pointer to the set that a sensor belongs to.
Definition: joysensor.cpp:774
JoyButtonXml
Definition: joybuttonxml.h:27
number
it is up to the author donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License If the distribution and or use of the Library is restricted in certain countries either by patents or by copyrighted the original copyright holder who places the Library under this License may add an geographical distribution limitation excluding those so that distribution is permitted only in or among countries not thus excluded In such this License incorporates the limitation as if written in the body of this License The Free Software Foundation may publish revised and or new versions of the Lesser General Public License from time to time Such new versions will be similar in spirit to the present but may differ in detail to address new problems or concerns Each version is given a distinguishing version number If the Library specifies a version number of this License which applies to it and any later you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation If the Library does not specify a license version number
Definition: lgpl-2.1.txt:423
JoySensor::getDeadZone
double getDeadZone() const
Get the assigned dead zone value.
Definition: joysensor.cpp:258
JoySensor::propertyUpdated
void propertyUpdated()
JoySensor::degToRad
static double degToRad(double value)
Utility function which converts a given value from degree to radians.
Definition: joysensor.cpp:524
JoySensor::setDiagonalRange
void setDiagonalRange(double value)
Set the diagonal range value for a sensor.
Definition: joysensor.cpp:629
JoySensor::maxZoneChanged
void maxZoneChanged(double value)
GlobalVariables::JoySensor::DEFAULTDEADZONE
static const double DEFAULTDEADZONE
Definition: globalvariables.h:233
JoySensor::getYCoordinate
virtual float getYCoordinate() const =0
JoySensor::active
void active(float xaxis, float yaxis, float zaxis)
JoySensor::m_sensor_delay
unsigned int m_sensor_delay
Definition: joysensor.h:139
ACCELEROMETER
@ ACCELEROMETER
Definition: joysensortype.h:25
JoySensor::inDeadZone
bool inDeadZone(float *values) const
Checks if the sensor vector is currently in the dead zone.
Definition: joysensor.cpp:282
JoySensor::establishPropertyUpdatedConnection
void establishPropertyUpdatedConnection()
Definition: joysensor.cpp:672
JoySensor::getPartialName
QString getPartialName(bool forceFullFormat=false, bool displayNames=false) const
Get the name of this sensor.
Definition: joysensor.cpp:219
JoySensor::getButtons
QHash< JoySensorDirection, JoySensorButton * > * getButtons()
Returns a QHash which maps the SensorDirection to the corresponding JoySensorButton.
Definition: joysensor.cpp:541