Last active
November 15, 2016 00:08
-
-
Save todbot/8cd6c3ebdd7357dabd346ac16d8735c8 to your computer and use it in GitHub Desktop.
Demonstrate Mac hidapi bug sending to Teensy doing RawHID sketch: https://www.pjrc.com/teensy/rawhid.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test-rawhid.c -- Demonstrate writing to Teensy running RawHID sketch | |
// On Mac, this will occasionally hang in hid_write() | |
// | |
// Build with: | |
// gcc -I ../hidapi/hidapi -framework IOKit -framework CoreFoundation../hidapi/mac/hid.c rawhid-test.c | |
// | |
// Tod E. Kurt / github.com/todbot | |
// | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "hidapi.h" | |
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 255 | |
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[1024]; // 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 == 0x16C0 && cur_dev->product_id == 0x0486 && 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 ); | |
memset(buf, 0, sizeof(buf)); | |
buf[0] = 'x'; | |
buf[1] = 'y'; | |
buf[2] = 'z'; | |
printbuf((char*)buf, sizeof(buf)); | |
res = hid_write(handle, buf, sizeof(buf)); | |
if (res < 0) { | |
printf("unable to write()"); | |
} | |
//#define ENABLE_READ | |
#ifdef ENABLE_READ | |
// Read requested state | |
res = hid_read(handle, buf, sizeof(buf)); | |
if (res < 0) { | |
printf("Unable to read()\n"); | |
} | |
else { | |
// Print out the returned buffer. | |
printbuf( (char*)buf, res); | |
} | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment