display-test/pkg/mt12232a/mt12232a.go

169 lines
3.2 KiB
Go
Raw Normal View History

2024-09-02 10:39:06 +00:00
package mt12232a
import (
"fmt"
"io"
"log"
"time"
"gitea.unprism.ru/yotia/display-test/pkg/parallel8bit"
"github.com/stianeikeland/go-rpio/v4"
)
2024-09-15 17:21:57 +00:00
const (
waitReadyTimeout = time.Microsecond
)
2024-09-02 10:39:06 +00:00
type mt12232a struct {
logger *log.Logger
dev parallel8bit.Device
pinCS rpio.Pin
pinRES rpio.Pin
}
type Device interface {
2024-09-09 07:31:36 +00:00
Reset() error
2024-09-02 10:39:06 +00:00
2024-09-09 07:31:36 +00:00
WriteCode(cs rpio.State, c byte) error
WriteData(cs rpio.State, b byte) error
WriteDatas(cs rpio.State, b []byte) error
2024-09-02 10:39:06 +00:00
2024-09-09 07:31:36 +00:00
ReadData(cs rpio.State) (byte, error)
2024-09-02 10:39:06 +00:00
ReadStatus(cs rpio.State) byte
io.Closer
}
func New(logger *log.Logger) (Device, error) {
rpio.Open()
d := mt12232a{
logger: logger,
dev: parallel8bit.New(logger, parallel8bit.DevicePins{
2024-09-11 15:55:26 +00:00
PinA0: rpio.Pin(1), // 19
2024-09-04 18:05:02 +00:00
PinRW: rpio.Pin(13),
PinE: rpio.Pin(12),
2024-09-02 10:39:06 +00:00
PinDB0: rpio.Pin(22),
PinDB1: rpio.Pin(10),
2024-09-04 18:05:02 +00:00
PinDB2: rpio.Pin(9),
2024-09-02 10:39:06 +00:00
PinDB3: rpio.Pin(11),
2024-09-04 18:05:02 +00:00
PinDB4: rpio.Pin(21),
PinDB5: rpio.Pin(20),
PinDB6: rpio.Pin(26),
PinDB7: rpio.Pin(16),
2024-09-02 10:39:06 +00:00
}),
2024-09-04 18:05:02 +00:00
pinCS: rpio.Pin(17),
pinRES: rpio.Pin(27),
2024-09-02 10:39:06 +00:00
}
d.pinCS.Output()
d.pinRES.Output()
d.dev.Reset()
2024-09-09 07:31:36 +00:00
d.pinCS.Low()
d.pinRES.High()
2024-09-02 10:39:06 +00:00
return &d, nil
}
func (d *mt12232a) Close() error {
2024-09-11 15:55:26 +00:00
d.pinCS.Low()
//d.pinRES.Low() // TMP
d.dev.Pins().PinA0.Low()
d.dev.Pins().PinE.Low()
d.dev.Pins().PinRW.Low()
d.dev.Pins().PinDB0.Low()
d.dev.Pins().PinDB1.Low()
d.dev.Pins().PinDB2.Low()
d.dev.Pins().PinDB3.Low()
d.dev.Pins().PinDB4.Low()
d.dev.Pins().PinDB5.Low()
d.dev.Pins().PinDB6.Low()
d.dev.Pins().PinDB7.Low()
2024-09-02 10:39:06 +00:00
return rpio.Close()
}
2024-09-04 18:05:02 +00:00
func (d *mt12232a) status() {
2024-09-09 07:31:36 +00:00
d.logger.Printf("ST L: %08b R: %08b\n", d.ReadStatus(0)&0xFF, d.ReadStatus(1)&0xFF)
2024-09-02 10:39:06 +00:00
}
2024-09-09 07:31:36 +00:00
func (d *mt12232a) Reset() error {
2024-09-02 10:39:06 +00:00
d.pinRES.Low()
2024-09-09 07:31:36 +00:00
time.Sleep(10 * time.Microsecond)
2024-09-02 10:39:06 +00:00
d.pinRES.High()
2024-09-09 07:31:36 +00:00
time.Sleep(2 * time.Millisecond)
2024-09-02 10:39:06 +00:00
return nil
}
// Write codes
2024-09-09 07:31:36 +00:00
func (d *mt12232a) WriteCode(cs rpio.State, c byte) error {
return d.writeByte(c, 0, cs)
2024-09-02 10:39:06 +00:00
}
// Write data as byte
2024-09-09 07:31:36 +00:00
func (d *mt12232a) WriteData(cs rpio.State, b byte) error {
return d.writeByte(b, 1, cs)
2024-09-02 10:39:06 +00:00
}
2024-09-09 07:31:36 +00:00
func (d *mt12232a) WriteDatas(cs rpio.State, b []byte) error {
return d.writeBytes(b, 1, cs)
2024-09-04 18:05:02 +00:00
}
2024-09-02 10:39:06 +00:00
// Read data
2024-09-09 07:31:36 +00:00
func (d *mt12232a) ReadData(cs rpio.State) (byte, error) {
return d.readByte(1, cs)
2024-09-02 10:39:06 +00:00
}
// Low level functions
func (d *mt12232a) writeByte(b byte, cd, cs rpio.State) error {
if err := d.waitReady(cs); err != nil {
return fmt.Errorf("wait ready: %w", err)
}
d.pinCS.Write(cs) // Select cristals
d.dev.WriteByte(b, cd)
return nil
}
2024-09-04 18:05:02 +00:00
func (d *mt12232a) writeBytes(b []byte, cd, cs rpio.State) error {
if err := d.waitReady(cs); err != nil {
return fmt.Errorf("wait ready: %w", err)
}
d.pinCS.Write(cs) // Select cristals
d.dev.WriteBytes(b, cd)
return nil
}
2024-09-02 10:39:06 +00:00
func (d *mt12232a) readByte(cd, cs rpio.State) (byte, error) {
// Setup
if err := d.waitReady(cs); err != nil {
return 0, fmt.Errorf("wait ready: %w", err)
}
d.pinCS.Write(cs) // Select cristals
return d.dev.ReadByte(cd), nil
}
// Wait, checking status byte
func (d *mt12232a) waitReady(cs rpio.State) error {
2024-09-15 14:19:19 +00:00
// d.pinCS.Write(cs) // Select cristals
// return d.dev.WaitReady()
2024-09-15 17:21:57 +00:00
time.Sleep(waitReadyTimeout)
2024-09-15 14:19:19 +00:00
return nil
2024-09-02 10:39:06 +00:00
}
func (d *mt12232a) ReadStatus(cs rpio.State) byte {
d.pinCS.Write(cs) // Select cristals
2024-09-11 15:55:26 +00:00
return d.dev.ReadByte(0)
2024-09-02 10:39:06 +00:00
}