Skip to content

Commit 4d0333d

Browse files
author
Yann Vernier
committedSep 10, 2017
Collect data field offsets in enums for clarity.
1 parent c1be595 commit 4d0333d

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed
 

‎com_osvr_Nolo.cpp

+38-15
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,22 @@ class NoloDevice {
204204
osvrQuatSetZ(quat, -j);
205205
}
206206
void decodeControllerCV1(int idx, unsigned char *data) {
207+
enum ControllerOffsets {
208+
hwversion = 0, // guessed!
209+
fwversion = 1,
210+
position = 3,
211+
orientation = 3+3*2,
212+
ofsbuttons = 3+3*2+4*2,
213+
touchid = 3+3*2+4*2+1,
214+
touchx = 3+3*2+4*2+2,
215+
touchy = 3+3*2+4*2+3,
216+
battery = 3+3*2+4*2+4,
217+
};
207218
OSVR_PoseState pose;
208219
uint8_t buttons, bit;
209220
int trigger_pressed = 0;
210221

211-
if (data[0] != 2 || data[1] != 1) {
222+
if (data[hwversion] != 2 || data[fwversion] != 1) {
212223
// Unknown version
213224
/* Happens when controllers aren't on.
214225
std::cout << "Nolo: Unknown controller " << idx
@@ -218,12 +229,12 @@ class NoloDevice {
218229
return;
219230
}
220231

221-
decodePosition(data+3, &pose.translation);
222-
decodeOrientation(data+3+3*2, &pose.rotation);
232+
decodePosition(data+position, &pose.translation);
233+
decodeOrientation(data+orientation, &pose.rotation);
223234

224235
osvrDeviceTrackerSendPoseTimestamped(m_dev, m_tracker, &pose, 2+idx, &m_lastreport_time);
225236

226-
buttons = data[3+3*2+4*2];
237+
buttons = data[ofsbuttons];
227238
// TODO: report buttons for both controllers in one call?
228239
for (bit=0; bit<NUM_BUTTONS; bit++){
229240
osvrDeviceButtonSetValueTimestamped(m_dev, m_button,
@@ -249,23 +260,30 @@ class NoloDevice {
249260
// normalize 0 to 1
250261
// x/255
251262
double axis_value;
252-
if (data[3+3*2+4*2+1]) { // Only report touch if there is one
253-
axis_value = 2*data[3+3*2+4*2+2]/255.0 - 1;
263+
if (data[touchid]) { // Only report touch if there is one
264+
axis_value = 2*data[touchx]/255.0 - 1;
254265
// invert axis
255266
axis_value *= -1;
256267
osvrDeviceAnalogSetValueTimestamped(m_dev, m_analog, axis_value, idx*NUM_AXIS+0, &m_lastreport_time);
257-
axis_value = 2*((int)data[3+3*2+4*2+2+1])/255.0 -1;
268+
axis_value = 2*((int)data[touchy])/255.0 -1;
258269
axis_value *= -1;
259270
osvrDeviceAnalogSetValueTimestamped(m_dev, m_analog, axis_value, idx*NUM_AXIS+1, &m_lastreport_time);
260271
}
261-
// trigger
272+
// trigger (emulated analog axis)
262273
osvrDeviceAnalogSetValueTimestamped(m_dev, m_analog, trigger_pressed, idx*NUM_AXIS+2, &m_lastreport_time);
263274
// battery level
264-
axis_value = data[3+3*2+4*2+2+2]/255.0;
275+
axis_value = data[battery]/255.0;
265276
osvrDeviceAnalogSetValueTimestamped(m_dev, m_analog, axis_value, idx*NUM_AXIS+3, &m_lastreport_time);
266277
}
267278
void decodeHeadsetMarkerCV1(unsigned char *data) {
268-
if (data[0] != 2 || data[1] != 1) {
279+
enum MarkerOffsets {
280+
hwversion = 0, // guessed!
281+
fwversion = 1,
282+
position = 3,
283+
homeposition = 3+3*2,
284+
orientation = 3+2*3*2+1,
285+
};
286+
if (data[hwversion] != 2 || data[fwversion] != 1) {
269287
/* Happens with corrupt packages (mixed with controller data)
270288
std::cout << "Nolo: Unknown headset marker"
271289
<< " version " << (int)data[0] << " " << (int)data[1]
@@ -278,9 +296,9 @@ class NoloDevice {
278296
OSVR_PositionState home;
279297
OSVR_PoseState hmd;
280298

281-
decodePosition(data+3, &hmd.translation);
282-
decodePosition(data+3+3*2, &home);
283-
decodeOrientation(data+3+2*3*2+1, &hmd.rotation);
299+
decodePosition(data+position, &hmd.translation);
300+
decodePosition(data+homeposition, &home);
301+
decodeOrientation(data+orientation, &hmd.rotation);
284302

285303
// Tracker viewer kept using the home for head.
286304
// Something wrong with how they handle the descriptors.
@@ -291,11 +309,16 @@ class NoloDevice {
291309
&m_lastreport_time);
292310
}
293311
void decodeBaseStationCV1(unsigned char *data) {
294-
if (data[0] != 2 || data[1] != 1)
312+
enum MarkerOffsets {
313+
hwversion = 0, // guessed!
314+
fwversion = 1,
315+
battery = 2
316+
};
317+
if (data[hwversion] != 2 || data[fwversion] != 1)
295318
// Unknown version
296319
return;
297320

298-
osvrDeviceAnalogSetValueTimestamped(m_dev, m_analog, data[2], 2*NUM_AXIS,
321+
osvrDeviceAnalogSetValueTimestamped(m_dev, m_analog, data[battery], 2*NUM_AXIS,
299322
&m_lastreport_time);
300323
}
301324

0 commit comments

Comments
 (0)
Please sign in to comment.