You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

github.com/yamamotoworksdev/button-shim-go

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/yamamotoworksdev/button-shim-go

v0.1.0
Source
Go
Version published
Created
Source

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:

  • Handle press/release events for the buttons
  • Update the color of the RGB LED.

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.

FAQs

Package last updated on 22 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts