rainbowhat

package
v3.4.991+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package rainbowhat implements interfacing code to Pimoroni's Rainbow hat.

This driver provides easy access to the peripherals available on the Rainbow Hat for Android Things:

BMP280 temperature and pressure sensor (I2C)

HT16K33 segment display (I2C)

Capacitive buttons (GPIO)

LEDs (GPIO)

APA102 RGB LEDs (SPI)

Piezo Buzzer (PWM)

Servo header (PWM)

More details

Product Page

https://shop.pimoroni.com/products/rainbow-hat-for-android-things

Example
package main

import (
	"fmt"
	"image"
	"image/color"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/physic"
	"periph.io/x/periph/devices/apa102"
	"periph.io/x/periph/experimental/devices/rainbowhat"
	"periph.io/x/periph/host"
)

func main() {

	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	hat, err := rainbowhat.NewRainbowHat(&apa102.DefaultOpts)
	if err != nil {
		log.Fatal(err)
	}
	defer hat.Halt()

	handleButton := func(btn gpio.PinIn, led gpio.PinOut) {
		ledState := false
		if err := led.Out(gpio.Low); err != nil {
			log.Fatal(err)
		}
		for {
			btn.WaitForEdge(-1)
			if btn.Read() == gpio.Low {
				if ledState {
					if err := led.Out(gpio.High); err != nil {
						log.Fatal(err)
					}
				} else {
					if err := led.Out(gpio.Low); err != nil {
						log.Fatal(err)
					}
				}
				ledState = !ledState
			}
		}
	}

	go handleButton(hat.GetButtonA(), hat.GetLedR())
	go handleButton(hat.GetButtonB(), hat.GetLedG())
	go handleButton(hat.GetButtonC(), hat.GetLedB())

	ledstrip := hat.GetLedStrip()
	ledstrip.Intensity = 50

	img := image.NewNRGBA(image.Rect(0, 0, ledstrip.Bounds().Dx(), 1))
	img.SetNRGBA(0, 0, color.NRGBA{148, 0, 211, 255})
	img.SetNRGBA(1, 0, color.NRGBA{75, 0, 130, 255})
	img.SetNRGBA(2, 0, color.NRGBA{0, 0, 255, 255})
	img.SetNRGBA(3, 0, color.NRGBA{0, 255, 0, 255})
	img.SetNRGBA(4, 0, color.NRGBA{255, 255, 0, 255})
	img.SetNRGBA(5, 0, color.NRGBA{255, 127, 0, 255})
	img.SetNRGBA(6, 0, color.NRGBA{255, 0, 0, 255})

	if err := ledstrip.Draw(ledstrip.Bounds(), img, image.Point{}); err != nil {
		log.Fatalf("failed to draw: %v", err)
	}

	display := hat.GetDisplay()
	sensor := hat.GetBmp280()
	ticker := time.NewTicker(3 * time.Second)
	go func() {
		for {
			var envi physic.Env
			if err := sensor.Sense(&envi); err != nil {
				log.Fatal(err)
			}

			temp := fmt.Sprintf("%5s", envi.Temperature)
			fmt.Printf("Pressure %8s \n", envi.Pressure)
			fmt.Printf("Temperature %8s \n", envi.Temperature)

			if _, err := display.WriteString(temp); err != nil {
				log.Fatal(err)
			}
			<-ticker.C
		}
	}()

	sigs := make(chan os.Signal, 1)
	done := make(chan bool, 1)

	signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)

	go func() {
		sig := <-sigs // Wait for signal
		log.Println(sig)
		done <- true
	}()

	log.Println("Press ctrl+c to stop...")
	<-done // Wait
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dev

type Dev struct {
	// contains filtered or unexported fields
}

Dev represents a Rainbow HAT (https://shop.pimoroni.com/products/rainbow-hat-for-android-things)

func NewRainbowHat

func NewRainbowHat(ao *apa102.Opts) (*Dev, error)

NewRainbowHat returns a rainbowhat driver.

func (*Dev) GetBmp280

func (d *Dev) GetBmp280() *bmxx80.Dev

GetBmp280 returns bmxx80.Dev handler.

func (*Dev) GetButtonA

func (d *Dev) GetButtonA() gpio.PinIn

GetButtonA returns gpio.PinIn corresponding to the A capacitive button.

func (*Dev) GetButtonB

func (d *Dev) GetButtonB() gpio.PinIn

GetButtonB returns gpio.PinIn corresponding to the B capacitive button.

func (*Dev) GetButtonC

func (d *Dev) GetButtonC() gpio.PinIn

GetButtonC returns gpio.PinIn corresponding to the C capacitive button.

func (*Dev) GetBuzzer

func (d *Dev) GetBuzzer() gpio.PinOut

GetBuzzer returns gpio.PinOut corresponding to the buzzer pin.

func (*Dev) GetDisplay

func (d *Dev) GetDisplay() *ht16k33.Display

GetDisplay returns ht16k33.Display with four alphanumeric digits.

func (*Dev) GetLedB

func (d *Dev) GetLedB() gpio.PinOut

GetLedB returns gpio.PinOut corresponding to the blue LED.

func (*Dev) GetLedG

func (d *Dev) GetLedG() gpio.PinOut

GetLedG returns gpio.PinOut corresponding to the green LED.

func (*Dev) GetLedR

func (d *Dev) GetLedR() gpio.PinOut

GetLedR returns gpio.PinOut corresponding to the red LED.

func (*Dev) GetLedStrip

func (d *Dev) GetLedStrip() *apa102.Dev

GetLedStrip returns apa102.Dev seven addressable led strip.

func (*Dev) GetServo

func (d *Dev) GetServo() gpio.PinOut

GetServo returns gpio.PinOut corresponding to the servo pin.

func (*Dev) Halt

func (d *Dev) Halt() error

Halt all internal devices.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL