waveshare2in13v2: add example (#8)

pull/10/head
Dan Kortschak 5 years ago committed by GitHub
parent 7cccb95509
commit 960e8d343b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,9 @@ module periph.io/x/devices/v3
go 1.13
require (
github.com/fogleman/gg v1.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb
periph.io/x/conn/v3 v3.6.7
periph.io/x/host/v3 v3.6.7
)

@ -1,3 +1,10 @@
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
periph.io/x/conn/v3 v3.6.7 h1:hem/gzoUI0tnvdJOJAk+XLBhqBGX9sHkwShBXRGGy0k=
periph.io/x/conn/v3 v3.6.7/go.mod h1:3OD27w9YVa5DS97VsUxsPGzD9Qrm5Ny7cF5b6xMMIWg=
periph.io/x/host/v3 v3.6.7 h1:hUVkGKJ235XocQIRiITxSmP8TT8f27oiN7R2dJkomIE=

@ -0,0 +1,120 @@
// Copyright 2021 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 waveshare2in13v2_test
import (
"image"
"image/draw"
"log"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font/gofont/goregular"
"periph.io/x/conn/v3/spi/spireg"
"periph.io/x/devices/v3/ssd1306/image1bit"
"periph.io/x/devices/v3/waveshare2in13v2"
"periph.io/x/host/v3"
)
func Example() {
// Make sure periph is initialized.
if _, err := host.Init(); err != nil {
log.Fatal(err)
}
// Use spireg SPI bus registry to find the first available SPI bus.
b, err := spireg.Open("")
if err != nil {
log.Fatal(err)
}
defer b.Close()
dev, err := waveshare2in13v2.NewHat(b, &waveshare2in13v2.EPD2in13v2) // Display config and size
if err != nil {
log.Fatalf("failed to initialize epd: %v", err)
}
// Draw on it. Black text on a white background.
img := image1bit.NewVerticalLSB(dev.Bounds())
draw.Draw(img, img.Bounds(), &image.Uniform{image1bit.On}, image.Point{}, draw.Src)
f := basicfont.Face7x13
drawer := font.Drawer{
Dst: img,
Src: &image.Uniform{image1bit.Off},
Face: f,
Dot: fixed.P(0, img.Bounds().Dy()-1-f.Descent),
}
drawer.DrawString("Hello from periph!")
if err := dev.Draw(dev.Bounds(), img, image.Point{}); err != nil {
log.Fatal(err)
}
}
func Example_other() {
// Make sure periph is initialized.
if _, err := host.Init(); err != nil {
log.Fatal(err)
}
// Use spireg SPI bus registry to find the first available SPI bus.
b, err := spireg.Open("")
if err != nil {
log.Fatal(err)
}
defer b.Close()
dev, err := waveshare2in13v2.NewHat(b, &waveshare2in13v2.EPD2in13v2) // Display config and size
if err != nil {
log.Fatalf("failed to initialize epd: %v", err)
}
var img image.Image
bounds := dev.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dc := gg.NewContext(w, h)
im, err := gg.LoadPNG("gopher.png")
if err != nil {
panic(err)
}
dc.SetRGB(1, 1, 1)
dc.Clear()
dc.SetRGB(0, 0, 0)
dc.Rotate(gg.Radians(90))
dc.Translate(0.0, -float64(h/2))
font, err := truetype.Parse(goregular.TTF)
if err != nil {
panic(err)
}
face := truetype.NewFace(font, &truetype.Options{
Size: 16,
})
dc.SetFontFace(face)
text := "Hello from periph!"
tw, th := dc.MeasureString(text)
dc.DrawImage(im, 120, 30)
padding := 8.0
dc.DrawRoundedRectangle(padding*2, padding*2, tw+padding*2, th+padding, 10)
dc.Stroke()
dc.DrawString(text, padding*3, padding*2+th)
for i := 0; i < 10; i++ {
dc.DrawCircle(float64(30+(10*i)), 100, 5)
}
for i := 0; i < 10; i++ {
dc.DrawRectangle(float64(30+(10*i)), 80, 5, 5)
}
dc.Fill()
img = dc.Image()
if err := dev.Draw(dev.Bounds(), img, image.Point{}); err != nil {
log.Fatal(err)
}
}
Loading…
Cancel
Save