Working on our architecture. Adding a bunch of HID and Futaba classes.
1.1 --- a/FutabaVfd.cpp Wed May 21 16:11:09 2014 +0200
1.2 +++ b/FutabaVfd.cpp Wed May 21 22:43:19 2014 +0200
1.3 @@ -3,6 +3,19 @@
1.4 //#include <stdlib.h>
1.5 #include <string.h>
1.6
1.7 +
1.8 +//
1.9 +//
1.10 +//
1.11 +
1.12 +
1.13 +
1.14 +
1.15 +
1.16 +//
1.17 +//
1.18 +//
1.19 +
1.20 FutabaVfdCommand::FutabaVfdCommand():/*iBuffer(NULL),*/iSize(0),iMaxSize(0)
1.21 {
1.22 }
1.23 @@ -52,3 +65,163 @@
1.24 */
1.25
1.26
1.27 +//
1.28 +// class HidDevice
1.29 +//
1.30 +
1.31 +/**
1.32 +*/
1.33 +int HidDevice::Open(const char* aPath)
1.34 + {
1.35 + Close();
1.36 +
1.37 + iHidDevice = hid_open_path(aPath);
1.38 +
1.39 + if (!iHidDevice)
1.40 + {
1.41 + //Fail to connect our device
1.42 + return 0;
1.43 + }
1.44 +
1.45 + return 1;
1.46 + }
1.47 +
1.48 +/**
1.49 +See hidapi documentation.
1.50 +*/
1.51 +int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
1.52 + {
1.53 + iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
1.54 +
1.55 + if (!iHidDevice)
1.56 + {
1.57 + //Fail to connect our device
1.58 + return 0;
1.59 + }
1.60 +
1.61 + return 1;
1.62 + }
1.63 +
1.64 +/**
1.65 +*/
1.66 +void HidDevice::Close()
1.67 + {
1.68 + hid_close(iHidDevice);
1.69 + iHidDevice=NULL;
1.70 + }
1.71 +
1.72 +
1.73 +/**
1.74 +*/
1.75 +const wchar_t* HidDevice::Error()
1.76 + {
1.77 + return hid_error(iHidDevice);
1.78 + }
1.79 +
1.80 +/**
1.81 +*/
1.82 +int HidDevice::SetNonBlocking(int aNonBlocking)
1.83 + {
1.84 + //Success we are now connected to our HID device
1.85 + //Set read operation as non blocking
1.86 + return hid_set_nonblocking(iHidDevice, aNonBlocking);
1.87 + }
1.88 +
1.89 +
1.90 +//
1.91 +// class GP1212A01A
1.92 +//
1.93 +
1.94 +int GP1212A01A::Open()
1.95 + {
1.96 + int success = HidDevice::Open(KFutabaVendorId,KFutabaProductIdGP1212A01A,NULL);
1.97 + if (success)
1.98 + {
1.99 + SetNonBlocking(1);
1.100 + }
1.101 + return success;
1.102 + }
1.103 +
1.104 +/**
1.105 +*/
1.106 +void GP1212A01A::SetPixel(int aX, int aY, bool aOn)
1.107 + {
1.108 + //Just specify a one pixel block
1.109 + SetPixelBlock(aX,aY,0x00,0x01,aOn);
1.110 + }
1.111 +
1.112 +/**
1.113 +*/
1.114 +void GP1212A01A::SetAllPixels(bool aOn)
1.115 + {
1.116 + //One pixel at a time
1.117 + /*
1.118 + for (int i=0;i<256;i++)
1.119 + {
1.120 + for (int j=0;j<64;j++)
1.121 + {
1.122 + SetPixel(i,j,0x01);
1.123 + }
1.124 + }
1.125 + */
1.126 +
1.127 + //16x16=256 pixels at a time goes much faster
1.128 + //TODO: use even larger blocks
1.129 + for (int i=0;i<256;i+=16)
1.130 + {
1.131 + for (int j=0;j<64;j+=16)
1.132 + {
1.133 + SetPixelBlock(i,j,15,32,(aOn?0xFF:0x00));
1.134 + //FXThread::sleep(1000000000);
1.135 + }
1.136 + }
1.137 +
1.138 + }
1.139 +
1.140 +/**
1.141 +*/
1.142 +void GP1212A01A::SetBrightness(int aBrightness)
1.143 + {
1.144 + }
1.145 +
1.146 +/**
1.147 +Set the defined pixel block to the given value.
1.148 +@param X coordinate of our pixel block starting point.
1.149 +@param Y coordinate of our pixel block starting point.
1.150 +@param The height of our pixel block.
1.151 +@param The size of our pixel data. Number of pixels divided by 8.
1.152 +@param The value set to 8 pixels.
1.153 +*/
1.154 +void GP1212A01A::SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue)
1.155 + {
1.156 + //Size must be 63 or below
1.157 + FutabaVfdReport report;
1.158 + report[0]=0x00; //Report ID
1.159 + report[1]=0x08+aSize; //Report length
1.160 + report[2]=0x1B; //
1.161 + report[3]=0x5B; //
1.162 + report[4]=0xF0; //
1.163 + report[5]=aX; //X
1.164 + report[6]=aY; //Y
1.165 + 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.
1.166 + report[8]=0x00; //Size of pixel data in bytes (MSB)
1.167 + report[9]=aSize; //Size of pixel data in bytes (LSB)
1.168 + memset(report.Buffer()+10, aValue, KFutabaMaxHidReportSize);
1.169 + //iOutputReportBuffer[10]=aValue; //Pixel data
1.170 + Write(report);
1.171 + }
1.172 +
1.173 +
1.174 +/**
1.175 +*/
1.176 +void GP1212A01A::Clear()
1.177 + {
1.178 + FutabaVfdReport report;
1.179 + report[0]=0x00; //Report ID
1.180 + report[1]=0x04; //Report length
1.181 + report[2]=0x1B; //
1.182 + report[3]=0x5B; //
1.183 + report[4]=0x32; //
1.184 + report[5]=0x4A; //
1.185 + Write(report);
1.186 + }
1.187 \ No newline at end of file
2.1 --- a/FutabaVfd.h Wed May 21 16:11:09 2014 +0200
2.2 +++ b/FutabaVfd.h Wed May 21 22:43:19 2014 +0200
2.3 @@ -9,7 +9,83 @@
2.4 //+1 was added for our header
2.5 const int KFutabaMaxCommandOutputReport = 33;
2.6 //TODO: Get ride of that constant once we figure out a way to get it from hidapi
2.7 -const int KFutabaOutputReportLength = 65;
2.8 +const int KFutabaMaxHidReportSize = 65;
2.9 +
2.10 +const int KHidReportIdIndex=0;
2.11 +const int KFutabaHidReportSizeIndex=1;
2.12 +//Define Futaba vendor ID to filter our list of device
2.13 +const unsigned short KFutabaVendorId = 0x1008;
2.14 +const unsigned short KFutabaProductIdGP1212A01A = 0x100C;
2.15 +const unsigned short KFutabaProductIdGP1212A02A = 0x1013; //Or is it 0x1015
2.16 +
2.17 +
2.18 +//typedef struct hid_device_info HidDeviceInfo;
2.19 +
2.20 +/**
2.21 +TODO: move to another header
2.22 +*/
2.23 +template <int S>
2.24 +class HidReport
2.25 + {
2.26 +public:
2.27 + HidReport(){Reset();};
2.28 + void Reset();
2.29 + inline unsigned char& operator[](int aIndex){return iBuffer[aIndex];}
2.30 + const unsigned char* Buffer() const {return iBuffer;};
2.31 + unsigned char* Buffer() {return iBuffer;};
2.32 +protected:
2.33 + unsigned char iBuffer[S];
2.34 + };
2.35 +
2.36 +template <int S>
2.37 +void HidReport<S>::Reset()
2.38 + {
2.39 + memset(iBuffer,0,sizeof(iBuffer));
2.40 + }
2.41 +
2.42 +/**
2.43 +TODO: move to another header
2.44 +*/
2.45 +class HidDevice
2.46 + {
2.47 +public:
2.48 + int Open(const char* aPath);
2.49 + int Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber);
2.50 + void Close();
2.51 + //
2.52 + int SetNonBlocking(int aNonBlocking);
2.53 + //
2.54 + template<int S>
2.55 + int Write(const HidReport<S>& aOutputReport);
2.56 + //
2.57 + const wchar_t* Error();
2.58 +
2.59 +
2.60 +
2.61 +private:
2.62 + ///Our USB HID device
2.63 + hid_device* iHidDevice;
2.64 + };
2.65 +
2.66 +
2.67 +/**
2.68 +*/
2.69 +template<int S>
2.70 +int HidDevice::Write(const HidReport<S>& aOutputReport)
2.71 + {
2.72 + return hid_write(iHidDevice,aOutputReport.Buffer(),S);
2.73 + }
2.74 +
2.75 +
2.76 +/**
2.77 +*/
2.78 +class FutabaVfdReport: public HidReport<KFutabaMaxHidReportSize>
2.79 + {
2.80 +
2.81 +private:
2.82 +
2.83 + };
2.84 +
2.85
2.86
2.87 /**
2.88 @@ -30,10 +106,72 @@
2.89
2.90 private:
2.91 //unsigned char* iBuffer;
2.92 - unsigned char iReports[KFutabaMaxCommandOutputReport][KFutabaOutputReportLength];
2.93 + FutabaVfdReport iReports[KFutabaMaxCommandOutputReport];
2.94 int iSize;
2.95 int iMaxSize;
2.96 };
2.97
2.98 +/**
2.99 +*/
2.100 +class FutabaVfd : protected HidDevice
2.101 + {
2.102 +public:
2.103 + virtual int MinBrightness()=0;
2.104 + virtual int MaxBrightness()=0;
2.105 + virtual void SetBrightness(int aBrightness)=0;
2.106 + virtual void Clear()=0;
2.107 + };
2.108 +
2.109 +
2.110 +/**
2.111 +*/
2.112 +class FutabaGraphicVfd : public FutabaVfd
2.113 + {
2.114 +public:
2.115 + virtual int WidthInPixels()=0;
2.116 + virtual int HeightInPixels()=0;
2.117 + virtual void SetPixel(int aX, int aY, bool aOn)=0;
2.118 + virtual void SetAllPixels(bool aOn)=0;
2.119 +
2.120 + };
2.121 +
2.122 +/**
2.123 +Common functionality between GP1212A01A and GP1212A02A
2.124 +*/
2.125 +class GP1212XXXX : public FutabaGraphicVfd
2.126 + {
2.127 +public:
2.128 + //From FutabaVfd
2.129 + virtual int MinBrightness(){return 0;};
2.130 + virtual int MaxBrightness(){return 5;};
2.131 + };
2.132 +
2.133 +/**
2.134 +GP1212A01A is a graphic display module using a FUTABA 256x64dots VFD.
2.135 +The module do not include character ROM, the customer will compile the character
2.136 +by themselves (from main system).
2.137 +*/
2.138 +class GP1212A01A : public GP1212XXXX
2.139 + {
2.140 +public:
2.141 + int Open();
2.142 + //From FutabaGraphicVfd
2.143 + virtual int WidthInPixels(){return 256;};
2.144 + virtual int HeightInPixels(){return 64;};
2.145 + virtual void SetPixel(int aX, int aY, bool aOn);
2.146 + virtual void SetAllPixels(bool aOn);
2.147 + //From FutabaVfd
2.148 + virtual void SetBrightness(int aBrightness);
2.149 + virtual void Clear();
2.150 + //
2.151 + void SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue);
2.152 +
2.153 +private:
2.154 + ///
2.155 + //FutabaVfdReport iReport;
2.156 + ///
2.157 + unsigned char iPixelBuffer[256][128];
2.158 + };
2.159 +
2.160
2.161 #endif
2.162 \ No newline at end of file
3.1 --- a/test.cpp Wed May 21 16:11:09 2014 +0200
3.2 +++ b/test.cpp Wed May 21 22:43:19 2014 +0200
3.3 @@ -258,7 +258,7 @@
3.4 iButtonSetAllPixels->enable();
3.5
3.6 //
3.7 - iOutputReportBuffer=new unsigned char[KFutabaOutputReportLength]; //TODO: use connected_device->output_report_length
3.8 + iOutputReportBuffer=new unsigned char[KFutabaMaxHidReportSize]; //TODO: use connected_device->output_report_length
3.9
3.10 return 1;
3.11 }
3.12 @@ -305,9 +305,6 @@
3.13
3.14 device_list->clearItems();
3.15
3.16 - //Define Futaba vendor ID to filter our list of device
3.17 - const unsigned short KFutabaVendorId = 0x1008;
3.18 -
3.19 // List the Devices
3.20 hid_free_enumeration(devices);
3.21 devices = hid_enumerate(0x0, 0x0);
3.22 @@ -505,14 +502,14 @@
3.23 long
3.24 MainWindow::onFutabaClearDisplay(FXObject *sender, FXSelector sel, void *ptr)
3.25 {
3.26 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.27 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.28 iOutputReportBuffer[0]=0x00; //Report ID
3.29 iOutputReportBuffer[1]=0x04; //Report length
3.30 iOutputReportBuffer[2]=0x1B; //
3.31 iOutputReportBuffer[3]=0x5B; //
3.32 iOutputReportBuffer[4]=0x32; //
3.33 iOutputReportBuffer[5]=0x4A; //
3.34 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.35 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.36
3.37 return 1;
3.38 }
3.39 @@ -520,7 +517,7 @@
3.40 long
3.41 MainWindow::onFutabaDimming(FXObject *sender, FXSelector sel, void *ptr)
3.42 {
3.43 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.44 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.45 iOutputReportBuffer[0]=0x00; //Report ID
3.46 iOutputReportBuffer[1]=0x06; //Report length
3.47 iOutputReportBuffer[2]=0x1B; //
3.48 @@ -530,7 +527,7 @@
3.49 iOutputReportBuffer[6]=0x44; //
3.50 iDimming = (iDimming==0x35?0x30:++iDimming);
3.51 iOutputReportBuffer[7]=iDimming;
3.52 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.53 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.54
3.55 return 1;
3.56 }
3.57 @@ -540,7 +537,7 @@
3.58 {
3.59 //@1B 5B F0 00 00 07 00 01 FF
3.60
3.61 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.62 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.63 iOutputReportBuffer[0]=0x00; //Report ID
3.64 iOutputReportBuffer[1]=0x09; //Report length
3.65 iOutputReportBuffer[2]=0x1B; //
3.66 @@ -552,7 +549,7 @@
3.67 iOutputReportBuffer[8]=0x00; //
3.68 iOutputReportBuffer[9]=0x01; //
3.69 iOutputReportBuffer[10]=0xFF; //
3.70 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.71 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.72
3.73
3.74 return 1;
3.75 @@ -584,7 +581,7 @@
3.76 void MainWindow::SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue)
3.77 {
3.78 //Size must be 63 or below
3.79 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.80 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.81 iOutputReportBuffer[0]=0x00; //Report ID
3.82 iOutputReportBuffer[1]=0x08+aSize; //Report length
3.83 iOutputReportBuffer[2]=0x1B; //
3.84 @@ -595,9 +592,9 @@
3.85 iOutputReportBuffer[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.
3.86 iOutputReportBuffer[8]=0x00; //Size of pixel data in bytes (MSB)
3.87 iOutputReportBuffer[9]=aSize; //Size of pixel data in bytes (LSB)
3.88 - memset(iOutputReportBuffer+10, aValue, KFutabaOutputReportLength);
3.89 + memset(iOutputReportBuffer+10, aValue, KFutabaMaxHidReportSize);
3.90 //iOutputReportBuffer[10]=aValue; //Pixel data
3.91 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.92 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.93 }
3.94
3.95 /**
3.96 @@ -607,7 +604,7 @@
3.97 void MainWindow::SendFutabaOutputReport(unsigned char* aReportData, unsigned char aSize)
3.98 {
3.99 //
3.100 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.101 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.102 iOutputReportBuffer[0]=0x00; //Report ID is always null
3.103 iOutputReportBuffer[1]=0x08+aSize; //Report length
3.104 iOutputReportBuffer[2]=0x1B; //
3.105 @@ -618,9 +615,9 @@
3.106 iOutputReportBuffer[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.
3.107 iOutputReportBuffer[8]=0x00; //Size of pixel data in bytes (MSB)
3.108 iOutputReportBuffer[9]=aSize; //Size of pixel data in bytes (LSB)
3.109 - memset(iOutputReportBuffer+10, aValue, KFutabaOutputReportLength);
3.110 + memset(iOutputReportBuffer+10, aValue, KFutabaMaxHidReportSize);
3.111 //iOutputReportBuffer[10]=aValue; //Pixel data
3.112 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.113 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.114 }
3.115 */
3.116
3.117 @@ -678,7 +675,7 @@
3.118 MainWindow::onFutabaReadId(FXObject *sender, FXSelector sel, void *ptr)
3.119 {
3.120 //1BH,5BH,63H,49H,44H
3.121 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.122 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.123 iOutputReportBuffer[0]=0x00; //Report ID
3.124 iOutputReportBuffer[1]=0x05; //Report length
3.125 iOutputReportBuffer[2]=0x1B; //
3.126 @@ -686,7 +683,7 @@
3.127 iOutputReportBuffer[4]=0x63; //
3.128 iOutputReportBuffer[5]=0x49; //
3.129 iOutputReportBuffer[6]=0x44; //
3.130 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.131 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.132
3.133 return 1;
3.134 }
3.135 @@ -695,7 +692,7 @@
3.136 MainWindow::onFutabaReadFirmwareRevision(FXObject *sender, FXSelector sel, void *ptr)
3.137 {
3.138 //1BH,5BH,63H,46H,52H
3.139 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.140 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.141 iOutputReportBuffer[0]=0x00; //Report ID
3.142 iOutputReportBuffer[1]=0x05; //Report length
3.143 iOutputReportBuffer[2]=0x1B; //
3.144 @@ -703,7 +700,7 @@
3.145 iOutputReportBuffer[4]=0x63; //
3.146 iOutputReportBuffer[5]=0x46; //
3.147 iOutputReportBuffer[6]=0x52; //
3.148 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.149 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.150
3.151 return 1;
3.152 }
3.153 @@ -712,7 +709,7 @@
3.154 MainWindow::onFutabaPowerSupplyMonitor(FXObject *sender, FXSelector sel, void *ptr)
3.155 {
3.156 //1BH,5BH,63H,50H,4DH
3.157 - memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
3.158 + memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
3.159 iOutputReportBuffer[0]=0x00; //Report ID
3.160 iOutputReportBuffer[1]=0x05; //Report length
3.161 iOutputReportBuffer[2]=0x1B; //
3.162 @@ -720,7 +717,7 @@
3.163 iOutputReportBuffer[4]=0x63; //
3.164 iOutputReportBuffer[5]=0x50; //
3.165 iOutputReportBuffer[6]=0x4D; //
3.166 - int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
3.167 + int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
3.168
3.169 return 1;
3.170 }