MiniDisplay/minidisplay.cpp
author sl
Wed, 28 May 2014 19:31:55 +0200
changeset 12 f0c61338a2e8
parent 11 b935de604982
child 14 9903a5edeb56
permissions -rw-r--r--
Fixing our font rendering issues by rendering into 32bits bitmap.
     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     //Rendering in mono is quite broken we need RGB32
   129     QImage image(size,QImage::Format_RGB32);
   130     image.fill(0xFFFFFFFF);
   131     //Draw some text into our image
   132     {
   133         QPainter painter(&image);
   134         //painter.begin(&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     //int sizeInBytes=image.byteCount();
   143     int pixelCount=w*h;
   144     BitArray bits(pixelCount);
   145 
   146     for (int i=0;i<w;i++)
   147         {
   148         for (int j=0;j<h;j++)
   149             {
   150             QRgb color=image.pixel(i,j);
   151             if (color!=0xffffffff)
   152                 {
   153                 bits.SetBit(i*h+j);
   154                 }
   155             }
   156         }
   157 
   158     if (iDisplay.IsOpen())
   159     {
   160         iDisplay.Clear();
   161         iDisplay.BitBlit(bits,w,h,0,0);
   162         iDisplay.SwapBuffers();
   163     }
   164     //
   165 }
   166 
   167 
   168 QString MiniDisplay::vendor()
   169 {
   170     return QString::fromWCharArray(iDisplay.Vendor());
   171 }
   172 
   173 QString MiniDisplay::product()
   174 {
   175     return QString::fromWCharArray(iDisplay.Product());
   176 }
   177 
   178 QString MiniDisplay::serialNumber()
   179 {
   180     return QString::fromWCharArray(iDisplay.SerialNumber());
   181 }
   182 
   183 /**
   184  * @brief MiniDisplay::readTimer
   185  * Called when our read timer completes.
   186  * We then attempt to complete our pending display request.
   187  * We typically attempt to read an input report from our HID device.
   188  */
   189 void MiniDisplay::readTimer()
   190 {
   191     if (!iDisplay.IsOpen()) return;
   192     if (!iDisplay.RequestPending()) return;
   193 
   194     iReadAttempt++;
   195     GP1212A01A::Request request = iDisplay.AttemptRequestCompletion();
   196 
   197     if (!request)
   198     {
   199         if (iReadAttempt<KMaxReadAttempt)
   200         {
   201             //Will try again later
   202             QTimer::singleShot(4, this, SLOT(readTimer()));
   203         }
   204 
   205         return;
   206     }
   207 
   208     //TODO: Find a way to remove device specific stuff from here
   209     if (request==GP1212A01A::ERequestFirmwareRevision)
   210     {
   211         QString version=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   212         emit firmwareVersion(version);
   213     }
   214     else if (request==GP1212A01A::ERequestDeviceId)
   215     {
   216         QString id=QString::fromLocal8Bit((const char*)iDisplay.InputReport().Buffer()+1);
   217         emit deviceId(id);
   218     }
   219     else if (request==GP1212A01A::ERequestPowerSupplyStatus)
   220     {
   221         emit powerStatus(iDisplay.PowerOn());
   222     }
   223 }
   224 
   225 
   226 int MiniDisplay::maxBrightness() const
   227 {
   228     return iDisplay.MaxBrightness();
   229 }
   230 
   231 int MiniDisplay::minBrightness() const
   232 {
   233     return iDisplay.MinBrightness();
   234 }
   235 
   236 int MiniDisplay::brightness() const
   237 {
   238     return iBrightness;
   239 }
   240 
   241 void MiniDisplay::setBrightness(int aBrightness)
   242 {
   243     //Still track the brightness when disconnected
   244     iBrightness=aBrightness;
   245 
   246     if (!iDisplay.IsOpen()) return;
   247 
   248     iDisplay.SetBrightness(aBrightness);
   249 }
   250 
   251 QPoint MiniDisplay::framePosition() const
   252 {
   253     return QPoint(iDisplay.DisplayPositionX(),iDisplay.DisplayPositionX());
   254 }
   255 
   256 void MiniDisplay::setFramePosition(const QPoint& aPoint)
   257 {
   258     iFramePosition = aPoint;
   259 
   260     if (!iDisplay.IsOpen()) return;
   261 
   262     iDisplay.SetDisplayPosition(aPoint.x(),aPoint.y());
   263 }
   264 
   265 
   266