Marquee loop can now be paused and resumed.
Cleaning up our MiniDisplay coneection to render loop API.
1 #include "minidisplay.h"
5 const int KMaxReadAttempt=100;
7 MiniDisplay::MiniDisplay(QQuickItem *parent):
10 iBrightness(iDisplay.MaxBrightness()),
11 iAfterAnimatingCallback(NULL),
15 // By default, QQuickItem does not draw anything. If you subclass
16 // QQuickItem to create a visual item, you will need to uncomment the
17 // following line and re-implement updatePaintNode()
19 // setFlag(ItemHasContents, true);
21 //qDebug() << "New MiniDisplay";
24 MiniDisplay::~MiniDisplay()
26 //qDebug() << "Delete MiniDisplay";
31 void MiniDisplay::open()
35 iDisplay.SetBrightness(iBrightness);
46 void MiniDisplay::close()
48 //qDebug() << "MiniDisplay::close";
49 //Try to put back ourframe position to RAM 0,0 which is a multiple of 128
50 if (iDisplay.IsOpen())
54 if (iDisplay.DisplayPositionY()%128!=0)
56 //qDebug() << "SwapBuffer to put back our frame position to zero " << iDisplay.DisplayPositionY();
57 iDisplay.SwapBuffers();
61 disconnectRenderLoop();
66 bool MiniDisplay::isOpen()
68 return iDisplay.IsOpen();
71 void MiniDisplay::clear()
73 if (!iDisplay.IsOpen()) return;
78 void MiniDisplay::fill()
80 if (!iDisplay.IsOpen()) return;
82 iDisplay.SetAllPixels(0xFF);
85 void MiniDisplay::swapBuffers()
87 if (!iDisplay.IsOpen()) return;
89 iDisplay.SwapBuffers();
92 void MiniDisplay::requestPowerStatus()
94 if (!iDisplay.IsOpen()) return;
96 iDisplay.RequestPowerSupplyStatus();
97 QTimer::singleShot(4, this, SLOT(readTimer()));
101 void MiniDisplay::requestDeviceId()
103 if (!iDisplay.IsOpen()) return;
105 iDisplay.RequestDeviceId();
106 QTimer::singleShot(4, this, SLOT(readTimer()));
110 void MiniDisplay::requestFirmwareVersion()
112 if (!iDisplay.IsOpen()) return;
114 iDisplay.RequestFirmwareRevision();
115 QTimer::singleShot(4, this, SLOT(readTimer()));
125 void MiniDisplay::setFont(const QFont& aFont)
128 iFont.setStyleStrategy(QFont::NoAntialias);
129 QString strDemo="0123456789ABCDEF";
130 QFontMetrics metrics(iFont);
131 int w=metrics.boundingRect(strDemo).width();
132 int h=metrics.height();
133 //int h=metrics.boundingRect(strDemo).height();
135 //Rendering in mono is quite broken we need RGB32
136 QImage image(size,QImage::Format_RGB32);
137 image.fill(0xFFFFFFFF);
138 //Draw some text into our image
140 QPainter painter(&image);
141 painter.setPen(0xFF000000);
142 painter.setFont(iFont);
143 painter.drawText(0,metrics.ascent(),strDemo);
145 //Save image as PNG for validation
146 //image.save("font.png");
152 * @brief MiniDisplay::renderWindow
154 void MiniDisplay::renderOwnWindow()
156 QImage image=window()->grabWindow();
157 //image.save("window.png");
162 * @brief MiniDisplay::render
165 void MiniDisplay::renderWindow(QQuickWindow* aWindow)
167 //if (!iDisplay.IsOpen()) return;
169 QImage image=aWindow->grabWindow();
170 //image.save("window.png");
175 * @brief MiniDisplay::render
178 void MiniDisplay::renderImage(QImage* aImage)
180 if (!iDisplay.IsOpen())
185 //Convert our image into a bit array
186 int w=aImage->width();
187 int h=aImage->height();
190 BitArray bits(pixelCount);
192 for (int i=0;i<w;i++)
194 for (int j=0;j<h;j++)
196 QRgb color=aImage->pixel(i,j);
197 if (color!=0xffffffff)
205 iDisplay.BitBlit(bits,w,h,0,0);
210 Connect to the render loop of the given window.
211 This is achieved using the afterAnimating event.
212 If any JavaScript callback is provided it will be called on every frame.
214 @brief MiniDisplay::connectRenderLoop
215 @param aWindow The window to connect to.
216 @param aFunction An optional JavaScript callback function
218 void MiniDisplay::connectRenderLoop(QQuickWindow* aWindow, QJSValue aFunction)
220 QObject::connect(aWindow, SIGNAL(afterAnimating()),
221 this, SLOT(onAfterAnimating()));
223 iAfterAnimatingCallback = aFunction;
229 @brief MiniDisplay::disconnectRenderLoop
231 void MiniDisplay::disconnectRenderLoop()
235 QObject::disconnect(iWindow, SIGNAL(afterAnimating()),this, SLOT(onAfterAnimating()));
237 iAfterAnimatingCallback = NULL;
244 Once connected to a window render loop this is called on every frame.
246 @brief MiniDisplay::onAfterAnimating
248 void MiniDisplay::onAfterAnimating()
250 //qDebug() << frame << "MiniDisplay::onAfterAnimating";
252 if (iAfterAnimatingCallback.isCallable())
254 iAfterAnimatingCallback.call();
257 //renderWindow(iWindow);
261 //Interrestingly rendering will suspend when we stop changing our text field content
264 if (iAfterAnimatingCallback.isCallable())
266 iAfterAnimatingCallback.call(QJSValueList() << iFrameCount);
275 * @brief MiniDisplay::setPixel
280 void MiniDisplay::setPixel(int x, int y, bool on)
282 if (!iDisplay.IsOpen())
287 iDisplay.SetPixel(x,y,on);
291 * @brief MiniDisplay::offScreenMode
294 bool MiniDisplay::offScreenMode()
296 return iDisplay.OffScreenMode();
300 * @brief MiniDisplay::setOffScreenMode
303 void MiniDisplay::setOffScreenMode(bool aOn)
305 if (!iDisplay.IsOpen())
310 iDisplay.SetOffScreenMode(aOn);
317 * @brief MiniDisplay::vendor
320 QString MiniDisplay::vendor()
322 return QString::fromWCharArray(iDisplay.Vendor());
326 * @brief MiniDisplay::product
329 QString MiniDisplay::product()
331 return QString::fromWCharArray(iDisplay.Product());
335 * @brief MiniDisplay::serialNumber
338 QString MiniDisplay::serialNumber()
340 return QString::fromWCharArray(iDisplay.SerialNumber());
344 * @brief MiniDisplay::readTimer
345 * Called when our read timer completes.
346 * We then attempt to complete our pending display request.
347 * We typically attempt to read an input report from our HID device.
349 void MiniDisplay::readTimer()
351 if (!iDisplay.IsOpen()) return;
352 if (!iDisplay.RequestPending()) return;
355 GP1212A01A::Request request = iDisplay.AttemptRequestCompletion();
359 if (iReadAttempt<KMaxReadAttempt)
361 //Will try again later
362 QTimer::singleShot(4, this, SLOT(readTimer()));
368 //TODO: Find a way to remove device specific stuff from here
369 if (request==GP1212A01A::ERequestFirmwareRevision)
371 QString version=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
372 emit firmwareVersion(version);
374 else if (request==GP1212A01A::ERequestDeviceId)
376 QString id=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
379 else if (request==GP1212A01A::ERequestPowerSupplyStatus)
381 emit powerStatus(iDisplay.PowerOn());
386 int MiniDisplay::maxBrightness() const
388 return iDisplay.MaxBrightness();
391 int MiniDisplay::minBrightness() const
393 return iDisplay.MinBrightness();
396 int MiniDisplay::brightness() const
401 void MiniDisplay::setBrightness(int aBrightness)
403 //Still track the brightness when disconnected
404 iBrightness=aBrightness;
406 if (!iDisplay.IsOpen()) return;
408 iDisplay.SetBrightness(aBrightness);
412 @brief MiniDisplay::frameDifferencing
415 bool MiniDisplay::frameDifferencing() const
417 return iDisplay.FrameDifferencing();
421 @brief MiniDisplay::setFrameDifferencing
424 void MiniDisplay::setFrameDifferencing(bool aOn)
426 iDisplay.SetFrameDifferencing(aOn);
430 QPoint MiniDisplay::framePosition() const
432 return QPoint(iDisplay.DisplayPositionX(),iDisplay.DisplayPositionX());
435 void MiniDisplay::setFramePosition(const QPoint& aPoint)
437 iFramePosition = aPoint;
439 if (!iDisplay.IsOpen()) return;
441 iDisplay.SetDisplayPosition(aPoint.x(),aPoint.y());