284 lines
7.8 KiB
Go
284 lines
7.8 KiB
Go
package display
|
|
|
|
import (
|
|
"context"
|
|
"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
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
// }
|
|
|
|
// Setup submit goroutine
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
d := displayMt12232a{
|
|
logger: logger,
|
|
dev: dev,
|
|
submitCtx: ctx,
|
|
submitCtxCancel: cancel,
|
|
}
|
|
defer d.submitLoop()
|
|
|
|
// Temp debug draw
|
|
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")
|
|
|
|
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 {
|
|
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
|
|
}
|
|
|
|
func (d *displayMt12232a) test() {
|
|
d.logger.Println("Write")
|
|
d.dev.WriteCode(0, 0xB8)
|
|
d.dev.WriteCode(0, 0x00)
|
|
d.dev.WriteData(0, 47)
|
|
d.logger.Println("Read")
|
|
d.dev.WriteCode(0, 0xB8)
|
|
d.dev.WriteCode(0, 0x00)
|
|
d.logger.Println(d.dev.ReadData(0))
|
|
d.logger.Println(d.dev.ReadData(0))
|
|
|
|
//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})
|
|
|
|
//d.status("4")
|
|
for p := byte(0); p < 4; p++ {
|
|
d.dev.WriteCode(0, p|0xB8)
|
|
// d.status("5")
|
|
d.dev.WriteCode(0, 0x13)
|
|
//d.status("6")
|
|
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.WriteDatas(0, []byte{0, 0, 0, 0, 0, 0, 0, 0})
|
|
}
|
|
////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})
|
|
//}
|
|
}
|
|
//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
|
|
|
|
}
|
|
|
|
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++ {
|
|
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],
|
|
})
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *displayMt12232a) Close() error {
|
|
if d.submitCtxCancel != nil {
|
|
d.submitCtxCancel()
|
|
}
|
|
return rpio.Close()
|
|
}
|
|
|
|
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
|
|
//
|
|
// }
|
|
// }
|
|
}
|