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

@ -9,7 +9,12 @@ import (
)
type Display interface {
Flush(img *image.Gray) error
Flush(crystal, page byte)
// Image functions
GetImg() *image.Gray
LockImg()
UnlockImg()
GetBounds() image.Rectangle
Test(ctx context.Context) error

View File

@ -6,18 +6,35 @@ import (
"image"
"log"
"math/rand"
"sync"
"sync/atomic"
"time"
"gitea.unprism.ru/yotia/display-test/pkg/mt12232a"
)
var _ = rand.Int() // TMP for import to exist
var _ = rand.Int() // for import existance
const (
mt12232aW = 122
mt12232aH = 32
flushChanCap = 24 // Flush channel capacity
)
type displayMt12232a struct {
logger *log.Logger
// Image
img *image.Gray
imgMutex sync.Mutex
// GPIO pins
dev mt12232a.Device
// Flush goroutine
flushCancel context.CancelFunc
pagesFlushFlags atomic.Uint32 // (!use only 8 bits) Every bit corresponds to page/crystal with this number
flushDone chan struct{}
}
func newMt12232a(logger *log.Logger) (Display, error) {
@ -31,15 +48,16 @@ func newMt12232a(logger *log.Logger) (Display, error) {
bits[i] = make([]byte, 122)
}
// if err := dev.PowerOn(); err != nil {
// return nil, fmt.Errorf("power on: %w", err)
// }
ctx, cancel := context.WithCancel(context.Background())
// Setup submit goroutine
d := displayMt12232a{
logger: logger,
dev: dev,
logger: logger,
dev: dev,
flushCancel: cancel,
img: image.NewGray(image.Rect(0, 0, mt12232aW, mt12232aH)),
flushDone: make(chan struct{}), // For waiting flush to finish before exiting
}
go d.flushLoop(ctx)
// Temp debug draw
//if st0, st1 := d.dev.ReadStatus(0), d.dev.ReadStatus(1); false { //st0&0x20 == 0 && st1&0x20 == 0 {
@ -131,66 +149,23 @@ func (d *displayMt12232a) powerOn() error {
}
func (d *displayMt12232a) Test(ctx context.Context) error {
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x14)
d.dev.WriteData(0, 0xAE)
start := time.Now()
d.status("4")
for p := 0; p < 4; p++ {
// First
// d.dev.WriteCode(0, byte(p)|0xB8)
// d.dev.WriteCode(0, 0x13)
// for c := 0; c < 61; c++ {
// d.dev.WriteData(0, 0)
// }
// d.dev.WriteCode(1, byte(p)|0xB8)
// d.dev.WriteCode(1, 0x00)
// for c := 0; c < 61; c++ {
// d.dev.WriteData(1, 0)
// }
//
// Second
d.dev.WriteCode(0, byte(p)|0xB8)
d.dev.WriteCode(0, 0x13)
for c := 0; c < 61; c++ {
//d.dev.WriteDatas(0, []byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)})
//d.dev.WriteData(0, byte(0xF0^(0xFF*(c%2))))
d.dev.WriteData(0, byte(rand.Int()))
// d.dev.WriteData(0, 0xFF)
//d.dev.WriteData(0, byte(p*122+c))
//time.Sleep(2 * time.Millisecond)
}
d.dev.WriteCode(1, byte(p)|0xB8)
d.dev.WriteCode(1, 0x00)
for c := 0; c < 61; c++ {
//d.dev.WriteDatas(1, []byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)})
d.dev.WriteData(1, byte(rand.Int()))
// d.dev.WriteData(1, 0xFF)
//d.dev.WriteData(1, byte(4*int(p)+c))
//d.dev.WriteData(1, byte(p*122+c+61))
//d.dev.WriteData(1, byte(rand.Int())>>1)
//time.Sleep(2 * time.Millisecond)
}
}
d.status("9")
end := time.Now()
d.logger.Println(end.Sub(start))
// for p := byte(0); p < 8; p++ {
// d.dev.WriteCodeL(p | 0xB8)
// d.dev.WriteCodeL(0x40)
// for c := 0; c < 64; c++ {
// d.dev.WriteDataL(Logo128[p][c])
// }
// d.dev.WriteCodeR(p | 0xB8)
// d.dev.WriteCodeR(0x40)
// for c := 64; c < 128; c++ {
// d.dev.WriteDataR(Logo128[p][c])
// }
// }
//d.dev.WriteCodeL(0xAF) // Display on
//d.dev.WriteCodeR(0xAF) // Display on
return nil
}
@ -199,7 +174,7 @@ func (d *displayMt12232a) GetBounds() image.Rectangle {
return image.Rect(0, 0, 122, 32)
}
func (d *displayMt12232a) Flush(img *image.Gray) error {
func (d *displayMt12232a) FlushFull(img *image.Gray) error {
st := time.Now()
for p := byte(0); p < 4; p++ {
@ -236,7 +211,83 @@ func (d *displayMt12232a) Flush(img *image.Gray) error {
return nil
}
func (d *displayMt12232a) Close() error {
func (d *displayMt12232a) Flush(crystal, page byte) {
// !!! TODO Need to update GO to 1.23 to use .Or !!!
bit := uint32(1 << (crystal*4 + page))
d.pagesFlushFlags.Store(d.pagesFlushFlags.Load() | bit)
}
func (d *displayMt12232a) Close() error {
if d.flushCancel != nil {
d.flushCancel()
d.flushCancel = nil
<-d.flushDone
}
return d.dev.Close()
}
func (d *displayMt12232a) flushLoop(ctx context.Context) {
for {
select {
case <-ctx.Done():
close(d.flushDone)
return
case <-time.After(time.Millisecond):
forUpdate := d.pagesFlushFlags.Swap(0)
checkBit := uint32(1)
st := time.Now()
d.LockImg()
for p := byte(0); p < 4; p++ {
if forUpdate&(checkBit) != 0 {
d.dev.WriteCode(0, (3-p)|0xB8)
d.dev.WriteCode(0, 0x13)
for c := 0; c < 61; c++ {
d.dev.WriteDatas(0, []byte{
d.img.Pix[int(p<<3+7)*122+c],
d.img.Pix[int(p<<3+6)*122+c],
d.img.Pix[int(p<<3+5)*122+c],
d.img.Pix[int(p<<3+4)*122+c],
d.img.Pix[int(p<<3+3)*122+c],
d.img.Pix[int(p<<3+2)*122+c],
d.img.Pix[int(p<<3+1)*122+c],
d.img.Pix[int(p<<3+0)*122+c],
})
}
}
checkBit = checkBit << 1
}
d.logger.Printf("%08b - %s\n", forUpdate, time.Since(st))
for p := byte(0); p < 4; p++ {
if forUpdate&(checkBit) != 0 {
d.dev.WriteCode(1, (3-p)|0xB8)
d.dev.WriteCode(1, 0x00)
for c := 61; c < 122; c++ {
d.dev.WriteDatas(1, []byte{
d.img.Pix[int(p<<3+7)*122+c],
d.img.Pix[int(p<<3+6)*122+c],
d.img.Pix[int(p<<3+5)*122+c],
d.img.Pix[int(p<<3+4)*122+c],
d.img.Pix[int(p<<3+3)*122+c],
d.img.Pix[int(p<<3+2)*122+c],
d.img.Pix[int(p<<3+1)*122+c],
d.img.Pix[int(p<<3+0)*122+c],
})
}
}
checkBit = checkBit << 1
}
d.UnlockImg()
}
}
}
func (d *displayMt12232a) GetImg() *image.Gray {
return d.img
}
func (d *displayMt12232a) LockImg() {
d.imgMutex.Lock()
}
func (d *displayMt12232a) UnlockImg() {
d.imgMutex.Unlock()
}

View File

@ -142,10 +142,19 @@ func (d *displayMt12864a) GetBounds() image.Rectangle {
return image.Rectangle{}
}
func (d *displayMt12864a) Flush(img *image.Gray) error {
return nil
func (d *displayMt12864a) Flush(crystal, page byte) {
}
func (d *displayMt12864a) Close() error {
return rpio.Close()
}
func (d *displayMt12864a) GetImg() *image.Gray {
return nil
}
func (d *displayMt12864a) LockImg() {
}
func (d *displayMt12864a) UnlockImg() {
}

View File

@ -57,8 +57,8 @@ func (d *displaySsd1306) Test(ctx context.Context) error {
return nil
}
func (d *displaySsd1306) Flush(img *image.Gray) error {
return d.dev.Draw(img.Bounds(), d.img, image.Point{})
func (d *displaySsd1306) Flush(crystal, page byte) {
//return d.dev.Draw(img.Bounds(), d.img, image.Point{})
}
func (d *displaySsd1306) Close() error {
@ -67,3 +67,13 @@ func (d *displaySsd1306) Close() error {
}
return nil
}
func (d *displaySsd1306) GetImg() *image.Gray {
return nil
}
func (d *displaySsd1306) LockImg() {
}
func (d *displaySsd1306) UnlockImg() {
}