Here is a JUCE C++ wrapper for the hidapi, a convenient library that interfaces with HID devices.
Thought it might be useful to someone!
There is a hid::DeviceScanner
class that will notify you when a device is connected, a central DeviceIO class that does most of the dirty work without needing to touch the lower-level C API, and a few other helper classes & functions.
I will usually do something like this:
if (hid::isDeviceAvailable (MY_DEVICE_VID, MY_DEVICE_PID) {
auto deviceInfo = hid::getDeviceInfo (MY_DEVICE_VID, MY_DEVICE_PID);
auto deviceIO = deviceInfo.connect();
deviceIO.setNonblocking (true);
if (hid::isConnected()) {
// write something to the device
auto result = hid::getConnectedDevice().write (&dataToSend, numBytesToSend);
if (! result.wasOk()) {
DBG("Couldn't write to device: " << result.getErrorMessage());
}
// read something back - I set it to nonblocking above, so we need to poll it regularly
// I usually do this on a timer thread, but whatever floats your boat
while (true) {
result = hid::getConnectedDevice().read (&dataToRead, numBytesToRead);
if (result.wasOk()) {
// Read some bytes from the device!
break;
}
}
}
}