HidDevice.cpp
author StephaneLenclud
Wed, 06 May 2015 21:03:53 +0200
changeset 36 3b94d0640115
parent 7 8bac7aac665c
permissions -rw-r--r--
Adding C# interop project and NuGet package.
     1 //
     2 // Copyright (C) 2014-2015 Stéphane Lenclud.
     3 //
     4 // This file is part of MiniDisplay.
     5 //
     6 // MiniDisplay is free software: you can redistribute it and/or modify
     7 // it under the terms of the GNU General Public License as published by
     8 // the Free Software Foundation, either version 3 of the License, or
     9 // (at your option) any later version.
    10 //
    11 // MiniDisplay is distributed in the hope that it will be useful,
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 // GNU General Public License for more details.
    15 //
    16 // You should have received a copy of the GNU General Public License
    17 // along with MiniDisplay.  If not, see <http://www.gnu.org/licenses/>.
    18 //
    19 
    20 #include "HidDevice.h"
    21 
    22 //
    23 // class HidDevice
    24 //
    25 
    26 HidDevice::HidDevice():iHidDevice(NULL)
    27     {
    28     Close();
    29     }
    30 
    31 /**
    32 */
    33 HidDevice::~HidDevice()
    34 	{
    35 	Close();
    36 	}
    37 
    38 
    39 /**
    40 */
    41 int HidDevice::Open(const char* aPath)
    42 	{
    43 	Close();
    44 
    45 	iHidDevice =  hid_open_path(aPath);
    46 
    47 	if (!iHidDevice)
    48 		{
    49 		//Fail to connect our device
    50 		return 0;
    51 		}
    52 
    53     FetchStrings();
    54 
    55 	return 1;
    56 	}
    57 
    58 /**
    59 See hidapi documentation.
    60 */
    61 int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
    62 	{
    63     Close();
    64 
    65 	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
    66 
    67 	if (!iHidDevice)
    68 		{
    69 		//Fail to connect our device
    70 		return 0;
    71 		}
    72 
    73     FetchStrings();
    74 
    75 	return 1;
    76 	}
    77 
    78 /**
    79 Close this HID device
    80 */
    81 void HidDevice::Close()
    82 	{
    83 	hid_close(iHidDevice); //No effect if device is null
    84 	iHidDevice=NULL;
    85     //
    86     memset(iVendor,0,sizeof(iVendor));
    87     memset(iProduct,0,sizeof(iProduct));
    88     memset(iSerialNumber,0,sizeof(iSerialNumber));
    89 	}
    90 
    91 /**
    92 */
    93 bool HidDevice::IsOpen()
    94     {
    95     return iHidDevice!=NULL;
    96     }
    97 
    98 
    99 /**
   100 */
   101 const wchar_t* HidDevice::Error()
   102 	{
   103 	return hid_error(iHidDevice);
   104 	}
   105 
   106 /**
   107 */
   108 int HidDevice::SetNonBlocking(int aNonBlocking)
   109 	{
   110 	//Success we are now connected to our HID device
   111 	//Set read operation as non blocking
   112 	return hid_set_nonblocking(iHidDevice, aNonBlocking);
   113 	}
   114 
   115 /**
   116 */
   117 wchar_t* HidDevice::Vendor()
   118     {
   119     return iVendor;
   120     }
   121 
   122 /**
   123 */
   124 wchar_t* HidDevice::Product()
   125     {
   126     return iProduct;
   127     }
   128 
   129 /**
   130 */
   131 wchar_t* HidDevice::SerialNumber()
   132     {
   133     return iSerialNumber;
   134     }
   135 
   136 /**
   137 
   138 */
   139 void HidDevice::FetchStrings()
   140     {
   141     hid_get_manufacturer_string(iHidDevice,iVendor,sizeof(iVendor));
   142     hid_get_product_string(iHidDevice,iProduct,sizeof(iProduct));
   143     hid_get_serial_number_string(iHidDevice,iSerialNumber,sizeof(iSerialNumber));
   144     }