This project demonstrates intefacing to a USB device in Go, the ThingM blink(1). The original interfacing examples are written in C, so this project uses CGO to integrate C code into Go.
The code was developed and tested on Mac OSX Mountain Lion, but uses nothing OS-specific, so should also work on Linux.
To run, you'll first need to install libusb. The easiest way to so is via MacPorts.
-
Install Xcode and the Command Line Tools for Xcode
-
Install MacPorts
-
Now use MacPorts to install libusb:
$ sudo port install libusb-compat
If your MacPorts installation does not install the libusb files into /opt/local/{include | lib}, then adjust the #cgo compiler directives in hid.go.
-
cd to the directory with this project's files, then to build and run
$ go build goBlink.go && ./goBlink
Several functions are shown, including blink, set, and random. Others can be added such as read and write.
The code is based on the C code in the blink1 repository, particularly the blink1-mini-tool. Christian Starkjohann's hiddata.c is used almost as is. The modifications are to use enums instead of #defines to make the values available in Go. Also, the single-inclusion #defines needed for C are not needed for Go and excised.
See also the other examples in the command line directory.