From 124fa6f98b2ce476d676d0b2121240200bc7765f Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Sun, 18 Mar 2018 19:43:03 -0400 Subject: [PATCH] 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. --- devices/apa102/apa102_test.go | 23 -------------- devices/apa102/example_test.go | 41 +++++++++++++++++++++++++ devices/bmxx80/bmx280_test.go | 19 ------------ devices/bmxx80/example_test.go | 38 +++++++++++++++++++++++ devices/ds248x/ds248x_test.go | 30 ------------------ devices/ds248x/example_test.go | 44 +++++++++++++++++++++++++++ devices/ssd1306/example_test.go | 54 +++++++++++++++++++++++++++++++++ devices/ssd1306/ssd1306_test.go | 36 ---------------------- devices/tm1637/example_test.go | 36 ++++++++++++++++++++++ devices/tm1637/tm1637_test.go | 20 ------------ 10 files changed, 213 insertions(+), 128 deletions(-) create mode 100644 devices/apa102/example_test.go create mode 100644 devices/bmxx80/example_test.go create mode 100644 devices/ds248x/example_test.go create mode 100644 devices/ssd1306/example_test.go create mode 100644 devices/tm1637/example_test.go diff --git a/devices/apa102/apa102_test.go b/devices/apa102/apa102_test.go index 6ffc009..0f08bb7 100644 --- a/devices/apa102/apa102_test.go +++ b/devices/apa102/apa102_test.go @@ -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 { diff --git a/devices/apa102/example_test.go b/devices/apa102/example_test.go new file mode 100644 index 0000000..02240f6 --- /dev/null +++ b/devices/apa102/example_test.go @@ -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{}) +} diff --git a/devices/bmxx80/bmx280_test.go b/devices/bmxx80/bmx280_test.go index 2301180..0c3d388 100644 --- a/devices/bmxx80/bmx280_test.go +++ b/devices/bmxx80/bmx280_test.go @@ -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 diff --git a/devices/bmxx80/example_test.go b/devices/bmxx80/example_test.go new file mode 100644 index 0000000..ced88b3 --- /dev/null +++ b/devices/bmxx80/example_test.go @@ -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) +} diff --git a/devices/ds248x/ds248x_test.go b/devices/ds248x/ds248x_test.go index d0b1c27..126e8b4 100644 --- a/devices/ds248x/ds248x_test.go +++ b/devices/ds248x/ds248x_test.go @@ -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{ diff --git a/devices/ds248x/example_test.go b/devices/ds248x/example_test.go new file mode 100644 index 0000000..afd207c --- /dev/null +++ b/devices/ds248x/example_test.go @@ -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") +} diff --git a/devices/ssd1306/example_test.go b/devices/ssd1306/example_test.go new file mode 100644 index 0000000..427fab7 --- /dev/null +++ b/devices/ssd1306/example_test.go @@ -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) + } +} diff --git a/devices/ssd1306/ssd1306_test.go b/devices/ssd1306/ssd1306_test.go index 9f444ca..aa772f7 100644 --- a/devices/ssd1306/ssd1306_test.go +++ b/devices/ssd1306/ssd1306_test.go @@ -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 { diff --git a/devices/tm1637/example_test.go b/devices/tm1637/example_test.go new file mode 100644 index 0000000..28dec1c --- /dev/null +++ b/devices/tm1637/example_test.go @@ -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) + } +} diff --git a/devices/tm1637/tm1637_test.go b/devices/tm1637/tm1637_test.go index 8856211..29a9b9e 100644 --- a/devices/tm1637/tm1637_test.go +++ b/devices/tm1637/tm1637_test.go @@ -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)