openpdu/src/display.go

92 lines
1.7 KiB
Go

package main
import (
"image"
"image/color"
"log"
"net"
"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"
)
func addLabel(img *image.RGBA, x, y int, label string) {
col := color.RGBA{200, 100, 0, 255}
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: point,
}
d.DrawString(label)
}
func getIPs() map[string]string {
out := map[string]string{}
ifaces, _ := net.Interfaces()
// handle err
for _, i := range ifaces {
if i.Name == "lo" {
continue
}
addrs, _ := i.Addrs()
// handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
// process IP address
if ip.To4() == nil {
continue
}
if len(out) < 1 {
out[i.Name] = ip.String()
}
}
}
return out
}
func disp() {
opts := ssd1306.Opts{
W: 128,
H: 32,
Rotated: false,
Sequential: true,
SwapTopBottom: false,
}
// Open a handle to a ssd1306 connected on the I²C bus:
dev, err := ssd1306.NewI2C(i2cbus, &opts)
if err != nil {
log.Fatal(err)
}
img := image.NewRGBA(image.Rect(0, 0, 128, 32))
ips := getIPs()
lineHeight := 12
// posX := 10
posX := 12
posY := lineHeight
// for name, ip := range ips {
for _, ip := range ips {
// addLabel(img, posX, posY, name)
// posY += lineHeight
addLabel(img, posX, posY, ip)
posY += lineHeight
}
dev.Draw(img.Bounds(), img, image.Point{})
}