-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsvrapi.cpp
438 lines (380 loc) · 13.4 KB
/
psvrapi.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "psvrapi.h"
namespace PSVRApi
{
/// -----------------------------------------------------------------------
/// \brief PSVRContext::PSVRContext
///
PSVRContext::PSVRContext ()
{
psvrControl = new PSVRControl();
psvrSensor = new PSVRSensor();
}
/// -----------------------------------------------------------------------
/// \brief PSVRContext::~PSVRContext
///
PSVRContext::~PSVRContext ()
{
psvrControl->exit();
// psvrControl->Close(HID_CONTROL);
delete psvrControl;
psvrSensor->exit();
psvrSensor->Close(HID_SENSOR);
delete psvrSensor;
}
/// -----------------------------------------------------------------------
/// \brief Sensor::Sensor
/// \param sensor
///
PSVRSensor::PSVRSensor ()
{
}
/// -----------------------------------------------------------------------
/// \brief PSVRSensor::PSVRSensor
///
PSVRSensor::~PSVRSensor ()
{
}
/// -----------------------------------------------------------------------
/// \brief PSVRSensor::run
/// Thread implementation for sensor
///
void PSVRSensor::run ()
{
struct PSVRSensorFrame frame;
int bytesRead = 0;
bool connected = false;
usleep(2000);
// read reply
while(isRunning())
{
// connect to usb, emit connect signal
if(!connected)
{
if(Open(7, HID_SENSOR))
{
emit connect(true);
connected = true;
}
}
// read from device
if(usbHdl != NULL)
bytesRead += usb_bulk_read(usbHdl, PSVR_EP_SENSOR, (char *)&frame, sizeof(PSVRSensorFrame), 0);
// we got some error from usb, eg. device is disconnected from usb
if (bytesRead < 0)
{
// close connection
if(connected)
{
Close(HID_CONTROL);
connected = false;
emit connect(false);
}
usleep(20);
continue;
}
PSVRSensorData data;
ProcessFrame(frame, data);
}
}
void PSVRSensor::ProcessFrame(PSVRApi::PSVRSensorFrame rawFrame, PSVRApi::PSVRSensorData rawData)
{
// buttons
rawData.volumeUpPressed = rawFrame.buttons & 0x02;
rawData.volumeDownPressed = rawFrame.buttons & 0x04;
rawData.mutePressed = rawFrame.buttons & 0x08;
// volume
rawData.volume = rawFrame.volume;
// status
rawData.isWorn = rawFrame.status & 0x01;
rawData.isDisplayActive = rawFrame.status & 0x02;
rawData.isMicMuted = rawFrame.status & 0x08;
rawData.earphonesConnected = rawFrame.status & 0x10;
rawData.timeStamp_A = rawFrame.timeStampA1 | (rawFrame.timeStampA2 << 8) | (rawFrame.timeStampA3 << 16) | (rawFrame.timeStampA3 << 24);
rawData.rawGyroYaw_A = rawFrame.rawGyroYaw_AL | (rawFrame.rawGyroYaw_AL << 8);
rawData.rawGyroPitch_A = rawFrame.rawGyroPitch_AL | (rawFrame.rawGyroPitch_AL << 8);
rawData.rawGyroRoll_A = rawFrame.rawGyroRoll_AL | (rawFrame.rawGyroRoll_AL << 8);
rawData.rawMotionX_A = (rawFrame.rawMotionX_AL | (rawFrame.rawMotionX_AH << 8)) >> 4;
rawData.rawMotionY_A = (rawFrame.rawMotionY_AL | (rawFrame.rawMotionY_AH << 8)) >> 4;
rawData.rawMotionZ_A = (rawFrame.rawMotionZ_AL | (rawFrame.rawMotionZ_AH << 8)) >> 4;
rawData.timeStamp_B = rawFrame.timeStamp_B1 | (rawFrame.timeStamp_B2 << 8) | (rawFrame.timeStamp_B3 << 16) | (rawFrame.timeStamp_B4 << 24);
rawData.rawGyroYaw_B = rawFrame.rawGyroYaw_BL | (rawFrame.rawGyroYaw_BL << 8);
rawData.rawGyroPitch_B = rawFrame.rawGyroPitch_BL | (rawFrame.rawGyroPitch_BL << 8);
rawData.rawGyroRoll_B = rawFrame.rawGyroRoll_BL | (rawFrame.rawGyroRoll_BL << 8);
rawData.rawMotionX_B = (rawFrame.rawMotionX_BL | (rawFrame.rawMotionX_BH << 8)) >> 4;
rawData.rawMotionY_B = (rawFrame.rawMotionY_BL | (rawFrame.rawMotionY_BH << 8)) >> 4;
rawData.rawMotionZ_B = (rawFrame.rawMotionZ_BL | (rawFrame.rawMotionZ_BH << 8)) >> 4;
rawData.calStatus = rawFrame.calStatus;
rawData.ready = rawFrame.ready;
rawData.voltageValue = rawFrame.voltageValue;
rawData.voltageReference = rawFrame.voltageReference;
rawData.frameSequence = rawFrame.frameSequence;
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::PSVRControl
///
PSVRControl::PSVRControl ()
{
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::~PSVRControl
///
PSVRControl::~PSVRControl ()
{
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::run
/// Thread implementation for control
///
void PSVRControl::emitUnsolicitedReport(PSVRFrame frame)
{
emit unsolicitedReport(frame.data[0], frame.data[1], QByteArray(frame.data[2], 58));
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::run
///
void PSVRControl::run ()
{
PSVRFrame frame;
int bytesRead = 0;
bool connected = false;
while(isRunning())
{
// connect to usb, emit connect signal
if(!connected)
{
if(Open(8, HID_CONTROL))
{
//
ReadInfo();
HeadSetPower(true);
EnableCinematic();
emit connect(true);
connected = true;
}
}
if(usbHdl != NULL)
bytesRead += usb_bulk_read(usbHdl, 132, (char *)&frame, sizeof(PSVRFrame), 0);
// we got some error from usb, eg. device is disconnected from usb
if (bytesRead < 0)
{
// close connection
if(connected)
{
Close(HID_CONTROL);
connected = false;
emit connect(false);
}
usleep(20);
continue;
}
processFrame(frame);
bytesRead = 0;
}
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::processFrame
/// \param frame
///
void PSVRControl::processFrame(PSVRFrame frame)
{
switch(frame.id)
{
case PSVR_INFO_REPORT :
emitInfoReport(frame);
break;
case PSVR_STATUS_REPORT :
emitStatusReport(frame);
break;
case PSVR_UNSOLICITED_REPORT :
emitUnsolicitedReport(frame);
break;
default:
break;
}
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::HeadSetPower
/// \param OnOff
/// \return
///
bool PSVRControl::HeadSetPower (bool OnOff)
{
PSVRFrame sendCmd = {0x17, 0x00, 0xAA, 4, (byte)OnOff};
return SendCommand(&sendCmd);
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::EnableVR
/// \param WithTracking
/// \return
///
bool PSVRControl::EnableVR (bool WithTracking)
{
// generate packet for shutdown
PSVRFrame sendCmd;
// clear memory
memset(&sendCmd, 0x00, sizeof(sendCmd));
if(WithTracking)
{
// without tracking
sendCmd.id = 0x11;
sendCmd.start = 0xAA;
sendCmd.length = 8;
// payload
sendCmd.data[1] = 0xFF;
sendCmd.data[2] = 0xFF;
sendCmd.data[3] = 0xFF;
}
else
{
// without tracking
sendCmd.id = 0x23;
sendCmd.start = 0xAA;
sendCmd.length = sizeof(int);
// payload
sendCmd.data[0] = 1; // VRWithoutTracking = 1 / Cinematic = 0
}
// send shutdown command and receive reply
return SendCommand(&sendCmd);
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::EnableCinematic
/// \return
///
bool PSVRControl::EnableCinematic()
{
// generate packet for shutdown
PSVRFrame sendCmd = {0x23, 0x00, 0xAA, 4, 0}; // VR = 1 / Cinematic = 0
return SendCommand(&sendCmd);
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::SetCinematic
/// \param distance
/// \param size
/// \param brightness
/// \param micVolume
/// \return
///
bool PSVRControl::SetCinematic(byte distance, byte size, byte brightness, byte micVolume)
{
PSVRFrame Cmd = {0x21, 0x00, 0xAA, 16, 0xC0, distance, size, 0x14, 0, 0, 0, 0, 0, 0, brightness, micVolume, 0, 0, 0, 0 };
return SendCommand(&Cmd);
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::Recenter
/// \return
///
bool PSVRControl::Recenter ()
{
return true;
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::Shutdown
/// \return
///
bool PSVRControl::Shutdown ()
{
// shutdown command
PSVRFrame Cmd = {0x13, 0x00, 0xAA, 4, 1};
return SendCommand(&Cmd);
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::ReadInfo
/// \return
///
bool PSVRControl::ReadInfo ()
{
// read info command
PSVRFrame Cmd = {0x81, 0x00, 0xAA, 8, 0x80};
return SendCommand(&Cmd);
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::emitInfoReport
/// \param frame
///
void PSVRControl::emitInfoReport(PSVRFrame frame)
{
QString firmware;
QString serial;
firmware = QString(frame.data[7] + 0x30) + "." + QString(frame.data[8] + 0x30);
serial = QByteArray((char *)&frame.data[12], 16);
emit infoReport(firmware, serial);
}
/// -----------------------------------------------------------------------
/// \brief PSVRControl::emitStatusReport
/// \param frame
///
void PSVRControl::emitStatusReport(PSVRFrame frame)
{
PSVRStatus *status = new PSVRStatus;
status->isHeadsetOn = frame.data[0] & (1 << 0);
status->isHeadsetWorn = frame.data[0] & (1 << 1);
status->isCinematic = frame.data[0] & (1 << 2);
status->areHeadphonesUsed = frame.data[0] & (1 << 4);
status->isMuted = frame.data[0] & (1 << 5);
status->isCECUsed = frame.data[0] & (1 << 7);
status->volume = frame.data[1] | (frame.data[2] << 8) | (frame.data[3] << 16) | (frame.data[4] << 24);
emit statusReport((void *)status);
}
/// -----------------------------------------------------------------------
/// \brief PSVRCommon::Open
/// \param productNumber
/// \return
///
bool PSVRCommon::Open(int productNumber, PSVR_USB_INTERFACE usbInterface)
{
usb_init();
usb_find_busses();
usb_find_devices();
usbHdl = NULL;
struct usb_bus *bus;
struct usb_device *dev;
// search for PSVR
for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if (dev->descriptor.idVendor == PSVR_VID && dev->descriptor.idProduct == PSVR_PID)
{
if(dev->descriptor.iProduct == productNumber)
{
// open PSVR Control Interface
usbHdl = usb_open(dev);
if(usbHdl && !usb_claim_interface(usbHdl, usbInterface))
continue;
}
}
}
}
if(usbHdl != NULL)
{
// reset endpoint
usb_resetep(usbHdl, PSVR_EP_CMD_READ);
return TRUE;
}
else
return FALSE;
}
/// -----------------------------------------------------------------------
/// \brief PSVRCommon::Close
/// \param usbInterface
///
void PSVRCommon::Close(PSVR_USB_INTERFACE usbInterface)
{
usb_release_interface(usbHdl, usbInterface);
usb_close(usbHdl);
usbHdl = NULL;
}
/// -----------------------------------------------------------------------
/// \brief PSVRCommon::SendCommand
/// \param Command
/// \return
///
bool PSVRCommon::SendCommand(PSVRFrame *sendCmd)
{
int bytesSent;
// send command
if((bytesSent = usb_bulk_write(usbHdl, PSVR_EP_CMD_WRITE, (char *)sendCmd, 64, 20)) != sizeof(PSVRFrame))
return false;
return true;
}
}