Design Pattern for receiving USB datastream real-time


I’ve been playing with (to communicate with my Wacom pen)

It’s really awesome. You can transfer bytes to/from HID devices. It’s just as single .h and a per-platform .c, so Dead easy to integrate with a JUCE project.

However I’m not sure how to design the main loop. This is what I’ve got so far:

const int /*enable_nonblocking = 1,*/ disable_nonblocking = 0;
jassert( hid_set_nonblocking(handle, disable_nonblocking) != FAIL); // block!
while(true) {
    int bytes_got = hid_read(handle, packet, 10);

This is obviously awful. But what’s a good solution?

I think blocking is bad period.

So my options seem to be either a timer or Thread::sleep.

I think the best design would be to spawn a new thread, and use nonblocking + Thread::sleep.

Can anyone see anything wrong with that?



Even if i can’t answer on your issue , must say that this HID API is really cool- just forked it !
thanks for telling =)


I’d use Thread + hid_read_timeout w/ blocking… blocking is ok, because you are in a thread, and it is doing it’s own sleep, or waiting on event, or something. and the timeout is needed so that you can check if you need to exit the thread (ie. program exiting, etc)