tomnz / button-shim-go

Go library for Pimoroni's Button SHIM
https://shop.pimoroni.com/products/button-shim
MIT License
3 stars 1 forks source link

Button SHIM Go library

build godocs

Provides a Go implementation for interfacing with Pimoroni's Button SHIM. The top-level library provides a lot of the same functionality as the reference Python library, including:

Overview

Installation

First, clone the project into your Go path:

go get github.com/tomnz/button-shim-go

The library depends on the periph.io framework for low level device communication. You can install this manually with go get, or (preferred) use dep:

go get -u github.com/golang/dep/cmd/dep
cd $GOPATH/src/github.com/tomnz/button-shim-go
dep ensure

Usage

First, initialize a periph.io I2C bus, and instantiate the device with it:

package main

import (
    "github.com/tomnz/button-shim-go"
    "periph.io/x/periph/conn/i2c/i2creg"
    "periph.io/x/periph/host"
)

func main() {
    // TODO: Handle errors
    _, _ := host.Init()
    bus, _ := i2creg.Open("1")
    shim, _ := buttonshim.New(bus)
}

The library implements button press and release handlers using channels. For example:

aPress := shim.ButtonPressChan(buttonshim.ButtonA)
bPress := shim.ButtonPressChan(buttonshim.ButtonB)
aRelease := shim.ButtonReleaseChan(buttonshim.ButtonA)
go func() {
    for {
        select {
        case <-aPress:
            fmt.Println("Button A pressed!")
        case <-bPress:
            fmt.Println("Button B pressed!")
        case holdDuration := <-aRelease:
            fmt.Printf("Button A held for %s!", holdDuration)
        }
    }
}()

The color and brightness of the pixel can also be changed:

shim.SetColor(255, 0, 0)
shim.SetBrightness(127)

Please refer to the godocs for full API reference.

Contributing

Contributions welcome! Please refer to the contributing guide.