1. 程式人生 > >Controlling a Digispark board · Applied Go

Controlling a Digispark board · Applied Go

A tiny microcontroller board

It was after the latest Munich Gophers Meetup when a few of us went to a local bar to talk about Go, life, and hardware. Yes, hardware. From foldable USB keyboards to Raspberry Pi Zero W to some incredibly small microcontroller board called Digispark.

A Digispark clone

This one caught my attention. (Ok, the board is nothing new, but I did not hear of it before.) The Digispark is a board built around an ATtiny85 microcontroller chip and features -

  • Incredible six (!) I/O ports
  • A whopping 8 KB of flash memory (that’s over eight thousand bytes, folks!)
  • A blazingly fast 16.5MHz system clock

This tiny board connects directly to an USB port of a host computer and can be equipped with Arduino scripts (with restrictions of course, it is a much smaller controller than the one on the Arduino boards), but this is not an Arduino blog, so here comes the Go part.

UPDATE: Since the article was first published, the installation steps have considerably changed, and are even simpler now, thanks to the helpful comment from Ron Evans of gobot.io. Hence most of the following section has been rewritten. (The original article is available here but it is really only of historical interest.)

Controlling a Digispark board from Go code

With a few more (software) ingredients, the I/O ports can be controlled from Go code running on a PC/Mac/LinuxBox/etc. These are:

  • LittleWire
  • Gobot
  • Gort

LittleWire is basically a script that adds USB communication capabilities to the Digispark. With an accompanying library at the USB host’s end, the board’s I/O ports can be remote-controlled from an app running on the host.

Gobot is a Go robotics framework that connects Go apps to a large array of electronic devices, from the little Digispark to Arduino, Raspberry Pi, and even Quadrocopters.

Gort is a CLI tool for Gobot and other (non-Go) robotics frameworks. Here we use it for installing LittleWire on the Digispark board.

A first success (Image: A first success)

And here is how I used all this to make the onboard status LED blink, and a servo motor move.

The steps

Step 1: Install Gobot

For Mac and Linux, some USB libraries need to be installed. On the Mac, this is a one-liner if you have Homebrew installed.

brew install libusb && brew install libusb-compat

On Ubuntu (and surely also on other Debian-based distributions), a simple apt-get does the trick.

sudo apt-get install libusb-dev

Now I was ready to fetch Gobot and install the Digispark platform package. (If you are new to Go: the ellipsis at the end of the go get line advise the go get tool to also download all subprojects, even if the top project does not import them. The -d is also important as this prevents the go get tool from installing anything at this point. The only thing we want to install is the Digispark package.)

  go get -d -u gobot.io/x/gobot/...

Step 2: Install Gort

Gort can be downloaded from gort.io or build from source. On a Mac you can also use Homebrew to enjoy automatic updates:

brew install hybridgroup/tools/gort

Which I did.

Step 3: Install LittleWire

Gort makes installing LittleWire a snap. One command downloads the LittleWire firmware,

gort digispark install

and a second one uploads it to the Digispark controller.

gort digispark upload littlewire

The second command waits until the controller is plugged into an USB port. This ensures that the controller listens to the host, as the micronucleus bootloader cuts the connection after a few seconds, in order to make all ports available for I/O.

On my end, the upload command failed until I switched to an old USB-2 hub. The Digispark docs on gobot.io mention an issue with USB-3 and Mac Book Pros from 2012. I have no Mac Book Pro, but my Mac Mini is also from 2012, maybe it shares the same USB controller with the MBP.

Anyway, the Digispark board is now ready to receive commands. Time to write some Gobot-fueled code.

The code

After everything was in place, I wrote the below code to make the onboard LED blink and let a servo move from 0 to 180 degrees in steps of 45 degrees. This video shows the outcome:

But now on to the code: