FutabaVfd.cpp
author sl
Wed, 21 May 2014 22:55:14 +0200
changeset 11 11a0e8a2346e
parent 10 4c3d32f38c09
permissions -rw-r--r--
Adding very basic code for testing our brand new Futaba VFD class without
breaking generic code.
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
// class HidDevice
sl@10
    70
//
sl@10
    71
sl@10
    72
/**
sl@10
    73
*/
sl@10
    74
int HidDevice::Open(const char* aPath)
sl@10
    75
	{
sl@10
    76
	Close();
sl@10
    77
sl@10
    78
	iHidDevice =  hid_open_path(aPath);
sl@10
    79
sl@10
    80
	if (!iHidDevice)
sl@10
    81
		{
sl@10
    82
		//Fail to connect our device
sl@10
    83
		return 0;
sl@10
    84
		}
sl@10
    85
sl@10
    86
	return 1;
sl@10
    87
	}
sl@10
    88
sl@10
    89
/**
sl@10
    90
See hidapi documentation.
sl@10
    91
*/
sl@10
    92
int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
sl@10
    93
	{
sl@10
    94
	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
sl@10
    95
sl@10
    96
	if (!iHidDevice)
sl@10
    97
		{
sl@10
    98
		//Fail to connect our device
sl@10
    99
		return 0;
sl@10
   100
		}
sl@10
   101
sl@10
   102
	return 1;
sl@10
   103
	}
sl@10
   104
sl@10
   105
/**
sl@10
   106
*/
sl@10
   107
void HidDevice::Close()
sl@10
   108
	{
sl@10
   109
	hid_close(iHidDevice);
sl@10
   110
	iHidDevice=NULL;
sl@10
   111
	}
sl@10
   112
sl@10
   113
sl@10
   114
/**
sl@10
   115
*/
sl@10
   116
const wchar_t* HidDevice::Error()
sl@10
   117
	{
sl@10
   118
	return hid_error(iHidDevice);
sl@10
   119
	}
sl@10
   120
sl@10
   121
/**
sl@10
   122
*/
sl@10
   123
int HidDevice::SetNonBlocking(int aNonBlocking)
sl@10
   124
	{
sl@10
   125
	//Success we are now connected to our HID device 
sl@10
   126
	//Set read operation as non blocking
sl@10
   127
	return hid_set_nonblocking(iHidDevice, aNonBlocking);
sl@10
   128
	}
sl@10
   129
sl@10
   130
sl@10
   131
//
sl@10
   132
// class GP1212A01A
sl@10
   133
//
sl@10
   134
sl@10
   135
int GP1212A01A::Open()
sl@10
   136
	{
sl@10
   137
	int success = HidDevice::Open(KFutabaVendorId,KFutabaProductIdGP1212A01A,NULL);
sl@10
   138
	if (success)
sl@10
   139
		{
sl@10
   140
		SetNonBlocking(1);
sl@10
   141
		}
sl@10
   142
	return success;
sl@10
   143
	}
sl@10
   144
sl@10
   145
/**
sl@10
   146
*/
sl@10
   147
void GP1212A01A::SetPixel(int aX, int aY, bool aOn)
sl@10
   148
	{
sl@10
   149
	//Just specify a one pixel block
sl@10
   150
	SetPixelBlock(aX,aY,0x00,0x01,aOn);
sl@10
   151
	}
sl@10
   152
sl@10
   153
/**
sl@10
   154
*/
sl@10
   155
void GP1212A01A::SetAllPixels(bool aOn)
sl@10
   156
	{
sl@10
   157
	//One pixel at a time
sl@10
   158
	/*
sl@10
   159
	for (int i=0;i<256;i++)
sl@10
   160
		{
sl@10
   161
		for (int j=0;j<64;j++)
sl@10
   162
			{
sl@10
   163
			SetPixel(i,j,0x01);
sl@10
   164
			}
sl@10
   165
		}
sl@10
   166
	*/
sl@10
   167
sl@10
   168
	//16x16=256 pixels at a time goes much faster
sl@10
   169
	//TODO: use even larger blocks
sl@10
   170
	for (int i=0;i<256;i+=16)
sl@10
   171
		{
sl@10
   172
		for (int j=0;j<64;j+=16)
sl@10
   173
			{
sl@10
   174
			SetPixelBlock(i,j,15,32,(aOn?0xFF:0x00));
sl@10
   175
			//FXThread::sleep(1000000000);
sl@10
   176
			}
sl@10
   177
		}
sl@10
   178
sl@10
   179
	}
sl@10
   180
sl@10
   181
/**
sl@10
   182
*/
sl@10
   183
void GP1212A01A::SetBrightness(int aBrightness)
sl@10
   184
	{
sl@10
   185
	}
sl@10
   186
sl@10
   187
/**
sl@10
   188
Set the defined pixel block to the given value.
sl@10
   189
@param X coordinate of our pixel block starting point.
sl@10
   190
@param Y coordinate of our pixel block starting point.
sl@10
   191
@param The height of our pixel block.
sl@10
   192
@param The size of our pixel data. Number of pixels divided by 8.
sl@10
   193
@param The value set to 8 pixels.
sl@10
   194
*/
sl@10
   195
void GP1212A01A::SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue)
sl@10
   196
	{
sl@10
   197
	//Size must be 63 or below
sl@10
   198
	FutabaVfdReport report;
sl@10
   199
	report[0]=0x00; //Report ID
sl@10
   200
	report[1]=0x08+aSize; //Report length
sl@10
   201
	report[2]=0x1B; //
sl@10
   202
	report[3]=0x5B; //
sl@10
   203
	report[4]=0xF0; //
sl@10
   204
	report[5]=aX; //X
sl@10
   205
	report[6]=aY; //Y
sl@10
   206
	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
   207
	report[8]=0x00; //Size of pixel data in bytes (MSB)
sl@10
   208
	report[9]=aSize; //Size of pixel data in bytes (LSB)
sl@11
   209
	memset(report.Buffer()+10, aValue, aSize);
sl@10
   210
	//iOutputReportBuffer[10]=aValue; //Pixel data
sl@10
   211
	Write(report);
sl@10
   212
	}
sl@10
   213
sl@10
   214
sl@10
   215
/**
sl@10
   216
*/
sl@10
   217
void GP1212A01A::Clear()
sl@10
   218
	{
sl@10
   219
	FutabaVfdReport report;
sl@10
   220
	report[0]=0x00; //Report ID
sl@10
   221
	report[1]=0x04; //Report length
sl@10
   222
	report[2]=0x1B; //
sl@10
   223
	report[3]=0x5B; //
sl@10
   224
	report[4]=0x32; //
sl@10
   225
	report[5]=0x4A; //
sl@10
   226
	Write(report);
sl@10
   227
	}