sl@0: // StephaneLenclud@35: // Copyright (C) 2014-2015 Stéphane Lenclud. sl@0: // StephaneLenclud@35: // This file is part of MiniDisplay. StephaneLenclud@35: // StephaneLenclud@35: // MiniDisplay is free software: you can redistribute it and/or modify StephaneLenclud@35: // it under the terms of the GNU General Public License as published by StephaneLenclud@35: // the Free Software Foundation, either version 3 of the License, or StephaneLenclud@35: // (at your option) any later version. StephaneLenclud@35: // StephaneLenclud@35: // MiniDisplay is distributed in the hope that it will be useful, StephaneLenclud@35: // but WITHOUT ANY WARRANTY; without even the implied warranty of StephaneLenclud@35: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the StephaneLenclud@35: // GNU General Public License for more details. StephaneLenclud@35: // StephaneLenclud@35: // You should have received a copy of the GNU General Public License StephaneLenclud@35: // along with MiniDisplay. If not, see . sl@0: // sl@0: sl@0: #include "HidDevice.h" sl@0: sl@0: // sl@0: // class HidDevice sl@0: // sl@0: sl@0: HidDevice::HidDevice():iHidDevice(NULL) sl@0: { sl@0: Close(); sl@0: } sl@0: sl@0: /** sl@0: */ sl@7: HidDevice::~HidDevice() sl@7: { sl@7: Close(); sl@7: } sl@7: sl@7: sl@7: /** sl@7: */ sl@0: int HidDevice::Open(const char* aPath) sl@0: { sl@0: Close(); sl@0: sl@0: iHidDevice = hid_open_path(aPath); sl@0: sl@0: if (!iHidDevice) sl@0: { sl@0: //Fail to connect our device sl@0: return 0; sl@0: } sl@0: sl@0: FetchStrings(); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: /** sl@0: See hidapi documentation. sl@0: */ sl@0: int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber) sl@0: { sl@0: Close(); sl@0: sl@0: iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber); sl@0: sl@0: if (!iHidDevice) sl@0: { sl@0: //Fail to connect our device sl@0: return 0; sl@0: } sl@0: sl@0: FetchStrings(); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: /** sl@7: Close this HID device sl@0: */ sl@0: void HidDevice::Close() sl@0: { sl@7: hid_close(iHidDevice); //No effect if device is null sl@0: iHidDevice=NULL; sl@0: // sl@0: memset(iVendor,0,sizeof(iVendor)); sl@0: memset(iProduct,0,sizeof(iProduct)); sl@0: memset(iSerialNumber,0,sizeof(iSerialNumber)); sl@0: } sl@0: sl@0: /** sl@0: */ sl@0: bool HidDevice::IsOpen() sl@0: { sl@0: return iHidDevice!=NULL; sl@0: } sl@0: sl@0: sl@0: /** sl@0: */ sl@0: const wchar_t* HidDevice::Error() sl@0: { sl@0: return hid_error(iHidDevice); sl@0: } sl@0: sl@0: /** sl@0: */ sl@0: int HidDevice::SetNonBlocking(int aNonBlocking) sl@0: { sl@0: //Success we are now connected to our HID device sl@0: //Set read operation as non blocking sl@0: return hid_set_nonblocking(iHidDevice, aNonBlocking); sl@0: } sl@0: sl@0: /** sl@0: */ sl@0: wchar_t* HidDevice::Vendor() sl@0: { sl@0: return iVendor; sl@0: } sl@0: sl@0: /** sl@0: */ sl@0: wchar_t* HidDevice::Product() sl@0: { sl@0: return iProduct; sl@0: } sl@0: sl@0: /** sl@0: */ sl@0: wchar_t* HidDevice::SerialNumber() sl@0: { sl@0: return iSerialNumber; sl@0: } sl@0: sl@0: /** sl@0: sl@0: */ sl@0: void HidDevice::FetchStrings() sl@0: { sl@0: hid_get_manufacturer_string(iHidDevice,iVendor,sizeof(iVendor)); sl@0: hid_get_product_string(iHidDevice,iProduct,sizeof(iProduct)); sl@0: hid_get_serial_number_string(iHidDevice,iSerialNumber,sizeof(iSerialNumber)); sl@0: }