display-test/drawer/drawer.go
2024-08-21 19:07:29 +03:00

77 lines
1.0 KiB
Go

package drawer
import (
"display-test/display"
"image"
"sync"
"golang.org/x/image/font"
"golang.org/x/image/font/inconsolata"
)
type drawer struct {
font font.Face
dev display.Display
img *image.Gray
imgMutex sync.Mutex
}
type Drawer interface {
GetFont() font.Face
// Lowlevel image
GetImg() *image.Gray
Lock()
Unlock()
Clear()
Flush() error
PutText(x, y int, text string)
PutBar(x0, y0, x1, y1, color int)
FillBar(x0, y0, x1, y1, color int)
}
func New(dev display.Display) Drawer {
return &drawer{
font: inconsolata.Regular8x16,
dev: dev,
img: image.NewGray(dev.GetBounds()),
}
}
func (d *drawer) GetImg() *image.Gray {
return d.img
}
func (d *drawer) GetFont() font.Face {
return d.font
}
func (d *drawer) Lock() {
d.imgMutex.Lock()
}
func (d *drawer) Unlock() {
d.imgMutex.Unlock()
}
func (d *drawer) Clear() {
d.Lock()
defer d.Unlock()
for i := range d.img.Pix {
d.img.Pix[i] = 0
}
}
func (d *drawer) Flush() error {
d.Lock()
defer d.Unlock()
return d.dev.Flush(d.img)
}