MiniDisplay/minidisplay.cpp
author sl
Sun, 01 Jun 2014 21:15:33 +0200
changeset 23 1f607fa8542f
parent 18 79801cc3bc94
child 29 a42cc76a2d5a
permissions -rw-r--r--
Adding an option to disable frame differencing.
     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::setPixel
   202  * @param x
   203  * @param y
   204  * @param on
   205  */
   206 void MiniDisplay::setPixel(int x, int y, bool on)
   207 {
   208     if (!iDisplay.IsOpen())
   209     {
   210         return;
   211     }
   212 
   213     iDisplay.SetPixel(x,y,on);
   214 }
   215 
   216 /**
   217  * @brief MiniDisplay::offScreenMode
   218  * @return
   219  */
   220 bool MiniDisplay::offScreenMode()
   221 {
   222    return iDisplay.OffScreenMode();
   223 }
   224 
   225 /**
   226  * @brief MiniDisplay::setOffScreenMode
   227  * @param aOn
   228  */
   229 void MiniDisplay::setOffScreenMode(bool aOn)
   230 {
   231     if (!iDisplay.IsOpen())
   232     {
   233         return;
   234     }
   235 
   236     iDisplay.SetOffScreenMode(aOn);
   237 }
   238 
   239 
   240 
   241 
   242 /**
   243  * @brief MiniDisplay::vendor
   244  * @return
   245  */
   246 QString MiniDisplay::vendor()
   247 {
   248     return QString::fromWCharArray(iDisplay.Vendor());
   249 }
   250 
   251 /**
   252  * @brief MiniDisplay::product
   253  * @return
   254  */
   255 QString MiniDisplay::product()
   256 {
   257     return QString::fromWCharArray(iDisplay.Product());
   258 }
   259 
   260 /**
   261  * @brief MiniDisplay::serialNumber
   262  * @return
   263  */
   264 QString MiniDisplay::serialNumber()
   265 {
   266     return QString::fromWCharArray(iDisplay.SerialNumber());
   267 }
   268 
   269 /**
   270  * @brief MiniDisplay::readTimer
   271  * Called when our read timer completes.
   272  * We then attempt to complete our pending display request.
   273  * We typically attempt to read an input report from our HID device.
   274  */
   275 void MiniDisplay::readTimer()
   276 {
   277     if (!iDisplay.IsOpen()) return;
   278     if (!iDisplay.RequestPending()) return;
   279 
   280     iReadAttempt++;
   281     GP1212A01A::Request request = iDisplay.AttemptRequestCompletion();
   282 
   283     if (!request)
   284     {
   285         if (iReadAttempt<KMaxReadAttempt)
   286         {
   287             //Will try again later
   288             QTimer::singleShot(4, this, SLOT(readTimer()));
   289         }
   290 
   291         return;
   292     }
   293 
   294     //TODO: Find a way to remove device specific stuff from here
   295     if (request==GP1212A01A::ERequestFirmwareRevision)
   296     {
   297         QString version=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   298         emit firmwareVersion(version);
   299     }
   300     else if (request==GP1212A01A::ERequestDeviceId)
   301     {
   302         QString id=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   303         emit deviceId(id);
   304     }
   305     else if (request==GP1212A01A::ERequestPowerSupplyStatus)
   306     {
   307         emit powerStatus(iDisplay.PowerOn());
   308     }
   309 }
   310 
   311 
   312 int MiniDisplay::maxBrightness() const
   313 {
   314     return iDisplay.MaxBrightness();
   315 }
   316 
   317 int MiniDisplay::minBrightness() const
   318 {
   319     return iDisplay.MinBrightness();
   320 }
   321 
   322 int MiniDisplay::brightness() const
   323 {
   324     return iBrightness;
   325 }
   326 
   327 void MiniDisplay::setBrightness(int aBrightness)
   328 {
   329     //Still track the brightness when disconnected
   330     iBrightness=aBrightness;
   331 
   332     if (!iDisplay.IsOpen()) return;
   333 
   334     iDisplay.SetBrightness(aBrightness);
   335 }
   336 
   337 /**
   338 @brief MiniDisplay::frameDifferencing
   339 @return
   340 */
   341 bool MiniDisplay::frameDifferencing() const
   342 {
   343     return iDisplay.FrameDifferencing();
   344 }
   345 
   346 /**
   347 @brief MiniDisplay::setFrameDifferencing
   348 @param aOn
   349 */
   350 void MiniDisplay::setFrameDifferencing(bool aOn)
   351 {
   352     iDisplay.SetFrameDifferencing(aOn);
   353 }
   354 
   355 
   356 QPoint MiniDisplay::framePosition() const
   357 {
   358     return QPoint(iDisplay.DisplayPositionX(),iDisplay.DisplayPositionX());
   359 }
   360 
   361 void MiniDisplay::setFramePosition(const QPoint& aPoint)
   362 {
   363     iFramePosition = aPoint;
   364 
   365     if (!iDisplay.IsOpen()) return;
   366 
   367     iDisplay.SetDisplayPosition(aPoint.x(),aPoint.y());
   368 }
   369 
   370 
   371