SDL 2.0
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
30int 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];
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(0x0, 0x0);
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 }
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;
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;
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;
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;
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.
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;
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;
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
185
186 /* Free static HIDAPI objects. */
187 hid_exit();
188
189#ifdef WIN32
190 system("pause");
191#endif
192
193 return 0;
194}
#define memset
Definition: SDL_malloc.c:627
GLuint res
GLenum GLuint GLenum GLsizei const GLchar * buf
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
HID_API_EXPORT hid_device *HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
Read an Input report from a HID device.
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
int HID_API_EXPORT HID_API_CALL hid_init(void)
Initialize the HIDAPI library.
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
Get a feature report from a HID device.
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error(hid_device *device)
Get a string describing the last error which occurred.
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
Close a HID device.
struct hid_device_ hid_device
Definition: hidapi.h:50
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
int HID_API_EXPORT HID_API_CALL hid_exit(void)
Finalize the HIDAPI library.
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
Write an Output report to a HID device.
int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
Send a Feature report to the device.
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
Set the device handle to be non-blocking.
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
int main(int argc, char *argv[])
Definition: hidtest.cpp:30
#define MAX_STR
unsigned short product_id
Definition: hidapi.h:59
struct hid_device_info * next
Definition: hidapi.h:82
wchar_t * manufacturer_string
Definition: hidapi.h:66
unsigned short vendor_id
Definition: hidapi.h:57
char * path
Definition: hidapi.h:55
unsigned short release_number
Definition: hidapi.h:64
wchar_t * serial_number
Definition: hidapi.h:61
int interface_number
Definition: hidapi.h:79
wchar_t * product_string
Definition: hidapi.h:68