HidDevice.cpp
changeset 0 0f874d9e4130
child 7 8bac7aac665c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/HidDevice.cpp	Tue Jun 17 09:55:20 2014 +0200
     1.3 @@ -0,0 +1,122 @@
     1.4 +//
     1.5 +//
     1.6 +//
     1.7 +
     1.8 +#include "HidDevice.h"
     1.9 +
    1.10 +
    1.11 +
    1.12 +//
    1.13 +// class HidDevice
    1.14 +//
    1.15 +
    1.16 +HidDevice::HidDevice():iHidDevice(NULL)
    1.17 +    {
    1.18 +    Close();
    1.19 +    }
    1.20 +
    1.21 +/**
    1.22 +*/
    1.23 +int HidDevice::Open(const char* aPath)
    1.24 +	{
    1.25 +	Close();
    1.26 +
    1.27 +	iHidDevice =  hid_open_path(aPath);
    1.28 +
    1.29 +	if (!iHidDevice)
    1.30 +		{
    1.31 +		//Fail to connect our device
    1.32 +		return 0;
    1.33 +		}
    1.34 +
    1.35 +    FetchStrings();
    1.36 +
    1.37 +	return 1;
    1.38 +	}
    1.39 +
    1.40 +/**
    1.41 +See hidapi documentation.
    1.42 +*/
    1.43 +int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
    1.44 +	{
    1.45 +    Close();
    1.46 +
    1.47 +	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
    1.48 +
    1.49 +	if (!iHidDevice)
    1.50 +		{
    1.51 +		//Fail to connect our device
    1.52 +		return 0;
    1.53 +		}
    1.54 +
    1.55 +    FetchStrings();
    1.56 +
    1.57 +	return 1;
    1.58 +	}
    1.59 +
    1.60 +/**
    1.61 +*/
    1.62 +void HidDevice::Close()
    1.63 +	{
    1.64 +	hid_close(iHidDevice);
    1.65 +	iHidDevice=NULL;
    1.66 +    //
    1.67 +    memset(iVendor,0,sizeof(iVendor));
    1.68 +    memset(iProduct,0,sizeof(iProduct));
    1.69 +    memset(iSerialNumber,0,sizeof(iSerialNumber));
    1.70 +	}
    1.71 +
    1.72 +/**
    1.73 +*/
    1.74 +bool HidDevice::IsOpen()
    1.75 +    {
    1.76 +    return iHidDevice!=NULL;
    1.77 +    }
    1.78 +
    1.79 +
    1.80 +/**
    1.81 +*/
    1.82 +const wchar_t* HidDevice::Error()
    1.83 +	{
    1.84 +	return hid_error(iHidDevice);
    1.85 +	}
    1.86 +
    1.87 +/**
    1.88 +*/
    1.89 +int HidDevice::SetNonBlocking(int aNonBlocking)
    1.90 +	{
    1.91 +	//Success we are now connected to our HID device
    1.92 +	//Set read operation as non blocking
    1.93 +	return hid_set_nonblocking(iHidDevice, aNonBlocking);
    1.94 +	}
    1.95 +
    1.96 +/**
    1.97 +*/
    1.98 +wchar_t* HidDevice::Vendor()
    1.99 +    {
   1.100 +    return iVendor;
   1.101 +    }
   1.102 +
   1.103 +/**
   1.104 +*/
   1.105 +wchar_t* HidDevice::Product()
   1.106 +    {
   1.107 +    return iProduct;
   1.108 +    }
   1.109 +
   1.110 +/**
   1.111 +*/
   1.112 +wchar_t* HidDevice::SerialNumber()
   1.113 +    {
   1.114 +    return iSerialNumber;
   1.115 +    }
   1.116 +
   1.117 +/**
   1.118 +
   1.119 +*/
   1.120 +void HidDevice::FetchStrings()
   1.121 +    {
   1.122 +    hid_get_manufacturer_string(iHidDevice,iVendor,sizeof(iVendor));
   1.123 +    hid_get_product_string(iHidDevice,iProduct,sizeof(iProduct));
   1.124 +    hid_get_serial_number_string(iHidDevice,iSerialNumber,sizeof(iSerialNumber));
   1.125 +    }