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