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
Marc-Antoine Ruel 8 years ago
parent 46011cfcbf
commit 124fa6f98b

@ -11,12 +11,10 @@ import (
"image"
"image/color"
"io/ioutil"
"log"
"testing"
"periph.io/x/periph/conn/conntest"
"periph.io/x/periph/conn/spi"
"periph.io/x/periph/conn/spi/spireg"
"periph.io/x/periph/conn/spi/spitest"
)
@ -579,27 +577,6 @@ func TestInit(t *testing.T) {
//
func Example() {
s, err := spireg.Open("")
if err != nil {
log.Fatalf("failed to open SPI: %v", err)
}
defer s.Close()
// Opens a strip of 150 lights are 50% intensity with color temperature at
// 5000 Kelvin.
dev, err := New(s, 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{})
}
//
func BenchmarkWriteWhite(b *testing.B) {
pixels := make([]byte, 150*3)
for i := range pixels {

@ -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{})
}

@ -6,7 +6,6 @@ package bmxx80
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
@ -14,7 +13,6 @@ import (
"time"
"periph.io/x/periph/conn/conntest"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/conn/i2c/i2ctest"
"periph.io/x/periph/conn/spi"
"periph.io/x/periph/conn/spi/spitest"
@ -729,23 +727,6 @@ func TestCalibration280_limits_419430400(t *testing.T) {
//
func Example() {
bus, err := i2creg.Open("")
if err != nil {
log.Fatalf("failed to open I²C: %v", err)
}
defer bus.Close()
dev, err := NewI2C(bus, 0x76, nil)
if err != nil {
log.Fatalf("failed to initialize bme280: %v", err)
}
env := devices.Environment{}
if err := dev.Sense(&env); err != nil {
log.Fatal(err)
}
fmt.Printf("%8s %10s %9s\n", env.Temperature, env.Pressure, env.Humidity)
}
func TestOversampling(t *testing.T) {
data := []struct {
o Oversampling

@ -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)
}

@ -5,42 +5,12 @@
package ds248x
import (
"fmt"
"log"
"testing"
"time"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/conn/i2c/i2ctest"
)
func Example() {
// Open the I²C bus to which the DS248x is connected.
i2cBus, err := i2creg.Open("")
if err != nil {
log.Fatal(err)
}
defer i2cBus.Close()
// Open the DS248x to get a 1-wire bus.
owBus, err := New(i2cBus, nil)
if err != nil {
log.Fatal(err)
}
// Search devices on the bus
devices, err := owBus.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")
}
//
func TestNew(t *testing.T) {
bus := i2ctest.Playback{
Ops: []i2ctest.IO{

@ -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)
}
}

@ -8,53 +8,17 @@ import (
"errors"
"image"
"image/color"
"log"
"testing"
"periph.io/x/periph/conn/conntest"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpiotest"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/conn/i2c/i2ctest"
"periph.io/x/periph/conn/spi"
"periph.io/x/periph/conn/spi/spitest"
"periph.io/x/periph/devices/ssd1306/image1bit"
)
func Example() {
bus, err := i2creg.Open("")
if err != nil {
log.Fatalf("failed to open I²C: %v", err)
}
defer bus.Close()
dev, err := NewI2C(bus, 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)
}
}
//
func TestNewI2C_fail(t *testing.T) {
bus := i2ctest.Playback{DontPanic: true}
if d, err := NewI2C(&bus, 0, 64, false); d != nil || err == nil {

@ -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)
}
}

@ -12,29 +12,9 @@ import (
"time"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/conn/gpio/gpiotest"
"periph.io/x/periph/host"
)
func Example() {
if _, err := host.Init(); err != nil {
log.Fatalf("failed to initialize periph: %v", err)
}
dev, err := New(gpioreg.ByName("GPIO6"), gpioreg.ByName("GPIO12"))
if err != nil {
log.Fatalf("failed to initialize tm1637: %v", err)
}
if err := dev.SetBrightness(Brightness10); err != nil {
log.Fatalf("failed to set brightness on tm1637: %v", err)
}
if _, err := dev.Write(Clock(12, 00, true)); err != nil {
log.Fatalf("failed to write to tm1637: %v", err)
}
}
//
func TestNew(t *testing.T) {
var clk, data gpiotest.Pin
dev, err := New(&clk, &data)

Loading…
Cancel
Save