62 lines
991 B
Go
62 lines
991 B
Go
package fb
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"os"
|
|
)
|
|
|
|
func Resolution() (w int, h int) {
|
|
resFile, err := os.Open("/sys/class/graphics/fb0/virtual_size")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
r := bufio.NewReader(resFile)
|
|
s, err := r.ReadString('\n')
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Sscanf(s, "%d,%d", &w, &h)
|
|
return
|
|
}
|
|
|
|
var devfb *os.File
|
|
var width, height = Resolution()
|
|
|
|
func init() {
|
|
var err error
|
|
devfb, err = os.OpenFile("/dev/fb0", os.O_WRONLY, os.ModePerm)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func Bytes(c color.Color) []byte {
|
|
r, g, b, a := c.RGBA()
|
|
return []byte{byte(b), byte(g), byte(r), byte(a)}
|
|
}
|
|
|
|
func Draw(img image.Image) error {
|
|
buf := bufio.NewWriterSize(devfb, 1<<16)
|
|
for h := 0; h < height; h++ {
|
|
for w := 0; w < width; w++ {
|
|
_, err := buf.Write(Bytes(img.At(w, h)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
err := buf.Flush()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = devfb.Seek(0,0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|