Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active November 16, 2020 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save todbot/12f4a26e840e3229a04aeac883fae332 to your computer and use it in GitHub Desktop.
Save todbot/12f4a26e840e3229a04aeac883fae332 to your computer and use it in GitHub Desktop.
hidapi list devices
// hidapi-enum-tst.c -- Demonstrate enumerating
//
// Build with: (assumes you have checked out 'hidapi' into a side directory)
// Mac:
// gcc -I ../hidapi/hidapi ../hidapi/mac/hid.c -framework IOKit -framework CoreFoundation hidapi-enum-tst.c -o hidapi-enum-tst
// Linux (hidraw):
// gcc -I ../hidapi/hidapi ../hidapi/linux/hid.c -ludev hidapi-enum-tst.c -o hidapi-enum-tst
// Linux (libusb):
// gcc -I ../hidapi/hidapi -I /usr/include/libusb-1.0 ../hidapi/libusb/hid.c -lpthread -lusb-1.0 hidapi-enum-tst.c -o hidapi-enum-tst
//
// Tod E. Kurt / github.com/todbot
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hidapi.h"
unsigned short vid_to_find = 0x27B8; // ThingM
unsigned short pid_to_find = 0x01ED; // blink1
void printbuf(char* buf, int bufsize) {
printf("buf:");
for (int i = 0; i < bufsize; i++) {
printf("%d,", buf[i]);
}
printf("\n");
}
int main(int argc, char* argv[])
{
int res;
unsigned char buf[64];
#define MAX_STR 1024
wchar_t wstr[MAX_STR];
hid_device *handle;
int i;
// Enumerate and print the HID devices on the system
struct hid_device_info *devs, *cur_dev;
char foundpath[MAX_STR]; // 1024 should be enough for anyone :)
devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
printf("Device: vid/pid: %04hx/%04hx\n path: %s\n serial_number: %ls usage_page: %x, usage: %x",
cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number, cur_dev->usage_page, cur_dev->usage);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf("\n");
if( cur_dev->vendor_id == vid_to_find && cur_dev->product_id == pid_to_find ) { // && cur_dev->usage_page == 0xFFAB && cur_dev->usage == 0x200 ) {
printf("Found our device!\n");
strcpy( foundpath, cur_dev->path );
}
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
// Open the device using the VID, PID,
// and optionally the Serial number.
// handle = hid_open(0x16C0, 0x0486, NULL);
// handle = hid_open_path( foundpath );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment