Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:41 +01:00
1 parent b8a493b93d
commit 85a9ec684c
54 files changed
+6855 -526

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
package st7789
// Registers
const (
NOP = 0x00
SWRESET = 0x01
RDDID = 0x04
RDDST = 0x09
SLPIN = 0x10
SLPOUT = 0x11
PTLON = 0x12
NORON = 0x13
INVOFF = 0x20
INVON = 0x21
DISPOFF = 0x28
DISPON = 0x29
CASET = 0x2A
RASET = 0x2B
RAMWR = 0x2C
RAMRD = 0x2E
PTLAR = 0x30
COLMOD = 0x3A
MADCTL = 0x36
MADCTL_MY = 0x80
MADCTL_MX = 0x40
MADCTL_MV = 0x20
MADCTL_ML = 0x10
MADCTL_RGB = 0x00
MADCTL_BGR = 0x08
MADCTL_MH = 0x04
RDID1 = 0xDA
RDID2 = 0xDB
RDID3 = 0xDC
RDID4 = 0xDD
FRMCTR1 = 0xB1
RGBCTRL = 0xB1
FRMCTR2 = 0xB2
PORCTRL = 0xB2
FRMCTR3 = 0xB3
INVCTR = 0xB4
DISSET5 = 0xB6
PWCTR1 = 0xC0
PWCTR2 = 0xC1
PWCTR3 = 0xC2
PWCTR4 = 0xC3
PWCTR5 = 0xC4
VMCTR1 = 0xC5
FRCTRL2 = 0xC6
PWCTR6 = 0xFC
GMCTRP1 = 0xE0
GMCTRN1 = 0xE1
GSCAN = 0x45
VSCRDEF = 0x33
VSCRSADD = 0x37
)
const (
MAX_VSYNC_SCANLINES = 254
defaultWidth = 240
defaultHeight = 240
)
const (
// Allowable frame rate codes for FRCTRL2 (Identifier is in Hz)
FrameRate111 FrameRate = 0x01
FrameRate105 FrameRate = 0x02
FrameRate99 FrameRate = 0x03
FrameRate94 FrameRate = 0x04
FrameRate90 FrameRate = 0x05
FrameRate86 FrameRate = 0x06
FrameRate82 FrameRate = 0x07
FrameRate78 FrameRate = 0x08
FrameRate75 FrameRate = 0x09
FrameRate72 FrameRate = 0x0A
FrameRate69 FrameRate = 0x0B
FrameRate67 FrameRate = 0x0C
FrameRate64 FrameRate = 0x0D
FrameRate62 FrameRate = 0x0E
FrameRate60 FrameRate = 0x0F // 60 is default
FrameRate58 FrameRate = 0x10
FrameRate57 FrameRate = 0x11
FrameRate55 FrameRate = 0x12
FrameRate53 FrameRate = 0x13
FrameRate52 FrameRate = 0x14
FrameRate50 FrameRate = 0x15
FrameRate49 FrameRate = 0x16
FrameRate48 FrameRate = 0x17
FrameRate46 FrameRate = 0x18
FrameRate45 FrameRate = 0x19
FrameRate44 FrameRate = 0x1A
FrameRate43 FrameRate = 0x1B
FrameRate42 FrameRate = 0x1C
FrameRate41 FrameRate = 0x1D
FrameRate40 FrameRate = 0x1E
FrameRate39 FrameRate = 0x1F
)
+586
View File
@@ -0,0 +1,586 @@
// Package st7789 provides a driver for Sitronix ST7789 IPS TFT displays.
package st7789
import (
"errors"
"fmt"
"image"
"image/color"
"time"
"github.com/sirupsen/logrus"
"go.maze.io/harmon/config"
"go.maze.io/harmon/hardware"
"go.maze.io/harmon/hardware/display"
"periph.io/x/conn/v3/gpio"
)
func init() {
display.Register("st7789", newDriver)
}
// FrameRate controls the frame rate used by the display.
type FrameRate uint8
// Driver wraps an SPI connection.
type Driver struct {
conn display.Conn
//dcPin gpio.PinOut
//csPin gpio.PinOut
resetPin gpio.PinOut
blPin gpio.PinOut
width int16
height int16
columnOffsetCfg int16
rowOffsetCfg int16
columnOffset int16
rowOffset int16
rotation display.Rotation
frameRate FrameRate
batchLength int32
isBGR bool
vSyncLines int16
}
// Config is the configuration for the display
type Config struct {
Width int16
Height int16
Rotation display.Rotation
RowOffset int16
ColumnOffset int16
FrameRate FrameRate
VSyncLines int16
}
func newDriver(config config.DisplayConfig) (hardware.Display, error) {
var (
opts *display.Options
conn display.Conn
err error
)
if opts, err = display.ParseOptions(config, defaultWidth, defaultHeight); err != nil {
return nil, err
}
var reset, bl gpio.PinOut
if bl, err = hardware.LookupPin(config.BL); err != nil {
return nil, err
}
if reset, err = hardware.LookupPin(config.Reset); err != nil {
return nil, err
}
log := logrus.WithFields(logrus.Fields{
"type": "st7789",
"size": fmt.Sprintf("%dx%d", opts.W, opts.H),
"rotate": opts.Rotate,
})
log.Debug("new driver")
if conn, err = display.OpenSPI(config.Port, config.Reset, config.DC); err != nil {
return nil, err
}
d := &Driver{
conn: conn,
blPin: bl,
resetPin: reset,
}
if err = d.Configure(Config{
Width: int16(opts.W),
Height: int16(opts.H),
Rotation: opts.Rotate,
}); err != nil {
return nil, err
}
return d, nil
}
// New creates a new ST7789 connection. The SPI wire must already be configured.
func New(conn display.Conn, resetPin, blPin gpio.PinOut) Driver {
return Driver{
conn: conn,
resetPin: resetPin,
blPin: blPin,
}
}
// Configure initializes the display with default configuration
func (d *Driver) Configure(cfg Config) error {
if cfg.Width != 0 {
d.width = cfg.Width
} else {
d.width = 240
}
if cfg.Height != 0 {
d.height = cfg.Height
} else {
d.height = 240
}
d.rotation = cfg.Rotation
d.rowOffsetCfg = cfg.RowOffset
d.columnOffsetCfg = cfg.ColumnOffset
if cfg.FrameRate != 0 {
d.frameRate = cfg.FrameRate
} else {
d.frameRate = FrameRate60
}
if cfg.VSyncLines >= 2 && cfg.VSyncLines <= 254 {
d.vSyncLines = cfg.VSyncLines
} else {
d.vSyncLines = 16
}
d.batchLength = int32(d.width)
if d.height > d.width {
d.batchLength = int32(d.height)
}
d.batchLength += d.batchLength & 1
// Reset the device
d.resetPin.Out(gpio.High)
time.Sleep(500 * time.Millisecond)
d.resetPin.Out(gpio.Low)
time.Sleep(500 * time.Millisecond)
d.resetPin.Out(gpio.High)
time.Sleep(500 * time.Millisecond)
// Common initialization
d.Command(SWRESET) // Soft reset
time.Sleep(150 * time.Millisecond) //
d.Command(SLPOUT) // Exit sleep mode
time.Sleep(500 * time.Millisecond) //
// Memory initialization
d.Command(COLMOD) // Set color mode
d.Data(0x55) // 16-bit color
time.Sleep(10 * time.Millisecond) //
d.SetRotation(d.rotation) // Memory orientation
d.setWindow(0, 0, d.width, d.height) // Full draw window
d.FillScreen(color.RGBA{0, 0, 0, 255}) // Clear screen
// Framerate
d.Command(FRCTRL2) // Frame rate for normal mode
d.Data(uint8(d.frameRate)) // Default is 60Hz
// Frame vertical sync and "porch"
//
// Front and back porch controls vertical scanline sync time before and after
// a frame, where memory can be safely written without tearing.
//
fp := uint8(d.vSyncLines / 2) // Split the desired pause half and half
bp := uint8(d.vSyncLines - int16(fp)) // between front and back porch.
d.Command(PORCTRL)
d.Data(bp) // Back porch 5bit (0x7F max 0x08 default)
d.Data(fp) // Front porch 5bit (0x7F max 0x08 default)
d.Data(0x00) // Seprarate porch (TODO: what is this?)
d.Data(0x22) // Idle mode porch (4bit-back 4bit-front 0x22 default)
d.Data(0x22) // Partial mode porch (4bit-back 4bit-front 0x22 default)
// Ready to display
d.Command(INVON) // Inversion ON
time.Sleep(10 * time.Millisecond) //
d.Command(NORON) // Normal mode ON
time.Sleep(10 * time.Millisecond) //
d.Command(DISPON) // Screen ON
time.Sleep(10 * time.Millisecond) //
d.blPin.Out(gpio.High) // Backlight ON
return nil
}
/*
// Sync waits for the display to hit the next VSYNC pause
func (d *Driver) Sync() {
d.SyncToScanLine(0)
}
// SyncToScanLine waits for the display to hit a specific scanline
//
// A scanline value of 0 will forward to the beginning of the next VSYNC,
// even if the display is currently in a VSYNC pause.
//
// Syncline values appear to increment once for every two vertical
// lines on the display.
//
// NOTE: Use GetHighestScanLine and GetLowestScanLine to obtain the highest
// and lowest useful values. Values are affected by front and back porch
// vsync settings (derived from VSyncLines configuration option).
func (d *Driver) SyncToScanLine(scanline uint16) {
scan := d.GetScanLine()
// Sometimes GetScanLine returns erroneous 0 on first call after draw, so double check
if scan == 0 {
scan = d.GetScanLine()
}
if scanline == 0 {
// we dont know where we are in an ongoing vsync so go around
for scan < 1 {
time.Sleep(1 * time.Millisecond)
scan = d.GetScanLine()
}
for scan > 0 {
scan = d.GetScanLine()
}
} else {
// go around unless we're very close to the target
for scan > scanline+4 {
time.Sleep(1 * time.Millisecond)
scan = d.GetScanLine()
}
for scan < scanline {
scan = d.GetScanLine()
}
}
}
// GetScanLine reads the current scanline value from the display
func (d *Driver) GetScanLine() uint16 {
data := []uint8{0x00, 0x00}
d.conn.Rx(GSCAN, data)
return uint16(data[0])<<8 + uint16(data[1])
}
// GetHighestScanLine calculates the last scanline id in the frame before VSYNC pause
func (d *Driver) GetHighestScanLine() uint16 {
// Last scanline id appears to be backporch/2 + 320/2
return uint16(math.Ceil(float64(d.vSyncLines)/2)/2) + 160
}
// GetLowestScanLine calculate the first scanline id to appear after VSYNC pause
func (d *Driver) GetLowestScanLine() uint16 {
// First scanline id appears to be backporch/2 + 1
return uint16(math.Ceil(float64(d.vSyncLines)/2)/2) + 1
}
*/
// Refresh does nothing, there's no buffer as it might be too big for some boards
func (d *Driver) Refresh() error {
return nil
}
// SetPixel sets a pixel in the screen
func (d *Driver) SetPixel(x int16, y int16, c color.RGBA) {
if x < 0 || y < 0 ||
(((d.rotation == display.NoRotation || d.rotation == display.Rotate180) && (x >= d.width || y >= d.height)) ||
((d.rotation == display.Rotate90 || d.rotation == display.Rotate270) && (x >= d.height || y >= d.width))) {
return
}
d.FillRectangle(x, y, 1, 1, c)
}
// setWindow prepares the screen to be modified at a given rectangle
func (d *Driver) setWindow(x, y, w, h int16) {
x += d.columnOffset
y += d.rowOffset
d.conn.Tx([]uint8{CASET}, true)
d.conn.Tx([]uint8{uint8(x >> 8), uint8(x), uint8((x + w - 1) >> 8), uint8(x + w - 1)}, false)
d.conn.Tx([]uint8{RASET}, true)
d.conn.Tx([]uint8{uint8(y >> 8), uint8(y), uint8((y + h - 1) >> 8), uint8(y + h - 1)}, false)
d.Command(RAMWR)
}
func (d *Driver) Clear() {
d.FillRectangle(0, 0, d.width, d.height, color.RGBA{})
}
// FillRectangle fills a rectangle at a given coordinates with a color
func (d *Driver) FillRectangle(x, y, width, height int16, c color.RGBA) error {
k, i := int16(d.Size().X), int16(d.Size().Y)
if x < 0 || y < 0 || width <= 0 || height <= 0 ||
x >= k || (x+width) > k || y >= i || (y+height) > i {
return errors.New("rectangle coordinates outside display area")
}
d.setWindow(x, y, width, height)
c565 := RGBATo565(c)
c1 := uint8(c565 >> 8)
c2 := uint8(c565)
data := make([]uint8, d.batchLength*2)
for i := int32(0); i < d.batchLength; i++ {
data[i*2] = c1
data[i*2+1] = c2
}
j := int32(width) * int32(height)
for j > 0 {
if j >= d.batchLength {
d.conn.Tx(data, false)
} else {
d.conn.Tx(data[:j*2], false)
}
j -= d.batchLength
}
return nil
}
// FillRectangleWithBuffer fills buffer with a rectangle at a given coordinates.
func (d *Driver) FillRectangleWithBuffer(x, y, width, height int16, buffer []color.RGBA) error {
i, j := int16(d.Size().X), int16(d.Size().Y)
if x < 0 || y < 0 || width <= 0 || height <= 0 ||
x >= i || (x+width) > i || y >= j || (y+height) > j {
return errors.New("rectangle coordinates outside display area")
}
if int32(width)*int32(height) != int32(len(buffer)) {
return errors.New("buffer length does not match with rectangle size")
}
d.setWindow(x, y, width, height)
k := int32(width) * int32(height)
data := make([]uint8, d.batchLength*2)
offset := int32(0)
for k > 0 {
for i := int32(0); i < d.batchLength; i++ {
if offset+i < int32(len(buffer)) {
c565 := RGBATo565(buffer[offset+i])
c1 := uint8(c565 >> 8)
c2 := uint8(c565)
data[i*2] = c1
data[i*2+1] = c2
}
}
if k >= d.batchLength {
d.conn.Tx(data, false)
//d.Tx(data, false)
} else {
d.conn.Tx(data[:k*2], false)
//d.Tx(data[:k*2], false)
}
k -= d.batchLength
offset += d.batchLength
}
return nil
}
// DrawFastVLine draws a vertical line faster than using SetPixel
func (d *Driver) DrawFastVLine(x, y0, y1 int16, c color.RGBA) {
if y0 > y1 {
y0, y1 = y1, y0
}
d.FillRectangle(x, y0, 1, y1-y0+1, c)
}
// DrawFastHLine draws a horizontal line faster than using SetPixel
func (d *Driver) DrawFastHLine(x0, x1, y int16, c color.RGBA) {
if x0 > x1 {
x0, x1 = x1, x0
}
d.FillRectangle(x0, y, x1-x0+1, 1, c)
}
// FillScreen fills the screen with a given color
func (d *Driver) FillScreen(c color.RGBA) {
if d.rotation == display.NoRotation || d.rotation == display.Rotate180 {
d.FillRectangle(0, 0, d.width, d.height, c)
} else {
d.FillRectangle(0, 0, d.height, d.width, c)
}
}
// SetRotation changes the rotation of the device (clock-wise)
func (d *Driver) SetRotation(rotation display.Rotation) {
madctl := uint8(0)
switch rotation % 4 {
case display.NoRotation:
madctl = MADCTL_MX | MADCTL_MY
d.rowOffset = d.rowOffsetCfg
d.columnOffset = d.columnOffsetCfg
break
case display.Rotate90:
madctl = MADCTL_MY | MADCTL_MV
d.rowOffset = d.columnOffsetCfg
d.columnOffset = d.rowOffsetCfg
break
case display.Rotate180:
d.rowOffset = 0
d.columnOffset = 0
break
case display.Rotate270:
madctl = MADCTL_MX | MADCTL_MV
d.rowOffset = 0
d.columnOffset = 0
break
}
if d.isBGR {
madctl |= MADCTL_BGR
}
d.Command(MADCTL)
d.Data(madctl)
}
// Command sends a command to the display.
func (d *Driver) Command(command uint8) error {
//d.Tx([]byte{command}, true)
return d.conn.Tx([]byte{command}, true)
}
// Data sends data to the display.
func (d *Driver) Data(data uint8) error {
//d.Tx([]byte{data}, false)
return d.conn.Tx([]byte{data}, false)
}
// Tx sends data to the display
/*
func (d *Driver) Tx(data []byte, isCommand bool) {
if isCommand {
d.dcPin.Out(gpio.Low)
} else {
d.dcPin.Out(gpio.High)
}
d.csPin.Out(gpio.Low)
d.bus.Tx(data, nil)
d.csPin.Out(gpio.High)
}
// Rx reads data from the display
func (d *Driver) Rx(command uint8, data []byte) {
d.dcPin.Out(gpio.Low)
d.csPin.Out(gpio.Low)
sendCommand(d.bus, command)
d.dcPin.Out(gpio.High)
for i := range data {
data[i], _ = sendCommand(d.bus, 0xFF)
}
d.csPin.Out(gpio.High)
}
func sendCommand(bus spi.Conn, command byte) (byte, error) {
answer := []byte{0}
err := bus.Tx(
[]byte{command},
[]byte(""),
)
if err != nil {
return 0, err
}
return answer[0], nil
}
*/
// Size returns the current size of the display.
/*
func (d *Driver) Size() (w, h int16) {
if d.rotation == display.NoRotation || d.rotation == display.Rotate180 {
return d.width, d.height
}
return d.height, d.width
}
*/
// EnableBacklight enables or disables the backlight
func (d *Driver) EnableBacklight(enable bool) {
if enable {
d.blPin.Out(gpio.High)
} else {
d.blPin.Out(gpio.Low)
}
}
// InvertColors inverts the colors of the screen
func (d *Driver) InvertColors(invert bool) {
if invert {
d.Command(INVON)
} else {
d.Command(INVOFF)
}
}
// IsBGR changes the color mode (RGB/BGR)
func (d *Driver) IsBGR(bgr bool) {
d.isBGR = bgr
}
// SetScrollArea sets an area to scroll with fixed top and bottom parts of the display.
func (d *Driver) SetScrollArea(topFixedArea, bottomFixedArea int16) {
d.Command(VSCRDEF)
d.conn.Tx([]uint8{
uint8(topFixedArea >> 8), uint8(topFixedArea),
uint8(d.height - topFixedArea - bottomFixedArea>>8), uint8(d.height - topFixedArea - bottomFixedArea),
uint8(bottomFixedArea >> 8), uint8(bottomFixedArea)},
false)
}
// SetScroll sets the vertical scroll address of the display.
func (d *Driver) SetScroll(line int16) {
d.Command(VSCRSADD)
d.conn.Tx([]uint8{uint8(line >> 8), uint8(line)}, false)
}
// StopScroll returns the display to its normal state.
func (d *Driver) StopScroll() {
d.Command(NORON)
}
// RGBATo565 converts a color.RGBA to uint16 used in the display
func RGBATo565(c color.RGBA) uint16 {
r, g, b, _ := c.RGBA()
return uint16((r & 0xF800) +
((g & 0xFC00) >> 5) +
((b & 0xF800) >> 11))
}
/* hardware.Display interface methods */
func (Driver) Type() string {
return "st7789"
}
func (d *Driver) At(x, y int) color.Color {
return color.Black // not supported!
}
func (d *Driver) Bounds() image.Rectangle {
if d.rotation == display.NoRotation || d.rotation == display.Rotate180 {
return image.Rect(0, 0, int(d.width), int(d.height))
}
return image.Rect(0, 0, int(d.height), int(d.width))
}
func (d *Driver) ColorModel() color.Model {
return color.RGBAModel
}
func (d *Driver) Fill(c color.Color) error {
return d.FillRectangle(0, 0, d.width, d.height, color.RGBAModel.Convert(c).(color.RGBA))
}
func (d *Driver) Halt() error {
return nil
}
func (d *Driver) Set(x, y int, c color.Color) {
d.SetPixel(int16(x), int16(y), color.RGBAModel.Convert(c).(color.RGBA))
}
func (d *Driver) Size() image.Point {
if d.rotation == display.NoRotation || d.rotation == display.Rotate180 {
return image.Pt(int(d.width), int(d.height))
}
return image.Pt(int(d.height), int(d.width))
}
func (d *Driver) Draw(r image.Rectangle, src image.Image, sp image.Point) error {
var buffer []color.RGBA
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
buffer = append(buffer, color.RGBAModel.Convert(src.At(x, y)).(color.RGBA))
}
}
return d.FillRectangleWithBuffer(int16(sp.X), int16(sp.Y), int16(r.Dx()), int16(r.Dy()), buffer)
}