Raw Hid + Teensy 3.1 + oF


Ubuntu 14.04 is my OS and I’m planning on using the Teensy 3.1 to send Raw HID packets to my pc:
If you read the hid_LINUX.c file from:
the author mentions the hidraw library over and over in the comments section. I’m assuming he’s talking about the libhidapi-hidraw0 library in Ubuntu 14.04.

My question:
If I want to be able to send Raw HID packets to openFrameworks is it just a matter of doing a #include to add the C library and putting the library in the proper oF folder? I’m not clear on how to be able to incorporate this into my code. If openFrameworks already has a library that can handle Raw HID on a Linux PC let me know because I was only able to find OSX related code.


isnt that what the host side functions are for?

Host (PC or Mac) Side Functions

   int rawhid_open(int max, int vid, int pid, int usage_page, int usage);

Open up to "max" devices that match vid, pid, usage_page and usage. Return is the number of devices actually opened.

If its built in system functionality there should be native system handlers which is what I assume they are referring to with the host functions. Is there a reason you need it to do rawhid vs firmata or some other communication protocol?

I want to keep latency to a minimum. From the sensor to the PC in 5 ms (or at least less than 10 ms). I’m also going to be using 200 kB/s of bandwidth. What can firmata do?

To get that in firmata you would have to alter the standard firmata to make the latency that low. Firmata comes packaged with arduino it utilizes a midi style communication protocol between arduinos and a client. A client is part of the core of openframeworks and there are examples how to use it.

I can see why using a raw hid might be applicable. try adding the header and appropriate c file to your project and then import it into ofApp.h which should allow you to connect once you call the appropriate functions. so in the setup call

these defines seem arbitrary and i just got them from the example but include them in ofApp.h

#define VENDOR_ID               0x16C0
#define PRODUCT_ID              0x0480
#define RAWHID_USAGE_PAGE       0xFFAB  // recommended: 0xFF00 to 0xFFFF
#define RAWHID_USAGE            0x0200  // recommended: 0x0100 to 0xFFFF

Then in setup in ofApp.cpp


you may want to thread this because if you want that kind of latency you will have to call rawhid_recv faster than update and draw is called in openframeworks unless you disable vertical syncing and another problem would be filling the buffer faster than it can be read or cleared which then you are losing accuracy anyways.

Thanks for the insight and help.

What keywords should I be searching for in openFrameworks to learn how to create a thread and make the reading part independent from update() and draw(). My understanding is that audioOut() is independent from update() and draw() so maybe I could come up with a function that takes care of reading the values from the USB buffers.

BTW: I’ve always wondered what happens in an OS when a variable is being written to while your program is trying to get its value. Is that something that is handled by the Linux OS?

Thanks again,

The documentation of ofThread is pretty decent and there should be examples out there

I would recommend against using audio out as your separate thread though it does get polled much more often. It will be better to make your own thread and then you can have it do more robust functions and signal events like a change in value etc…

Your question about variable read writes is why mutexes exist so that such a case does not happen

1 Like

I’ve got my work cut out for me… I have to learn how to call C from C++ but that seems straight forward. Learning how to use hidapi is going to be the hard part. Finally learning some Linux fundamentals won’t hurt either.

BTW I visited your site… You’re doing some amazing research.

Take care,

C from C++ is quite easy this little forum post explains it pretty decently

Good luck!

I’m also trying to wrap the rawhid_test.c (LINUX) from https://www.pjrc.com/teensy/rawhid.html into openframeworks (C++). Do someone succeed with it?