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