538 lines
9.6 KiB
Go
538 lines
9.6 KiB
Go
// Package gc9a01 provides a driver for the GC9A01 round display controller.
|
|
package gc9a01
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"time"
|
|
|
|
"git.maze.io/go/bragi/hardware"
|
|
"git.maze.io/go/bragi/hardware/display/pixel"
|
|
"periph.io/x/conn/v3/gpio"
|
|
"periph.io/x/conn/v3/physic"
|
|
"periph.io/x/conn/v3/spi"
|
|
)
|
|
|
|
var supportedResolutions = [][2]int16{
|
|
{80, 160},
|
|
{120, 120},
|
|
{128, 128},
|
|
{160, 160},
|
|
{240, 240},
|
|
}
|
|
|
|
// Orientation is the display orientation.
|
|
type Orientation uint8
|
|
|
|
const (
|
|
Horizontal Orientation = iota
|
|
Vertical
|
|
)
|
|
|
|
// Config for the display.
|
|
type Config struct {
|
|
hardware.SPIConfig
|
|
Width int
|
|
Height int
|
|
Orientation Orientation
|
|
VSyncLines int
|
|
RowOffset int
|
|
ColumnOffset int
|
|
}
|
|
|
|
type Device struct {
|
|
conn spi.Conn
|
|
cs gpio.PinOut
|
|
dc gpio.PinOut
|
|
reset gpio.PinOut
|
|
bl gpio.PinOut
|
|
width int16
|
|
height int16
|
|
columnOffsetCfg int16
|
|
rowOffsetCfg int16
|
|
columnOffset int16
|
|
rowOffset int16
|
|
vSyncLines int16
|
|
orientation Orientation
|
|
batchLength int16
|
|
batchData []uint8
|
|
image *pixel.Image565
|
|
}
|
|
|
|
func New(port spi.Port, config Config) (*Device, error) {
|
|
f := config.Frequency
|
|
if f == 0 {
|
|
f = 80 * physic.MegaHertz // 80_000_000
|
|
}
|
|
|
|
c, err := port.Connect(f, spi.Mode0, 8)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
d := &Device{
|
|
conn: c,
|
|
cs: config.CS,
|
|
dc: config.DC,
|
|
reset: config.Reset,
|
|
bl: config.BL,
|
|
}
|
|
|
|
if err = d.configure(config); err != nil {
|
|
return nil, err
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// Reset the display.
|
|
func (d *Device) Reset() error {
|
|
var err error
|
|
if err = d.reset.Out(gpio.High); err != nil {
|
|
return err
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
if err = d.reset.Out(gpio.Low); err != nil {
|
|
return err
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
if err = d.reset.Out(gpio.High); err != nil {
|
|
return err
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
return nil
|
|
}
|
|
|
|
func (d *Device) command(v uint8) error {
|
|
return d.tx([]byte{v}, true)
|
|
}
|
|
|
|
func (d *Device) data(v uint8) error {
|
|
return d.tx([]byte{v}, false)
|
|
}
|
|
|
|
func (d *Device) tx(w []byte, cmd bool) (err error) {
|
|
level := gpio.Low
|
|
if cmd {
|
|
level = gpio.High
|
|
}
|
|
if err = d.dc.Out(level); err != nil {
|
|
return
|
|
}
|
|
return d.conn.Tx(w, nil)
|
|
}
|
|
|
|
/*
|
|
func (d *Device) rx(command uint8, data []byte) (err error) {
|
|
if err = d.dc.Out(gpio.Low); err != nil {
|
|
return
|
|
}
|
|
if err = d.cs.Out(gpio.Low); err != nil {
|
|
return
|
|
}
|
|
if err = d.conn.Tx([]byte{command}, nil); err != nil {
|
|
return
|
|
}
|
|
if err = d.dc.Out(gpio.High); err != nil {
|
|
return
|
|
}
|
|
if err = d.conn.Tx(nil, data); err != nil {
|
|
return
|
|
}
|
|
return d.cs.Out(gpio.High)
|
|
}
|
|
*/
|
|
|
|
func (d *Device) setOrientation() (err error) {
|
|
var reg uint8
|
|
if d.orientation == Horizontal {
|
|
reg = 0xc8
|
|
} else {
|
|
reg = 0x68
|
|
}
|
|
|
|
if err = d.command(MADCTR); err != nil {
|
|
return
|
|
}
|
|
return d.command(reg)
|
|
}
|
|
|
|
func (d *Device) setWindow(x, y, w, h int) (err error) {
|
|
if d.orientation == Horizontal {
|
|
x += int(d.columnOffset)
|
|
y += int(d.rowOffset)
|
|
} else {
|
|
x += int(d.rowOffset)
|
|
y += int(d.columnOffset)
|
|
}
|
|
if err = d.command(CASET); err != nil {
|
|
return
|
|
}
|
|
if err = d.tx([]uint8{uint8(x >> 8), uint8(x), uint8((x + w - 1) >> 8), uint8(x + w - 1)}, false); err != nil {
|
|
return
|
|
}
|
|
if err = d.command(RASET); err != nil {
|
|
return
|
|
}
|
|
if err = d.tx([]uint8{uint8(y >> 8), uint8(y), uint8((y + h - 1) >> 8), uint8(y + h - 1)}, false); err != nil {
|
|
return
|
|
}
|
|
return d.command(RAMWR)
|
|
}
|
|
|
|
func (d *Device) Backlight(on bool) error {
|
|
if on {
|
|
return d.bl.Out(gpio.High)
|
|
}
|
|
return d.bl.Out(gpio.Low)
|
|
}
|
|
|
|
func (d *Device) Invert(on bool) error {
|
|
if on {
|
|
return d.command(INVON)
|
|
}
|
|
return d.command(INVOFF)
|
|
}
|
|
|
|
func (d *Device) FillRectangle(x, y, w, h int, c color.Color) (err error) {
|
|
if x < 0 || y < 0 || w <= 0 || h <= 0 ||
|
|
x >= int(d.width) || (x+w) > int(d.width) || y >= int(d.height) || (y+h) > int(d.height) {
|
|
return errors.New("gc9a01: rectangle coordinates outside display area")
|
|
}
|
|
|
|
d.setWindow(x, y, w, h)
|
|
|
|
c565 := pixel.Color565Model.Convert(c).(pixel.Color565)
|
|
c1 := uint8(c565 >> 8)
|
|
c2 := uint8(c565)
|
|
for i := 0; i < int(d.batchLength); i++ {
|
|
d.batchData[i*2+0] = c1
|
|
d.batchData[i*2+1] = c2
|
|
}
|
|
|
|
i := w * h
|
|
l := int(d.batchLength)
|
|
for i > 0 {
|
|
if i >= l {
|
|
if err = d.tx(d.batchData, false); err != nil {
|
|
return
|
|
}
|
|
} else {
|
|
if err = d.tx(d.batchData[:i*2], false); err != nil {
|
|
return
|
|
}
|
|
}
|
|
i -= l
|
|
}
|
|
|
|
for yy := y; yy < y+h; yy++ {
|
|
for xx := x; xx < x+w; xx++ {
|
|
d.image.Set(xx, yy, c)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *Device) At(x, y int) color.Color {
|
|
if x < 0 || x >= int(d.width) || y < 0 || y >= int(d.height) {
|
|
return color.Black
|
|
}
|
|
return d.image.At(x, y)
|
|
}
|
|
|
|
func (d *Device) Set(x, y int, c color.Color) {
|
|
if x < 0 || x >= int(d.width) || y < 0 || y >= int(d.height) {
|
|
return
|
|
}
|
|
_ = d.FillRectangle(x, y, 1, 1, c)
|
|
}
|
|
|
|
func (d *Device) Bounds() image.Rectangle {
|
|
return image.Rect(0, 0, int(d.width), int(d.height))
|
|
}
|
|
|
|
func (d *Device) ColorModel() color.Model {
|
|
return pixel.Color565Model
|
|
}
|
|
|
|
func (d *Device) configure(config Config) (err error) {
|
|
if config.Width > 0 {
|
|
d.width = int16(config.Width)
|
|
} else {
|
|
d.width = 240
|
|
}
|
|
if config.Height > 0 {
|
|
d.height = int16(config.Height)
|
|
} else {
|
|
d.height = 240
|
|
}
|
|
|
|
var supportedResolution bool
|
|
for _, res := range supportedResolutions {
|
|
if supportedResolution = d.width == res[0] && d.height == res[1]; supportedResolution {
|
|
break
|
|
}
|
|
}
|
|
if !supportedResolution {
|
|
return fmt.Errorf("gc9a01: unsupported resolution %dx%d", d.width, d.height)
|
|
}
|
|
|
|
d.image = pixel.NewImage565(image.Rect(0, 0, int(d.width), int(d.height)))
|
|
d.orientation = config.Orientation
|
|
d.rowOffsetCfg = int16(config.RowOffset)
|
|
d.columnOffsetCfg = int16(config.ColumnOffset)
|
|
d.batchLength = d.width
|
|
|
|
if config.VSyncLines >= 2 && config.VSyncLines <= 254 {
|
|
d.vSyncLines = int16(config.VSyncLines)
|
|
} else {
|
|
d.vSyncLines = 2
|
|
}
|
|
d.batchLength += d.batchLength & 1
|
|
d.batchData = make([]uint8, d.batchLength*2)
|
|
|
|
if err := d.Reset(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := d.setOrientation(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Common initialization
|
|
_ = d.command(0xEF)
|
|
_ = d.command(0xEB)
|
|
_ = d.data(0x14)
|
|
|
|
_ = d.command(INTEN1)
|
|
_ = d.command(0xEF)
|
|
|
|
_ = d.command(0xEB)
|
|
_ = d.data(0x14)
|
|
|
|
_ = d.command(0x84)
|
|
_ = d.data(0x40)
|
|
|
|
_ = d.command(0x85)
|
|
_ = d.data(0xFF)
|
|
|
|
_ = d.command(0x86)
|
|
_ = d.data(0xFF)
|
|
|
|
_ = d.command(0x87)
|
|
_ = d.data(0xFF)
|
|
|
|
_ = d.command(0x88)
|
|
_ = d.data(0x0A)
|
|
|
|
_ = d.command(0x89)
|
|
_ = d.data(0x21)
|
|
|
|
_ = d.command(0x8A)
|
|
_ = d.data(0x00)
|
|
|
|
_ = d.command(0x8B)
|
|
_ = d.data(0x80)
|
|
|
|
_ = d.command(0x8C)
|
|
_ = d.data(0x01)
|
|
|
|
_ = d.command(0x8D)
|
|
_ = d.data(0x01)
|
|
|
|
_ = d.command(0x8E)
|
|
_ = d.data(0xFF)
|
|
|
|
_ = d.command(0x8F)
|
|
_ = d.data(0xFF)
|
|
|
|
_ = d.command(DISFNCTL)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x20)
|
|
|
|
_ = d.command(MADCTR)
|
|
_ = d.data(0x08) //Set as vertical screen
|
|
|
|
_ = d.command(COLMOD)
|
|
_ = d.data(0x05)
|
|
|
|
_ = d.command(0x90)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x08)
|
|
|
|
_ = d.command(0xBD)
|
|
_ = d.data(0x06)
|
|
|
|
_ = d.command(0xBC)
|
|
_ = d.data(0x00)
|
|
|
|
_ = d.command(0xFF)
|
|
_ = d.data(0x60)
|
|
_ = d.data(0x01)
|
|
_ = d.data(0x04)
|
|
|
|
_ = d.command(PWCTR3)
|
|
_ = d.data(0x13)
|
|
_ = d.command(PWCTR4)
|
|
_ = d.data(0x13)
|
|
|
|
_ = d.command(0xC9)
|
|
_ = d.data(0x22)
|
|
|
|
_ = d.command(0xBE)
|
|
_ = d.data(0x11)
|
|
|
|
_ = d.command(0xE1)
|
|
_ = d.data(0x10)
|
|
_ = d.data(0x0E)
|
|
|
|
_ = d.command(0xDF)
|
|
_ = d.data(0x21)
|
|
_ = d.data(0x0c)
|
|
_ = d.data(0x02)
|
|
|
|
_ = d.command(GMSET1)
|
|
_ = d.data(0x45)
|
|
_ = d.data(0x09)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x26)
|
|
_ = d.data(0x2A)
|
|
|
|
_ = d.command(GMSET2)
|
|
_ = d.data(0x43)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x72)
|
|
_ = d.data(0x36)
|
|
_ = d.data(0x37)
|
|
_ = d.data(0x6F)
|
|
|
|
_ = d.command(GMSET3)
|
|
_ = d.data(0x45)
|
|
_ = d.data(0x09)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x26)
|
|
_ = d.data(0x2A)
|
|
|
|
_ = d.command(GMSET4)
|
|
_ = d.data(0x43)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x72)
|
|
_ = d.data(0x36)
|
|
_ = d.data(0x37)
|
|
_ = d.data(0x6F)
|
|
|
|
_ = d.command(0xED)
|
|
_ = d.data(0x1B)
|
|
_ = d.data(0x0B)
|
|
|
|
_ = d.command(0xAE)
|
|
_ = d.data(0x77)
|
|
|
|
_ = d.command(0xCD)
|
|
_ = d.data(0x63)
|
|
|
|
_ = d.command(0x70)
|
|
_ = d.data(0x07)
|
|
_ = d.data(0x07)
|
|
_ = d.data(0x04)
|
|
_ = d.data(0x0E)
|
|
_ = d.data(0x0F)
|
|
_ = d.data(0x09)
|
|
_ = d.data(0x07)
|
|
_ = d.data(0x08)
|
|
_ = d.data(0x03)
|
|
|
|
_ = d.command(FRMCTL)
|
|
_ = d.data(0x34)
|
|
|
|
_ = d.command(0x62)
|
|
_ = d.data(0x18)
|
|
_ = d.data(0x0D)
|
|
_ = d.data(0x71)
|
|
_ = d.data(0xED)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x18)
|
|
_ = d.data(0x0F)
|
|
_ = d.data(0x71)
|
|
_ = d.data(0xEF)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x70)
|
|
|
|
_ = d.command(0x63)
|
|
_ = d.data(0x18)
|
|
_ = d.data(0x11)
|
|
_ = d.data(0x71)
|
|
_ = d.data(0xF1)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x18)
|
|
_ = d.data(0x13)
|
|
_ = d.data(0x71)
|
|
_ = d.data(0xF3)
|
|
_ = d.data(0x70)
|
|
_ = d.data(0x70)
|
|
|
|
_ = d.command(0x64)
|
|
_ = d.data(0x28)
|
|
_ = d.data(0x29)
|
|
_ = d.data(0xF1)
|
|
_ = d.data(0x01)
|
|
_ = d.data(0xF1)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x07)
|
|
|
|
_ = d.command(0x66)
|
|
_ = d.data(0x3C)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0xCD)
|
|
_ = d.data(0x67)
|
|
_ = d.data(0x45)
|
|
_ = d.data(0x45)
|
|
_ = d.data(0x10)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x00)
|
|
|
|
_ = d.command(0x67)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x3C)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x01)
|
|
_ = d.data(0x54)
|
|
_ = d.data(0x10)
|
|
_ = d.data(0x32)
|
|
_ = d.data(0x98)
|
|
|
|
_ = d.command(0x74)
|
|
_ = d.data(0x10)
|
|
_ = d.data(0x85)
|
|
_ = d.data(0x80)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x00)
|
|
_ = d.data(0x4E)
|
|
_ = d.data(0x00)
|
|
|
|
_ = d.command(0x98)
|
|
_ = d.data(0x3e)
|
|
_ = d.data(0x07)
|
|
|
|
_ = d.command(TEON)
|
|
_ = d.command(0x21)
|
|
|
|
_ = d.command(SLPOUT)
|
|
time.Sleep(120 * time.Millisecond)
|
|
_ = d.command(DISPON)
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
return d.Backlight(true)
|
|
}
|