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