Adding MiniDisplay methods to display image and window.
Using our test tab to render our display frame in QML and send it to our display.
Using a timer and frame counter to profile our performance.
1 #include "minidisplay.h"
5 const int KMaxReadAttempt=100;
7 MiniDisplay::MiniDisplay(QQuickItem *parent):
8 QQuickItem(parent),iReadAttempt(0),iBrightness(iDisplay.MaxBrightness())
10 // By default, QQuickItem does not draw anything. If you subclass
11 // QQuickItem to create a visual item, you will need to uncomment the
12 // following line and re-implement updatePaintNode()
14 // setFlag(ItemHasContents, true);
16 //qDebug() << "New MiniDisplay";
19 MiniDisplay::~MiniDisplay()
21 //qDebug() << "Delete MiniDisplay";
26 void MiniDisplay::open()
30 iDisplay.SetBrightness(iBrightness);
41 void MiniDisplay::close()
43 //qDebug() << "MiniDisplay::close";
44 //Try to put back ourframe position to RAM 0,0 which is a multiple of 128
45 if (iDisplay.IsOpen())
49 if (iDisplay.DisplayPositionY()%128!=0)
51 //qDebug() << "SwapBuffer to put back our frame position to zero " << iDisplay.DisplayPositionY();
52 iDisplay.SwapBuffers();
60 bool MiniDisplay::isOpen()
62 return iDisplay.IsOpen();
65 void MiniDisplay::clear()
67 if (!iDisplay.IsOpen()) return;
72 void MiniDisplay::fill()
74 if (!iDisplay.IsOpen()) return;
76 iDisplay.SetAllPixels(0xFF);
79 void MiniDisplay::swapBuffers()
81 if (!iDisplay.IsOpen()) return;
83 iDisplay.SwapBuffers();
86 void MiniDisplay::requestPowerStatus()
88 if (!iDisplay.IsOpen()) return;
90 iDisplay.RequestPowerSupplyStatus();
91 QTimer::singleShot(4, this, SLOT(readTimer()));
95 void MiniDisplay::requestDeviceId()
97 if (!iDisplay.IsOpen()) return;
99 iDisplay.RequestDeviceId();
100 QTimer::singleShot(4, this, SLOT(readTimer()));
104 void MiniDisplay::requestFirmwareVersion()
106 if (!iDisplay.IsOpen()) return;
108 iDisplay.RequestFirmwareRevision();
109 QTimer::singleShot(4, this, SLOT(readTimer()));
119 void MiniDisplay::setFont(const QFont& aFont)
122 iFont.setStyleStrategy(QFont::NoAntialias);
123 QString strDemo="0123456789ABCDEF";
124 QFontMetrics metrics(iFont);
125 int w=metrics.boundingRect(strDemo).width();
126 int h=metrics.height();
127 //int h=metrics.boundingRect(strDemo).height();
129 //Rendering in mono is quite broken we need RGB32
130 QImage image(size,QImage::Format_RGB32);
131 image.fill(0xFFFFFFFF);
132 //Draw some text into our image
134 QPainter painter(&image);
135 painter.setPen(0xFF000000);
136 painter.setFont(iFont);
137 painter.drawText(0,metrics.ascent(),strDemo);
139 //Save image as PNG for validation
140 //image.save("font.png");
146 * @brief MiniDisplay::renderWindow
148 void MiniDisplay::renderOwnWindow()
150 QImage image=window()->grabWindow();
151 //image.save("window.png");
156 * @brief MiniDisplay::render
159 void MiniDisplay::renderWindow(QQuickWindow* aWindow)
161 QImage image=aWindow->grabWindow();
162 //image.save("window.png");
167 * @brief MiniDisplay::render
170 void MiniDisplay::renderImage(QImage* aImage)
172 if (!iDisplay.IsOpen())
177 //Convert our image into a bit array
178 int w=aImage->width();
179 int h=aImage->height();
182 BitArray bits(pixelCount);
184 for (int i=0;i<w;i++)
186 for (int j=0;j<h;j++)
188 QRgb color=aImage->pixel(i,j);
189 if (color!=0xffffffff)
197 iDisplay.BitBlit(bits,w,h,0,0);
201 * @brief MiniDisplay::vendor
204 QString MiniDisplay::vendor()
206 return QString::fromWCharArray(iDisplay.Vendor());
210 * @brief MiniDisplay::product
213 QString MiniDisplay::product()
215 return QString::fromWCharArray(iDisplay.Product());
219 * @brief MiniDisplay::serialNumber
222 QString MiniDisplay::serialNumber()
224 return QString::fromWCharArray(iDisplay.SerialNumber());
228 * @brief MiniDisplay::readTimer
229 * Called when our read timer completes.
230 * We then attempt to complete our pending display request.
231 * We typically attempt to read an input report from our HID device.
233 void MiniDisplay::readTimer()
235 if (!iDisplay.IsOpen()) return;
236 if (!iDisplay.RequestPending()) return;
239 GP1212A01A::Request request = iDisplay.AttemptRequestCompletion();
243 if (iReadAttempt<KMaxReadAttempt)
245 //Will try again later
246 QTimer::singleShot(4, this, SLOT(readTimer()));
252 //TODO: Find a way to remove device specific stuff from here
253 if (request==GP1212A01A::ERequestFirmwareRevision)
255 QString version=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
256 emit firmwareVersion(version);
258 else if (request==GP1212A01A::ERequestDeviceId)
260 QString id=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
263 else if (request==GP1212A01A::ERequestPowerSupplyStatus)
265 emit powerStatus(iDisplay.PowerOn());
270 int MiniDisplay::maxBrightness() const
272 return iDisplay.MaxBrightness();
275 int MiniDisplay::minBrightness() const
277 return iDisplay.MinBrightness();
280 int MiniDisplay::brightness() const
285 void MiniDisplay::setBrightness(int aBrightness)
287 //Still track the brightness when disconnected
288 iBrightness=aBrightness;
290 if (!iDisplay.IsOpen()) return;
292 iDisplay.SetBrightness(aBrightness);
295 QPoint MiniDisplay::framePosition() const
297 return QPoint(iDisplay.DisplayPositionX(),iDisplay.DisplayPositionX());
300 void MiniDisplay::setFramePosition(const QPoint& aPoint)
302 iFramePosition = aPoint;
304 if (!iDisplay.IsOpen()) return;
306 iDisplay.SetDisplayPosition(aPoint.x(),aPoint.y());