Add: async partial flush

This commit is contained in:
Andrey Egorov
2024-09-18 22:32:47 +03:00
parent eff00d2351
commit f29bbf8345
8 changed files with 266 additions and 173 deletions

View File

@ -3,8 +3,8 @@ package drawer
import "math"
func (d *drawer) FillBar(sx, sy, ex, ey, color int) {
d.Lock()
defer d.Unlock()
d.dev.LockImg()
defer d.dev.UnlockImg()
bounds := d.img.Bounds()
// Crop
@ -24,8 +24,8 @@ func (d *drawer) FillBar(sx, sy, ex, ey, color int) {
}
func (d *drawer) PutBar(sx, sy, ex, ey, color int) {
d.Lock()
defer d.Unlock()
d.dev.LockImg()
defer d.dev.UnlockImg()
bounds := d.img.Bounds()
// Crop

View File

@ -2,26 +2,21 @@ package drawer
import (
"image"
"sync"
"gitea.unprism.ru/yotia/display-test/display"
)
type drawer struct {
dev display.Display
img *image.Gray
imgMutex sync.Mutex
dev display.Display
img *image.Gray
}
type Drawer interface {
// Lowlevel image
GetImg() *image.Gray
Lock()
Unlock()
GetDisplay() display.Display
Clear()
Flush() error
Flush()
PutText(x, y int, text string)
@ -30,36 +25,35 @@ type Drawer interface {
}
func New(dev display.Display) Drawer {
return &drawer{
d := &drawer{
dev: dev,
img: image.NewGray(dev.GetBounds()),
}
d.img = d.dev.GetImg()
return d
}
func (d *drawer) GetImg() *image.Gray {
return d.img
}
func (d *drawer) Lock() {
d.imgMutex.Lock()
}
func (d *drawer) Unlock() {
d.imgMutex.Unlock()
func (d *drawer) GetDisplay() display.Display {
return d.dev
}
func (d *drawer) Clear() {
d.Lock()
defer d.Unlock()
d.dev.LockImg()
defer d.dev.UnlockImg()
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)
func (d *drawer) Flush() {
// Flush all pages
d.dev.Flush(0, 0)
d.dev.Flush(0, 1)
d.dev.Flush(0, 2)
d.dev.Flush(0, 3)
d.dev.Flush(1, 0)
d.dev.Flush(1, 1)
d.dev.Flush(1, 2)
d.dev.Flush(1, 3)
}

View File

@ -143,70 +143,70 @@ var stdFont = []byte{
0x41, 0x36, 0x8, 0x0, 0x0, // }
0x8, 0x4, 0x8, 0x4, 0x0, // ~
0x0, 0x0, 0x0, 0x0, 0x0, // Hollow Up Arrow
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x0, 0x0, 0x0, 0x0, 0x0, // Empty
0x7f, 0x55, 0x49, 0x55, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7e, 0x11, 0x11, 0x7e, 0x0, // А
0x7f, 0x49, 0x49, 0x31, 0x0, // Б
0x7f, 0x49, 0x49, 0x36, 0x0, // В
@ -280,21 +280,31 @@ func IntMin(a, b int) int {
return b
}
func IntMax(a, b int) int {
if a > b {
return a
}
return b
}
func (d *drawer) putChar(x0, y0 int, c byte) {
if int(c) > len(stdFont)/5 {
return
}
if y0 >= d.img.Rect.Dy() || x0 >= d.img.Rect.Dx() {
return
}
//if x0+7 < 0 || y0+5 < 0 || y0 >= d.img.Rect.Dy() || x0 >= d.img.Rect.Dx() {
// return
//}
maxYi := IntMin(y0+7, d.img.Rect.Dy()) - y0
maxXi := IntMin(x0+5, d.img.Rect.Dx()) - x0
minX := IntMax(x0, 0) - x0
minY := IntMax(y0, 0) - y0
for yi := range maxYi {
maxY := IntMin(y0+7, d.img.Rect.Dy()) - y0
maxX := IntMin(x0+5, d.img.Rect.Dx()) - x0
for yi := minY; yi < maxY; yi++ {
lineaddr := (y0+yi)*d.img.Stride + x0
for xi := range maxXi {
for xi := minX; xi < maxX; xi++ {
d.img.Pix[lineaddr+xi] = ((stdFont[int(c)*5+xi] >> yi) & 1) * 255
}
}
@ -309,8 +319,8 @@ func convertStr(str string) []byte {
}
func (d *drawer) PutText(x0, y0 int, text string) {
d.Lock()
defer d.Unlock()
d.dev.LockImg()
defer d.dev.UnlockImg()
asciiStr := convertStr(text)
//asciiStr := []byte(text)