Add: drawer, status bar, beta mt12864a support

This commit is contained in:
Andrey Egorov
2024-08-21 19:07:29 +03:00
parent ae312a0457
commit 26edd24fef
9 changed files with 608 additions and 70 deletions

69
drawer/bar.go Normal file
View File

@ -0,0 +1,69 @@
package drawer
import "math"
func (d *drawer) FillBar(sx, sy, ex, ey, color int) {
d.Lock()
defer d.Unlock()
bounds := d.img.Bounds()
// Crop
sx = int(math.Max(float64(sx), 0))
sy = int(math.Max(float64(sy), 0))
ex = int(math.Min(float64(ex), float64(bounds.Dx())))
ey = int(math.Min(float64(ey), float64(bounds.Dy())))
w := ex - sx
// Fill
for lineaddr := sy*bounds.Dx() + sx; lineaddr < (ey-1)*bounds.Dx()+ex; lineaddr += bounds.Dx() {
addr := lineaddr
le := addr + w
for ; addr < le; addr++ {
d.img.Pix[addr] = 255
}
}
}
func (d *drawer) PutBar(sx, sy, ex, ey, color int) {
d.Lock()
defer d.Unlock()
bounds := d.img.Bounds()
// Crop
// Top
if sy >= 0 && sy < bounds.Dy() {
x0 := int(math.Max(float64(sx), 0))
x1 := int(math.Min(float64(ex), float64(bounds.Dx())))
for addr := sy*bounds.Dx() + x0; addr < sy*bounds.Dx()+x1; addr++ {
d.img.Pix[addr] = 255
}
}
// Bottom
if ey >= 0 && ey <= bounds.Dy() {
x0 := int(math.Max(float64(sx), 0))
x1 := int(math.Min(float64(ex), float64(bounds.Dx())))
for addr := (ey-1)*bounds.Dx() + x0; addr < (ey-1)*bounds.Dx()+x1; addr++ {
d.img.Pix[addr] = 255
}
}
// Left
if sx >= 0 && sx < bounds.Dx() {
y0 := int(math.Max(float64(sy), 0))
y1 := int(math.Min(float64(ey), float64(bounds.Dy())))
for addr := y0*bounds.Dx() + sx; addr < y1*bounds.Dx()+sx; addr += bounds.Dx() {
d.img.Pix[addr] = 255
}
}
// Right
if ex >= 0 && ex <= bounds.Dx() {
y0 := int(math.Max(float64(sy), 0))
y1 := int(math.Min(float64(ey), float64(bounds.Dy())))
for addr := y0*bounds.Dx() + (ex - 1); addr < y1*bounds.Dx()+(ex-1); addr += bounds.Dx() {
d.img.Pix[addr] = 255
}
}
}

76
drawer/drawer.go Normal file
View File

@ -0,0 +1,76 @@
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)
}

26
drawer/text.go Normal file
View File

@ -0,0 +1,26 @@
package drawer
import (
"image"
"image/color"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
func (d *drawer) PutText(x, y int, text string) {
d.Lock()
defer d.Unlock()
// Create font drawer
col := color.Gray{255}
point := fixed.Point26_6{X: fixed.I(x), Y: fixed.I(y)}
drawer := &font.Drawer{
Dst: d.img,
Src: image.NewUniform(col),
Face: d.font,
Dot: point,
}
drawer.DrawString(text)
}