-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpushdisplay.cpp
175 lines (141 loc) · 3.91 KB
/
pushdisplay.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
#include "pushdisplay.h"
#include "dither.h"
#include <QImage>
#include <QPainter>
#if defined(Q_CC_MSVC)
QT_WARNING_PUSH
QT_WARNING_DISABLE_MSVC(4200)
# include "libusb.h"
QT_WARNING_POP
#else
# include <libusb-1.0/libusb.h>
#endif
class PushDisplayPrivate : public QObject
{
Q_OBJECT
public:
PushDisplayPrivate();
~PushDisplayPrivate();
void drawImage(const QImage &image);
void drawNativeImage(const QImage &image);
QSize paddedSize() const;
QSize size() const;
protected:
void timerEvent(QTimerEvent *event);
public:
libusb_context *context;
libusb_device_handle *device;
QImage currentImage;
bool dithering;
};
PushDisplayPrivate::PushDisplayPrivate() :
dithering(false)
{
libusb_init(&context);
device = libusb_open_device_with_vid_pid(context, 0x2982, 0x1967);
if (!device)
return;
libusb_claim_interface(device, 0);
// Push display will automatically clear to black if another frame has not been received within 2 seconds
startTimer(1000);
}
PushDisplayPrivate::~PushDisplayPrivate()
{
if (device) {
libusb_release_interface(device, 0);
libusb_close(device);
}
if (context)
libusb_exit(context);
}
void PushDisplayPrivate::drawImage(const QImage &image)
{
QImage source = image;
if (source.isNull()) {
source = QImage(size(), QImage::Format_ARGB32_Premultiplied);
source.fill(Qt::black);
}
if (dithering) {
// Scale and dither to BGR565
drawNativeImage(ditherToBgr565(source.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
} else{
// Scale and convert to BGR565
drawNativeImage(source.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)
.convertToFormat(QImage::Format_RGB16).rgbSwapped());
}
}
void PushDisplayPrivate::drawNativeImage(const QImage &image)
{
if (!device)
return;
Q_ASSERT(image.size() == size());
Q_ASSERT(image.format() == QImage::Format_RGB16);
// Each horizontal line is 960 pixels but we need to pad it to 1024 pixels
QImage paddedImage(paddedSize(), image.format());
QPainter painter;
paddedImage.fill(Qt::black);
painter.begin(&paddedImage);
painter.drawImage(QPoint(0, 0), image);
painter.end();
Q_ASSERT(paddedImage.byteCount() == 20 * 16384);
unsigned char 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
libusb_bulk_transfer(device, 0x01 | LIBUSB_ENDPOINT_OUT, header, sizeof(header), &transferred, 3000);
// Transfer image
libusb_bulk_transfer(device, 0x01 | LIBUSB_ENDPOINT_OUT, const_cast<uchar *>(paddedImage.bits()),
20 * 16384, &transferred, 3000);
}
QSize PushDisplayPrivate::paddedSize() const
{
return QSize(1024, 160);
}
QSize PushDisplayPrivate::size() const
{
return QSize(960, 160);
}
void PushDisplayPrivate::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
drawImage(currentImage);
}
PushDisplay::PushDisplay(QObject *parent) :
QObject(parent),
d_ptr(new PushDisplayPrivate)
{
}
PushDisplay::~PushDisplay()
{
}
bool PushDisplay::isOpen() const
{
Q_D(const PushDisplay);
return d->device != 0;
}
bool PushDisplay::dithering() const
{
Q_D(const PushDisplay);
return d->dithering;
}
void PushDisplay::setDithering(bool value)
{
Q_D(PushDisplay);
if (d->dithering != value) {
d->dithering = value;
d->drawImage(d->currentImage);
emit ditheringChanged();
}
}
void PushDisplay::drawImage(const QImage &image)
{
Q_D(PushDisplay);
d->currentImage = image;
d->drawImage(d->currentImage);
}
QSize PushDisplay::size() const
{
Q_D(const PushDisplay);
return d->size();
}
#include "pushdisplay.moc"