Moving base HID classes into separate files.
authorsl
Thu, 22 May 2014 07:50:02 +0200
changeset 144a5538e0ccbf
parent 13 69f1fcfdf6a5
child 15 e5b84f315be7
Moving base HID classes into separate files.
FutabaVfd.vcxproj
inc/FutabaVfd.h
inc/HidDevice.h
inc/HidReport.h
src/FutabaVfd.cpp
src/HidDevice.cpp
     1.1 --- a/FutabaVfd.vcxproj	Thu May 22 07:30:05 2014 +0200
     1.2 +++ b/FutabaVfd.vcxproj	Thu May 22 07:50:02 2014 +0200
     1.3 @@ -105,12 +105,15 @@
     1.4    <ItemGroup>
     1.5      <ClCompile Include="..\hidapi\windows\hid.c" />
     1.6      <ClCompile Include="src\FutabaVfd.cpp" />
     1.7 +    <ClCompile Include="src\HidDevice.cpp" />
     1.8      <ClCompile Include="src\Main.cpp" />
     1.9      <ClCompile Include="src\test.cpp" />
    1.10    </ItemGroup>
    1.11    <ItemGroup>
    1.12      <ClInclude Include="..\hidapi\hidapi\hidapi.h" />
    1.13      <ClInclude Include="inc\FutabaVfd.h" />
    1.14 +    <ClInclude Include="inc\HidDevice.h" />
    1.15 +    <ClInclude Include="inc\HidReport.h" />
    1.16      <ClInclude Include="inc\MainWindow.h" />
    1.17    </ItemGroup>
    1.18    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
     2.1 --- a/inc/FutabaVfd.h	Thu May 22 07:30:05 2014 +0200
     2.2 +++ b/inc/FutabaVfd.h	Thu May 22 07:50:02 2014 +0200
     2.3 @@ -4,6 +4,7 @@
     2.4  #define FUTABA_VFD_H
     2.5  
     2.6  #include "hidapi.h"
     2.7 +#include "HidDevice.h"
     2.8  
     2.9  //This was computed from our number of pixels as follow 256x64/8/64 = 32 + 1 = 33
    2.10  //+1 was added for our header
    2.11 @@ -21,61 +22,6 @@
    2.12  
    2.13  //typedef struct hid_device_info HidDeviceInfo;
    2.14  
    2.15 -/**
    2.16 -TODO: move to another header
    2.17 -*/
    2.18 -template <int S>
    2.19 -class HidReport
    2.20 -	{
    2.21 -public:
    2.22 -	HidReport(){Reset();};
    2.23 -	void Reset();
    2.24 -	inline unsigned char& operator[](int aIndex){return iBuffer[aIndex];}
    2.25 -	const unsigned char* Buffer() const {return iBuffer;};
    2.26 -	unsigned char* Buffer() {return iBuffer;};
    2.27 -protected:
    2.28 -	unsigned char iBuffer[S];
    2.29 -	};
    2.30 -
    2.31 -template <int S>
    2.32 -void HidReport<S>::Reset()
    2.33 -    {
    2.34 -    memset(iBuffer,0,sizeof(iBuffer));
    2.35 -    }
    2.36 -
    2.37 -/**
    2.38 -TODO: move to another header
    2.39 -*/
    2.40 -class HidDevice
    2.41 -	{
    2.42 -public:
    2.43 -	int Open(const char* aPath);
    2.44 -	int Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber);
    2.45 -	void Close();
    2.46 -    bool IsOpen();
    2.47 -	//
    2.48 -	int SetNonBlocking(int aNonBlocking);
    2.49 -	//
    2.50 -	template<int S>
    2.51 -	int Write(const HidReport<S>& aOutputReport);
    2.52 -	//
    2.53 -	const wchar_t* Error();
    2.54 -
    2.55 -
    2.56 -
    2.57 -private:
    2.58 -	///Our USB HID device
    2.59 -	hid_device* iHidDevice;
    2.60 -	};
    2.61 -
    2.62 -
    2.63 -/**
    2.64 -*/
    2.65 -template<int S>
    2.66 -int HidDevice::Write(const HidReport<S>& aOutputReport)
    2.67 -	{
    2.68 -	return hid_write(iHidDevice,aOutputReport.Buffer(),S);
    2.69 -	}
    2.70  
    2.71  
    2.72  /**
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/inc/HidDevice.h	Thu May 22 07:50:02 2014 +0200
     3.3 @@ -0,0 +1,45 @@
     3.4 +//
     3.5 +//
     3.6 +//
     3.7 +
     3.8 +#ifndef HID_DEVICE_H
     3.9 +#define HID_DEVICE_H
    3.10 +
    3.11 +#include "HidReport.h"
    3.12 +#include "hidapi.h"
    3.13 +
    3.14 +
    3.15 +/**
    3.16 +TODO: move to another header
    3.17 +*/
    3.18 +class HidDevice
    3.19 +    {
    3.20 +public:
    3.21 +    int Open(const char* aPath);
    3.22 +    int Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber);
    3.23 +    void Close();
    3.24 +    bool IsOpen();
    3.25 +    //
    3.26 +    int SetNonBlocking(int aNonBlocking);
    3.27 +    //
    3.28 +    template<int S>
    3.29 +    int Write(const HidReport<S>& aOutputReport);
    3.30 +    //
    3.31 +    const wchar_t* Error();
    3.32 +
    3.33 +private:
    3.34 +    ///Our USB HID device
    3.35 +    hid_device* iHidDevice;
    3.36 +    };
    3.37 +
    3.38 +
    3.39 +/**
    3.40 +*/
    3.41 +template<int S>
    3.42 +int HidDevice::Write(const HidReport<S>& aOutputReport)
    3.43 +    {
    3.44 +    return hid_write(iHidDevice,aOutputReport.Buffer(),S);
    3.45 +    }
    3.46 +
    3.47 +
    3.48 +#endif
    3.49 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/inc/HidReport.h	Thu May 22 07:50:02 2014 +0200
     4.3 @@ -0,0 +1,30 @@
     4.4 +#ifndef HID_REPORT_H
     4.5 +#define HID_REPORT_H
     4.6 +
     4.7 +#include <string.h>
     4.8 +
     4.9 +/**
    4.10 +Define an HID report.
    4.11 +Can be used as input and output.
    4.12 +*/
    4.13 +template <int S>
    4.14 +class HidReport
    4.15 +    {
    4.16 +public:
    4.17 +    HidReport(){Reset();};
    4.18 +    void Reset();
    4.19 +    inline unsigned char& operator[](int aIndex){return iBuffer[aIndex];}
    4.20 +    const unsigned char* Buffer() const {return iBuffer;};
    4.21 +    unsigned char* Buffer() {return iBuffer;};
    4.22 +protected:
    4.23 +    unsigned char iBuffer[S];
    4.24 +    };
    4.25 +
    4.26 +template <int S>
    4.27 +void HidReport<S>::Reset()
    4.28 +    {
    4.29 +    memset(iBuffer,0,sizeof(iBuffer));
    4.30 +    }
    4.31 +
    4.32 +
    4.33 +#endif
    4.34 \ No newline at end of file
     5.1 --- a/src/FutabaVfd.cpp	Thu May 22 07:30:05 2014 +0200
     5.2 +++ b/src/FutabaVfd.cpp	Thu May 22 07:50:02 2014 +0200
     5.3 @@ -65,74 +65,6 @@
     5.4  */
     5.5  
     5.6  
     5.7 -//
     5.8 -// class HidDevice
     5.9 -//
    5.10 -
    5.11 -/**
    5.12 -*/
    5.13 -int HidDevice::Open(const char* aPath)
    5.14 -	{
    5.15 -	Close();
    5.16 -
    5.17 -	iHidDevice =  hid_open_path(aPath);
    5.18 -
    5.19 -	if (!iHidDevice)
    5.20 -		{
    5.21 -		//Fail to connect our device
    5.22 -		return 0;
    5.23 -		}
    5.24 -
    5.25 -	return 1;
    5.26 -	}
    5.27 -
    5.28 -/**
    5.29 -See hidapi documentation.
    5.30 -*/
    5.31 -int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
    5.32 -	{
    5.33 -	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
    5.34 -
    5.35 -	if (!iHidDevice)
    5.36 -		{
    5.37 -		//Fail to connect our device
    5.38 -		return 0;
    5.39 -		}
    5.40 -
    5.41 -	return 1;
    5.42 -	}
    5.43 -
    5.44 -/**
    5.45 -*/
    5.46 -void HidDevice::Close()
    5.47 -	{
    5.48 -	hid_close(iHidDevice);
    5.49 -	iHidDevice=NULL;
    5.50 -	}
    5.51 -
    5.52 -/**
    5.53 -*/
    5.54 -bool HidDevice::IsOpen()
    5.55 -    {
    5.56 -    return iHidDevice!=NULL;
    5.57 -    }
    5.58 -
    5.59 -
    5.60 -/**
    5.61 -*/
    5.62 -const wchar_t* HidDevice::Error()
    5.63 -	{
    5.64 -	return hid_error(iHidDevice);
    5.65 -	}
    5.66 -
    5.67 -/**
    5.68 -*/
    5.69 -int HidDevice::SetNonBlocking(int aNonBlocking)
    5.70 -	{
    5.71 -	//Success we are now connected to our HID device
    5.72 -	//Set read operation as non blocking
    5.73 -	return hid_set_nonblocking(iHidDevice, aNonBlocking);
    5.74 -	}
    5.75  
    5.76  
    5.77  //
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/HidDevice.cpp	Thu May 22 07:50:02 2014 +0200
     6.3 @@ -0,0 +1,76 @@
     6.4 +//
     6.5 +//
     6.6 +//
     6.7 +
     6.8 +#include "HidDevice.h"
     6.9 +
    6.10 +
    6.11 +
    6.12 +//
    6.13 +// class HidDevice
    6.14 +//
    6.15 +
    6.16 +/**
    6.17 +*/
    6.18 +int HidDevice::Open(const char* aPath)
    6.19 +	{
    6.20 +	Close();
    6.21 +
    6.22 +	iHidDevice =  hid_open_path(aPath);
    6.23 +
    6.24 +	if (!iHidDevice)
    6.25 +		{
    6.26 +		//Fail to connect our device
    6.27 +		return 0;
    6.28 +		}
    6.29 +
    6.30 +	return 1;
    6.31 +	}
    6.32 +
    6.33 +/**
    6.34 +See hidapi documentation.
    6.35 +*/
    6.36 +int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
    6.37 +	{
    6.38 +	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
    6.39 +
    6.40 +	if (!iHidDevice)
    6.41 +		{
    6.42 +		//Fail to connect our device
    6.43 +		return 0;
    6.44 +		}
    6.45 +
    6.46 +	return 1;
    6.47 +	}
    6.48 +
    6.49 +/**
    6.50 +*/
    6.51 +void HidDevice::Close()
    6.52 +	{
    6.53 +	hid_close(iHidDevice);
    6.54 +	iHidDevice=NULL;
    6.55 +	}
    6.56 +
    6.57 +/**
    6.58 +*/
    6.59 +bool HidDevice::IsOpen()
    6.60 +    {
    6.61 +    return iHidDevice!=NULL;
    6.62 +    }
    6.63 +
    6.64 +
    6.65 +/**
    6.66 +*/
    6.67 +const wchar_t* HidDevice::Error()
    6.68 +	{
    6.69 +	return hid_error(iHidDevice);
    6.70 +	}
    6.71 +
    6.72 +/**
    6.73 +*/
    6.74 +int HidDevice::SetNonBlocking(int aNonBlocking)
    6.75 +	{
    6.76 +	//Success we are now connected to our HID device
    6.77 +	//Set read operation as non blocking
    6.78 +	return hid_set_nonblocking(iHidDevice, aNonBlocking);
    6.79 +	}