HidDevice.cpp
author StephaneLenclud
Tue, 10 Feb 2015 15:38:27 +0100
changeset 34 892f755c2612
parent 0 0f874d9e4130
child 35 638eb0763e20
permissions -rw-r--r--
MDM166AA: Clock and Clear adjustments.
     1 //
     2 //
     3 //
     4 
     5 #include "HidDevice.h"
     6 
     7 
     8 
     9 //
    10 // class HidDevice
    11 //
    12 
    13 HidDevice::HidDevice():iHidDevice(NULL)
    14     {
    15     Close();
    16     }
    17 
    18 /**
    19 */
    20 HidDevice::~HidDevice()
    21 	{
    22 	Close();
    23 	}
    24 
    25 
    26 /**
    27 */
    28 int HidDevice::Open(const char* aPath)
    29 	{
    30 	Close();
    31 
    32 	iHidDevice =  hid_open_path(aPath);
    33 
    34 	if (!iHidDevice)
    35 		{
    36 		//Fail to connect our device
    37 		return 0;
    38 		}
    39 
    40     FetchStrings();
    41 
    42 	return 1;
    43 	}
    44 
    45 /**
    46 See hidapi documentation.
    47 */
    48 int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
    49 	{
    50     Close();
    51 
    52 	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
    53 
    54 	if (!iHidDevice)
    55 		{
    56 		//Fail to connect our device
    57 		return 0;
    58 		}
    59 
    60     FetchStrings();
    61 
    62 	return 1;
    63 	}
    64 
    65 /**
    66 Close this HID device
    67 */
    68 void HidDevice::Close()
    69 	{
    70 	hid_close(iHidDevice); //No effect if device is null
    71 	iHidDevice=NULL;
    72     //
    73     memset(iVendor,0,sizeof(iVendor));
    74     memset(iProduct,0,sizeof(iProduct));
    75     memset(iSerialNumber,0,sizeof(iSerialNumber));
    76 	}
    77 
    78 /**
    79 */
    80 bool HidDevice::IsOpen()
    81     {
    82     return iHidDevice!=NULL;
    83     }
    84 
    85 
    86 /**
    87 */
    88 const wchar_t* HidDevice::Error()
    89 	{
    90 	return hid_error(iHidDevice);
    91 	}
    92 
    93 /**
    94 */
    95 int HidDevice::SetNonBlocking(int aNonBlocking)
    96 	{
    97 	//Success we are now connected to our HID device
    98 	//Set read operation as non blocking
    99 	return hid_set_nonblocking(iHidDevice, aNonBlocking);
   100 	}
   101 
   102 /**
   103 */
   104 wchar_t* HidDevice::Vendor()
   105     {
   106     return iVendor;
   107     }
   108 
   109 /**
   110 */
   111 wchar_t* HidDevice::Product()
   112     {
   113     return iProduct;
   114     }
   115 
   116 /**
   117 */
   118 wchar_t* HidDevice::SerialNumber()
   119     {
   120     return iSerialNumber;
   121     }
   122 
   123 /**
   124 
   125 */
   126 void HidDevice::FetchStrings()
   127     {
   128     hid_get_manufacturer_string(iHidDevice,iVendor,sizeof(iVendor));
   129     hid_get_product_string(iHidDevice,iProduct,sizeof(iProduct));
   130     hid_get_serial_number_string(iHidDevice,iSerialNumber,sizeof(iSerialNumber));
   131     }