dev/fb/main.go

61 lines
952 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"
)
var (
2025-01-26 07:07:51 +04:00
devfb *os.File
2025-01-26 06:33:51 +04:00
Width, Height int
)
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
}
func init() {
Width, Height = resolution()
2025-01-26 07:07:51 +04:00
var err error
devfb, err = os.OpenFile("/dev/fb", 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)
for h := 0; h < Height; h++ {
for w := 0; w < Width; w++ {
_, 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 06:38:11 +04:00
return nil
2025-01-26 06:33:51 +04:00
}