src/FutabaVfd.cpp
author sl
Thu, 22 May 2014 14:57:11 +0200
changeset 21 7d89d719583e
parent 20 23cacc1d17a3
child 22 efa6ff02287c
permissions -rw-r--r--
Adding support for setting GP1212A01A display position.
     1 
     2 #include "FutabaVfd.h"
     3 //#include <stdlib.h>
     4 #include <string.h>
     5 
     6 
     7 //
     8 //
     9 //
    10 
    11 
    12 
    13 
    14 
    15 //
    16 //
    17 //
    18 
    19 FutabaVfdCommand::FutabaVfdCommand():/*iBuffer(NULL),*/iSize(0),iMaxSize(0)
    20     {
    21     }
    22 
    23 FutabaVfdCommand::~FutabaVfdCommand()
    24     {
    25     //Delete();
    26     }
    27 
    28 
    29 /**
    30 
    31 */
    32 void FutabaVfdCommand::Reset()
    33     {
    34     memset(iReports,0,sizeof(iReports));
    35     }
    36 
    37 
    38 
    39 /**
    40 
    41 */
    42 /*
    43 void FutabaVfdCommand::Create(int aMaxSize)
    44     {
    45     iBuffer=new unsigned char[aMaxSize];
    46     if (iBuffer)
    47         {
    48         iMaxSize = aMaxSize;
    49         iSize = 0;
    50         }
    51     }
    52 */
    53 
    54 /**
    55 
    56 */
    57 /*
    58 void FutabaVfdCommand::Delete()
    59 {
    60     delete[] iBuffer;
    61     iBuffer = NULL;
    62     iMaxSize = 0;
    63     iSize = 0;
    64 }
    65 */
    66 
    67 
    68 
    69 
    70 //
    71 // class GP1212A01A
    72 //
    73 
    74 int GP1212A01A::Open()
    75 	{
    76 	int success = HidDevice::Open(KFutabaVendorId,KFutabaProductIdGP1212A01A,NULL);
    77 	if (success)
    78 		{
    79 		SetNonBlocking(1);
    80 		}
    81 	return success;
    82 	}
    83 
    84 /**
    85 */
    86 void GP1212A01A::SetPixel(int aX, int aY, bool aOn)
    87 	{
    88 	//Just specify a one pixel block
    89 	SetPixelBlock(aX,aY,0x00,0x01,aOn);
    90 	}
    91 
    92 /**
    93 */
    94 void GP1212A01A::SetAllPixels(bool aOn)
    95 	{
    96 	//One pixel at a time
    97 	/*
    98 	for (int i=0;i<256;i++)
    99 		{
   100 		for (int j=0;j<64;j++)
   101 			{
   102 			SetPixel(i,j,0x01);
   103 			}
   104 		}
   105 	*/
   106 
   107 	//16x16=256 pixels at a time goes much faster
   108 	//TODO: use even larger blocks
   109 	for (int i=0;i<256;i+=16)
   110 		{
   111 		for (int j=0;j<64;j+=16)
   112 			{
   113 			SetPixelBlock(i,j,15,32,(aOn?0xFF:0x00));
   114 			//FXThread::sleep(1000000000);
   115 			}
   116 		}
   117 
   118 	}
   119 
   120 /**
   121 */
   122 void GP1212A01A::SetBrightness(int aBrightness)
   123     {
   124 	if (aBrightness<MinBrightness()||aBrightness>MaxBrightness())
   125         {
   126         //Brightness out of range.
   127         //Just ignore that request.
   128         return;
   129         }
   130 
   131     FutabaVfdReport report;
   132     report[0]=0x00; //Report ID
   133     report[1]=0x06; //Report size
   134     report[2]=0x1B; //Command ID
   135     report[3]=0x5C; //Command ID
   136     report[4]=0x3F; //Command ID
   137     report[5]=0x4C; //Command ID
   138     report[6]=0x44; //Command ID
   139     report[7]=0x30+aBrightness; //Brightness level
   140     Write(report);
   141 
   142 	}
   143 
   144 /**
   145 Set the defined pixel block to the given value.
   146 @param X coordinate of our pixel block starting point.
   147 @param Y coordinate of our pixel block starting point.
   148 @param The height of our pixel block.
   149 @param The size of our pixel data. Number of pixels divided by 8.
   150 @param The value set to 8 pixels used as a pattern.
   151 */
   152 void GP1212A01A::SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue)
   153 	{
   154 	//Size must be 63 or below
   155 	FutabaVfdReport report;
   156 	report[0]=0x00; //Report ID
   157 	report[1]=0x08+aSize; //Report length
   158 	report[2]=0x1B; //
   159 	report[3]=0x5B; //
   160 	report[4]=0xF0; //
   161 	report[5]=aX; //X
   162 	report[6]=aY; //Y
   163 	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.
   164 	report[8]=0x00; //Size of pixel data in bytes (MSB)
   165 	report[9]=aSize; //Size of pixel data in bytes (LSB)
   166 	memset(report.Buffer()+10, aValue, aSize);
   167 	//iOutputReportBuffer[10]=aValue; //Pixel data
   168 	Write(report);
   169 	}
   170 
   171 /**
   172 Set the defined pixel block to the given value.
   173 @param X coordinate of our pixel block starting point.
   174 @param Y coordinate of our pixel block starting point.
   175 @param The height of our pixel block.
   176 @param The size of our pixel data. Number of pixels divided by 8.
   177 @param Pointer to our pixel data.
   178 */
   179 void GP1212A01A::SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char* aPixels)
   180     {
   181     FutabaVfdReport report;
   182     report[0]=0x00; //Report ID
   183     report[1]=0x08+(aSize<=report.Size()-10?aSize:64); //Report length. -10 is for our header first 10 bytes
   184     report[2]=0x1B; //Command ID
   185     report[3]=0x5B; //Command ID
   186     report[4]=0xF0; //Command ID
   187     report[5]=aX;   //X
   188     report[6]=aY;   //Y
   189     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.
   190     unsigned short* sizePtr=(unsigned short*)report.Buffer()+8; //Size of pixel data in bytes (MSB)
   191     *sizePtr=aSize; //Put our size over 2 bytes
   192     int sizeWritten=MIN(aSize,report.Size()-10);
   193     memcpy(report.Buffer()+10, aPixels, sizeWritten);
   194     Write(report);
   195 
   196     int remainingSize=aSize;
   197     //We need to keep on sending our pixel data until we are done
   198     while (report[1]==64)
   199         {
   200         report.Reset();
   201         remainingSize-=sizeWritten;
   202         report[0]=0x00; //Report ID
   203         report[1]=(remainingSize<=report.Size()-2?aSize:64); //Report length, should be 64 or the remaining size
   204         sizeWritten=(report[1]==64?63:report[1]);
   205         memcpy(report.Buffer()+2, aPixels+(aSize-remainingSize), sizeWritten);
   206         Write(report);
   207         }
   208     }
   209 
   210 
   211 /**
   212 Clear our display's screen.
   213 */
   214 void GP1212A01A::Clear()
   215 	{
   216     //1BH,5BH,32H,4AH
   217     //Send Clear Display Command
   218 	FutabaVfdReport report;
   219 	report[0]=0x00; //Report ID
   220 	report[1]=0x04; //Report length
   221 	report[2]=0x1B; //Command ID
   222 	report[3]=0x5B; //Command ID
   223 	report[4]=0x32; //Command ID
   224 	report[5]=0x4A; //Command ID
   225 	Write(report);
   226 	}
   227 
   228 /**
   229 Change our display position within our buffer.
   230 */
   231 void GP1212A01A::SetDisplayPosition(DW aDw,int aX, int aY)
   232     {
   233     //1BH,5BH,Dw,Px,Py
   234     //Send Display Position Settings Command
   235     FutabaVfdReport report;
   236     report[0]=0x00; //Report ID
   237     report[1]=0x05; //Report length
   238     report[2]=0x1B; //Command ID
   239     report[3]=0x5B; //Command ID
   240     report[4]=aDw;  //Specify our DW
   241     report[5]=aX;   //X coordinate of our DW top-left corner
   242     report[5]=aY;   //Y coordinate of our DW top-left corner
   243     Write(report);
   244     }
   245 
   246 /**
   247 */
   248 void GP1212A01A::RequestId()
   249     {
   250     //1BH,5BH,63H,49H,44H
   251     //Send Read ID command
   252     FutabaVfdReport report;
   253     report[0]=0x00; //Report ID
   254     report[1]=0x05; //Report length
   255     report[2]=0x1B; //Command ID
   256     report[3]=0x5B; //Command ID
   257     report[4]=0x63; //Command ID
   258     report[5]=0x49; //Command ID
   259     report[6]=0x44; //Command ID
   260     Write(report);
   261     }
   262 
   263 /**
   264 */
   265 void GP1212A01A::RequestFirmwareRevision()
   266     {
   267     //1BH,5BH,63H,46H,52H
   268     //Send Software Revision Read Command
   269     FutabaVfdReport report;
   270     report[0]=0x00; //Report ID
   271     report[1]=0x05; //Report length
   272     report[2]=0x1B; //Command ID
   273     report[3]=0x5B; //Command ID
   274     report[4]=0x63; //Command ID
   275     report[5]=0x46; //Command ID
   276     report[6]=0x52; //Command ID
   277     Write(report);
   278     }
   279 
   280 /**
   281 */
   282 void GP1212A01A::RequestPowerSupplyStatus()
   283     {
   284     //1BH,5BH,63H,50H,4DH
   285     //Send Power Suppply Monitor Command
   286     FutabaVfdReport report;
   287     report[0]=0x00; //Report ID
   288     report[1]=0x05; //Report length
   289     report[2]=0x1B; //Command ID
   290     report[3]=0x5B; //Command ID
   291     report[4]=0x63; //Command ID
   292     report[5]=0x50; //Command ID
   293     report[6]=0x4D; //Command ID
   294     Write(report);
   295     }