Sorting out start-up/shutdown sequence to avoid flashing old frame upon connection.
Adding a couple of option to clear our frames upong close and/or open.
1 #include "minidisplay.h"
5 const int KMaxReadAttempt=100;
7 MiniDisplay::MiniDisplay(QQuickItem *parent):
8 QQuickItem(parent),iReadAttempt(0),iBrightness(0)
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.width(strDemo);
126 int h=metrics.height();
128 QImage image(size,QImage::Format_Mono);
129 image.fill(0xFFFFFFFF);
130 //Draw some text into our image
132 QPainter painter(&image);
133 //painter.begin(&image);
134 painter.setPen(0xFF000000);
135 painter.setFont(iFont);
136 painter.drawText(0,metrics.ascent(),strDemo);
138 //Save image as PNG for validation
139 image.save("font.png");
141 //int sizeInBytes=image.byteCount();
143 BitArray bits(pixelCount);
145 for (int i=0;i<w;i++)
147 for (int j=0;j<h;j++)
149 QRgb color=image.pixel(i,j);
150 if (color!=0xffffffff)
157 if (iDisplay.IsOpen())
160 iDisplay.BitBlit(bits,w,h,0,0);
161 iDisplay.SwapBuffers();
167 QString MiniDisplay::vendor()
169 return QString::fromWCharArray(iDisplay.Vendor());
172 QString MiniDisplay::product()
174 return QString::fromWCharArray(iDisplay.Product());
177 QString MiniDisplay::serialNumber()
179 return QString::fromWCharArray(iDisplay.SerialNumber());
183 * @brief MiniDisplay::readTimer
184 * Called when our read timer completes.
185 * We then attempt to complete our pending display request.
186 * We typically attempt to read an input report from our HID device.
188 void MiniDisplay::readTimer()
190 if (!iDisplay.IsOpen()) return;
191 if (!iDisplay.RequestPending()) return;
194 GP1212A01A::Request request = iDisplay.AttemptRequestCompletion();
198 if (iReadAttempt<KMaxReadAttempt)
200 //Will try again later
201 QTimer::singleShot(4, this, SLOT(readTimer()));
207 //TODO: Find a way to remove device specific stuff from here
208 if (request==GP1212A01A::ERequestFirmwareRevision)
210 QString version=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
211 emit firmwareVersion(version);
213 else if (request==GP1212A01A::ERequestDeviceId)
215 QString id=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
218 else if (request==GP1212A01A::ERequestPowerSupplyStatus)
220 emit powerStatus(iDisplay.PowerOn());
225 int MiniDisplay::maxBrightness() const
227 return iDisplay.MaxBrightness();
230 int MiniDisplay::minBrightness() const
232 return iDisplay.MinBrightness();
235 int MiniDisplay::brightness() const
240 void MiniDisplay::setBrightness(int aBrightness)
242 //Still track the brightness when disconnected
243 iBrightness=aBrightness;
245 if (!iDisplay.IsOpen()) return;
247 iDisplay.SetBrightness(aBrightness);
250 QPoint MiniDisplay::framePosition() const
252 return QPoint(iDisplay.DisplayPositionX(),iDisplay.DisplayPositionX());
255 void MiniDisplay::setFramePosition(const QPoint& aPoint)
257 iFramePosition = aPoint;
259 if (!iDisplay.IsOpen()) return;
261 iDisplay.SetDisplayPosition(aPoint.x(),aPoint.y());