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