dev/fb/main.go

62 lines
992 B
Go
Raw Normal View History

2025-01-26 06:33:51 +04:00
package fb
import (
2025-01-26 06:51:04 +04:00
"bufio"
"fmt"
"image"
"image/color"
2025-01-26 06:33:51 +04:00
"os"
)
2025-01-26 07:20:50 +04:00
func Resolution() (w int, h int) {
2025-01-26 06:51:04 +04:00
resFile, err := os.Open("/sys/class/graphics/fb0/virtual_size")
2025-01-26 06:33:51 +04:00
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
}
2025-01-26 07:16:49 +04:00
var devfb *os.File
2025-01-26 07:24:52 +04:00
var width, height = Resolution()
2025-01-26 07:16:49 +04:00
2025-01-26 06:33:51 +04:00
func init() {
2025-01-26 07:07:51 +04:00
var err error
2025-01-26 07:30:58 +04:00
devfb, err = os.OpenFile("/dev/fb0", os.O_WRONLY, os.ModePerm)
2025-01-26 06:33:51 +04:00
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)
2025-01-26 07:24:52 +04:00
for h := 0; h < height; h++ {
for w := 0; w < width; w++ {
2025-01-26 06:33:51 +04:00
_, err := buf.Write(Bytes(img.At(w, h)))
if err != nil {
2025-01-26 06:38:11 +04:00
return err
2025-01-26 06:33:51 +04:00
}
}
}
err := buf.Flush()
if err != nil {
2025-01-26 06:38:11 +04:00
return err
2025-01-26 06:33:51 +04:00
}
2025-01-26 07:34:10 +04:00
_, err := devfb.Seek(0,0)
if err != nil {
return err
}
2025-01-26 06:38:11 +04:00
return nil
2025-01-26 06:33:51 +04:00
}