-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathholoCalibration.cpp
More file actions
167 lines (146 loc) · 5.96 KB
/
Copy pathholoCalibration.cpp
File metadata and controls
167 lines (146 loc) · 5.96 KB
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
#include "holoCalibration.h"
#include "json.hpp"
#include <exception>
#include <iostream>
#include <fstream>
#include <libusb-1.0/libusb.h>
#include <vector>
void printError(int e)
{
const char *es;
es = libusb_strerror(e);
std::cerr << es << std::endl;
}
HoloCalibration::Calibration HoloCalibration::getCalibration(std::string calibrationFilePath)
{
try
{
std::string info;
if(calibrationFilePath.empty())
{
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};
DeviceType deviceType{NONE};
int vendorId{0};
int interfaceId{2};
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_FIRSTGEN)
{
deviceType = FIRSTGEN;
interfaceId = INTERFACE_NUM_FIRSTGEN;
}
else if(desc.idVendor == VENDOR_ID_PORTRAIT)
{
deviceType = PORTRAIT;
}
if(deviceType != NONE)
{
vendorId = desc.idVendor;
productId = desc.idProduct;
break;
}
}
if(productId == 0)
throw std::runtime_error("No LKG display found");
if(deviceType == PORTRAIT)
{
std::ifstream ifs("/proc/mounts");
std::string line;
std::string mountPoint;
while (getline(ifs, line))
{
if (line.find("LKG-") != std::string::npos)
{
int firstSpace = line.find(" ");
int secondSpace = line.find(" ", firstSpace + 1);
mountPoint = line.substr(firstSpace+1, secondSpace - firstSpace-1);
}
}
std::ifstream ifsCal(mountPoint+"/LKG_calibration/visual.json");
std::stringstream ss;
ss << ifsCal.rdbuf();
info = ss.str();
}
else if(deviceType == FIRSTGEN)
{
struct libusb_device_handle *dh = NULL;
dh = libusb_open_device_with_vid_pid(NULL,vendorId,productId);
if(!dh)
throw std::runtime_error("Cannot connect to device");
libusb_detach_kernel_driver(dh,interfaceId);
if(libusb_claim_interface(dh,interfaceId) < 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;
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);
for(int i=0; i<7; i++)
info.erase(remove(info.begin(), info.end(), i), info.end());
libusb_close(dh);
}
libusb_exit(NULL);
}
else
{
std::ifstream ifsCal(calibrationFilePath);
std::stringstream ss;
ss << ifsCal.rdbuf();
info = ss.str();
}
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 {};
}