forked from vrpn/vrpn
/
vrpn_Contour.C
357 lines (317 loc) · 8.92 KB
/
vrpn_Contour.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
// vrpn_Contour.C: VRPN driver for Contour Design devices
#include <stdio.h> // for fprintf, stderr, NULL
#include <string.h> // for memset
#include <math.h> // for fabs
#include "vrpn_Contour.h"
VRPN_SUPPRESS_EMPTY_OBJECT_WARNING()
#if defined(VRPN_USE_HID)
static const double POLL_INTERVAL = 1e+6 / 30.0; // If we have not heard, ask.
// USB vendor and product IDs for the models we support
static const vrpn_uint16 CONTOUR_VENDOR = 0x0b33;
static const vrpn_uint16 CONTOUR_SHUTTLEXPRESS = 0x0020;
static const vrpn_uint16 CONTOUR_SHUTTLEPROV2 = 0x0030;
static void normalize_axis(const unsigned int value, const short deadzone, const vrpn_float64 scale, vrpn_float64& channel) {
channel = (static_cast<float>(value) - 128.0f);
if (fabs(channel) < deadzone)
{
channel = 0.0f;
}
else
{
channel /= 128.0f;
}
channel *= scale;
if (channel < -1.0) { channel = -1.0; }
if (channel > 1.0) { channel = 1.0; }
}
vrpn_Contour::vrpn_Contour(vrpn_HidAcceptor *filter, const char *name, vrpn_Connection *c)
: vrpn_BaseClass(name, c)
, vrpn_HidInterface(filter)
, _filter(filter)
{
init_hid();
// We've not yet gotten a dial response.
_gotDial = false;
}
vrpn_Contour::~vrpn_Contour(void)
{
delete _filter;
}
void vrpn_Contour::init_hid(void) {
// 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_Contour::on_data_received(size_t bytes, vrpn_uint8 *buffer)
{
decodePacket(bytes, buffer);
}
int vrpn_Contour::on_last_disconnect(void* /*thisPtr*/, vrpn_HANDLERPARAM /*p*/)
{
return 0;
}
int vrpn_Contour::on_connect(void* /*thisPtr*/, vrpn_HANDLERPARAM /*p*/)
{
return 0;
}
vrpn_Contour_ShuttleXpress::vrpn_Contour_ShuttleXpress(const char *name, vrpn_Connection *c)
: vrpn_Contour(_filter = new vrpn_HidProductAcceptor(CONTOUR_VENDOR, CONTOUR_SHUTTLEXPRESS), name, c)
, vrpn_Analog(name, c)
, vrpn_Button_Filter(name, c)
, vrpn_Dial(name, c)
{
vrpn_Analog::num_channel = 2;
vrpn_Dial::num_dials = 1;
vrpn_Button::num_buttons = 5;
// Initialize the state of all the analogs, buttons, and dials
_lastDial = 0;
memset(buttons, 0, sizeof(buttons));
memset(lastbuttons, 0, sizeof(lastbuttons));
memset(channel, 0, sizeof(channel));
memset(last, 0, sizeof(last));
}
void vrpn_Contour_ShuttleXpress::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();
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::server_mainloop();
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::server_mainloop();
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::server_mainloop();
}
}
}
void vrpn_Contour_ShuttleXpress::report(vrpn_uint32 class_of_service) {
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::timestamp = _timestamp;
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::timestamp = _timestamp;
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::timestamp = _timestamp;
}
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::report(class_of_service);
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::report_changes();
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::report();
}
}
void vrpn_Contour_ShuttleXpress::report_changes(vrpn_uint32 class_of_service) {
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::timestamp = _timestamp;
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::timestamp = _timestamp;
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::timestamp = _timestamp;
}
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::report_changes(class_of_service);
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::report_changes();
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::report_changes();
}
}
void vrpn_Contour_ShuttleXpress::decodePacket(size_t bytes, vrpn_uint8 *buffer) {
// Decode all full reports, each of which is 5 bytes long.
// Because there is only one type of report, the initial "0" report-type
// byte is removed by the HIDAPI driver.
if (bytes == 5) {
if (!_gotDial) {
_gotDial = true;
}
else {
dials[0] = static_cast<vrpn_int8>(buffer[1] - _lastDial) / 10.0;
}
_lastDial = buffer[1];
// analog (1st byte): 0 center, 1..7 right, -1..-7 left
normalize_axis((unsigned int)((static_cast<float>(static_cast<vrpn_int8>(buffer[0])) * 128.0f / 7.0f) + 128.0f), 0, 1.0f, channel[0]);
// Second analog integrates the dial value.
channel[1] += dials[0];
vrpn_uint8 value;
// buttons (4th byte):
value = buffer[3];
for (int btn = 0; btn < 4; btn++) {
vrpn_uint8 mask = static_cast<vrpn_uint8>((1 << (btn % 8)) << 4);
buttons[btn] = ((value & mask) != 0);
}
// buttons (5th byte):
value = buffer[4];
for (int btn = 0; btn < 1; btn++) {
vrpn_uint8 mask = static_cast<vrpn_uint8>(1 << (btn % 8));
buttons[btn + 4] = ((value & mask) != 0);
}
} else {
fprintf(stderr, "vrpn_Contour_ShuttleXpress: Found a corrupted report; # total bytes = %u\n", static_cast<unsigned>(bytes));
}
}
vrpn_Contour_ShuttlePROv2::vrpn_Contour_ShuttlePROv2(const char *name, vrpn_Connection *c)
: vrpn_Contour(_filter = new vrpn_HidProductAcceptor(CONTOUR_VENDOR, CONTOUR_SHUTTLEPROV2), name, c)
, vrpn_Analog(name, c)
, vrpn_Button_Filter(name, c)
, vrpn_Dial(name, c)
{
vrpn_Analog::num_channel = 2;
vrpn_Dial::num_dials = 1;
vrpn_Button::num_buttons = 15;
// Initialize the state of all the analogs, buttons, and dials
_lastDial = 0;
memset(buttons, 0, sizeof(buttons));
memset(lastbuttons, 0, sizeof(lastbuttons));
memset(channel, 0, sizeof(channel));
memset(last, 0, sizeof(last));
}
void vrpn_Contour_ShuttlePROv2::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();
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::server_mainloop();
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::server_mainloop();
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::server_mainloop();
}
}
}
void vrpn_Contour_ShuttlePROv2::report(vrpn_uint32 class_of_service)
{
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::timestamp = _timestamp;
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::timestamp = _timestamp;
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::timestamp = _timestamp;
}
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::report(class_of_service);
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::report_changes();
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::report();
}
}
void vrpn_Contour_ShuttlePROv2::report_changes(vrpn_uint32 class_of_service)
{
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::timestamp = _timestamp;
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::timestamp = _timestamp;
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::timestamp = _timestamp;
}
if (vrpn_Analog::num_channel > 0)
{
vrpn_Analog::report_changes(class_of_service);
}
if (vrpn_Button::num_buttons > 0)
{
vrpn_Button::report_changes();
}
if (vrpn_Dial::num_dials > 0)
{
vrpn_Dial::report_changes();
}
}
void vrpn_Contour_ShuttlePROv2::decodePacket(size_t bytes, vrpn_uint8 *buffer)
{
// Print the report so we can figure out what is going on.
/* for (size_t i = 0; i < bytes; i++) {
printf("%02x ", buffer[i]);
}
printf("\n");
return; */
// Decode all full reports, each of which is 5 bytes long.
// Because there is only one type of report, the initial "0" report-type
// byte is removed by the HIDAPI driver.
if (bytes == 5) {
if (!_gotDial) {
_gotDial = true;
}
else {
dials[0] = static_cast<vrpn_int8>(buffer[1] - _lastDial) / 10.0;
}
_lastDial = buffer[1];
// analog (1st byte): 0 center, 1..7 right, -1..-7 left
normalize_axis((unsigned int)((static_cast<float>(static_cast<vrpn_int8>(buffer[0])) * 128.0f / 7.0f) + 128.0f), 0, 1.0f, channel[0]);
// Second analog integrates the dial value.
channel[1] += dials[0];
// Top row of four buttons, from left to right: index[bit]
// 3[0], 3[1], 3[2], 3[3]
// Second row of 5 buttons, from left to right:
// 3[4], 3[5], 3[6], 3[7], 4[0]
// Four lower buttons, from left to right and then down to next row:
// 4[1], 4[2], 4[3], 4[4]
// Two black buttons, from left to right:
// 4[5], 4[6]
for (int btn = 0; btn <= 15; btn++) {
vrpn_uint8 *offset, mask;
offset = buffer + btn / 8 + 3;
mask = static_cast<vrpn_uint8>(1 << (btn % 8));
buttons[btn] = (*offset & mask) != 0;
}
}
else {
fprintf(stderr, "vrpn_Contour_ShuttlePROv2: Found a corrupted report; # total bytes = %u\n", static_cast<unsigned>(bytes));
}
}
// End of VRPN_USE_HID
#endif