Go GPIO library for Raspberry Pi

GopherBeen playing a bit with Go (-lang) lately, it seems like a fun little language. Very minimalistic and clean, yet quite powerful – you can do so many things with the simple constructs they provide (especially the goroutines, channels and type-system). Sadly, it looks like it’s pretty slow in benchmarks compared to the other natural alternatives – but it’s still early.

animatedAnyway, in an attempt to mix Gophers and Pi, I’ve made a small native GPIO library for Go on the Raspberry Pi (or the bcm2835 chipset in general). Nothing advanced, but it provides the usual suspects: PinMode (Output, Input), Write, Read, PullUp, PullDown, PullOff, etc. It works by memory-mapping the GPIO addresses in /dev/mem, so it will require root.

To use, and blink a little LED (“hello world” of the electronics/microcontroller realm) just do something like:


import (
	"fmt"
	"github.com/stianeikeland/go-rpio"
	"os"
	"time"
)

func main() {
	if err := rpio.Open(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer rpio.Close()

	pin = rpio.Pin(10)
	pin.Output()

	for x := 0; x < 20; x++ {
		pin.Toggle()
		time.Sleep(time.Second)
	}
}

Available over at GitHub.

Would be awesome to add PWM, I2C, SPI, etc.. who knows, maybe one day..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s