MiniDisplay/minidisplay.cpp
author sl
Thu, 29 May 2014 14:27:08 +0200
changeset 15 737f8bb110be
parent 14 9903a5edeb56
child 16 35f01a0d460a
permissions -rw-r--r--
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"
     2 #include <QPainter>
     3 #include <QTimer>
     4 
     5 const int KMaxReadAttempt=100;
     6 
     7 MiniDisplay::MiniDisplay(QQuickItem *parent):
     8     QQuickItem(parent),iReadAttempt(0),iBrightness(iDisplay.MaxBrightness())
     9 {
    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()
    13 
    14     // setFlag(ItemHasContents, true);
    15     //
    16     //qDebug() << "New MiniDisplay";
    17 }
    18 
    19 MiniDisplay::~MiniDisplay()
    20 {
    21     //qDebug() << "Delete MiniDisplay";
    22     close();
    23 }
    24 
    25 
    26 void MiniDisplay::open()
    27 {
    28     if (iDisplay.Open())
    29     {
    30         iDisplay.SetBrightness(iBrightness);
    31         emit opened();
    32         emit statusChanged();
    33     }
    34     else
    35     {
    36         emit openError();
    37     }
    38 }
    39 
    40 
    41 void MiniDisplay::close()
    42 {
    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())
    46     {
    47         emit closing();
    48 
    49         if (iDisplay.DisplayPositionY()%128!=0)
    50         {
    51             //qDebug() << "SwapBuffer to put back our frame position to zero " << iDisplay.DisplayPositionY();
    52             iDisplay.SwapBuffers();
    53         }
    54     }
    55     iDisplay.Close();
    56     emit closed();
    57     emit statusChanged();
    58 }
    59 
    60 bool MiniDisplay::isOpen()
    61 {
    62     return iDisplay.IsOpen();
    63 }
    64 
    65 void MiniDisplay::clear()
    66 {
    67     if (!iDisplay.IsOpen()) return;
    68     //
    69     iDisplay.Clear();
    70 }
    71 
    72 void MiniDisplay::fill()
    73 {
    74     if (!iDisplay.IsOpen()) return;
    75     //
    76     iDisplay.SetAllPixels(0xFF);
    77 }
    78 
    79 void MiniDisplay::swapBuffers()
    80 {
    81     if (!iDisplay.IsOpen()) return;
    82     //
    83     iDisplay.SwapBuffers();
    84 }
    85 
    86 void MiniDisplay::requestPowerStatus()
    87 {
    88     if (!iDisplay.IsOpen()) return;
    89 
    90     iDisplay.RequestPowerSupplyStatus();
    91     QTimer::singleShot(4, this, SLOT(readTimer()));
    92     iReadAttempt=0;
    93 }
    94 
    95 void MiniDisplay::requestDeviceId()
    96 {
    97     if (!iDisplay.IsOpen()) return;
    98 
    99     iDisplay.RequestDeviceId();
   100     QTimer::singleShot(4, this, SLOT(readTimer()));
   101     iReadAttempt=0;
   102 }
   103 
   104 void MiniDisplay::requestFirmwareVersion()
   105 {
   106     if (!iDisplay.IsOpen()) return;
   107 
   108     iDisplay.RequestFirmwareRevision();
   109     QTimer::singleShot(4, this, SLOT(readTimer()));
   110     iReadAttempt=0;
   111 }
   112 
   113 
   114 
   115 /**
   116  * @brief setFont
   117  * @param aFont
   118  */
   119 void MiniDisplay::setFont(const QFont& aFont)
   120 {
   121     iFont=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();
   128     QSize size(w,h);
   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
   133     {
   134         QPainter painter(&image);
   135         painter.setPen(0xFF000000);
   136         painter.setFont(iFont);
   137         painter.drawText(0,metrics.ascent(),strDemo);
   138     }
   139     //Save image as PNG for validation
   140     //image.save("font.png");
   141     //
   142     renderImage(&image);
   143 }
   144 
   145 /**
   146  * @brief MiniDisplay::renderWindow
   147  */
   148 void MiniDisplay::renderOwnWindow()
   149 {
   150     QImage image=window()->grabWindow();
   151     //image.save("window.png");
   152     renderImage(&image);
   153 }
   154 
   155 /**
   156  * @brief MiniDisplay::render
   157  * @param aWindow
   158  */
   159 void MiniDisplay::renderWindow(QQuickWindow* aWindow)
   160 {
   161     QImage image=aWindow->grabWindow();
   162     //image.save("window.png");
   163     renderImage(&image);
   164 }
   165 
   166 /**
   167  * @brief MiniDisplay::render
   168  * @param aImage
   169  */
   170 void MiniDisplay::renderImage(QImage* aImage)
   171 {
   172     if (!iDisplay.IsOpen())
   173     {
   174         return;
   175     }
   176 
   177     //Convert our image into a bit array
   178     int w=aImage->width();
   179     int h=aImage->height();
   180 
   181     int pixelCount=w*h;
   182     BitArray bits(pixelCount);
   183 
   184     for (int i=0;i<w;i++)
   185         {
   186         for (int j=0;j<h;j++)
   187             {
   188             QRgb color=aImage->pixel(i,j);
   189             if (color!=0xffffffff)
   190                 {
   191                 bits.SetBit(i*h+j);
   192                 }
   193             }
   194         }
   195 
   196     //Just blit it then
   197     iDisplay.BitBlit(bits,w,h,0,0);
   198 }
   199 
   200 /**
   201  * @brief MiniDisplay::vendor
   202  * @return
   203  */
   204 QString MiniDisplay::vendor()
   205 {
   206     return QString::fromWCharArray(iDisplay.Vendor());
   207 }
   208 
   209 /**
   210  * @brief MiniDisplay::product
   211  * @return
   212  */
   213 QString MiniDisplay::product()
   214 {
   215     return QString::fromWCharArray(iDisplay.Product());
   216 }
   217 
   218 /**
   219  * @brief MiniDisplay::serialNumber
   220  * @return
   221  */
   222 QString MiniDisplay::serialNumber()
   223 {
   224     return QString::fromWCharArray(iDisplay.SerialNumber());
   225 }
   226 
   227 /**
   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.
   232  */
   233 void MiniDisplay::readTimer()
   234 {
   235     if (!iDisplay.IsOpen()) return;
   236     if (!iDisplay.RequestPending()) return;
   237 
   238     iReadAttempt++;
   239     GP1212A01A::Request request = iDisplay.AttemptRequestCompletion();
   240 
   241     if (!request)
   242     {
   243         if (iReadAttempt<KMaxReadAttempt)
   244         {
   245             //Will try again later
   246             QTimer::singleShot(4, this, SLOT(readTimer()));
   247         }
   248 
   249         return;
   250     }
   251 
   252     //TODO: Find a way to remove device specific stuff from here
   253     if (request==GP1212A01A::ERequestFirmwareRevision)
   254     {
   255         QString version=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   256         emit firmwareVersion(version);
   257     }
   258     else if (request==GP1212A01A::ERequestDeviceId)
   259     {
   260         QString id=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   261         emit deviceId(id);
   262     }
   263     else if (request==GP1212A01A::ERequestPowerSupplyStatus)
   264     {
   265         emit powerStatus(iDisplay.PowerOn());
   266     }
   267 }
   268 
   269 
   270 int MiniDisplay::maxBrightness() const
   271 {
   272     return iDisplay.MaxBrightness();
   273 }
   274 
   275 int MiniDisplay::minBrightness() const
   276 {
   277     return iDisplay.MinBrightness();
   278 }
   279 
   280 int MiniDisplay::brightness() const
   281 {
   282     return iBrightness;
   283 }
   284 
   285 void MiniDisplay::setBrightness(int aBrightness)
   286 {
   287     //Still track the brightness when disconnected
   288     iBrightness=aBrightness;
   289 
   290     if (!iDisplay.IsOpen()) return;
   291 
   292     iDisplay.SetBrightness(aBrightness);
   293 }
   294 
   295 QPoint MiniDisplay::framePosition() const
   296 {
   297     return QPoint(iDisplay.DisplayPositionX(),iDisplay.DisplayPositionX());
   298 }
   299 
   300 void MiniDisplay::setFramePosition(const QPoint& aPoint)
   301 {
   302     iFramePosition = aPoint;
   303 
   304     if (!iDisplay.IsOpen()) return;
   305 
   306     iDisplay.SetDisplayPosition(aPoint.x(),aPoint.y());
   307 }
   308 
   309 
   310