-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPush2UsbDisplay.cpp
187 lines (173 loc) · 5.36 KB
/
Push2UsbDisplay.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
#include "Push2UsbDisplay.h"
#include "FinalAction.h"
#include <iostream>
/*
// ----- Time measuring -----
#include "CyclicDataOutputterThread.h"
#include "Histogram.h"
#include "Measurer.h"
#include "OutputterDestinationsZmq.h"
using TenthMs = std::chrono::duration<int, std::ratio<1, 10000>>;
using DataHolderTenthMs = TimeMeasure::Histogram<TenthMs>;
template <unsigned int Id>
using MeasurerTenthMs = TimeMeasure::Measurer<Id, DataHolderTenthMs>;
TimeMeasure::CyclicDataOutputterThread<DataHolderTenthMs,
TimeMeasure::Destination::Zmq>
outThreadZmq({
&MeasurerTenthMs<0>::instance().dataHolder(),
&MeasurerTenthMs<1>::instance().dataHolder()
});
// --------------------------
*/
using namespace push2device;
UsbDisplay::UsbDisplay() :
m_pContext(nullptr),
m_pDevice(nullptr)
{}
UsbDisplay::~UsbDisplay()
{
if(m_pDevice)
{
libusb_release_interface(m_pDevice, 0);
libusb_close(m_pDevice);
m_pDevice = nullptr;
}
if(m_pContext)
{
libusb_exit(m_pContext);
m_pContext = nullptr;
}
}
bool UsbDisplay::init()
{
int ret = libusb_init(&m_pContext);
if(0 != ret)
{
std::cerr << "libusb_init failed, ret: " << ret << std::endl;
return false;
}
auto cleanupContext = finally([this](){
libusb_exit(m_pContext);
m_pContext = nullptr;
});
// to make this work as non-root:
// create in /etc/udev/rules.d/60-push2.rules
// with content: SUBSYSTEM=="usb", ATTR{idProduct}=="1967", ATTR{idVendor}=="2982", MODE="0666"
m_pDevice = libusb_open_device_with_vid_pid(m_pContext, 0x2982, 0x1967);
if (!m_pDevice)
{
std::cerr << "libusb_open_device_with_vid_pid() failed" << std::endl;
return false;
}
auto cleanupDevice = finally([this](){
libusb_release_interface(m_pDevice, 0);
libusb_close(m_pDevice);
m_pDevice = nullptr;
});
switch(libusb_claim_interface(m_pDevice, 0))
{
case 0:
{
// Success
break;
}
case LIBUSB_ERROR_NOT_FOUND:
{
std::cerr << "libusb_init failed : the requested interface does not exist" << std::endl;
return false;
}
case LIBUSB_ERROR_BUSY:
{
std::cerr << "libusb_init failed : a program or driver has claimed the interface" << std::endl;
return false;
}
case LIBUSB_ERROR_NO_DEVICE:
{
std::cerr << "libusb_init failed : the device has been disconnected " << std::endl;
return false;
}
default:
{
std::cerr << "libusb_init failed" << std::endl;
return false;
}
}
cleanupContext.disable();
cleanupDevice.disable();
//MeasurerTenthMs<0>::instance().dataHolder().setHistogramRange(1000);
//MeasurerTenthMs<1>::instance().dataHolder().setHistogramRange(1000);
//outThreadZmq.destination().bind("tcp://*:12342");
//outThreadZmq.startThread(500);
return true;
}
// TODO: remove this assertion later
#include <cassert>
bool UsbDisplay::sendFrameToDisplay(const FrameBuffer<uint16_t>& frameBuffer)
{
//MeasurerTenthMs<1>::instance().sample();
//MeasurerTenthMs<0>::Guard guard;
static uint8_t header[] = { 0xEF, 0xCD, 0xAB, 0x89, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int transferred = 0;
// Transfer header which indicates start of image transfer
int ret = 0;
ret = libusb_bulk_transfer(m_pDevice,
0x01 | LIBUSB_ENDPOINT_OUT,
header,
sizeof(header),
&transferred,
3000);
if(!handleBulkTransferRet(ret))
{
return false;
}
// TODO: remove this assertion later
assert(20 * 16384 == frameBuffer.size());
// Transfer image
ret = libusb_bulk_transfer(m_pDevice,
0x01 | LIBUSB_ENDPOINT_OUT,
const_cast<uint8_t*>(frameBuffer.getRawBuffer()),
frameBuffer.size(), // 20 * 16384,
&transferred,
3000);
if(!handleBulkTransferRet(ret))
{
return false;
}
return true;
}
bool UsbDisplay::handleBulkTransferRet(int ret)
{
switch(ret)
{
case 0:
{
return true;
}
case LIBUSB_ERROR_TIMEOUT:
{
std::cerr << "handleBulkTransferRet error: transfer timed out" << std::endl;
return false;
}
case LIBUSB_ERROR_PIPE:
{
std::cerr << "handleBulkTransferRet error: the control request was not supported" << std::endl;
return false;
}
case LIBUSB_ERROR_OVERFLOW:
{
std::cerr << "handleBulkTransferRet error: the device offered more data" << std::endl;
return false;
}
case LIBUSB_ERROR_NO_DEVICE:
{
std::cerr << "handleBulkTransferRet error: the device has been disconnected" << std::endl;
return false;
}
default:
{
std::cerr << "handleBulkTransferRet error" << std::endl;
return false;
}
}
}