SDL
2.0
Main Page
Related Pages
Modules
Namespaces
Data Structures
Files
File List
Globals
hidtest.cpp
Go to the documentation of this file.
1
/*******************************************************
2
Windows HID simplification
3
4
Alan Ott
5
Signal 11 Software
6
7
8/22/2009
8
9
Copyright 2009
10
11
This contents of this file may be used by anyone
12
for any reason without any conditions and may be
13
used as a starting point for your own applications
14
which use HIDAPI.
15
********************************************************/
16
17
#include <stdio.h>
18
#include <wchar.h>
19
#include <string.h>
20
#include <stdlib.h>
21
#include "
hidapi.h
"
22
23
// Headers needed for sleeping.
24
#ifdef _WIN32
25
#include <windows.h>
26
#else
27
#include <unistd.h>
28
#endif
29
30
int
main
(
int
argc,
char
* argv[])
31
{
32
int
res
;
33
unsigned
char
buf
[256];
34
#define MAX_STR 255
35
wchar_t
wstr[
MAX_STR
];
36
hid_device
*
handle
;
37
int
i
;
38
39
#ifdef WIN32
40
UNREFERENCED_PARAMETER(argc);
41
UNREFERENCED_PARAMETER(argv);
42
#endif
43
44
struct
hid_device_info
*devs, *cur_dev;
45
46
if
(
hid_init
())
47
return
-1;
48
49
devs =
hid_enumerate
(0
x0
, 0
x0
);
50
cur_dev = devs;
51
while
(cur_dev) {
52
printf(
"Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls"
, cur_dev->
vendor_id
, cur_dev->
product_id
, cur_dev->
path
, cur_dev->
serial_number
);
53
printf(
"\n"
);
54
printf(
" Manufacturer: %ls\n"
, cur_dev->
manufacturer_string
);
55
printf(
" Product: %ls\n"
, cur_dev->
product_string
);
56
printf(
" Release: %hx\n"
, cur_dev->
release_number
);
57
printf(
" Interface: %d\n"
, cur_dev->
interface_number
);
58
printf(
"\n"
);
59
cur_dev = cur_dev->
next
;
60
}
61
hid_free_enumeration
(devs);
62
63
// Set up the command buffer.
64
memset
(buf,0x00,
sizeof
(buf));
65
buf[0] = 0x01;
66
buf[1] = 0x81;
67
68
69
// Open the device using the VID, PID,
70
// and optionally the Serial number.
71
////handle = hid_open(0x4d8, 0x3f, L"12345");
72
handle =
hid_open
(0x4d8, 0x3f,
NULL
);
73
if
(!handle) {
74
printf(
"unable to open device\n"
);
75
return
1;
76
}
77
78
// Read the Manufacturer String
79
wstr[0] = 0x0000;
80
res =
hid_get_manufacturer_string
(handle, wstr,
MAX_STR
);
81
if
(res < 0)
82
printf(
"Unable to read manufacturer string\n"
);
83
printf(
"Manufacturer String: %ls\n"
, wstr);
84
85
// Read the Product String
86
wstr[0] = 0x0000;
87
res =
hid_get_product_string
(handle, wstr,
MAX_STR
);
88
if
(res < 0)
89
printf(
"Unable to read product string\n"
);
90
printf(
"Product String: %ls\n"
, wstr);
91
92
// Read the Serial Number String
93
wstr[0] = 0x0000;
94
res =
hid_get_serial_number_string
(handle, wstr,
MAX_STR
);
95
if
(res < 0)
96
printf(
"Unable to read serial number string\n"
);
97
printf(
"Serial Number String: (%d) %ls"
, wstr[0], wstr);
98
printf(
"\n"
);
99
100
// Read Indexed String 1
101
wstr[0] = 0x0000;
102
res =
hid_get_indexed_string
(handle, 1, wstr,
MAX_STR
);
103
if
(res < 0)
104
printf(
"Unable to read indexed string 1\n"
);
105
printf(
"Indexed String 1: %ls\n"
, wstr);
106
107
// Set the hid_read() function to be non-blocking.
108
hid_set_nonblocking
(handle, 1);
109
110
// Try to read from the device. There shoud be no
111
// data here, but execution should not block.
112
res =
hid_read
(handle, buf, 17);
113
114
// Send a Feature Report to the device
115
buf[0] = 0x2;
116
buf[1] = 0xa0;
117
buf[2] = 0x0a;
118
buf[3] = 0x00;
119
buf[4] = 0x00;
120
res =
hid_send_feature_report
(handle, buf, 17);
121
if
(res < 0) {
122
printf(
"Unable to send a feature report.\n"
);
123
}
124
125
memset
(buf,0,
sizeof
(buf));
126
127
// Read a Feature Report from the device
128
buf[0] = 0x2;
129
res =
hid_get_feature_report
(handle, buf,
sizeof
(buf));
130
if
(res < 0) {
131
printf(
"Unable to get a feature report.\n"
);
132
printf(
"%ls"
,
hid_error
(handle));
133
}
134
else
{
135
// Print out the returned buffer.
136
printf(
"Feature Report\n "
);
137
for
(i = 0; i <
res
; i++)
138
printf(
"%02hhx "
, buf[i]);
139
printf(
"\n"
);
140
}
141
142
memset
(buf,0,
sizeof
(buf));
143
144
// Toggle LED (cmd 0x80). The first byte is the report number (0x1).
145
buf[0] = 0x1;
146
buf[1] = 0x80;
147
res =
hid_write
(handle, buf, 17);
148
if
(res < 0) {
149
printf(
"Unable to write()\n"
);
150
printf(
"Error: %ls\n"
,
hid_error
(handle));
151
}
152
153
154
// Request state (cmd 0x81). The first byte is the report number (0x1).
155
buf[0] = 0x1;
156
buf[1] = 0x81;
157
hid_write
(handle, buf, 17);
158
if
(res < 0)
159
printf(
"Unable to write() (2)\n"
);
160
161
// Read requested state. hid_read() has been set to be
162
// non-blocking by the call to hid_set_nonblocking() above.
163
// This loop demonstrates the non-blocking nature of hid_read().
164
res = 0;
165
while
(res == 0) {
166
res =
hid_read
(handle, buf,
sizeof
(buf));
167
if
(res == 0)
168
printf(
"waiting...\n"
);
169
if
(res < 0)
170
printf(
"Unable to read()\n"
);
171
#ifdef WIN32
172
Sleep(500);
173
#else
174
usleep(500*1000);
175
#endif
176
}
177
178
printf(
"Data read:\n "
);
179
// Print out the returned buffer.
180
for
(i = 0; i <
res
; i++)
181
printf(
"%02hhx "
, buf[i]);
182
printf(
"\n"
);
183
184
hid_close
(handle);
185
186
/* Free static HIDAPI objects. */
187
hid_exit
();
188
189
#ifdef WIN32
190
system(
"pause"
);
191
#endif
192
193
return
0;
194
}
src
hidapi
hidtest
hidtest.cpp
Generated on Sun Jun 26 2022 23:07:11 for SDL by
1.8.1.2