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