MiniDisplay/minidisplay.cpp
author sl
Wed, 28 May 2014 18:44:15 +0200
changeset 11 b935de604982
parent 10 64cfde8062c7
child 12 f0c61338a2e8
permissions -rw-r--r--
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"
     2 #include <QPainter>
     3 #include <QTimer>
     4 
     5 const int KMaxReadAttempt=100;
     6 
     7 MiniDisplay::MiniDisplay(QQuickItem *parent):
     8     QQuickItem(parent),iReadAttempt(0),iBrightness(0)
     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.width(strDemo);
   126     int h=metrics.height();
   127     QSize size(w,h);
   128     QImage image(size,QImage::Format_Mono);
   129     image.fill(0xFFFFFFFF);
   130     //Draw some text into our image
   131     {
   132         QPainter painter(&image);
   133         //painter.begin(&image);
   134         painter.setPen(0xFF000000);
   135         painter.setFont(iFont);
   136         painter.drawText(0,metrics.ascent(),strDemo);
   137     }
   138     //Save image as PNG for validation
   139     image.save("font.png");
   140     //
   141     //int sizeInBytes=image.byteCount();
   142     int pixelCount=w*h;
   143     BitArray bits(pixelCount);
   144 
   145     for (int i=0;i<w;i++)
   146         {
   147         for (int j=0;j<h;j++)
   148             {
   149             QRgb color=image.pixel(i,j);
   150             if (color!=0xffffffff)
   151                 {
   152                 bits.SetBit(i*h+j);
   153                 }
   154             }
   155         }
   156 
   157     if (iDisplay.IsOpen())
   158     {
   159         iDisplay.Clear();
   160         iDisplay.BitBlit(bits,w,h,0,0);
   161         iDisplay.SwapBuffers();
   162     }
   163     //
   164 }
   165 
   166 
   167 QString MiniDisplay::vendor()
   168 {
   169     return QString::fromWCharArray(iDisplay.Vendor());
   170 }
   171 
   172 QString MiniDisplay::product()
   173 {
   174     return QString::fromWCharArray(iDisplay.Product());
   175 }
   176 
   177 QString MiniDisplay::serialNumber()
   178 {
   179     return QString::fromWCharArray(iDisplay.SerialNumber());
   180 }
   181 
   182 /**
   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.
   187  */
   188 void MiniDisplay::readTimer()
   189 {
   190     if (!iDisplay.IsOpen()) return;
   191     if (!iDisplay.RequestPending()) return;
   192 
   193     iReadAttempt++;
   194     GP1212A01A::Request request = iDisplay.AttemptRequestCompletion();
   195 
   196     if (!request)
   197     {
   198         if (iReadAttempt<KMaxReadAttempt)
   199         {
   200             //Will try again later
   201             QTimer::singleShot(4, this, SLOT(readTimer()));
   202         }
   203 
   204         return;
   205     }
   206 
   207     //TODO: Find a way to remove device specific stuff from here
   208     if (request==GP1212A01A::ERequestFirmwareRevision)
   209     {
   210         QString version=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   211         emit firmwareVersion(version);
   212     }
   213     else if (request==GP1212A01A::ERequestDeviceId)
   214     {
   215         QString id=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   216         emit deviceId(id);
   217     }
   218     else if (request==GP1212A01A::ERequestPowerSupplyStatus)
   219     {
   220         emit powerStatus(iDisplay.PowerOn());
   221     }
   222 }
   223 
   224 
   225 int MiniDisplay::maxBrightness() const
   226 {
   227     return iDisplay.MaxBrightness();
   228 }
   229 
   230 int MiniDisplay::minBrightness() const
   231 {
   232     return iDisplay.MinBrightness();
   233 }
   234 
   235 int MiniDisplay::brightness() const
   236 {
   237     return iBrightness;
   238 }
   239 
   240 void MiniDisplay::setBrightness(int aBrightness)
   241 {
   242     //Still track the brightness when disconnected
   243     iBrightness=aBrightness;
   244 
   245     if (!iDisplay.IsOpen()) return;
   246 
   247     iDisplay.SetBrightness(aBrightness);
   248 }
   249 
   250 QPoint MiniDisplay::framePosition() const
   251 {
   252     return QPoint(iDisplay.DisplayPositionX(),iDisplay.DisplayPositionX());
   253 }
   254 
   255 void MiniDisplay::setFramePosition(const QPoint& aPoint)
   256 {
   257     iFramePosition = aPoint;
   258 
   259     if (!iDisplay.IsOpen()) return;
   260 
   261     iDisplay.SetDisplayPosition(aPoint.x(),aPoint.y());
   262 }
   263 
   264 
   265