135 lines
2.6 KiB
Go
135 lines
2.6 KiB
Go
package display
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
"periph.io/x/conn/v3"
|
|
"periph.io/x/conn/v3/gpio"
|
|
"periph.io/x/conn/v3/gpio/gpioreg"
|
|
"periph.io/x/conn/v3/physic"
|
|
)
|
|
|
|
type MonochromeDisplayInfo struct {
|
|
DC gpio.PinOut
|
|
Reset gpio.PinOut
|
|
ResetPulseWidth time.Duration
|
|
ResetWait time.Duration
|
|
PreChipEnableWait time.Duration
|
|
PostChipEnableWait time.Duration
|
|
DataSetupTime time.Duration
|
|
WritePulseWidth time.Duration
|
|
Width uint16
|
|
Height uint16
|
|
XOffset uint16
|
|
YOffset uint16
|
|
Speed physic.Frequency
|
|
}
|
|
|
|
// MonochromeDisplay is the hardware abstraction layer for monochrome displays.
|
|
type MonochromeDisplay interface {
|
|
// Init initializes the display.
|
|
Init() error
|
|
|
|
// Halt the display.
|
|
Halt() error
|
|
|
|
// StartTransfer starts a data transfer.
|
|
StartTransfer() error
|
|
|
|
// EndTransfer ends a data transfer.
|
|
EndTransfer() error
|
|
|
|
// Reset the display.
|
|
Reset(on bool) error
|
|
|
|
// SendCommand sends a command to the display.
|
|
SendCommand([]byte) error
|
|
|
|
// SendData sends data to the display.
|
|
SendData([]byte) error
|
|
}
|
|
|
|
type ssd13xxDisplay struct {
|
|
conn conn.Conn
|
|
info MonochromeDisplayInfo
|
|
}
|
|
|
|
func NewSSD13xxDisplay(conn conn.Conn, info MonochromeDisplayInfo) *ssd13xxDisplay {
|
|
return &ssd13xxDisplay{
|
|
conn: conn,
|
|
info: info,
|
|
}
|
|
}
|
|
|
|
func (d *ssd13xxDisplay) Init() (err error) {
|
|
if err = d.Reset(true); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(d.info.ResetPulseWidth)
|
|
if err = d.Reset(false); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(d.info.ResetPulseWidth)
|
|
if err = d.Reset(true); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(d.info.ResetWait)
|
|
return
|
|
}
|
|
|
|
func (d *ssd13xxDisplay) Halt() error {
|
|
if c, ok := d.conn.(io.Closer); ok {
|
|
return c.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *ssd13xxDisplay) Reset(on bool) error {
|
|
if d.info.Reset == nil {
|
|
// No reset pin.
|
|
return nil
|
|
}
|
|
level := gpio.Low
|
|
if on {
|
|
level = gpio.High
|
|
}
|
|
return d.info.Reset.Out(level)
|
|
}
|
|
|
|
func (d *ssd13xxDisplay) StartTransfer() error {
|
|
switch d.conn.(type) {
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (d *ssd13xxDisplay) EndTransfer() error {
|
|
switch d.conn.(type) {
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (d *ssd13xxDisplay) SendCommand(data []byte) error {
|
|
return d.conn.Tx(append([]byte{0x00}, data...), nil)
|
|
}
|
|
|
|
func (d *ssd13xxDisplay) SendData(data []byte) error {
|
|
return d.conn.Tx(append([]byte{0x40}, data...), nil)
|
|
}
|
|
|
|
func LookupPinOut(name string) (gpio.PinOut, error) {
|
|
pin := gpioreg.ByName(name)
|
|
if pin == nil || pin == gpio.INVALID {
|
|
return nil, fmt.Errorf("invalid GPIO pin %q: not found", name)
|
|
}
|
|
return pin, nil
|
|
}
|
|
|
|
// Interface check.
|
|
var (
|
|
_ MonochromeDisplay = &ssd13xxDisplay{}
|
|
)
|