-
Notifications
You must be signed in to change notification settings - Fork 0
/
holoCalibration.cpp
112 lines (95 loc) · 3.89 KB
/
holoCalibration.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
#include <holoCalibration.h>
#include <exception>
#include <iostream>
#include <libusb-1.0/libusb.h>
#include <vector>
#include <json.hpp>
HoloCalibration::Calibration HoloCalibration::getCalibration()
{
try
{
libusb_context *c;
if(libusb_init(&c) < 0)
throw std::runtime_error("Cannot Initialize libusb");
//libusb_set_debug(c, 3);
libusb_device **devs;
ssize_t devCount = libusb_get_device_list(c, &devs);
if(devCount <= 0)
throw std::runtime_error("No devices listed");
int productId{0};
for(int i=0; i<devCount; i++)
{
struct libusb_device_descriptor desc;
if(libusb_get_device_descriptor(devs[i], &desc)<0)
continue;
if(desc.idVendor == VENDOR_ID)
{
productId = desc.idProduct;
break;
}
}
if(productId == 0)
throw std::runtime_error("No LKG display found");
struct libusb_device_handle *dh = NULL;
dh = libusb_open_device_with_vid_pid(NULL,VENDOR_ID,productId);
if(!dh)
throw std::runtime_error("Cannot connect to device");
libusb_detach_kernel_driver(dh,INTERFACE_NUM);
if(libusb_claim_interface(dh,INTERFACE_NUM) < 0 )
throw std::runtime_error("Cannot claim interface");
uint8_t bmReqType = 0x80;
uint8_t bReq = 6;
uint16_t wVal = 0x0302;
uint16_t wIndex = 0x0409;
unsigned char data[255] = {0};
std::vector<unsigned char> buffer;
unsigned int to = 0;
libusb_control_transfer(dh,bmReqType,bReq,0x0303,wIndex,data,1026,to);
libusb_control_transfer(dh,bmReqType,bReq,0x0301,wIndex,data,1026,to);
libusb_control_transfer(dh,bmReqType,bReq,wVal,wIndex,data,255,to);
buffer.resize(BUFFER_SIZE);
std::fill(buffer.begin(), buffer.end(), 0);
int len = 0;
std::string info;
for(int i=0; i<PACKET_NUM; i++)
{
int r;
std::fill(buffer.begin(), buffer.end(), 0);
buffer[2] = i;
r = libusb_control_transfer(dh,0x21,9,0x300,0x02,buffer.data(),INTERRUPT_SIZE,to);
if(r<0)
throw std::runtime_error("Cannot send control");
std::fill(buffer.begin(), buffer.end(), 0);
r = libusb_interrupt_transfer(dh, 0x84, buffer.data(), buffer.size(), &len, 0);
info.insert(info.end() ,buffer.begin(), buffer.begin()+INTERRUPT_SIZE+1);
if(r<0)
throw std::runtime_error("Cannot interrupt");
}
info = info.substr(info.find("{"), info.find("}}")-info.find("{")+2);
//std::cerr << info << std::endl;
libusb_close(dh);
libusb_exit(NULL);
for(int i=0; i<7; i++)
info.erase(remove(info.begin(), info.end(), i), info.end());
auto j = nlohmann::json::parse(info.c_str());
return {static_cast<float>(j["pitch"]["value"]),
static_cast<float>(j["slope"]["value"]),
static_cast<float>(j["center"]["value"]),
static_cast<float>(j["viewCone"]["value"]),
static_cast<float>(j["invView"]["value"]),
static_cast<float>(j["verticalAngle"]["value"]),
static_cast<float>(j["DPI"]["value"]),
static_cast<float>(j["screenW"]["value"]),
static_cast<float>(j["screenH"]["value"]),
static_cast<float>(j["flipImageX"]["value"]),
static_cast<float>(j["flipImageY"]["value"]),
static_cast<float>(j["flipSubp"]["value"]),
};
}
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
std::cerr << "Returning default calibration for small LKG" << std::endl;
}
return {};
}