SDL  2.0
hidtest.cpp File Reference
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <stdlib.h>
#include "hidapi.h"
#include <windows.h>
+ Include dependency graph for hidtest.cpp:

Go to the source code of this file.

Macros

#define MAX_STR   255
 

Functions

int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ MAX_STR

#define MAX_STR   255

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 30 of file hidtest.cpp.

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;
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 }

References hid_close(), hid_enumerate(), hid_error(), hid_exit(), hid_free_enumeration(), hid_get_feature_report(), hid_get_indexed_string(), hid_get_manufacturer_string(), hid_get_product_string(), hid_get_serial_number_string(), hid_init(), hid_open(), hid_read(), hid_send_feature_report(), hid_set_nonblocking(), hid_write(), i, hid_device_info::interface_number, hid_device_info::manufacturer_string, MAX_STR, memset, hid_device_info::next, NULL, hid_device_info::path, hid_device_info::product_id, hid_device_info::product_string, hid_device_info::release_number, hid_device_info::serial_number, and hid_device_info::vendor_id.

hid_error
const HID_API_EXPORT wchar_t *HID_API_CALL hid_error(hid_device *device)
Get a string describing the last error which occurred.
hid_device_info::interface_number
int interface_number
Definition: hidapi.h:79
hid_write
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.
hid_send_feature_report
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.
NULL
#define NULL
Definition: begin_code.h:167
handle
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
hid_exit
int HID_API_EXPORT HID_API_CALL hid_exit(void)
Finalize the HIDAPI library.
hid_close
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
Close a HID device.
hid_free_enumeration
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
x0
GLuint GLfloat x0
Definition: SDL_opengl_glext.h:8583
hid_init
int HID_API_EXPORT HID_API_CALL hid_init(void)
Initialize the HIDAPI library.
hid_get_indexed_string
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.
hid_device_info::manufacturer_string
wchar_t * manufacturer_string
Definition: hidapi.h:66
hid_open
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.
hid_device_info::next
struct hid_device_info * next
Definition: hidapi.h:82
hid_device_info
Definition: hidapi.h:53
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: SDL_opengl_glext.h:2480
hid_enumerate
struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
hid_device_info::product_id
unsigned short product_id
Definition: hidapi.h:59
hid_device_info::serial_number
wchar_t * serial_number
Definition: hidapi.h:61
MAX_STR
#define MAX_STR
hid_device_info::release_number
unsigned short release_number
Definition: hidapi.h:64
hid_get_serial_number_string
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.
memset
#define memset
Definition: SDL_malloc.c:627
hid_get_feature_report
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_set_nonblocking
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
Set the device handle to be non-blocking.
hid_read
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.
hid_get_product_string
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.
hid_device
struct hid_device_ hid_device
Definition: hidapi.h:50
res
GLuint res
Definition: SDL_opengl_glext.h:7937
hid_device_info::path
char * path
Definition: hidapi.h:55
hid_get_manufacturer_string
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.
i
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
hid_device_info::vendor_id
unsigned short vendor_id
Definition: hidapi.h:57
hid_device_info::product_string
wchar_t * product_string
Definition: hidapi.h:68