package display import ( "context" "fmt" "image" "log" "math/rand" "sync" "sync/atomic" "time" "gitea.unprism.ru/yotia/display-test/pkg/mt12232a" ) var _ = rand.Int() // for import existance const ( mt12232aW = 122 mt12232aH = 32 flushChanCap = 24 // Flush channel capacity flushUpdateTimeout = 5 * time.Millisecond ) type displayMt12232a struct { logger *log.Logger // Some state flags isTurnedOn bool // 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) { dev, err := mt12232a.New(log.New(logger.Writer(), "display-mt12864 : ", log.LstdFlags)) if err != nil { return nil, fmt.Errorf("mt12864 create: %w", err) } // Allocate bits bits := make([][]byte, 32) for i := 0; i < 32; i++ { bits[i] = make([]byte, 122) } ctx, cancel := context.WithCancel(context.Background()) // Setup submit goroutine d := displayMt12232a{ 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 { // d.logger.Println("Display is already on") //} else { d.status("Setup start") if err := d.dev.Reset(); err != nil { return nil, fmt.Errorf("reset: %w", err) } d.status("After reset") time.Sleep(200 * time.Millisecond) if st0, st1 := d.dev.ReadStatus(0), d.dev.ReadStatus(1); st0 == 0 && st1 == 0 { return nil, fmt.Errorf("No status response from dysplay") } if err := d.powerOn(); err != nil { return nil, fmt.Errorf("power on: %w", err) } d.status("Setup end") return &d, nil } func (d *displayMt12232a) status(label string) { //d.logger.Printf("STATUS %s -- L: %08b R: %08b\n", label, d.dev.ReadStatus(0)&0xFF, d.dev.ReadStatus(1)&0xFF) } func (d *displayMt12232a) powerOn() error { // Reset if err := d.dev.WriteCode(0, 0xE2); err != nil { return fmt.Errorf("reset: %w", err) } if err := d.dev.WriteCode(1, 0xE2); err != nil { return fmt.Errorf("reset: %w", err) } // ReadModifyWrite off if err := d.dev.WriteCode(0, 0xEE); err != nil { return fmt.Errorf("RMW off: %w", err) } if err := d.dev.WriteCode(1, 0xEE); err != nil { return fmt.Errorf("RMW off: %w", err) } // Turn on common mode if err := d.dev.WriteCode(0, 0xA4); err != nil { return fmt.Errorf("turn on common mode: %w", err) } if err := d.dev.WriteCode(1, 0xA4); err != nil { return fmt.Errorf("turn on common mode: %w", err) } // Multiplex 1/32 if err := d.dev.WriteCode(0, 0xA9); err != nil { return fmt.Errorf("multiplex 1/32: %w", err) } if err := d.dev.WriteCode(1, 0xA9); err != nil { return fmt.Errorf("multiplex 1/32: %w", err) } // Top line to 0 if err := d.dev.WriteCode(0, 0xC0); err != nil { return fmt.Errorf("top line to 0: %w", err) } if err := d.dev.WriteCode(1, 0xC0); err != nil { return fmt.Errorf("top line to 0: %w", err) } // Invert scan RAM if err := d.dev.WriteCode(0, 0xA1); err != nil { return fmt.Errorf("inver scan RAM: %w", err) } if err := d.dev.WriteCode(1, 0xA0); err != nil { return fmt.Errorf("inver scan RAM: %w", err) } // Display on if err := d.dev.WriteCode(0, 0xAF); err != nil { return fmt.Errorf("display on: %w", err) } if err := d.dev.WriteCode(1, 0xAF); err != nil { return fmt.Errorf("display on: %w", err) } //time.Sleep(time.Second) // Check that crystals are turned on // The same but with error if d.dev.ReadStatus(0)&0x20 != 0 { return fmt.Errorf("Left cristal is off") } if d.dev.ReadStatus(1)&0x20 != 0 { return fmt.Errorf("Right cristal is off") } d.isTurnedOn = true return nil } func (d *displayMt12232a) IsReady() error { if !d.isTurnedOn { return fmt.Errorf("display is turned off") } return nil } func (d *displayMt12232a) Test(ctx context.Context) error { start := time.Now() for p := 0; p < 4; p++ { d.dev.WriteCode(0, byte(p)|0xB8) d.dev.WriteCode(0, 0x13) for c := 0; c < 61; c++ { d.dev.WriteData(0, byte(rand.Int())) } d.dev.WriteCode(1, byte(p)|0xB8) d.dev.WriteCode(1, 0x00) for c := 0; c < 61; c++ { d.dev.WriteData(1, byte(rand.Int())) } } end := time.Now() d.logger.Println(end.Sub(start)) return nil } func (d *displayMt12232a) GetBounds() image.Rectangle { return image.Rect(0, 0, 122, 32) } func (d *displayMt12232a) FlushFull(img *image.Gray) error { st := time.Now() for p := byte(0); p < 4; p++ { d.dev.WriteCode(0, (3-p)|0xB8) d.dev.WriteCode(0, 0x13) for c := 0; c < 61; c++ { d.dev.WriteDatas(0, []byte{ img.Pix[int(p<<3+7)*122+c], img.Pix[int(p<<3+6)*122+c], img.Pix[int(p<<3+5)*122+c], img.Pix[int(p<<3+4)*122+c], img.Pix[int(p<<3+3)*122+c], img.Pix[int(p<<3+2)*122+c], img.Pix[int(p<<3+1)*122+c], img.Pix[int(p<<3+0)*122+c], }) } d.dev.WriteCode(1, (3-p)|0xB8) d.dev.WriteCode(1, 0x00) for c := 61; c < 122; c++ { d.dev.WriteDatas(1, []byte{ img.Pix[int(p<<3+7)*122+c], img.Pix[int(p<<3+6)*122+c], img.Pix[int(p<<3+5)*122+c], img.Pix[int(p<<3+4)*122+c], img.Pix[int(p<<3+3)*122+c], img.Pix[int(p<<3+2)*122+c], img.Pix[int(p<<3+1)*122+c], img.Pix[int(p<<3+0)*122+c], }) } } d.logger.Println("Flush time:", time.Since(st)) return nil } 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) FlushByMask(mask uint32) { // !!! TODO Need to update GO to 1.23 to use .Or !!! d.pagesFlushFlags.Store(d.pagesFlushFlags.Load() | mask) } func (d *displayMt12232a) GetFlushMaskBit(crystal, page byte) uint32 { return uint32(1 << (crystal*4 + page)) } func (d *displayMt12232a) Close() error { if d.flushCancel != nil { d.flushCancel() d.flushCancel = nil <-d.flushDone } d.isTurnedOn = false return d.dev.Close() } func (d *displayMt12232a) flushLoop(ctx context.Context) { for { select { case <-ctx.Done(): close(d.flushDone) return case <-time.After(flushUpdateTimeout): 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() }