mirror of https://github.com/periph/devices
I was so happy with recent Example refactors, did it for all packages
This creates an example_test.go for each package with at least one Example, and move all examples in it. Make the example_test.go file live into a separate package 'foo_test' so it can call host.Init() and thus can be copied as-is.pull/1/head
parent
46011cfcbf
commit
124fa6f98b
@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2018 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package apa102_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"periph.io/x/periph/conn/spi/spireg"
|
||||||
|
"periph.io/x/periph/devices/apa102"
|
||||||
|
"periph.io/x/periph/host"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example() {
|
||||||
|
// Make sure periph is initialized.
|
||||||
|
if _, err := host.Init(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use spireg SPI port registry to find the first available SPI bus.
|
||||||
|
p, err := spireg.Open("")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer p.Close()
|
||||||
|
|
||||||
|
// Opens a strip of 150 lights are 50% intensity with color temperature at
|
||||||
|
// 5000 Kelvin.
|
||||||
|
dev, err := apa102.New(p, 150, 127, 5000)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to open apa102: %v", err)
|
||||||
|
}
|
||||||
|
img := image.NewNRGBA(image.Rect(0, 0, dev.Bounds().Dy(), 1))
|
||||||
|
for x := 0; x < img.Rect.Max.X; x++ {
|
||||||
|
img.SetNRGBA(x, 0, color.NRGBA{uint8(x), uint8(255 - x), 0, 255})
|
||||||
|
}
|
||||||
|
dev.Draw(dev.Bounds(), img, image.Point{})
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright 2018 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package bmxx80_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"periph.io/x/periph/conn/i2c/i2creg"
|
||||||
|
"periph.io/x/periph/devices"
|
||||||
|
"periph.io/x/periph/devices/bmxx80"
|
||||||
|
"periph.io/x/periph/host"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example() {
|
||||||
|
// Make sure periph is initialized.
|
||||||
|
if _, err := host.Init(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use i2creg I²C bus registry to find the first available I²C bus.
|
||||||
|
b, err := i2creg.Open("")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to open I²C: %v", err)
|
||||||
|
}
|
||||||
|
defer b.Close()
|
||||||
|
d, err := bmxx80.NewI2C(b, 0x76, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to initialize bme280: %v", err)
|
||||||
|
}
|
||||||
|
e := devices.Environment{}
|
||||||
|
if err := d.Sense(&e); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%8s %10s %9s\n", e.Temperature, e.Pressure, e.Humidity)
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
// Copyright 2018 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package ds248x_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"periph.io/x/periph/conn/i2c/i2creg"
|
||||||
|
"periph.io/x/periph/devices/ds248x"
|
||||||
|
"periph.io/x/periph/host"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example() {
|
||||||
|
// Make sure periph is initialized.
|
||||||
|
if _, err := host.Init(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use i2creg I²C bus registry to find the first available I²C bus.
|
||||||
|
b, err := i2creg.Open("")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
// Open the DS248x to get a 1-wire bus.
|
||||||
|
ob, err := ds248x.New(b, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
// Search devices on the bus
|
||||||
|
devices, err := ob.Search(false)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Found %d 1-wire devices: ", len(devices))
|
||||||
|
for _, d := range devices {
|
||||||
|
fmt.Printf(" %#16x", uint64(d))
|
||||||
|
}
|
||||||
|
fmt.Print("\n")
|
||||||
|
}
|
||||||
@ -0,0 +1,54 @@
|
|||||||
|
// Copyright 2018 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package ssd1306_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"periph.io/x/periph/conn/i2c/i2creg"
|
||||||
|
"periph.io/x/periph/devices/ssd1306"
|
||||||
|
"periph.io/x/periph/devices/ssd1306/image1bit"
|
||||||
|
"periph.io/x/periph/host"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example() {
|
||||||
|
// Make sure periph is initialized.
|
||||||
|
if _, err := host.Init(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use i2creg I²C bus registry to find the first available I²C bus.
|
||||||
|
b, err := i2creg.Open("")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
dev, err := ssd1306.NewI2C(b, 128, 64, false)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to initialize ssd1306: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw on it.
|
||||||
|
img := image1bit.NewVerticalLSB(dev.Bounds())
|
||||||
|
// Note: this code is commented out so periph does not depend on:
|
||||||
|
// "golang.org/x/image/font"
|
||||||
|
// "golang.org/x/image/font/basicfont"
|
||||||
|
// "golang.org/x/image/math/fixed"
|
||||||
|
//
|
||||||
|
// f := basicfont.Face7x13
|
||||||
|
// drawer := font.Drawer{
|
||||||
|
// Dst: img,
|
||||||
|
// Src: &image.Uniform{image1bit.On},
|
||||||
|
// Face: f,
|
||||||
|
// Dot: fixed.P(0, img.Bounds().Dy()-1-f.Descent),
|
||||||
|
// }
|
||||||
|
// drawer.DrawString("Hello from periph!")
|
||||||
|
dev.Draw(dev.Bounds(), img, image.Point{})
|
||||||
|
if err := dev.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
// Copyright 2018 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tm1637_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"periph.io/x/periph/conn/gpio/gpioreg"
|
||||||
|
"periph.io/x/periph/devices/tm1637"
|
||||||
|
"periph.io/x/periph/host"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example() {
|
||||||
|
// Make sure periph is initialized.
|
||||||
|
if _, err := host.Init(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
clk := gpioreg.ByName("GPIO6")
|
||||||
|
data := gpioreg.ByName("GPIO12")
|
||||||
|
if clk == nil || data == nil {
|
||||||
|
log.Fatal("Failed to find pins")
|
||||||
|
}
|
||||||
|
dev, err := tm1637.New(clk, data)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to initialize tm1637: %v", err)
|
||||||
|
}
|
||||||
|
if err := dev.SetBrightness(tm1637.Brightness10); err != nil {
|
||||||
|
log.Fatalf("failed to set brightness on tm1637: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := dev.Write(tm1637.Clock(12, 00, true)); err != nil {
|
||||||
|
log.Fatalf("failed to write to tm1637: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue