「Raspberry Pi 3によるOLEDディスプレイ への表示」で、Python言語よりRaspberry Piに接続したOLED「SSD1306」に表示しました。今回はGo言語を使って、動画とテキストをOLED「SSD1306」に表示します。
使用するSSD1306ライブラリ
SSD1306ライブラリは、「package ssd1306」を使用します。
animated GIFファイルの再生
「SSD1306」のExampleを使いましたが、そのままだとコンパイルできなかったので修正しています。
package main
import (
"image"
"image/draw"
"image/gif"
"log"
"os"
"time"
"github.com/nfnt/resize"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/devices/ssd1306"
"periph.io/x/periph/host"
)
// convertAndResizeAndCenter takes an image, resizes and centers it on a
// image.Gray of size w*h.
func convertAndResizeAndCenter(w, h int, src image.Image) *image.Gray {
src = resize.Thumbnail(uint(w), uint(h), src, resize.Bicubic)
img := image.NewGray(image.Rect(0, 0, w, h))
r := src.Bounds()
r = r.Add(image.Point{(w - r.Max.X) / 2, (h - r.Max.Y) / 2})
draw.Draw(img, r, src, image.Point{}, draw.Src)
return img
}
func main() {
// Load all the drivers:
if _, err := host.Init(); err != nil {
log.Fatal(err)
}
// Open a handle to the first available I?C bus:
bus, err := i2creg.Open("")
if err != nil {
log.Fatal(err)
}
var DefaultOpts = ssd1306.Opts{
W: 128,
H: 64,
Rotated: false,
Sequential: false,
SwapTopBottom: false,
}
dev, err := ssd1306.NewI2C(bus, &DefaultOpts)
if err != nil {
log.Fatal(err)
}
// Decodes an animated GIF as specified on the command line:
if len(os.Args) != 2 {
log.Fatal("please provide the path to an animated GIF")
}
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
g, err := gif.DecodeAll(f)
f.Close()
if err != nil {
log.Fatal(err)
}
// Converts every frame to image.Gray and resize them:
imgs := make([]*image.Gray, len(g.Image))
for i := range g.Image {
imgs[i] = convertAndResizeAndCenter(DefaultOpts.W, DefaultOpts.H, g.Image[i])
}
// Display the frames in a loop:
for i := 0; ; i++ {
index := i % len(imgs)
c := time.After(time.Duration(10*g.Delay[index]) * time.Millisecond)
img := imgs[index]
dev.Draw(img.Bounds(), img, image.Point{})
<-c
}
}
次のコマンドでコンパイルして実行します。実行時のパラメータとして与えている「DfQqM.gif」は、animated GIFファイルです。
$ go build $ ./goframe DfQqM.gif
SSD1306に表示された動画を次に示します。
テキスト表示
SSD1306にテキストを表示します。
- 使用するパッケージを次に示します。
- 35行目でI2 Cバスをパラメータとして、SSD1306の初期化を行っています。
- 40行目でグレースケールのイメージ領域を作成して、50行目で描画します。
- 49行目でfixed.Point26_6関数で描画するx,y位置を与えます。
「golang.org/x/image/font」
「golang.org/x/image/font/basicfont」
「golang.org/x/image/math/fixed」
「periph.io/x/periph/conn/i2c/i2creg」
「periph.io/x/periph/devices/ssd1306」
package main
import (
"image"
"image/color"
"log"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/devices/ssd1306"
"periph.io/x/periph/host"
)
func main() {
// Load all the drivers:
if _, err := host.Init(); err != nil {
log.Fatal(err)
}
// Open a handle to the first available I?C bus:
bus, err := i2creg.Open("")
if err != nil {
log.Fatal(err)
}
var DefaultOpts = ssd1306.Opts{
W: 128,
H: 64,
Rotated: false,
Sequential: false,
SwapTopBottom: false,
}
dev, err := ssd1306.NewI2C(bus, &DefaultOpts)
if err != nil {
log.Fatal(err)
}
img := image.NewGray(image.Rect(0, 0, 128, 64))
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(color.Gray{255}),
Face: basicfont.Face7x13,
}
for i := 0; i < 6; i++ {
d.Dot = fixed.Point26_6{fixed.Int26_6(2 * 64), fixed.Int26_6((9 + (i * 11)) * 64)}
d.DrawString("012345678901234567")
}
dev.Draw(img.Bounds(), img, image.Point{})
}
次のコマンドでコンパイルして実行します。
$ go build $ ./gotext
SSD1306に18文字6行のテキストが表示されます。
