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"
|
2024-09-11 15:55:26 +00:00
|
|
|
"math/rand"
|
2024-09-04 18:05:02 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitea.unprism.ru/yotia/display-test/pkg/mt12232a"
|
|
|
|
)
|
|
|
|
|
2024-09-11 15:55:26 +00:00
|
|
|
var _ = rand.Int() // TMP for import to exist
|
|
|
|
|
2024-09-04 18:05:02 +00:00
|
|
|
type displayMt12232a struct {
|
|
|
|
logger *log.Logger
|
|
|
|
|
|
|
|
// GPIO pins
|
|
|
|
dev mt12232a.Device
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-09-04 18:05:02 +00:00
|
|
|
d := displayMt12232a{
|
2024-09-11 15:55:26 +00:00
|
|
|
logger: logger,
|
|
|
|
dev: dev,
|
2024-09-04 18:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Temp debug draw
|
2024-09-11 15:55:26 +00:00
|
|
|
//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")
|
2024-09-09 07:31:36 +00:00
|
|
|
if err := d.dev.Reset(); err != nil {
|
|
|
|
return nil, fmt.Errorf("reset: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
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")
|
|
|
|
}
|
2024-09-09 07:31:36 +00:00
|
|
|
if err := d.powerOn(); err != nil {
|
|
|
|
return nil, fmt.Errorf("power on: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
d.status("Setup 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) {
|
2024-09-15 14:19:19 +00:00
|
|
|
//d.logger.Printf("STATUS %s -- L: %08b R: %08b\n", label, d.dev.ReadStatus(0)&0xFF, d.dev.ReadStatus(1)&0xFF)
|
2024-09-09 07:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *displayMt12232a) powerOn() error {
|
2024-09-11 15:55:26 +00:00
|
|
|
// Reset
|
|
|
|
if err := d.dev.WriteCode(0, 0xE2); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("reset: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if err := d.dev.WriteCode(1, 0xE2); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("reset: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
// ReadModifyWrite off
|
|
|
|
if err := d.dev.WriteCode(0, 0xEE); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("RMW off: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if err := d.dev.WriteCode(1, 0xEE); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("RMW off: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
// Turn on common mode
|
|
|
|
if err := d.dev.WriteCode(0, 0xA4); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("turn on common mode: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if err := d.dev.WriteCode(1, 0xA4); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("turn on common mode: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
// Multiplex 1/32
|
|
|
|
if err := d.dev.WriteCode(0, 0xA9); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("multiplex 1/32: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if err := d.dev.WriteCode(1, 0xA9); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("multiplex 1/32: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
// Top line to 0
|
|
|
|
if err := d.dev.WriteCode(0, 0xC0); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("top line to 0: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if err := d.dev.WriteCode(1, 0xC0); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("top line to 0: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
// Invert scan RAM
|
|
|
|
if err := d.dev.WriteCode(0, 0xA1); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("inver scan RAM: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if err := d.dev.WriteCode(1, 0xA0); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("inver scan RAM: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
// Display on
|
|
|
|
if err := d.dev.WriteCode(0, 0xAF); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
return fmt.Errorf("display on: %w", err)
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if err := d.dev.WriteCode(1, 0xAF); err != nil {
|
2024-09-09 07:31:36 +00:00
|
|
|
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")
|
|
|
|
}
|
2024-09-11 15:55:26 +00:00
|
|
|
if d.dev.ReadStatus(1)&0x20 != 0 {
|
|
|
|
return fmt.Errorf("Right cristal is off")
|
|
|
|
}
|
2024-09-09 07:31:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-11 15:55:26 +00:00
|
|
|
func (d *displayMt12232a) Test(ctx context.Context) error {
|
2024-09-09 07:31:36 +00:00
|
|
|
d.dev.WriteCode(0, 0xB8)
|
2024-09-15 14:19:19 +00:00
|
|
|
d.dev.WriteCode(0, 0x14)
|
|
|
|
d.dev.WriteData(0, 0xAE)
|
2024-09-04 18:05:02 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2024-09-11 15:55:26 +00:00
|
|
|
d.status("4")
|
2024-09-15 14:19:19 +00:00
|
|
|
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)
|
2024-09-09 07:31:36 +00:00
|
|
|
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{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)})
|
2024-09-11 15:55:26 +00:00
|
|
|
//d.dev.WriteData(0, byte(0xF0^(0xFF*(c%2))))
|
2024-09-15 14:19:19 +00:00
|
|
|
d.dev.WriteData(0, byte(rand.Int()))
|
|
|
|
// d.dev.WriteData(0, 0xFF)
|
|
|
|
//d.dev.WriteData(0, byte(p*122+c))
|
2024-09-11 15:55:26 +00:00
|
|
|
//time.Sleep(2 * time.Millisecond)
|
|
|
|
}
|
2024-09-15 14:19:19 +00:00
|
|
|
d.dev.WriteCode(1, byte(p)|0xB8)
|
2024-09-11 15:55:26 +00:00
|
|
|
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)})
|
2024-09-15 14:19:19 +00:00
|
|
|
d.dev.WriteData(1, byte(rand.Int()))
|
|
|
|
// d.dev.WriteData(1, 0xFF)
|
2024-09-11 15:55:26 +00:00
|
|
|
//d.dev.WriteData(1, byte(4*int(p)+c))
|
2024-09-15 14:19:19 +00:00
|
|
|
//d.dev.WriteData(1, byte(p*122+c+61))
|
|
|
|
//d.dev.WriteData(1, byte(rand.Int())>>1)
|
2024-09-11 15:55:26 +00:00
|
|
|
//time.Sleep(2 * time.Millisecond)
|
2024-09-04 18:05:02 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-11 15:55:26 +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
|
2024-09-11 15:55:26 +00:00
|
|
|
return nil
|
2024-09-04 18:05:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-09-11 15:55:26 +00:00
|
|
|
return d.dev.Close()
|
2024-09-09 07:31:36 +00:00
|
|
|
}
|