-
Notifications
You must be signed in to change notification settings - Fork 2
/
qjoystickinterface.cpp
179 lines (158 loc) · 4.11 KB
/
qjoystickinterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "qjoystickinterface.h"
#include <QtGlobal>
QJoystickInterface::QJoystickInterface()
{
connect(&mTimer, SIGNAL(timeout()), this, SLOT(getData()));
mTimer.start(POLL_INTERVAL);
}
QJoystickInterface::~QJoystickInterface()
{
}
bool QJoystickInterface::currentJoystick(const int id)
{
bool ret = false;
if(0 <= id && id < numJoysticks())
{
mCurrentId = id;
init(); //update js model
ret = true;
}
return ret;
}
int QJoystickInterface::currentJoystick() const
{
return mCurrentId;
}
void QJoystickInterface::bilinearEnable(bool enable)
{
mBilinearEnabled = enable;
}
bool QJoystickInterface::bilinearEnabled() const
{
return mBilinearEnabled;
}
void QJoystickInterface::bilinearConstant(double k)
{
mBilinearConstant = k;
}
double QJoystickInterface::bilinearConstant() const
{
return mBilinearConstant;
}
bool QJoystickInterface::deadzone(int axisId, int size)
{
bool ret = false;
if(0 <= axisId && axisId < mDeadzones.count() &&
0 <= size && size <= 32767)
{
mDeadzones[axisId] = size;
ret = true;
}
return ret;
}
int QJoystickInterface::deadzone(int axisId) const
{
int ret = -1;
if(0 <= axisId && axisId < mDeadzones.count())
{
ret = mDeadzones[axisId];
}
return ret;
}
void QJoystickInterface::init()
{
//Reset axes information
mDeadzones.clear();
mAxesCurrent.clear();
for(int i=0; i<numAxes(); i++)
{
mDeadzones.append(0);
mAxesCurrent.append(0);
}
//Reset button information
mButtonsCurrent.clear();
mButtonsPrevious.clear();
mButtonsToggled.clear();
for(int i=0; i<numButtons(); i++)
{
mButtonsCurrent.append(false);
mButtonsPrevious.append(false);
mButtonsToggled.append(false);
}
//Reset hat information
mHatsCurrent.clear();
mHatsPrevious.clear();
mHatsToggled.clear();
for(int i=0; i<numHats(); i++)
{
QMap<int, bool> temp;
mHatsCurrent.append(temp);
mHatsPrevious.append(temp);
mHatsToggled.append(temp);
}
mBilinearEnabled = false;
mBilinearConstant = 1;
}
void QJoystickInterface::processDeadzones()
{
for(int i=0; i<mAxesCurrent.count(); i++)
{
int dz = mDeadzones[i];
if(qAbs(mAxesCurrent[i]) <= dz)
{
mAxesCurrent[i] = 0;
}
else
{
const int posMax = 32767;
const int negMax = -32768;
double slope;
int b;
if(mAxesCurrent[i] > 0)
{
slope = (double)posMax/(posMax-dz);
b = -1 * slope * dz;
}
else
{
slope = (double)negMax/(negMax+dz);
b = slope * dz;
}
//Calculate the new value
mAxesCurrent[i] = (int)(mAxesCurrent[i] * slope) + b;
}
}
}
void QJoystickInterface::processBilinear()
{
if(bilinearEnabled())
{
Q_ASSERT(mBilinearConstant > 0);
double bilinearThreshold = 1.0/mBilinearConstant;
const int min = -32768;
const int max = 32767;
for(int i=0; i<mAxesCurrent.count(); i++)
{
Q_ASSERT(min <= mAxesCurrent[i]);
Q_ASSERT(mAxesCurrent[i] <= max);
int cur = mAxesCurrent[i];
//If the stick is within the range
if(bilinearThreshold * min <= cur && cur <= bilinearThreshold * max)
{
mAxesCurrent[i] = (mAxesCurrent[i] / mBilinearConstant);
}
//If the stick is in the upper section of the range
else if((bilinearThreshold * max) < cur && cur <= max)
{
mAxesCurrent[i] = (mBilinearConstant * cur) +
((mBilinearConstant * min) + max) + 2;
}
//If the stick is in the lower section of the range
else if(min <= cur && cur < (bilinearThreshold * min))
{
mAxesCurrent[i] = (mBilinearConstant * cur) +
((mBilinearConstant * max) + min) + 1;
}
}
}
}