forked from vrpn/vrpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vrpn_CHProducts_Controller_Raw.C
360 lines (327 loc) · 9.6 KB
/
vrpn_CHProducts_Controller_Raw.C
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// vrpn_CHProducts_Controller_Raw.C: VRPN driver for CHProducts Controller Raw devices
#include <stdio.h> // for fprintf, stderr, NULL
#include <string.h> // for memset
#include <math.h> // for sqrt and fabs
#include "vrpn_CHProducts_Controller_Raw.h"
VRPN_SUPPRESS_EMPTY_OBJECT_WARNING()
#if defined(VRPN_USE_HID)
// USB vendor and product IDs for the models we support
static const vrpn_uint16 CHPRODUCTS_VENDOR = 0x068e;
static const vrpn_uint16 FIGHTERSTICK_USB = 0x00f3;
static const double POLL_INTERVAL = 1e+6 / 30.0; // If we have not heard, ask.
#define GAMEPAD_TRIGGER_THRESHOLD 30
//////////////////////////////////////////////////////////////////////////
// helpers
//////////////////////////////////////////////////////////////////////////
static vrpn_float64 normalize_dpad(unsigned char up, unsigned char right, unsigned char down, unsigned char left)
{
int x = 0;
int y = 0;
if (right) {
x += 1;
}
if (left) {
x -= 1;
}
if (up) {
y += 1;
}
if (down) {
y -= 1;
}
size_t index = ((x + 1) * 3) + (y + 1);
vrpn_float64 angles[] = {225, 270, 315, 180, -1, 0, 135, 90, 45};
return (angles[index]);
}
static void normalize_axis(const unsigned int value, const short deadzone, const vrpn_float64 scale, vrpn_float64& channel, int wordSize = 16)
{
channel = (static_cast<float>(value) - (float) (1 << (wordSize - 1)));
if (fabs(channel) < (deadzone * 3 / 4))
{
channel = 0.0f;
}
else
{
channel /= (float) (1 << (wordSize - 1));
}
channel *= scale;
if (channel < -1.0) { channel = -1.0; }
if (channel > 1.0) { channel = 1.0; }
}
static void normalize_axes(const unsigned int x, const unsigned int y, const short deadzone, const vrpn_float64 scale, vrpn_float64& channelX, vrpn_float64& channelY, int wordSize = 16)
{
normalize_axis(x, deadzone, scale, channelX, wordSize);
normalize_axis(y, deadzone, scale, channelY, wordSize);
}
static vrpn_float64 normalize_trigger(unsigned int trigger)
{
// Filter out low-intensity signals
int value = trigger - 0x80;
return ((fabs(static_cast<double>(value)) < GAMEPAD_TRIGGER_THRESHOLD) ? 0.0f : (value * 2.0f / 255.0f));
}
//////////////////////////////////////////////////////////////////////////
// Common base class
//////////////////////////////////////////////////////////////////////////
vrpn_CHProducts_Controller_Raw::vrpn_CHProducts_Controller_Raw(vrpn_HidAcceptor *filter, const char *name, vrpn_Connection *c) :
vrpn_HidInterface(filter)
, vrpn_BaseClass(name, c)
, _filter(filter)
{
init_hid();
}
vrpn_CHProducts_Controller_Raw::~vrpn_CHProducts_Controller_Raw(void)
{
delete _filter;
}
void vrpn_CHProducts_Controller_Raw::init_hid()
{
// Get notifications when clients connect and disconnect
register_autodeleted_handler(d_connection->register_message_type(vrpn_dropped_last_connection), on_last_disconnect, this);
register_autodeleted_handler(d_connection->register_message_type(vrpn_got_connection), on_connect, this);
}
void vrpn_CHProducts_Controller_Raw::on_data_received(size_t bytes, vrpn_uint8 *buffer)
{
decodePacket(bytes, buffer);
}
int vrpn_CHProducts_Controller_Raw::on_last_disconnect(void *thisPtr, vrpn_HANDLERPARAM /*p*/)
{
vrpn_CHProducts_Controller_Raw* me = static_cast<vrpn_CHProducts_Controller_Raw*>(thisPtr);
return (0);
}
int vrpn_CHProducts_Controller_Raw::on_connect(void* thisPtr, vrpn_HANDLERPARAM /*p*/)
{
vrpn_CHProducts_Controller_Raw* me = static_cast<vrpn_CHProducts_Controller_Raw*>(thisPtr);
return (0);
}
//////////////////////////////////////////////////////////////////////////
// ST290 Pro Joystick
//////////////////////////////////////////////////////////////////////////
vrpn_CHProducts_Fighterstick_USB::vrpn_CHProducts_Fighterstick_USB(const char *name, vrpn_Connection *c) :
vrpn_CHProducts_Controller_Raw(_filter = new vrpn_HidProductAcceptor(CHPRODUCTS_VENDOR, FIGHTERSTICK_USB), name, c),
vrpn_Button_Filter(name, c), vrpn_Analog(name, c), vrpn_Dial(name, c)
{
vrpn_Analog::num_channel = 8;
vrpn_Dial::num_dials = 0;
vrpn_Button::num_buttons = 24;
// Initialize the state of all the analogs, buttons, and dials
memset(buttons, 0, sizeof(buttons));
memset(lastbuttons, 0, sizeof(lastbuttons));
memset(channel, 0, sizeof(channel));
memset(last, 0, sizeof(last));
}
void vrpn_CHProducts_Fighterstick_USB::mainloop(void)
{
update();
server_mainloop();
struct timeval current_time;
vrpn_gettimeofday(¤t_time, NULL);
if (vrpn_TimevalDuration(current_time, _timestamp) > POLL_INTERVAL)
{
_timestamp = current_time;
report_changes();
vrpn_Analog::server_mainloop();
vrpn_Button::server_mainloop();
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::server_mainloop();
}
}
}
void vrpn_CHProducts_Fighterstick_USB::report(vrpn_uint32 class_of_service)
{
vrpn_Analog::timestamp = _timestamp;
vrpn_Button::timestamp = _timestamp;
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::timestamp = _timestamp;
}
vrpn_Analog::report_changes(class_of_service);
vrpn_Button::report_changes();
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::report();
}
}
void vrpn_CHProducts_Fighterstick_USB::report_changes(vrpn_uint32 class_of_service)
{
vrpn_Analog::timestamp = _timestamp;
vrpn_Button::timestamp = _timestamp;
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::timestamp = _timestamp;
}
vrpn_Analog::report(class_of_service);
vrpn_Button::report_changes();
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::report();
}
}
void vrpn_CHProducts_Fighterstick_USB::decodePacket(size_t bytes, vrpn_uint8 *buffer)
{
// Fighterstick USB joystick
// Decode all full reports, each of which is 6 bytes long.
// Because there is only one type of report, the initial "0" report-type
// byte is removed by the HIDAPI driver.
/*
[0]: X-axis (left=00, right=ff, center=80)
[1]: Y-axis (up=00, down=ff, center=80)
[2]: throttle wheel (up=00, down=ff)
[3]: buttons high nibble: 0x10=trigger, 0x20=top red, 0x40=upper index red (toggles buttons 17-19), 0x80=pinky red,
- - low nibble 8-way POV hat upper-right on top: none=0x00, N=0x01, NE=0x02, ... NW=0x08
[4]: high nibble 4-way hat #2 (lower-right on top): none=0x00, N=0x10, E=0x20, S=0x40, W=0x80
- - low nibble 4-way hat #1 (left on top): none=0x00, N=0x01, E=0x02, S=0x04, W=0x08
[5]: upper nibble mode bits: 0x10=green, 0x20=red, 0x40=yellow, 0x80=<not used>
- - low nibble 4-way hat #3 (thumb): none=0x00, N=0x01, E=0x02, S=0x04, W=0x08
*/
// XXX Check to see that this works with HIDAPI, there may be two smaller reports.
if (bytes == 6)
{
normalize_axes(buffer[0], buffer[1], 0x08, 1.0f, channel[0], channel[1], 8);
normalize_axis(buffer[2], 0x08, 1.0f, channel[2], 8);
vrpn_uint8 value, mask;
value = (buffer[3] >> 4);
for (int btn = 0; btn < 4; btn++)
{
mask = static_cast<vrpn_uint8>(1 << (btn % 8));
buttons[btn] = ((value & mask) != 0);
}
// Point of View Hat
buttons[4] = buttons[5] = buttons[6] = buttons[7] = 0;
switch (buffer[3] & 0x0f)
{
case 1: // up
buttons[4] = true;
break;
case 2:
buttons[4] = buttons[5] = true;
break;
case 3: // right
buttons[5] = true;
break;
case 4:
buttons[5] = buttons[6] = true;
break;
case 5: // down
buttons[6] = true;
break;
case 6:
buttons[6] = buttons[7] = true;
break;
case 7: // left
buttons[7] = true;
break;
case 8:
buttons[7] = buttons[4] = true;
break;
case 0:
default:
// nothing to do
break;
}
channel[3] = normalize_dpad(buttons[4], buttons[5], buttons[6], buttons[7]);
// 4-way Hat #2
buttons[8] = buttons[9] = buttons[10] = buttons[11] = 0;
switch (buffer[4] >> 4)
{
case 1: // up
buttons[8] = true;
break;
case 2: // right
buttons[9] = true;
break;
case 4: // down
buttons[10] = true;
break;
case 8: // left
buttons[11] = true;
break;
case 0:
default:
// nothing to do
break;
}
channel[4] = normalize_dpad(buttons[8], buttons[9], buttons[10], buttons[11]);
// 4-way Hat #1
buttons[12] = buttons[13] = buttons[14] = buttons[15] = 0;
switch (buffer[4] & 0x0f)
{
case 1: // up
buttons[12] = true;
break;
case 2: // right
buttons[13] = true;
break;
case 4: // down
buttons[14] = true;
break;
case 8: // left
buttons[15] = true;
break;
case 0:
default:
// nothing to do
break;
}
channel[5] = normalize_dpad(buttons[12], buttons[13], buttons[14], buttons[15]);
// mode
// keep pseudo-button pressed to indicate mode
//buttons[16] = buttons[17] = buttons[18] = buttons[19] = 0;
switch (buffer[5] >> 4)
{
case 1: // green
buttons[16] = true;
buttons[17] = buttons[18] = false;
break;
case 2: // red
buttons[17] = true;
buttons[16] = buttons[18] = false;
break;
case 4: // yellow
buttons[18] = true;
buttons[16] = buttons[17] = false;
break;
/*
case 8: // none
buttons[19] = true;
break;
*/
case 0:
default:
// nothing to do
break;
}
if (buttons[16] || buttons[17] || buttons[18] || buttons[19])
{
channel[6] = (normalize_dpad(buttons[16], buttons[17], buttons[18], buttons[19]) / 90.0f) + 1.0f;
}
// 4-way Hat #3
buttons[20] = buttons[21] = buttons[22] = buttons[23] = 0;
switch (buffer[5] & 0x0f)
{
case 1: // up
buttons[20] = true;
break;
case 2: // right
buttons[21] = true;
break;
case 4: // down
buttons[22] = true;
break;
case 8: // left
buttons[23] = true;
break;
case 0:
default:
// nothing to do
break;
}
channel[7] = normalize_dpad(buttons[20], buttons[21], buttons[22], buttons[23]);
}
else
{
fprintf(stderr, "vrpn_CHProducts_Fighterstick_USB: Found a corrupted report; # total bytes = %u\n", static_cast<unsigned>(bytes));
}
}
// End of VRPN_USE_HID
#endif