Last active
June 9, 2020 10:39
-
-
Save suapapa/abac0730cf876411deb421946373dfdb to your computer and use it in GitHub Desktop.
example for github.com/sztanpet/sh1106
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"image" | |
"image/draw" | |
"image/gif" | |
"log" | |
"os" | |
"time" | |
"github.com/nfnt/resize" | |
"github.com/sztanpet/sh1106" | |
"periph.io/x/periph/conn/gpio" | |
"periph.io/x/periph/conn/gpio/gpioreg" | |
"periph.io/x/periph/conn/i2c/i2creg" | |
"periph.io/x/periph/host" | |
) | |
func main() { | |
_, err := host.Init() | |
if err != nil { | |
panic(err) | |
} | |
// chk(err) | |
resetPin := gpioreg.ByName("4") | |
if resetPin == nil { | |
panic("can't get reset pin") | |
} | |
resetPin.Out(gpio.High) | |
time.Sleep(1 * time.Millisecond) | |
resetPin.Out(gpio.Low) | |
time.Sleep(10 * time.Millisecond) | |
resetPin.Out(gpio.High) | |
time.Sleep(10 * time.Millisecond) | |
bus, err := i2creg.Open("") | |
chk(err) | |
dev, err := sh1106.NewI2C(bus, &sh1106.DefaultOpts) | |
chk(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]) | |
chk(err) | |
g, err := gif.DecodeAll(f) | |
f.Close() | |
chk(err) | |
// Converts every frame to image.Gray and resize them: | |
imgs := make([]*image.Gray, len(g.Image)) | |
w, h := dev.Bounds().Dx(), dev.Bounds().Dy() | |
for i := range g.Image { | |
imgs[i] = convertAndResizeAndCenter(w, 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 | |
} | |
} | |
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 chk(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment