-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathViveLoader.cpp
294 lines (262 loc) · 9.87 KB
/
ViveLoader.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
/** @file
@brief Implementation
@date 2016
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2016 Razer Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Internal Includes
#include "ChaperoneData.h"
#include "DriverWrapper.h"
#include "GetComponent.h"
#include "ServerPropertyHelper.h"
// Library/third-party includes
#include <openvr_driver.h>
// Standard includes
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
static const auto PREFIX = "[ViveLoader] ";
static void whatIsThisDevice(vr::ITrackedDeviceServerDriver *dev,
vr::TrackedDeviceIndex_t idx) {
{
auto disp = osvr::vive::getComponent<vr::IVRDisplayComponent>(dev);
if (disp) {
std::cout << PREFIX << "-- it's a display, too!" << std::endl;
vr::ETrackedPropertyError err;
uint64_t universe = 0;
std::tie(universe, err) =
osvr::vive::getProperty<osvr::vive::Props::CurrentUniverseId>(
idx);
if (vr::TrackedProp_Success == err) {
std::cout << PREFIX << " -- In tracking universe " << universe
<< std::endl;
} else if (vr::TrackedProp_NotYetAvailable == err) {
std::cout << PREFIX << " -- Tracking universe not yet known"
<< std::endl;
} else {
std::cout << PREFIX
<< " -- Some other error trying to figure "
"out the tracking universe: "
<< err << std::endl;
}
}
}
{
auto controller =
osvr::vive::getComponent<vr::IVRControllerComponent>(dev);
if (controller) {
std::cout << PREFIX << "-- it's a controller, too!" << std::endl;
}
}
{
auto cam = osvr::vive::getComponent<vr::IVRCameraComponent>(dev);
if (cam) {
std::cout << PREFIX << "-- it's a camera, too!" << std::endl;
}
}
std::cout << "\n";
}
void lookForUniverseData(osvr::vive::DriverWrapper &vive) {
if (!vive.haveChaperoneData()) {
std::cout
<< PREFIX
<< "DriverWrapper couldn't get to starting up the chaperone data."
<< std::endl;
return;
}
if (!vive.chaperone()) {
std::cout << PREFIX
<< "Chaperone data error: " << vive.chaperone().getMessage()
<< std::endl;
return;
}
if (vive.chaperone().hasMessages()) {
std::cout << PREFIX
<< "Chaperone data warning: " << vive.chaperone().getMessage()
<< std::endl;
}
std::uint64_t universe = 0;
vr::TrackedDeviceIndex_t idx = 0;
for (auto &devPtr :
vive.devices().rawDeviceVectorAccess_NOT_RECOMMENDED_TODO_FIXME()) {
if (!devPtr) {
continue;
}
vr::ETrackedPropertyError err;
uint64_t myUniverse = 0;
std::tie(myUniverse, err) =
osvr::vive::getProperty<osvr::vive::Props::CurrentUniverseId>(idx);
if (vr::TrackedProp_Success != err || myUniverse == 0) {
continue;
}
auto model =
osvr::vive::getProperty<osvr::vive::Props::ModelNumber>(idx).first;
std::cout << PREFIX << "Got a real universe (" << myUniverse
<< ") from " << model << std::endl;
universe = myUniverse;
idx++;
}
if (vive.chaperone().knowUniverseId(universe)) {
std::cout << PREFIX << "ChaperoneData has info on universe " << universe
<< std::endl;
auto data = vive.chaperone().getDataForUniverse(universe);
std::cout << PREFIX << "Translation: [" << data.translation[0] << ", "
<< data.translation[1] << ", " << data.translation[2]
<< "], yaw " << data.yaw << std::endl;
} else {
std::cout << PREFIX << "ChaperoneData sadly has no info on universe "
<< universe << std::endl;
}
}
int main() {
auto vive = osvr::vive::DriverWrapper();
/// These lines are just informational printout - the real check is later -
/// if (!vive)
if (vive.foundDriver()) {
std::cout << PREFIX << "Found the Vive driver at "
<< vive.getDriverFileLocation() << std::endl;
}
if (!vive.haveDriverLoaded()) {
std::cout << PREFIX << "Could not open driver." << std::endl;
}
if (vive.foundConfigDirs()) {
std::cout << PREFIX
<< "Driver config dir is: " << vive.getDriverConfigDir()
<< std::endl;
}
if (!vive) {
std::cerr << PREFIX
<< "Error in first-stage Vive driver startup. Exiting"
<< std::endl;
return 1;
}
std::cout << PREFIX << "Vive is connected!!!" << std::endl;
auto handleNewDevice = [&](const char *serialNum,
vr::ETrackedDeviceClass eDeviceClass,
vr::ITrackedDeviceServerDriver *pDriver) {
auto dev = pDriver;
if (!dev) {
std::cout << PREFIX << "null input device" << std::endl;
return false;
}
auto ret = vive.devices().addAndActivateDevice(dev);
if (!ret) {
std::cout << PREFIX << "Device with serial number " << serialNum
<< " couldn't be added to the devices vector."
<< std::endl;
return false;
}
std::cout << "\n"
<< PREFIX << "Device with s/n " << serialNum
<< " activated, assigned ID " << ret.value << std::endl;
vr::TrackedDeviceIndex_t idx = ret.value;
whatIsThisDevice(dev, idx);
return true;
};
vive.driverHost().onTrackedDeviceAdded = handleNewDevice;
if (!vive.startServerDeviceProvider()) {
// can either check return value of this, or do another if (!vive) after
// calling - equivalent.
std::cerr << PREFIX
<< "Error: could not start the server device provider in the "
"Vive driver. Exiting."
<< std::endl;
return 1;
}
std::cout << "Vive has started." << std::endl;
/// but now, we can do things with vive.serverDevProvider()
/// first thing we should do is check driver compatibility.
auto interfaceStatus = vive.checkServerDeviceProviderInterfaces();
switch (interfaceStatus) {
case osvr::vive::DriverWrapper::InterfaceVersionStatus::AllInterfacesOK:
std::cerr << PREFIX
<< "All interface versions mentioned by the driver "
"are available and supported."
<< std::endl;
break;
case osvr::vive::DriverWrapper::InterfaceVersionStatus::AllUsedInterfacesOK:
std::cerr << PREFIX
<< "Not all interface versions mentioned by the "
"driver are available and supported, but the "
"ones used by this code match."
<< std::endl;
break;
case osvr::vive::DriverWrapper::InterfaceVersionStatus::InterfaceMismatch:
std::cerr
<< PREFIX
<< "Driver requires unavailable/unsupported interface versions."
<< std::endl;
break;
default:
break;
}
switch (interfaceStatus) {
case osvr::vive::DriverWrapper::InterfaceVersionStatus::AllUsedInterfacesOK:
case osvr::vive::DriverWrapper::InterfaceVersionStatus::InterfaceMismatch:
std::cerr << PREFIX
<< "Unavailable interface version strings:" << std::endl;
for (auto iface : vive.getUnsupportedRequestedInterfaces()) {
std::cerr << PREFIX << " - " << iface << std::endl;
}
break;
case osvr::vive::DriverWrapper::InterfaceVersionStatus::AllInterfacesOK:
default:
break;
}
if (osvr::vive::DriverWrapper::InterfaceVersionStatus::InterfaceMismatch ==
interfaceStatus) {
std::cerr << PREFIX << "Cannot continue." << std::endl;
return 1;
}
/// Power the system up.
vive.serverDevProvider().LeaveStandby();
#if 0
{
osvr::vive::DeviceHolder devHolder = std::move(vive.devices());
auto numDevices = devHolder.numDevices();
std::cout << PREFIX << "Got " << numDevices
<< " tracked devices at startup" << std::endl;
for (decltype(numDevices) i = 0; i < numDevices; ++i) {
vr::ITrackedDeviceServerDriver *dev = &(devHolder.getDevice(i));
std::cout << PREFIX << "Device " << i << std::endl;
whatIsThisDevice(dev, i);
}
}
#endif
std::cout << "*** Entering dummy mainloop" << std::endl;
#if 1
for (int i = 0; i < 3000; ++i) {
vive.serverDevProvider().RunFrame();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
#else
std::this_thread::sleep_for(std::chrono::seconds(10));
#endif
std::cout << "*** Done with dummy mainloop" << std::endl;
lookForUniverseData(vive);
/// The vive object will automatically deactivate all of them on
/// destruction, or on a call to stop().
std::cout << PREFIX << "Stopping the Vive." << std::endl;
vive.stop();
#if 0
/// This line will turn off the wireless wands.
vive.serverDevProvider().EnterStandby();
#endif
return 0;
}