display-test/display/mt-12232a.go

284 lines
7.8 KiB
Go
Raw Normal View History

2024-09-04 18:05:02 +00:00
package display
import (
2024-09-09 07:31:36 +00:00
"context"
2024-09-04 18:05:02 +00:00
"fmt"
"image"
"log"
"time"
"gitea.unprism.ru/yotia/display-test/pkg/mt12232a"
"github.com/stianeikeland/go-rpio/v4"
)
type displayMt12232a struct {
logger *log.Logger
// GPIO pins
dev mt12232a.Device
2024-09-09 07:31:36 +00:00
submitCtx context.Context // Context for submit goroutine
submitCtxCancel context.CancelFunc
submitQueue chan uint16 // Rectangle for submit
// From highter to lower:
// 1 bit - cristal
// 2 pit - page
// 6 bit - x in coords of crystal
// 6 bit - len
// Sum - 15 bit
2024-09-04 18:05:02 +00:00
}
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)
}
// if err := dev.PowerOn(); err != nil {
// return nil, fmt.Errorf("power on: %w", err)
// }
2024-09-09 07:31:36 +00:00
// Setup submit goroutine
ctx, cancel := context.WithCancel(context.Background())
2024-09-04 18:05:02 +00:00
d := displayMt12232a{
2024-09-09 07:31:36 +00:00
logger: logger,
dev: dev,
submitCtx: ctx,
submitCtxCancel: cancel,
2024-09-04 18:05:02 +00:00
}
2024-09-09 07:31:36 +00:00
defer d.submitLoop()
2024-09-04 18:05:02 +00:00
// Temp debug draw
2024-09-09 07:31:36 +00:00
d.status("start")
if err := d.dev.Reset(); err != nil {
return nil, fmt.Errorf("reset: %w", err)
}
if err := d.powerOn(); err != nil {
return nil, fmt.Errorf("power on: %w", err)
}
d.status("end")
d.status("test start")
d.test()
d.status("test end")
2024-09-04 18:05:02 +00:00
return &d, nil
}
2024-09-09 07:31:36 +00:00
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 {
d.logger.Println("Power on")
if err := d.dev.WriteCode(0, 0xE2); err != nil { // Reset
return fmt.Errorf("reset: %w", err)
}
d.status("1L")
if err := d.dev.WriteCode(1, 0xE2); err != nil { // Reset
return fmt.Errorf("reset: %w", err)
}
d.status("1R")
if err := d.dev.WriteCode(0, 0xEE); err != nil { // ReadModifyWrite off
return fmt.Errorf("RMW off: %w", err)
}
d.status("2L")
if err := d.dev.WriteCode(1, 0xEE); err != nil { // ReadModifyWrite off
return fmt.Errorf("RMW off: %w", err)
}
d.status("2R")
if err := d.dev.WriteCode(0, 0xA4); err != nil { // Turn on common mode
return fmt.Errorf("turn on common mode: %w", err)
}
d.status("3L")
if err := d.dev.WriteCode(1, 0xA4); err != nil { // Turn on common mode
return fmt.Errorf("turn on common mode: %w", err)
}
d.status("3R")
if err := d.dev.WriteCode(0, 0xA9); err != nil { // Multiplex 1/32
return fmt.Errorf("multiplex 1/32: %w", err)
}
d.status("4L")
if err := d.dev.WriteCode(1, 0xA9); err != nil { // Multiplex 1/32
return fmt.Errorf("multiplex 1/32: %w", err)
}
d.status("4R")
if err := d.dev.WriteCode(0, 0xC0); err != nil { // Top line to 0
return fmt.Errorf("top line to 0: %w", err)
}
d.status("5L")
if err := d.dev.WriteCode(1, 0xC0); err != nil { // Top line to 0
return fmt.Errorf("top line to 0: %w", err)
}
d.status("5R")
if err := d.dev.WriteCode(0, 0xA0); err != nil { // Invert scan RAM
return fmt.Errorf("inver scan RAM: %w", err)
}
d.status("6L")
if err := d.dev.WriteCode(1, 0xA0); err != nil { // Invert scan RAM
return fmt.Errorf("inver scan RAM: %w", err)
}
d.status("6R")
if err := d.dev.WriteCode(0, 0xAF); err != nil { // Display on
return fmt.Errorf("display on: %w", err)
}
d.status("7L")
if err := d.dev.WriteCode(1, 0xAF); err != nil { // Display on
return fmt.Errorf("display on: %w", err)
}
d.status("7R")
//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")
}
return nil
}
2024-09-04 18:05:02 +00:00
func (d *displayMt12232a) test() {
d.logger.Println("Write")
2024-09-09 07:31:36 +00:00
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x00)
d.dev.WriteData(0, 47)
2024-09-04 18:05:02 +00:00
d.logger.Println("Read")
2024-09-09 07:31:36 +00:00
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x00)
d.logger.Println(d.dev.ReadData(0))
d.logger.Println(d.dev.ReadData(0))
2024-09-04 18:05:02 +00:00
//color := []byte{1, 0, 1, 0, 1, 0, 1, 0}
//color := []byte{1, 0, 1, 0, 1, 0, 0, 0}
//color := []byte{0, 0, 0, 0, 0, 0, 0, 0}
//color := byte(3)
start := time.Now()
//d.dev.WriteCodeL(0xB8)
//d.dev.WriteCodeL(0x13)
//d.dev.WriteDatasL([]byte{0, 0, 0, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 0, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 1, 0, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 1, 1, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 1, 1, 1, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 1, 0, 0})
//d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 1, 1, 0})
//d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 1, 1, 1})
//
//d.dev.WriteDatasL([]byte{1, 0, 0, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 1, 0})
//d.dev.WriteCodeL(0xB8 | 1)
//d.dev.WriteCodeL(0x13)
//d.dev.WriteDatasL([]byte{1, 0, 0, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 0, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 1, 0})
2024-09-09 07:31:36 +00:00
//d.status("4")
2024-09-04 18:05:02 +00:00
for p := byte(0); p < 4; p++ {
2024-09-09 07:31:36 +00:00
d.dev.WriteCode(0, p|0xB8)
// d.status("5")
d.dev.WriteCode(0, 0x13)
//d.status("6")
2024-09-04 18:05:02 +00:00
for c := 0; c < 61; c++ {
2024-09-09 07:31:36 +00:00
//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.WriteDatas(0, []byte{0, 0, 0, 0, 0, 0, 0, 0})
2024-09-04 18:05:02 +00:00
}
2024-09-09 07:31:36 +00:00
////d.status("7")
//d.dev.WriteCode(1, p|0xB8)
////d.status("8")
//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.WriteDatas(1, []byte{0, 0, 0, 1, 0, 0, 0, 0})
//}
2024-09-04 18:05:02 +00:00
}
2024-09-09 07:31:36 +00:00
//d.status("9")
2024-09-04 18:05:02 +00:00
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
}
func (d *displayMt12232a) GetBounds() image.Rectangle {
return image.Rect(0, 0, 122, 32)
}
func (d *displayMt12232a) Flush(img *image.Gray) error {
for p := byte(0); p < 4; p++ {
2024-09-09 07:31:36 +00:00
d.dev.WriteCode(0, (3-p)|0xB8)
d.dev.WriteCode(0, 0x13)
2024-09-04 18:05:02 +00:00
for c := 0; c < 61; c++ {
2024-09-09 07:31:36 +00:00
d.dev.WriteDatas(0, []byte{
2024-09-04 18:05:02 +00:00
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],
})
}
2024-09-09 07:31:36 +00:00
d.dev.WriteCode(1, (3-p)|0xB8)
d.dev.WriteCode(1, 0x00)
2024-09-04 18:05:02 +00:00
for c := 61; c < 122; c++ {
2024-09-09 07:31:36 +00:00
d.dev.WriteDatas(1, []byte{
2024-09-04 18:05:02 +00:00
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],
})
}
}
return nil
}
func (d *displayMt12232a) Close() error {
2024-09-09 07:31:36 +00:00
if d.submitCtxCancel != nil {
d.submitCtxCancel()
}
2024-09-04 18:05:02 +00:00
return rpio.Close()
}
2024-09-09 07:31:36 +00:00
func (d *displayMt12232a) submitLoop() {
// for {
// select {
// case <-d.submitCtx.Done():
// return
// case msg := <-d.submitQueue:
// crystal := (msg >> 14) & 0x01
// page := (msg >> 12) & 0x04
// x := (msg >> 6) & 0x4F
// len := (msg >> 0) & 0x4F
//
// }
// }
}