-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhot_plugin.cpp
80 lines (67 loc) · 2.68 KB
/
hot_plugin.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
// Copyright (c) Orbbec Inc. All Rights Reserved.
// Licensed under the MIT License.
#include <libobsensor/ObSensor.hpp>
#include "utils.hpp"
#include <iomanip>
#include <iostream>
void printDeviceList(const std::string &prompt, std::shared_ptr<ob::DeviceList> deviceList) {
auto count = deviceList->getCount();
if(count == 0) {
return;
}
std::cout << count << " device(s) " << prompt << ": " << std::endl;
for(uint32_t i = 0; i < count; i++) {
auto uid = deviceList->getUid(i);
auto vid = deviceList->getVid(i);
auto pid = deviceList->getPid(i);
auto serialNumber = deviceList->getSerialNumber(i);
auto connection = deviceList->getConnectionType(i);
std::cout << " - uid: " << uid << ", vid: 0x" << std::hex << std::setfill('0') << std::setw(4) << vid << ", pid: 0x" << pid
<< ", serial number: " << serialNumber << ", connection: " << connection << std::endl;
}
std::cout << std::endl;
}
void rebootDevices(std::shared_ptr<ob::DeviceList> deviceList) {
for(uint32_t i = 0; i < deviceList->getCount(); i++) {
// get device from device list
auto device = deviceList->getDevice(i);
// reboot device
device->reboot();
}
}
int main(void) try {
// create context
ob::Context ctx;
// register device callback
ctx.setDeviceChangedCallback([](std::shared_ptr<ob::DeviceList> removedList, std::shared_ptr<ob::DeviceList> deviceList) {
printDeviceList("added", deviceList);
printDeviceList("removed", removedList);
});
// query current device list
auto currentList = ctx.queryDeviceList();
printDeviceList("connected", currentList);
std::cout << "Press 'r' to reboot the connected devices to trigger the device disconnect and reconnect event, or manually unplug and plugin the device."
<< std::endl;
std::cout << "Press 'Esc' to exit." << std::endl << std::endl;
// main loop, wait for key press
while(true) {
auto key = ob_smpl::waitForKeyPressed(100);
// Press the esc key to exit
if(key == 27) {
break;
}
else if(key == 'r' || key == 'R') {
// update device list
currentList = ctx.queryDeviceList();
std::cout << "Rebooting devices..." << std::endl;
rebootDevices(currentList);
}
}
return 0;
}
catch(ob::Error &e) {
std::cerr << "function:" << e.getFunction() << "\nargs:" << e.getArgs() << "\nmessage:" << e.what() << "\ntype:" << e.getExceptionType() << std::endl;
std::cout << "\nPress any key to exit.";
ob_smpl::waitForKeyPressed();
exit(EXIT_FAILURE);
}