Muddy Boots..and getting Qt 5.1.0 (MSVC2012) working with HIDAPI

Hey all.
Tons of apologies for the silence. I've been extremely busy with the said Industrial Attachment. Apparently real-life work takes it out of you.
I hope you've all been doing well, and things have been going great on your various ends of the world.
Today I'm going to talk about getting Qt 5.1.0 (latest and greatest at the time of this writing) working with Signal11's HIDAPI, a cross-platform wrapper library for communicating with HID devices. This is particularly useful to me, on account of the fact that I happen to own a Teensy 2.0 which lets me run LUFA, a USB framework for AVR Microcontrollers which simplifies (greatly, let me tell you) the process of building custom USB devices. A year or so ago I happened to do just that: a custom HID device I could control from a PC with custom software written in C# using this library. However, its immediately apparent that such a program could only ever run on Windows. What if I needed to make it cross-platform?
As I've mentioned before, I've dabbled with a bit of C++. Of recent, I've been studying the Qt cross-platform framework, which is amazing in its own way. Write once run anywhere and all that.
So I dug out my Teensy a few weeks ago, and decided to find some way of building a cross-platform version of my C# control app, mostly for fun, but also for the masochistic side I seem to have..more on that later.
So while digging around, I happened to find HIDAPI..the code looked simple and straightforward enough, so I took a leap of faith and downloaded it. I was successfully able to compile the included test program after some knocking around.
I also recently migrated to Windows 8 and Qt 5.1, and it was a bit of a pain getting the same example to run once again. As such, I've decided to put this down for reference purposes.
Here we go:
1) Download HIDAPI from the github link provided above.
2) Fire up Qt Creator (I got the MSVC2012 x86 download from this site..perhaps they'll be at a new milestone when you see this)
3) Create a Qt Console app and name it what you will
4) Once you're at the code editor window in Qt creator, return to the directory where you've got the HIDAPI sources.
5) Open the hidapi folder in the HIDAPI source tree and copy hidapi.h to your Qt project source directory, then go back to the HIDAPI source tree again
6) Open the windows folder in the HIDAPI source tree and copy hid.c to your Qt project source directory

Now, this is where it gets tricky.
There are essentially two ways to use HIDAPI in your Qt/C++ project really..compiling it as part of your project source or linking against its library. To do the latter, you'll need to build it using Visual Studio. However, I will not cover that. Send me a shout at @DarkDarkM though if you need a hand with that.
I prefer compiling it as part of the project source, so I don't end up having to distribute more binaries than I need to. Yes, a little neurosis isn't a bad thing.

7) Return to Qt creator, select your project from the tree in the top-left-hand corner and right click it.
8) Select Add Existing files from the menu that appears
9) Well....add the hidapi.h and hid.c files
10) In the same pane, you should be able to see a .pro file listed. Right now, just take note of that
11) In your main.cpp file, include the following headers: QDebug, stdio.h, stdlib,string.h,wchar.h,Windows.h and "hidapi.h"
12) Replace your main method in the same main.cpp file with this code
13) Next, try to build the project. You may or may not end up with some weird errors regarding some symbol called _enumerate_ somethingorother not being found.
14) Return to the .pro file I mentioned in Step 10. On opening it, you'll see a file that closely resembles some sort of makefile, with SOURCES=(stuff), HEADERS=(stuff) etc etc
15) At the bottom of the file, add this line: LIBS += -lsetupapi

Essentially, we're telling the linker to add the setupapi.lib file to its search path. That file should provide the missing symbols it complained about earlier.

16) Attempt to build the project again. You may or may not get any errors. If you got no errors, skip to step 20.
17) Crap, you're reading this because you got an error. From the Build menu, select Run qmake
18) After that, which should complete successfully, select Build from the same build menu. All things being equal, your build should complete with no errors and/or about 3 warnings or so. For some reason even rebuilding the project does not invoke qmake automatically, which I posit may need to be called everytime a new source, header or library file is added so it regenerates the background makefile...or something like that.
19) You may now run the code, which should pop out a console window and list out the HID devices its found (if any) and end with 'unable to open device' message at the bottom. Plug in the nearest USB mouse and try again to see if its picked it up.
20) That's it, for the most part. HIDAPI has excellent documentation, so modifying this example should not be much hassle. For kicks, you can rummage through the code and change the VID/PID pairs to your own device's and see how that works out for you.

I hope this helps someone out there, somewhere, sometime.
See you guys in the next few minutes. I just remembered there was something else I wanted to blog about.

Comments

Post a Comment

Popular posts from this blog

Bitbanging SPI on the Raspberry Pi (via spi-gpio)

Enabling SPI1 on the Raspberry Pi B+/Zero/2/3

Getting Started with Logic Analyzers and Pulseview