FutabaVfd.cpp
author sl
Mon, 07 Jul 2014 21:32:47 +0200
changeset 2 d8186e7b69bf
child 4 36894fc4dad6
permissions -rw-r--r--
Implementing and C API to control our MiniDisplay.
sl@0
     1
sl@0
     2
#include "FutabaVfd.h"
sl@0
     3
//#include <stdlib.h>
sl@0
     4
#include <string.h>
sl@0
     5
sl@0
     6
sl@0
     7
#ifdef DEBUG_FRAME_DIFF
sl@0
     8
#include <QImage>
sl@0
     9
#include <QTextStream>
sl@0
    10
#endif
sl@0
    11
sl@0
    12
sl@0
    13
const int KNumberOfFrameBeforeDiffAlgo = 3;
sl@0
    14
sl@0
    15
//
sl@0
    16
//
sl@0
    17
//
sl@0
    18
sl@0
    19
FutabaVfdCommand::FutabaVfdCommand():/*iBuffer(NULL),*/iSize(0),iMaxSize(0)
sl@0
    20
    {
sl@0
    21
    }
sl@0
    22
sl@0
    23
FutabaVfdCommand::~FutabaVfdCommand()
sl@0
    24
    {
sl@0
    25
    //Delete();
sl@0
    26
    }
sl@0
    27
sl@0
    28
sl@0
    29
/**
sl@0
    30
sl@0
    31
*/
sl@0
    32
void FutabaVfdCommand::Reset()
sl@0
    33
    {
sl@0
    34
    memset(iReports,0,sizeof(iReports));
sl@0
    35
    }
sl@0
    36
sl@0
    37
sl@0
    38
sl@0
    39
/**
sl@0
    40
sl@0
    41
*/
sl@0
    42
/*
sl@0
    43
void FutabaVfdCommand::Create(int aMaxSize)
sl@0
    44
    {
sl@0
    45
    iBuffer=new unsigned char[aMaxSize];
sl@0
    46
    if (iBuffer)
sl@0
    47
        {
sl@0
    48
        iMaxSize = aMaxSize;
sl@0
    49
        iSize = 0;
sl@0
    50
        }
sl@0
    51
    }
sl@0
    52
*/
sl@0
    53
sl@0
    54
/**
sl@0
    55
sl@0
    56
*/
sl@0
    57
/*
sl@0
    58
void FutabaVfdCommand::Delete()
sl@0
    59
{
sl@0
    60
    delete[] iBuffer;
sl@0
    61
    iBuffer = NULL;
sl@0
    62
    iMaxSize = 0;
sl@0
    63
    iSize = 0;
sl@0
    64
}
sl@0
    65
*/
sl@0
    66
sl@0
    67
sl@0
    68
sl@0
    69
sl@0
    70
//
sl@0
    71
// class GP1212A01A
sl@0
    72
//
sl@0
    73
sl@0
    74
GP1212A01A::GP1212A01A():
sl@0
    75
	iDisplayPositionX(0),iDisplayPositionY(0),
sl@0
    76
    iOffScreenMode(true),
sl@0
    77
    iUseFrameDifferencing(true),
sl@0
    78
    iFrameNext(NULL),
sl@0
    79
    iFrameCurrent(NULL),
sl@0
    80
    iFramePrevious(NULL),
sl@0
    81
    iFrameAlpha(NULL),
sl@0
    82
    iFrameBeta(NULL),
sl@0
    83
    iFrameGamma(NULL),
sl@0
    84
    iNeedFullFrameUpdate(0),
sl@0
    85
    iRequest(ERequestNone),iPowerOn(false)
sl@0
    86
	{
sl@0
    87
	//ResetBuffers();
sl@0
    88
	}
sl@0
    89
sl@0
    90
/**
sl@0
    91
*/
sl@0
    92
GP1212A01A::~GP1212A01A()
sl@0
    93
	{
sl@0
    94
    delete iFrameAlpha;
sl@0
    95
    iFrameAlpha=NULL;
sl@0
    96
    //
sl@0
    97
    delete iFrameBeta;
sl@0
    98
    iFrameBeta=NULL;
sl@0
    99
    //
sl@0
   100
    delete iFrameGamma;
sl@0
   101
    iFrameGamma=NULL;
sl@0
   102
    //
sl@0
   103
    iFrameNext=NULL;
sl@0
   104
    iFrameCurrent=NULL;
sl@0
   105
    iFramePrevious=NULL;
sl@0
   106
    //
sl@0
   107
    iNeedFullFrameUpdate=0;
sl@0
   108
	}
sl@0
   109
sl@0
   110
/**
sl@0
   111
*/
sl@0
   112
int GP1212A01A::Open()
sl@0
   113
	{
sl@0
   114
	int success = HidDevice::Open(KFutabaVendorId,KFutabaProductIdGP1212A01A,NULL);
sl@0
   115
	if (success)
sl@0
   116
		{
sl@0
   117
        //Allocate both frames
sl@0
   118
        delete iFrameAlpha;
sl@0
   119
        iFrameAlpha=NULL;
sl@0
   120
        iFrameAlpha=new BitArray(KGP12xFrameBufferPixelCount);
sl@0
   121
        //
sl@0
   122
        delete iFrameBeta;
sl@0
   123
        iFrameBeta=NULL;
sl@0
   124
        iFrameBeta=new BitArray(KGP12xFrameBufferPixelCount);
sl@0
   125
        //
sl@0
   126
        delete iFrameGamma;
sl@0
   127
        iFrameGamma=NULL;
sl@0
   128
        iFrameGamma=new BitArray(KGP12xFrameBufferPixelCount);
sl@0
   129
        //
sl@0
   130
        iFrameNext=iFrameAlpha;
sl@0
   131
        iFrameCurrent=iFrameBeta;
sl@0
   132
        iFramePrevious=iFrameGamma;
sl@0
   133
sl@0
   134
sl@0
   135
        //To make sure it is synced properly
sl@0
   136
        iNeedFullFrameUpdate=0;
sl@0
   137
        //
sl@0
   138
		SetNonBlocking(1);
sl@0
   139
        //Since we can't get our display position we force it to our default
sl@0
   140
		//This makes sure frames are in sync from the start
sl@0
   141
        //Clever clients will have taken care of putting back frame (0,0) before closing
sl@0
   142
		SetDisplayPosition(iDisplayPositionX,iDisplayPositionY);
sl@0
   143
		}
sl@0
   144
	return success;
sl@0
   145
	}
sl@0
   146
sl@0
   147
/**
sl@0
   148
*/
sl@0
   149
void GP1212A01A::SetPixel(unsigned char aX, unsigned char aY, bool aOn)
sl@0
   150
	{
sl@0
   151
	//
sl@0
   152
	//int byteOffset=(aX*HeightInPixels()+aY)/8;
sl@0
   153
	//int bitOffset=(aX*HeightInPixels()+aY)%8;
sl@0
   154
    //iNextFrame[byteOffset] |= ( (aOn?0x01:0x00) << bitOffset );
sl@0
   155
sl@0
   156
    if (iOffScreenMode)
sl@0
   157
        {
sl@0
   158
        if (aOn)
sl@0
   159
            {
sl@0
   160
            iFrameNext->SetBit(aX*HeightInPixels()+aY);
sl@0
   161
            }
sl@0
   162
        else
sl@0
   163
            {
sl@0
   164
            iFrameNext->ClearBit(aX*HeightInPixels()+aY);
sl@0
   165
            }
sl@0
   166
        }
sl@0
   167
    else
sl@0
   168
        {
sl@0
   169
        //Just specify a one pixel block
sl@0
   170
        SetPixelBlock(aX,aY,0x00,0x01,aOn);
sl@0
   171
        }
sl@0
   172
	}
sl@0
   173
sl@0
   174
/**
sl@0
   175
*/
sl@0
   176
void GP1212A01A::BitBlit(const BitArray& aBitmap, int aSrcWidth, int aSrcHeight, int aTargetX, int aTargetY) const
sl@0
   177
	{
sl@0
   178
	//TODO: amend loop values so that we don't keep on looping past our frame buffer dimensions.
sl@0
   179
	for (int i=0;i<aSrcWidth;i++)
sl@0
   180
		{
sl@0
   181
		for (int j=0;j<aSrcHeight;j++)
sl@0
   182
			{
sl@0
   183
            iFrameNext->SetBitValue((aTargetX+i)*HeightInPixels()+aTargetY+j,aBitmap[+i*aSrcHeight+j]);
sl@0
   184
			}
sl@0
   185
		}
sl@0
   186
	}
sl@0
   187
sl@0
   188
/**
sl@0
   189
Clear our client side back buffer.
sl@0
   190
Call to SwapBuffers must follow to actually clear the display.
sl@0
   191
*/
sl@0
   192
void GP1212A01A::Clear()
sl@0
   193
    {
sl@0
   194
    //memset(iNextFrame->Ptr(),0x00,FrameBufferSizeInBytes());
sl@0
   195
    if (iOffScreenMode)
sl@0
   196
        {
sl@0
   197
        iFrameNext->ClearAll();
sl@0
   198
        }
sl@0
   199
    else
sl@0
   200
        {
sl@0
   201
        SendClearCommand();
sl@0
   202
        }
sl@0
   203
    }
sl@0
   204
sl@0
   205
/**
sl@0
   206
Set all pixels on our screen to the desired value.
sl@0
   207
This operation is performed off screen to avoid tearing.
sl@0
   208
@param 8 pixels pattern
sl@0
   209
*/
sl@0
   210
void GP1212A01A::SetAllPixels(unsigned char aPattern)
sl@0
   211
	{
sl@0
   212
	//With a single buffer
sl@0
   213
	//unsigned char screen[2048]; //One screen worth of pixels
sl@0
   214
	//memset(screen,0xFF,sizeof(screen));
sl@0
   215
	//SetPixelBlock(0,0,63,sizeof(screen),screen);
sl@0
   216
sl@0
   217
sl@0
   218
    if (iOffScreenMode)
sl@0
   219
        {
sl@0
   220
        memset(iFrameNext->Ptr(),aPattern,FrameBufferSizeInBytes());
sl@0
   221
        }
sl@0
   222
    else
sl@0
   223
        {
sl@0
   224
        //Using pattern SetPixelBlock variant.
sl@0
   225
        SetPixelBlock(0,0,63,FrameBufferSizeInBytes(),aPattern);
sl@0
   226
        }
sl@0
   227
	//
sl@0
   228
	}
sl@0
   229
sl@0
   230
sl@0
   231
/**
sl@0
   232
Set the defined pixel block to the given value.
sl@0
   233
@param X coordinate of our pixel block starting point.
sl@0
   234
@param Y coordinate of our pixel block starting point.
sl@0
   235
@param The height of our pixel block.
sl@0
   236
@param The size of our pixel data. Number of pixels divided by 8.
sl@0
   237
@param The value set to 8 pixels used as a pattern.
sl@0
   238
*/
sl@0
   239
void GP1212A01A::SetPixelBlock(unsigned char aX, unsigned char aY, int aHeight, int aSize, unsigned char aValue)
sl@0
   240
	{
sl@0
   241
	OffScreenTranslation(aX,aY);
sl@0
   242
    FutabaVfdReport report;
sl@0
   243
    report[0]=0x00; //Report ID
sl@0
   244
    report[1]=(aSize<=report.Size()-10?aSize+0x08:64); //Report length. -10 is for our header first 10 bytes. +8 is for our Futaba header size
sl@0
   245
    report[2]=0x1B; //Command ID
sl@0
   246
    report[3]=0x5B; //Command ID
sl@0
   247
    report[4]=0xF0; //Command ID
sl@0
   248
    report[5]=aX;   //X
sl@0
   249
    report[6]=aY;   //Y
sl@0
   250
    report[7]=aHeight; //Y length before return. Though outside the specs, setting this to zero apparently allows us to modify a single pixel without touching any other.
sl@0
   251
	report[8]=aSize>>8; //Size of pixel data in bytes (MSB)
sl@0
   252
	report[9]=aSize;	//Size of pixel data in bytes (LSB)
sl@0
   253
    int sizeWritten=MIN(aSize,report.Size()-10);
sl@0
   254
    memset(report.Buffer()+10, aValue, sizeWritten);
sl@0
   255
    Write(report);
sl@0
   256
sl@0
   257
    int remainingSize=aSize;
sl@0
   258
    //We need to keep on sending our pixel data until we are done
sl@0
   259
    while (report[1]==64)
sl@0
   260
        {
sl@0
   261
        report.Reset();
sl@0
   262
        remainingSize-=sizeWritten;
sl@0
   263
        report[0]=0x00; //Report ID
sl@0
   264
        report[1]=(remainingSize<=report.Size()-2?remainingSize:64); //Report length, should be 64 or the remaining size
sl@0
   265
        sizeWritten=(report[1]==64?63:report[1]);
sl@0
   266
        memset(report.Buffer()+2, aValue, sizeWritten);
sl@0
   267
        Write(report);
sl@0
   268
        }
sl@0
   269
	}
sl@0
   270
sl@0
   271
/**
sl@0
   272
Set the defined pixel block to the given value.
sl@0
   273
@param X coordinate of our pixel block starting point.
sl@0
   274
@param Y coordinate of our pixel block starting point.
sl@0
   275
@param The height of our pixel block.
sl@0
   276
@param The size of our pixel data. Number of pixels divided by 8.
sl@0
   277
@param Pointer to our pixel data.
sl@0
   278
*/
sl@0
   279
void GP1212A01A::SetPixelBlock(unsigned char aX, unsigned char aY, int aHeight, int aSize, unsigned char* aPixels)
sl@0
   280
    {
sl@0
   281
	OffScreenTranslation(aX,aY);
sl@0
   282
    FutabaVfdReport report;
sl@0
   283
    report[0]=0x00; //Report ID
sl@0
   284
    report[1]=(aSize<=report.Size()-10?aSize+0x08:64); //Report length. -10 is for our header first 10 bytes. +8 is for our Futaba header size
sl@0
   285
    report[2]=0x1B; //Command ID
sl@0
   286
    report[3]=0x5B; //Command ID
sl@0
   287
    report[4]=0xF0; //Command ID
sl@0
   288
    report[5]=aX;   //X
sl@0
   289
    report[6]=aY;   //Y
sl@0
   290
    report[7]=aHeight; //Y length before return. Though outside the specs, setting this to zero apparently allows us to modify a single pixel without touching any other.
sl@0
   291
	report[8]=aSize>>8; //Size of pixel data in bytes (MSB)
sl@0
   292
	report[9]=aSize;	//Size of pixel data in bytes (LSB)
sl@0
   293
    int sizeWritten=MIN(aSize,report.Size()-10);
sl@0
   294
    memcpy(report.Buffer()+10, aPixels, sizeWritten);
sl@0
   295
    Write(report);
sl@0
   296
sl@0
   297
    int remainingSize=aSize;
sl@0
   298
    //We need to keep on sending our pixel data until we are done
sl@0
   299
    while (report[1]==64)
sl@0
   300
        {
sl@0
   301
        report.Reset();
sl@0
   302
        remainingSize-=sizeWritten;
sl@0
   303
        report[0]=0x00; //Report ID
sl@0
   304
        report[1]=(remainingSize<=report.Size()-2?remainingSize:64); //Report length, should be 64 or the remaining size
sl@0
   305
        sizeWritten=(report[1]==64?63:report[1]);
sl@0
   306
        memcpy(report.Buffer()+2, aPixels+(aSize-remainingSize), sizeWritten);
sl@0
   307
        Write(report);
sl@0
   308
        }
sl@0
   309
    }
sl@0
   310
sl@0
   311
/**
sl@0
   312
Using this function is advised against as is causes tearing.
sl@0
   313
Use Clear instead.
sl@0
   314
*/
sl@0
   315
void GP1212A01A::SendClearCommand()
sl@0
   316
	{
sl@0
   317
    //1BH,5BH,32H,4AH
sl@0
   318
    //Send Clear Display Command
sl@0
   319
	FutabaVfdReport report;
sl@0
   320
	report[0]=0x00; //Report ID
sl@0
   321
	report[1]=0x04; //Report length
sl@0
   322
	report[2]=0x1B; //Command ID
sl@0
   323
	report[3]=0x5B; //Command ID
sl@0
   324
	report[4]=0x32; //Command ID
sl@0
   325
	report[5]=0x4A; //Command ID
sl@0
   326
	Write(report);
sl@0
   327
	}
sl@0
   328
sl@0
   329
/**
sl@0
   330
Change our display position within our buffer.
sl@0
   331
*/
sl@0
   332
void GP1212A01A::SetDisplayPosition(DW aDw,unsigned char aX, unsigned char aY)
sl@0
   333
    {
sl@0
   334
    //1BH,5BH,Dw,Px,Py
sl@0
   335
    //Send Display Position Settings Command
sl@0
   336
    FutabaVfdReport report;
sl@0
   337
    report[0]=0x00; //Report ID
sl@0
   338
    report[1]=0x05; //Report length
sl@0
   339
    report[2]=0x1B; //Command ID
sl@0
   340
    report[3]=0x5B; //Command ID
sl@0
   341
    report[4]=aDw;  //Specify our DW
sl@0
   342
    report[5]=aX;   //X coordinate of our DW top-left corner
sl@0
   343
    report[6]=aY;   //Y coordinate of our DW top-left corner
sl@0
   344
    Write(report);
sl@0
   345
    }
sl@0
   346
sl@0
   347
/**
sl@0
   348
Change our display position within our buffer.
sl@0
   349
*/
sl@0
   350
void GP1212A01A::SetDisplayPosition(unsigned char aX, unsigned char aY)
sl@0
   351
	{
sl@0
   352
	//Specs apparently says both DW should remain the same
sl@0
   353
	//Just don't ask
sl@0
   354
    SetDisplayPosition(GP1212A01A::DW1,aX,aY);
sl@0
   355
    SetDisplayPosition(GP1212A01A::DW2,aX,aY);
sl@0
   356
	iDisplayPositionX=aX;
sl@0
   357
	iDisplayPositionY=aY;
sl@0
   358
	}
sl@0
   359
sl@0
   360
/**
sl@0
   361
Provide Y coordinate of our off screen buffer.
sl@0
   362
*/
sl@0
   363
unsigned char GP1212A01A::OffScreenY() const
sl@0
   364
	{
sl@0
   365
	//Overflowing is fine this is just what we want
sl@0
   366
	return iDisplayPositionY+HeightInPixels();
sl@0
   367
	}
sl@0
   368
sl@0
   369
/**
sl@0
   370
Put our off screen buffer on screen.
sl@0
   371
On screen buffer goes off screen.
sl@0
   372
*/
sl@0
   373
void GP1212A01A::SwapBuffers()
sl@0
   374
	{
sl@0
   375
	//Only perform buffer swapping if off screen mode is enabled
sl@0
   376
	if (OffScreenMode())
sl@0
   377
		{
sl@0
   378
		//Send host back buffer to device back buffer
sl@0
   379
        if (!iUseFrameDifferencing || iNeedFullFrameUpdate<KNumberOfFrameBeforeDiffAlgo)
sl@0
   380
            {
sl@0
   381
            iNeedFullFrameUpdate++;
sl@0
   382
            SetPixelBlock(0,0,63,FrameBufferSizeInBytes(),iFrameNext->Ptr());
sl@0
   383
            }
sl@0
   384
        else
sl@0
   385
            {
sl@0
   386
            //Frame diff algo is enabled
sl@0
   387
            //We are going to send to our device only the differences between next frame and previous frame
sl@0
   388
            SendModifiedPixelBlocks();
sl@0
   389
            }
sl@0
   390
		//Swap device front and back buffer
sl@0
   391
		SetDisplayPosition(iDisplayPositionX,OffScreenY());
sl@0
   392
sl@0
   393
        //Cycle through our frame buffers
sl@0
   394
        //We keep track of previous frame which is in fact our device back buffer.
sl@0
   395
        //We can then compare previous and next frame and send only the differences to our device.
sl@0
   396
        //This mechanism allows us to reduce traffic over our USB bus thus improving our frame rate from 14 FPS to 30 FPS.
sl@0
   397
        //Keep our previous frame pointer
sl@0
   398
        BitArray* previousFrame=iFramePrevious;
sl@0
   399
        //Current frame becomes the previous one
sl@0
   400
        iFramePrevious = iFrameCurrent;
sl@0
   401
        //Next frame becomes the current one
sl@0
   402
        iFrameCurrent = iFrameNext;
sl@0
   403
        //Next frame is now our former previous
sl@0
   404
        iFrameNext = previousFrame;
sl@0
   405
		}
sl@0
   406
	}
sl@0
   407
sl@0
   408
sl@0
   409
//Define the edge of our pixel block
sl@0
   410
//Pixel blocks of 32x32 seems to run almost as fast as full screen update in worse case scenarii.
sl@0
   411
//Though I wonder if in some situations 16 could be better. Make this an attribute at some point if need be.
sl@0
   412
const int KPixelBlockEdge = 32;
sl@0
   413
const int KPixelBlockSizeInBits = KPixelBlockEdge*KPixelBlockEdge;
sl@0
   414
const int KPixelBlockSizeInBytes = KPixelBlockSizeInBits/8;
sl@0
   415
sl@0
   416
sl@0
   417
/**
sl@0
   418
 * @brief GP1212A01A::SendModifiedPixelBlocks
sl@0
   419
 * Compare our back and front buffer and send to the device only the modified pixels.
sl@0
   420
 */
sl@0
   421
void GP1212A01A::SendModifiedPixelBlocks()
sl@0
   422
    {
sl@0
   423
    int w=WidthInPixels();
sl@0
   424
    int h=HeightInPixels();
sl@0
   425
sl@0
   426
sl@0
   427
    //TODO: optimize with memcmp and 16 inc
sl@0
   428
    /*
sl@0
   429
sl@0
   430
    for (int i=0;i<w;i++)
sl@0
   431
        {
sl@0
   432
        for (int j=0;j<h;j++)
sl@0
   433
            {
sl@0
   434
            //aX*HeightInPixels()+aY
sl@0
   435
            if ((*iFrameNext)[i*h+j]!=(*iFramePrevious)[i*h+j])
sl@0
   436
                {
sl@0
   437
                //We need to update that pixel
sl@0
   438
                SetPixelBlock(i,j,0,1,((*iFrameNext)[i*h+j]?0x01:0x00));
sl@0
   439
                //SetDisplayPosition(iDisplayPositionX,OffScreenY());
sl@0
   440
                //SetDisplayPosition(iDisplayPositionX,OffScreenY());
sl@0
   441
sl@0
   442
                //SetPixelBlock(i,j,15,32,iNextFrame->Ptr()+offset);
sl@0
   443
                }
sl@0
   444
            }
sl@0
   445
        }
sl@0
   446
    */
sl@0
   447
sl@0
   448
    BitArray nextBlock(KPixelBlockSizeInBits);
sl@0
   449
    BitArray previousBlock(KPixelBlockSizeInBits);
sl@0
   450
sl@0
   451
    for (int i=0;i<w;i+=KPixelBlockEdge)
sl@0
   452
        {
sl@0
   453
        for (int j=0;j<h;j+=KPixelBlockEdge)
sl@0
   454
            {
sl@0
   455
            //aX*HeightInPixels()+aY
sl@0
   456
            //int offset=(i*w/8)+(j/8);
sl@0
   457
sl@0
   458
#ifdef DEBUG_FRAME_DIFF
sl@0
   459
            QImage imagePrevious(KPixelBlockEdge,KPixelBlockEdge,QImage::Format_RGB32);
sl@0
   460
            QImage imageNext(KPixelBlockEdge,KPixelBlockEdge,QImage::Format_RGB32);
sl@0
   461
#endif
sl@0
   462
sl@0
   463
            //Get both our blocks from our buffers
sl@0
   464
            for (int x=i;x<i+KPixelBlockEdge;x++)
sl@0
   465
                {
sl@0
   466
                for (int y=j;y<j+KPixelBlockEdge;y++)
sl@0
   467
                    {
sl@0
   468
                    int blockOffset=(x-i)*KPixelBlockEdge+(y-j);
sl@0
   469
                    int frameOffset=x*h+y;
sl@0
   470
                    nextBlock.SetBitValue(blockOffset,(*iFrameNext)[frameOffset]);
sl@0
   471
                    previousBlock.SetBitValue(blockOffset,(*iFramePrevious)[frameOffset]);
sl@0
   472
sl@0
   473
#ifdef DEBUG_FRAME_DIFF
sl@0
   474
                    imageNext.setPixel(x-i,y-j,(nextBlock[blockOffset]?0xFFFFFFFF:0x00000000));
sl@0
   475
                    imagePrevious.setPixel(x-i,y-j,(previousBlock[blockOffset]?0xFFFFFFFF:0x00000000));
sl@0
   476
#endif
sl@0
   477
                    }
sl@0
   478
                }
sl@0
   479
sl@0
   480
#ifdef DEBUG_FRAME_DIFF
sl@0
   481
            QString previousName;
sl@0
   482
            QString nextName;
sl@0
   483
            QTextStream(&previousName) << "p" << i << "x" << j << ".png";
sl@0
   484
            QTextStream(&nextName) << "n" << i << "x" << j << ".png";
sl@0
   485
            imagePrevious.save(previousName);
sl@0
   486
            imageNext.save(nextName);
sl@0
   487
#endif
sl@0
   488
sl@0
   489
sl@0
   490
            //if (memcmp(iFrameNext->Ptr()+offset,iFramePrevious->Ptr()+offset,32 )) //32=(16*16/8)
sl@0
   491
            if (memcmp(nextBlock.Ptr(),previousBlock.Ptr(),KPixelBlockSizeInBytes)!=0)
sl@0
   492
                {
sl@0
   493
                //We need to update that block
sl@0
   494
                SetPixelBlock(i,j,KPixelBlockEdge-1,KPixelBlockSizeInBytes,nextBlock.Ptr());
sl@0
   495
                //SetPixelBlock(i,j,15,32,0xFF/*nextBlock.Ptr()*/);
sl@0
   496
                //SetDisplayPosition(iDisplayPositionX,OffScreenY());
sl@0
   497
                //SetDisplayPosition(iDisplayPositionX,OffScreenY());
sl@0
   498
sl@0
   499
                //SetPixelBlock(i,j,15,32,iFrameNext->Ptr()+offset);
sl@0
   500
                }
sl@0
   501
            }
sl@0
   502
        }
sl@0
   503
sl@0
   504
    }
sl@0
   505
sl@0
   506
/**
sl@0
   507
Translate the given pixel coordinate according to our off screen mode.
sl@0
   508
*/
sl@0
   509
void GP1212A01A::OffScreenTranslation(unsigned char& aX, unsigned char& aY)
sl@0
   510
	{
sl@0
   511
	if (OffScreenMode())
sl@0
   512
		{
sl@0
   513
		aX+=WidthInPixels()-iDisplayPositionX;
sl@0
   514
		aY+=HeightInPixels()-iDisplayPositionY;
sl@0
   515
		}
sl@0
   516
	}
sl@0
   517
sl@0
   518
sl@0
   519
/**
sl@0
   520
*/
sl@0
   521
void GP1212A01A::ResetBuffers()
sl@0
   522
	{
sl@0
   523
    //iNextFrame->ClearAll();
sl@0
   524
    //memset(iFrameAlpha,0x00,sizeof(iFrameAlpha));
sl@0
   525
	//memset(iFrameBeta,0x00,sizeof(iFrameBeta));
sl@0
   526
	}
sl@0
   527
sl@0
   528
/**
sl@0
   529
*/
sl@0
   530
void GP1212A01A::RequestDeviceId()
sl@0
   531
    {
sl@0
   532
    if (RequestPending())
sl@0
   533
        {
sl@0
   534
        //Abort silently for now
sl@0
   535
        return;
sl@0
   536
        }
sl@0
   537
sl@0
   538
    //1BH,5BH,63H,49H,44H
sl@0
   539
    //Send Read ID command
sl@0
   540
    FutabaVfdReport report;
sl@0
   541
    report[0]=0x00; //Report ID
sl@0
   542
    report[1]=0x05; //Report length
sl@0
   543
    report[2]=0x1B; //Command ID
sl@0
   544
    report[3]=0x5B; //Command ID
sl@0
   545
    report[4]=0x63; //Command ID
sl@0
   546
    report[5]=0x49; //Command ID
sl@0
   547
    report[6]=0x44; //Command ID
sl@0
   548
    if (Write(report)==report.Size())
sl@0
   549
        {
sl@0
   550
        iRequest=ERequestDeviceId;
sl@0
   551
        }
sl@0
   552
    }
sl@0
   553
sl@0
   554
/**
sl@0
   555
*/
sl@0
   556
void GP1212A01A::RequestFirmwareRevision()
sl@0
   557
    {
sl@0
   558
    if (RequestPending())
sl@0
   559
        {
sl@0
   560
        //Abort silently for now
sl@0
   561
        return;
sl@0
   562
        }
sl@0
   563
sl@0
   564
    //1BH,5BH,63H,46H,52H
sl@0
   565
    //Send Software Revision Read Command
sl@0
   566
    FutabaVfdReport report;
sl@0
   567
    report[0]=0x00; //Report ID
sl@0
   568
    report[1]=0x05; //Report length
sl@0
   569
    report[2]=0x1B; //Command ID
sl@0
   570
    report[3]=0x5B; //Command ID
sl@0
   571
    report[4]=0x63; //Command ID
sl@0
   572
    report[5]=0x46; //Command ID
sl@0
   573
    report[6]=0x52; //Command ID
sl@0
   574
    if (Write(report)==report.Size())
sl@0
   575
        {
sl@0
   576
        iRequest=ERequestFirmwareRevision;
sl@0
   577
        }
sl@0
   578
    }
sl@0
   579
sl@0
   580
/**
sl@0
   581
*/
sl@0
   582
void GP1212A01A::RequestPowerSupplyStatus()
sl@0
   583
    {
sl@0
   584
    if (RequestPending())
sl@0
   585
        {
sl@0
   586
        //Abort silently for now
sl@0
   587
        return;
sl@0
   588
        }
sl@0
   589
    //1BH,5BH,63H,50H,4DH
sl@0
   590
    //Send Power Suppply Monitor Command
sl@0
   591
    FutabaVfdReport report;
sl@0
   592
    report[0]=0x00; //Report ID
sl@0
   593
    report[1]=0x05; //Report length
sl@0
   594
    report[2]=0x1B; //Command ID
sl@0
   595
    report[3]=0x5B; //Command ID
sl@0
   596
    report[4]=0x63; //Command ID
sl@0
   597
    report[5]=0x50; //Command ID
sl@0
   598
    report[6]=0x4D; //Command ID
sl@0
   599
    if (Write(report)==report.Size())
sl@0
   600
        {
sl@0
   601
        iRequest=ERequestPowerSupplyStatus;
sl@0
   602
        }
sl@0
   603
    }
sl@0
   604
sl@0
   605
sl@0
   606
/**
sl@0
   607
This is for development purposes only.
sl@0
   608
Production application should stick to off-screen mode to avoid tearing.
sl@0
   609
*/
sl@0
   610
void GP1212A01A::ToggleOffScreenMode()
sl@0
   611
	{
sl@0
   612
    SetOffScreenMode(!iOffScreenMode);
sl@0
   613
	}
sl@0
   614
sl@0
   615
/**
sl@0
   616
 * @brief GP1212A01A::SetOffScreenMode
sl@0
   617
 * @param aOn
sl@0
   618
 * @return
sl@0
   619
 */
sl@0
   620
void GP1212A01A::SetOffScreenMode(bool aOn)
sl@0
   621
    {
sl@0
   622
    if (aOn==iOffScreenMode)
sl@0
   623
    {
sl@0
   624
        //Nothing to do here
sl@0
   625
        return;
sl@0
   626
    }
sl@0
   627
sl@0
   628
    iOffScreenMode=aOn;
sl@0
   629
sl@0
   630
    //Clean up our buffers upon switching modes
sl@0
   631
    SetDisplayPosition(0,0);
sl@0
   632
    Clear();
sl@0
   633
    SwapBuffers();
sl@0
   634
    Clear();
sl@0
   635
    }
sl@0
   636
sl@0
   637
/**
sl@0
   638
 */
sl@0
   639
GP1212A01A::Request GP1212A01A::AttemptRequestCompletion()
sl@0
   640
    {
sl@0
   641
    if (!RequestPending())
sl@0
   642
        {
sl@0
   643
        return ERequestNone;
sl@0
   644
        }
sl@0
   645
sl@0
   646
    int res=Read(iInputReport);
sl@0
   647
sl@0
   648
    if (!res)
sl@0
   649
        {
sl@0
   650
        return ERequestNone;
sl@0
   651
        }
sl@0
   652
sl@0
   653
    //Process our request
sl@0
   654
    if (CurrentRequest()==GP1212A01A::ERequestPowerSupplyStatus)
sl@0
   655
        {
sl@0
   656
        if (iInputReport[1]==0x4F && iInputReport[2]==0x4E)
sl@0
   657
            {
sl@0
   658
            iPowerOn = true;
sl@0
   659
            }
sl@0
   660
        else if (iInputReport[1]==0x4F && iInputReport[2]==0x46 && iInputReport[3]==0x46)
sl@0
   661
            {
sl@0
   662
            iPowerOn = false;
sl@0
   663
            }
sl@0
   664
        }
sl@0
   665
sl@0
   666
    Request completed=iRequest;
sl@0
   667
    //Our request was completed
sl@0
   668
    iRequest=ERequestNone;
sl@0
   669
sl@0
   670
    return completed;
sl@0
   671
    }
sl@0
   672
sl@0
   673
sl@0
   674
/**
sl@0
   675
Set our screen brightness.
sl@0
   676
@param The desired brightness level. Must be between MinBrightness and MaxBrightness.
sl@0
   677
*/
sl@0
   678
void GP1212A01A::SetBrightness(int aBrightness)
sl@0
   679
    {
sl@0
   680
    if (aBrightness<MinBrightness()||aBrightness>MaxBrightness())
sl@0
   681
        {
sl@0
   682
        //Brightness out of range.
sl@0
   683
        //Just ignore that request.
sl@0
   684
        return;
sl@0
   685
        }
sl@0
   686
sl@0
   687
    FutabaVfdReport report;
sl@0
   688
    report[0]=0x00; //Report ID
sl@0
   689
    report[1]=0x06; //Report size
sl@0
   690
    report[2]=0x1B; //Command ID
sl@0
   691
    report[3]=0x5C; //Command ID
sl@0
   692
    report[4]=0x3F; //Command ID
sl@0
   693
    report[5]=0x4C; //Command ID
sl@0
   694
    report[6]=0x44; //Command ID
sl@0
   695
    report[7]=0x30+aBrightness; //Brightness level
sl@0
   696
    Write(report);
sl@0
   697
    }
sl@0
   698
sl@0
   699