display-test/pkg/parallel8bit/parallel8bit.go

219 lines
4.4 KiB
Go
Raw Normal View History

2024-09-02 10:11:36 +00:00
package parallel8bit
import (
"fmt"
"log"
"time"
"github.com/stianeikeland/go-rpio/v4"
)
const (
2024-09-15 14:19:19 +00:00
adressWriteTimeout = 50 * time.Nanosecond // 40
dataStrobeTimeout = 200 * time.Nanosecond // (Data transfer) 160
dataReadTimeout = 400 * time.Nanosecond // 300
gapTimeout = 2000 * time.Nanosecond // TIM // 2000
2024-09-02 10:11:36 +00:00
maxWaitCycles = 100
)
// Interface for MT displays (do not set cristals bits)
type Device interface {
2024-09-02 10:39:06 +00:00
Reset()
2024-09-02 10:11:36 +00:00
WriteByte(b byte, cd rpio.State)
2024-09-04 18:05:02 +00:00
WriteBytes(b []byte, cd rpio.State)
2024-09-02 10:11:36 +00:00
ReadByte(cd rpio.State) byte
2024-09-04 18:05:02 +00:00
Pins() DevicePins
2024-09-02 10:11:36 +00:00
WaitReady() error
}
type DevicePins struct {
// GPIO pins
PinA0 rpio.Pin
PinRW rpio.Pin
PinE rpio.Pin
PinDB0 rpio.Pin
PinDB1 rpio.Pin
PinDB2 rpio.Pin
PinDB3 rpio.Pin
PinDB4 rpio.Pin
PinDB5 rpio.Pin
PinDB6 rpio.Pin
PinDB7 rpio.Pin
}
type device struct {
logger *log.Logger
2024-09-04 18:05:02 +00:00
isBusOutput bool
2024-09-02 10:11:36 +00:00
DevicePins
}
func New(logger *log.Logger, pins DevicePins) Device {
return &device{
logger: logger,
DevicePins: pins,
}
}
2024-09-04 18:05:02 +00:00
func (d *device) Pins() DevicePins {
return d.DevicePins
}
2024-09-02 10:39:06 +00:00
func (d *device) Reset() {
d.PinA0.Output()
d.PinRW.Output()
d.PinE.Output()
d.busOutput()
2024-09-09 07:31:36 +00:00
d.PinA0.Low()
d.PinRW.Low()
2024-09-11 15:55:26 +00:00
d.PinE.High()
2024-09-09 07:31:36 +00:00
d.PinDB0.Low()
d.PinDB1.Low()
d.PinDB2.Low()
d.PinDB3.Low()
d.PinDB4.Low()
d.PinDB5.Low()
d.PinDB6.Low()
d.PinDB7.Low()
2024-09-02 10:39:06 +00:00
}
2024-09-02 10:11:36 +00:00
func (d *device) WriteByte(b byte, cd rpio.State) {
d.busOutput()
d.PinRW.Low() // We write
d.PinA0.Write(cd)
// Write bus
d.PinDB0.Write(rpio.State((b >> 0) & 1))
d.PinDB1.Write(rpio.State((b >> 1) & 1))
d.PinDB2.Write(rpio.State((b >> 2) & 1))
d.PinDB3.Write(rpio.State((b >> 3) & 1))
d.PinDB4.Write(rpio.State((b >> 4) & 1))
d.PinDB5.Write(rpio.State((b >> 5) & 1))
d.PinDB6.Write(rpio.State((b >> 6) & 1))
d.PinDB7.Write(rpio.State((b >> 7) & 1))
2024-09-11 15:55:26 +00:00
time.Sleep(adressWriteTimeout)
d.PinE.Low() // Strobe start
2024-09-02 10:11:36 +00:00
time.Sleep(dataStrobeTimeout)
2024-09-11 15:55:26 +00:00
d.PinE.High() // Strobe end
2024-09-02 10:11:36 +00:00
2024-09-15 14:19:19 +00:00
time.Sleep(gapTimeout - dataStrobeTimeout - adressWriteTimeout)
2024-09-02 10:11:36 +00:00
}
2024-09-04 18:05:02 +00:00
func (d *device) WriteBytes(b []byte, cd rpio.State) {
d.busOutput()
2024-09-09 07:31:36 +00:00
d.PinRW.Low() // We write
2024-09-15 17:21:57 +00:00
d.PinA0.Write(cd)
2024-09-04 18:05:02 +00:00
// Write bus
d.PinDB0.Write(rpio.State(b[0]))
d.PinDB1.Write(rpio.State(b[1]))
d.PinDB2.Write(rpio.State(b[2]))
d.PinDB3.Write(rpio.State(b[3]))
d.PinDB4.Write(rpio.State(b[4]))
d.PinDB5.Write(rpio.State(b[5]))
d.PinDB6.Write(rpio.State(b[6]))
d.PinDB7.Write(rpio.State(b[7]))
2024-09-09 07:31:36 +00:00
time.Sleep(adressWriteTimeout)
2024-09-11 15:55:26 +00:00
d.PinE.Low() // Strobe start
2024-09-04 18:05:02 +00:00
time.Sleep(dataStrobeTimeout)
2024-09-11 15:55:26 +00:00
d.PinE.High() // Strobe end
2024-09-04 18:05:02 +00:00
2024-09-09 07:31:36 +00:00
time.Sleep(gapTimeout - dataStrobeTimeout - adressWriteTimeout)
2024-09-04 18:05:02 +00:00
}
2024-09-02 10:11:36 +00:00
func (d *device) ReadByte(cd rpio.State) byte {
// Setup
var b byte
d.PinA0.Write(cd)
2024-09-09 07:31:36 +00:00
d.PinRW.High() // We read
2024-09-11 15:55:26 +00:00
d.busInput()
2024-09-02 10:11:36 +00:00
// Strobe start
2024-09-09 07:31:36 +00:00
time.Sleep(adressWriteTimeout)
2024-09-11 15:55:26 +00:00
d.PinE.Low()
2024-09-09 07:31:36 +00:00
time.Sleep(dataReadTimeout)
2024-09-02 10:11:36 +00:00
// Read
b = uint8(d.PinDB0.Read()) |
(uint8(d.PinDB1.Read()) << 1) |
(uint8(d.PinDB2.Read()) << 2) |
(uint8(d.PinDB3.Read()) << 3) |
(uint8(d.PinDB4.Read()) << 4) |
(uint8(d.PinDB5.Read()) << 5) |
(uint8(d.PinDB6.Read()) << 6) |
(uint8(d.PinDB7.Read()) << 7)
// Strobe end
2024-09-11 15:55:26 +00:00
d.PinE.High()
2024-09-09 07:31:36 +00:00
time.Sleep(gapTimeout - dataReadTimeout - adressWriteTimeout)
2024-09-02 10:11:36 +00:00
return b
}
func (d *device) WaitReady() error {
d.busInput() // Set bus to input
d.PinA0.Low() // Status
2024-09-09 07:31:36 +00:00
d.PinRW.High() // We read
2024-09-02 10:11:36 +00:00
time.Sleep(adressWriteTimeout)
// Strobe start
2024-09-11 15:55:26 +00:00
d.PinE.Low()
2024-09-09 07:31:36 +00:00
time.Sleep(dataReadTimeout)
2024-09-02 10:11:36 +00:00
// Wait status flag drop
ok := false
for counter := 0; counter < maxWaitCycles; counter++ {
if d.PinDB7.Read() != rpio.High {
2024-09-09 07:31:36 +00:00
//d.logger.Printf("BUS:%d%d%d%d%d%d%d%d\n", d.PinDB0.Read(), d.PinDB1.Read(), d.PinDB2.Read(), d.PinDB3.Read(), d.PinDB4.Read(), d.PinDB5.Read(), d.PinDB6.Read(), d.PinDB7.Read())
2024-09-02 10:11:36 +00:00
ok = true
break
}
2024-09-11 15:55:26 +00:00
fmt.Print(".")
2024-09-02 10:11:36 +00:00
}
if !ok {
return fmt.Errorf("busy timeout")
}
// Strobe end
2024-09-11 15:55:26 +00:00
d.PinE.High()
2024-09-09 07:31:36 +00:00
time.Sleep(gapTimeout - dataReadTimeout - adressWriteTimeout)
2024-09-02 10:11:36 +00:00
// d.logger.Println("Ready")
return nil
}
2024-09-09 07:31:36 +00:00
// Set bus Pins to output
2024-09-02 10:11:36 +00:00
func (d *device) busOutput() {
2024-09-15 14:19:19 +00:00
if d.isBusOutput {
return
}
2024-09-02 10:11:36 +00:00
d.PinDB0.Output()
d.PinDB1.Output()
d.PinDB2.Output()
d.PinDB3.Output()
d.PinDB4.Output()
d.PinDB5.Output()
d.PinDB6.Output()
d.PinDB7.Output()
2024-09-04 18:05:02 +00:00
d.isBusOutput = true
2024-09-02 10:11:36 +00:00
}
func (d *device) busInput() {
2024-09-15 14:19:19 +00:00
if !d.isBusOutput {
return
}
2024-09-02 10:11:36 +00:00
d.PinDB0.Input()
d.PinDB1.Input()
d.PinDB2.Input()
d.PinDB3.Input()
d.PinDB4.Input()
d.PinDB5.Input()
d.PinDB6.Input()
d.PinDB7.Input()
2024-09-04 18:05:02 +00:00
d.isBusOutput = false
2024-09-02 10:11:36 +00:00
}