889 lines
24 KiB
Go
889 lines
24 KiB
Go
// Package st7789 implements a driver for the ST7789 TFT displays, it comes in various screen sizes.
|
|
//
|
|
// Datasheets: https://cdn-shop.adafruit.com/product-files/3787/3787_tft_QT154H2201__________20190228182902.pdf
|
|
//
|
|
// http://www.newhavendisplay.com/appnotes/datasheets/LCDs/ST7789V.pdf
|
|
package st7789
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"periph.io/x/conn/v3/gpio"
|
|
"periph.io/x/conn/v3/physic"
|
|
"periph.io/x/conn/v3/spi"
|
|
"periph.io/x/conn/v3/spi/spireg"
|
|
|
|
"git.maze.io/go/harmon/config"
|
|
"git.maze.io/go/harmon/display"
|
|
"git.maze.io/go/harmon/hardware"
|
|
"git.maze.io/go/harmon/internal/pixel"
|
|
)
|
|
|
|
// Rotation controls the rotation used by the display.
|
|
//
|
|
// Deprecated: use drivers.Rotation instead.
|
|
type Rotation = display.Rotation
|
|
|
|
// The color format used on the display, like RGB565, RGB666, and RGB444.
|
|
type ColorFormat uint8
|
|
|
|
// Pixel formats supported by the st7789 driver.
|
|
type Color interface {
|
|
pixel.RGB444BE | pixel.RGB565BE
|
|
|
|
pixel.BaseColor
|
|
}
|
|
|
|
// FrameRate controls the frame rate used by the display.
|
|
type FrameRate uint8
|
|
|
|
var (
|
|
errOutOfBounds = errors.New("rectangle coordinates outside display area")
|
|
)
|
|
|
|
func init() {
|
|
display.Register("st7789", func(config *config.Display) (display.Display, error) {
|
|
log := logrus.WithFields(logrus.Fields{
|
|
"type": "st7789",
|
|
"name": config.Name,
|
|
})
|
|
|
|
width, height, err := display.ParseSize(config.Size, 240, 320)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rotation, err := display.ParseRotation(config.Rotate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.WithField("port", config.Port).Trace("opening SPI port")
|
|
port, err := spireg.Open(config.Port)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var (
|
|
bits = 8
|
|
reset gpio.PinOut
|
|
dc gpio.PinOut
|
|
cs gpio.PinOut
|
|
bl gpio.PinOut
|
|
)
|
|
if dc, err = hardware.LookupPinOut(config.DC); err != nil {
|
|
if !hardware.IsNotFound(err) {
|
|
return nil, fmt.Errorf("invalid dc pin: %w", err)
|
|
}
|
|
bits = 9 // no DC pin
|
|
}
|
|
if reset, err = hardware.LookupPinOut(config.Reset); err != nil {
|
|
return nil, fmt.Errorf("invalid reset pin: %w", err)
|
|
}
|
|
if cs, err = hardware.LookupPinOut(config.CS); err != nil && !hardware.IsNotFound(err) {
|
|
return nil, fmt.Errorf("invalid cs pin: %w", err)
|
|
} else {
|
|
cs = gpio.INVALID
|
|
}
|
|
if bl, err = hardware.LookupPinOut(config.BL); err != nil {
|
|
return nil, fmt.Errorf("invalid bl pin: %w", err)
|
|
}
|
|
|
|
log.WithFields(logrus.Fields{
|
|
"mode": spi.Mode0,
|
|
"bits": bits,
|
|
"pin_reset": reset,
|
|
"pin_dc": dc,
|
|
"pin_cs": cs,
|
|
"pin_bl": bl,
|
|
}).Debug("connecting on SPI port")
|
|
conn, err := port.Connect(75*physic.MegaHertz, spi.Mode0, bits)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
d := New[pixel.RGB565BE](conn, reset, dc, cs, bl)
|
|
d.log = log
|
|
|
|
if err = d.Configure(Config{
|
|
Width: int16(width),
|
|
Height: int16(height),
|
|
Rotation: rotation,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return d, nil
|
|
})
|
|
}
|
|
|
|
// Device wraps an SPI connection.
|
|
type Device = DeviceOf[pixel.RGB565BE]
|
|
|
|
// DeviceOf is a generic version of Device. It supports multiple different pixel
|
|
// formats.
|
|
type DeviceOf[T Color] struct {
|
|
log *logrus.Entry
|
|
bus spi.Conn
|
|
dcPin gpio.PinOut
|
|
resetPin gpio.PinOut
|
|
csPin gpio.PinOut
|
|
blPin gpio.PinOut
|
|
width int16
|
|
height int16
|
|
columnOffsetCfg int16
|
|
rowOffsetCfg int16
|
|
columnOffset int16
|
|
rowOffset int16
|
|
rotation display.Rotation
|
|
frameRate FrameRate
|
|
batchLength int32
|
|
batchData pixel.Image[T] // "image" with (width, height) of (batchLength, 1)
|
|
isBGR bool
|
|
vSyncLines int16
|
|
cmdBuf [1]byte
|
|
buf [6]byte
|
|
}
|
|
|
|
// 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
|
|
|
|
// Gamma control. Look in the LCD panel datasheet or provided example code
|
|
// to find these values. If not set, the defaults will be used.
|
|
PVGAMCTRL []uint8 // Positive voltage gamma control (14 bytes)
|
|
NVGAMCTRL []uint8 // Negative voltage gamma control (14 bytes)
|
|
}
|
|
|
|
// New creates a new ST7789 connection. The SPI wire must already be configured.
|
|
//func New(bus spi.Conn, resetPin, dcPin, csPin, blPin gpio.PinOut) *Device {
|
|
// return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
|
|
//}
|
|
|
|
// NewOf creates a new ST7789 connection with a particular pixel format. The SPI
|
|
// wire must already be configured.
|
|
func New[T Color](bus spi.Conn, reset, dc, cs, bl gpio.PinOut) *DeviceOf[T] {
|
|
return &DeviceOf[T]{
|
|
log: logrus.WithFields(logrus.Fields{
|
|
"display": "st7789",
|
|
"pin_reset": reset,
|
|
"pin_dc": dc,
|
|
"pin_cs": cs,
|
|
"pin_bl": bl,
|
|
}),
|
|
bus: bus,
|
|
dcPin: dc,
|
|
resetPin: reset,
|
|
csPin: cs,
|
|
blPin: bl,
|
|
}
|
|
}
|
|
|
|
// Configure initializes the display with default configuration
|
|
func (d *DeviceOf[T]) Configure(cfg Config) (err error) {
|
|
start := time.Now()
|
|
d.log.Trace("start display configuration")
|
|
defer func() {
|
|
ago := time.Since(start)
|
|
d.log.WithField("duration", ago).Trace("ended display configuration")
|
|
}()
|
|
|
|
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 = FRAMERATE_60
|
|
}
|
|
d.log.Tracef("%d frame rate", d.frameRate)
|
|
|
|
if cfg.VSyncLines >= 2 && cfg.VSyncLines <= 254 {
|
|
d.vSyncLines = cfg.VSyncLines
|
|
} else {
|
|
d.vSyncLines = 16
|
|
}
|
|
d.log.Tracef("%d vsync lines", d.vSyncLines)
|
|
|
|
d.batchLength = int32(d.width)
|
|
if d.height > d.width {
|
|
d.batchLength = int32(d.height)
|
|
}
|
|
d.batchLength += d.batchLength & 1
|
|
d.log.Tracef("%d batch length", d.batchLength)
|
|
|
|
// Reset the device
|
|
d.log.Debug("reset device")
|
|
if err = d.resetPin.Out(gpio.High); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
if err = d.resetPin.Out(gpio.Low); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
if err = d.resetPin.Out(gpio.High); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
// Common initialization
|
|
d.startWrite()
|
|
d.sendCommand(SWRESET, nil) // Soft reset
|
|
d.endWrite()
|
|
time.Sleep(150 * time.Millisecond) //
|
|
d.startWrite()
|
|
|
|
d.sendCommand(SLPOUT, nil) // Exit sleep mode
|
|
|
|
// Memory initialization
|
|
var zeroColor T
|
|
switch any(zeroColor).(type) {
|
|
case pixel.RGB444BE:
|
|
d.setColorFormat(ColorRGB444) // 12 bits per pixel
|
|
default:
|
|
// Use default RGB565 color format.
|
|
d.setColorFormat(ColorRGB565) // 16 bits per pixel
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
d.setRotation(d.rotation) // Memory orientation
|
|
|
|
d.setWindow(0, 0, d.width, d.height) // Full draw window
|
|
var black T
|
|
d.fillScreen(black) // Clear screen
|
|
|
|
// Framerate
|
|
d.sendCommand(FRCTRL2, []byte{byte(d.frameRate)}) // Frame rate for normal mode (default 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.sendCommand(PORCTRL, []byte{
|
|
bp, // Back porch 5bit (0x7F max 0x08 default)
|
|
fp, // Front porch 5bit (0x7F max 0x08 default)
|
|
0x00, // Seprarate porch (TODO: what is this?)
|
|
0x22, // Idle mode porch (4bit-back 4bit-front 0x22 default)
|
|
0x22, // Partial mode porch (4bit-back 4bit-front 0x22 default)
|
|
})
|
|
|
|
// Ready to display
|
|
d.sendCommand(INVON, nil) // Inversion ON
|
|
time.Sleep(10 * time.Millisecond) //
|
|
|
|
// Set gamma tables, if configured.
|
|
if len(cfg.PVGAMCTRL) == 14 {
|
|
d.sendCommand(GMCTRP1, cfg.PVGAMCTRL) // PVGAMCTRL: Positive Voltage Gamma Control
|
|
}
|
|
if len(cfg.NVGAMCTRL) == 14 {
|
|
d.sendCommand(GMCTRN1, cfg.NVGAMCTRL) // NVGAMCTRL: Negative Voltage Gamma Control
|
|
}
|
|
|
|
// Normal mode ON
|
|
if err = d.sendCommand(NORON, nil); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
// Screen ON
|
|
if err = d.sendCommand(DISPON, nil); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
if err = d.endWrite(); err != nil {
|
|
return
|
|
}
|
|
|
|
// Backlight ON
|
|
if err = d.blPin.Out(gpio.High); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Send a command with data to the display. It does not change the chip select
|
|
// pin (it must be low when calling). The DC pin is left high after return,
|
|
// meaning that data can be sent right away.
|
|
func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) (err error) {
|
|
//d.log.WithFields(logrus.Fields{"command": command, "data": data}).Trace("send command")
|
|
d.cmdBuf[0] = command
|
|
if hardware.IsValidPinOut(d.dcPin) {
|
|
if err = d.dcPin.Out(gpio.Low); err != nil {
|
|
return
|
|
}
|
|
}
|
|
if err = d.bus.Tx(d.cmdBuf[:1], nil); err != nil {
|
|
return
|
|
}
|
|
if hardware.IsValidPinOut(d.dcPin) {
|
|
if err = d.dcPin.Out(gpio.High); err != nil {
|
|
return
|
|
}
|
|
}
|
|
if len(data) != 0 {
|
|
err = d.bus.Tx(data, nil)
|
|
}
|
|
return
|
|
}
|
|
|
|
// startWrite must be called at the beginning of all exported methods to set the
|
|
// chip select pin low.
|
|
func (d *DeviceOf[T]) startWrite() error {
|
|
if hardware.IsValidPinOut(d.csPin) {
|
|
return d.csPin.Out(gpio.Low)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// endWrite must be called at the end of all exported methods to set the chip
|
|
// select pin high.
|
|
func (d *DeviceOf[T]) endWrite() error {
|
|
if hardware.IsValidPinOut(d.csPin) {
|
|
return d.csPin.Out(gpio.High)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// getBuffer returns the image buffer, that's always d.batchLength wide and 1
|
|
// pixel high. It can be used as a temporary buffer to transmit image data.
|
|
func (d *DeviceOf[T]) getBuffer() pixel.Image[T] {
|
|
if d.batchData.Len() == 0 {
|
|
d.batchData = pixel.NewImage[T](int(d.batchLength), 1)
|
|
}
|
|
return d.batchData
|
|
}
|
|
|
|
// Sync waits for the display to hit the next VSYNC pause
|
|
func (d *DeviceOf[T]) 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 *DeviceOf[T]) 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 *DeviceOf[T]) GetScanLine() uint16 {
|
|
d.startWrite()
|
|
data := []uint8{0x00, 0x00}
|
|
if hardware.IsValidPinOut(d.dcPin) {
|
|
d.dcPin.Out(gpio.Low)
|
|
}
|
|
//d.bus.Transfer(GSCAN)
|
|
d.bus.Tx([]byte{GSCAN}, nil)
|
|
if hardware.IsValidPinOut(d.dcPin) {
|
|
d.dcPin.Out(gpio.High)
|
|
}
|
|
/*
|
|
for i := range data {
|
|
data[i], _ = d.bus.Transfer(0xFF)
|
|
}
|
|
*/
|
|
d.bus.Tx(data, data)
|
|
scanline := uint16(data[0])<<8 + uint16(data[1])
|
|
d.endWrite()
|
|
return scanline
|
|
}
|
|
|
|
// GetHighestScanLine calculates the last scanline id in the frame before VSYNC pause
|
|
func (d *DeviceOf[T]) 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 *DeviceOf[T]) GetLowestScanLine() uint16 {
|
|
// First scanline id appears to be backporch/2 + 1
|
|
return uint16(math.Ceil(float64(d.vSyncLines)/2)/2) + 1
|
|
}
|
|
|
|
// Display does nothing, there's no buffer as it might be too big for some boards
|
|
func (d *DeviceOf[T]) Display() error {
|
|
return nil
|
|
}
|
|
|
|
// SetPixel sets a pixel in the screen
|
|
// func (d *DeviceOf[T]) SetPixel(x int16, y int16, c color.RGBA) {
|
|
func (d *DeviceOf[T]) SetPixel(x int16, y int16, c T) {
|
|
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 *DeviceOf[T]) setWindow(x, y, w, h int16) {
|
|
//d.log.WithFields(logrus.Fields{"x": x, "y": y, "w": w, "h": h}).Debug("updating drawing window")
|
|
x += d.columnOffset
|
|
y += d.rowOffset
|
|
copy(d.buf[:4], []uint8{uint8(x >> 8), uint8(x), uint8((x + w - 1) >> 8), uint8(x + w - 1)})
|
|
d.sendCommand(CASET, d.buf[:4])
|
|
copy(d.buf[:4], []uint8{uint8(y >> 8), uint8(y), uint8((y + h - 1) >> 8), uint8(y + h - 1)})
|
|
d.sendCommand(RASET, d.buf[:4])
|
|
d.sendCommand(RAMWR, nil)
|
|
}
|
|
|
|
// FillRectangle fills a rectangle at a given coordinates with a color
|
|
// func (d *DeviceOf[T]) FillRectangle(x, y, width, height int16, c color.RGBA) error {
|
|
func (d *DeviceOf[T]) FillRectangle(x, y, width, height int16, c T) error {
|
|
d.startWrite()
|
|
err := d.fillRectangle(x, y, width, height, c)
|
|
d.endWrite()
|
|
return err
|
|
}
|
|
|
|
// func (d *DeviceOf[T]) fillRectangle(x, y, width, height int16, c color.RGBA) error {
|
|
func (d *DeviceOf[T]) fillRectangle(x, y, width, height int16, c T) error {
|
|
k, i := d.Size16()
|
|
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)
|
|
|
|
image := d.getBuffer()
|
|
image.FillSolidColor(c)
|
|
//image.FillSolidColor(pixel.NewColor[T](c.R, c.G, c.B))
|
|
j := int(width) * int(height)
|
|
for j > 0 {
|
|
// The DC pin is already set to data in the setWindow call, so we can
|
|
// just write bytes on the SPI bus.
|
|
if j >= image.Len() {
|
|
d.bus.Tx(image.RawBuffer(), nil)
|
|
} else {
|
|
d.bus.Tx(image.Rescale(j, 1).RawBuffer(), nil)
|
|
}
|
|
j -= image.Len()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DrawRGBBitmap8 copies an RGB bitmap to the internal buffer at given coordinates
|
|
//
|
|
// Deprecated: use DrawBitmap instead.
|
|
func (d *DeviceOf[T]) DrawRGBBitmap8(x, y int16, data []uint8, w, h int16) error {
|
|
k, i := d.Size16()
|
|
if x < 0 || y < 0 || w <= 0 || h <= 0 ||
|
|
x >= k || (x+w) > k || y >= i || (y+h) > i {
|
|
return errOutOfBounds
|
|
}
|
|
d.startWrite()
|
|
d.setWindow(x, y, w, h)
|
|
d.bus.Tx(data, nil)
|
|
d.endWrite()
|
|
return nil
|
|
}
|
|
|
|
// DrawBitmap copies the bitmap to the internal buffer on the screen at the
|
|
// given coordinates. It returns once the image data has been sent completely.
|
|
func (d *DeviceOf[T]) DrawBitmap(x, y int16, bitmap pixel.Image[T]) error {
|
|
width, height := bitmap.Size()
|
|
return d.DrawRGBBitmap8(x, y, bitmap.RawBuffer(), int16(width), int16(height))
|
|
}
|
|
|
|
// FillRectangleWithBuffer fills buffer with a rectangle at a given coordinates.
|
|
func (d *DeviceOf[T]) FillRectangleWithBuffer(x, y, width, height int16, buffer []color.RGBA) error {
|
|
i, j := d.Size16()
|
|
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.startWrite()
|
|
d.setWindow(x, y, width, height)
|
|
|
|
k := int(width) * int(height)
|
|
image := d.getBuffer()
|
|
offset := 0
|
|
for k > 0 {
|
|
for i := 0; i < image.Len(); i++ {
|
|
if offset+i < len(buffer) {
|
|
c := buffer[offset+i]
|
|
image.Set(i, 0, pixel.NewColor[T](c.R, c.G, c.B))
|
|
}
|
|
}
|
|
// The DC pin is already set to data in the setWindow call, so we don't
|
|
// have to set it here.
|
|
if k >= image.Len() {
|
|
d.bus.Tx(image.RawBuffer(), nil)
|
|
} else {
|
|
d.bus.Tx(image.Rescale(k, 1).RawBuffer(), nil)
|
|
}
|
|
k -= image.Len()
|
|
offset += image.Len()
|
|
}
|
|
d.endWrite()
|
|
return nil
|
|
}
|
|
|
|
// DrawFastVLine draws a vertical line faster than using SetPixel
|
|
func (d *DeviceOf[T]) DrawFastVLine(x, y0, y1 int16, c T) {
|
|
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 *DeviceOf[T]) DrawFastHLine(x0, x1, y int16, c T) {
|
|
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 *DeviceOf[T]) FillScreen(c T) {
|
|
d.startWrite()
|
|
d.fillScreen(c)
|
|
d.endWrite()
|
|
}
|
|
|
|
func (d *DeviceOf[T]) fillScreen(c T) {
|
|
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
|
|
d.fillRectangle(0, 0, d.width, d.height, c)
|
|
} else {
|
|
d.fillRectangle(0, 0, d.height, d.width, c)
|
|
}
|
|
}
|
|
|
|
// Control the color format that is used when writing to the screen.
|
|
// The default is RGB565, setting it to any other value will break functions
|
|
// like SetPixel, FillRectangle, etc. Instead, you can write color data in the
|
|
// specified color format using DrawRGBBitmap8.
|
|
func (d *DeviceOf[T]) SetColorFormat(format ColorFormat) {
|
|
d.startWrite()
|
|
d.setColorFormat(format)
|
|
d.endWrite()
|
|
}
|
|
|
|
func (d *DeviceOf[T]) setColorFormat(format ColorFormat) {
|
|
// Lower 4 bits set the color format used in SPI.
|
|
// Upper 4 bits set the color format used in the direct RGB interface.
|
|
// The RGB interface is not currently supported, so it is left at a
|
|
// reasonable default. Also, the RGB interface doesn't support RGB444.
|
|
colmod := byte(format) | 0x50
|
|
d.sendCommand(COLMOD, []byte{colmod})
|
|
}
|
|
|
|
// Rotation returns the current rotation of the device.
|
|
func (d *DeviceOf[T]) Rotation() display.Rotation {
|
|
return d.rotation
|
|
}
|
|
|
|
// SetRotation changes the rotation of the device (clock-wise)
|
|
func (d *DeviceOf[T]) SetRotation(rotation Rotation) error {
|
|
d.rotation = rotation
|
|
d.startWrite()
|
|
err := d.setRotation(rotation)
|
|
d.endWrite()
|
|
return err
|
|
}
|
|
|
|
func (d *DeviceOf[T]) setRotation(rotation Rotation) error {
|
|
madctl := uint8(0)
|
|
switch rotation % 4 {
|
|
case display.NoRotation:
|
|
d.rowOffset = 0
|
|
d.columnOffset = 0
|
|
case display.Rotate90:
|
|
madctl = MADCTL_MX | MADCTL_MV
|
|
d.rowOffset = 0
|
|
d.columnOffset = 0
|
|
case display.Rotate180:
|
|
madctl = MADCTL_MX | MADCTL_MY
|
|
d.rowOffset = d.rowOffsetCfg
|
|
d.columnOffset = d.columnOffsetCfg
|
|
case display.Rotate270:
|
|
madctl = MADCTL_MY | MADCTL_MV
|
|
d.rowOffset = d.columnOffsetCfg
|
|
d.columnOffset = d.rowOffsetCfg
|
|
}
|
|
if d.isBGR {
|
|
madctl |= MADCTL_BGR
|
|
}
|
|
return d.sendCommand(MADCTL, []byte{madctl})
|
|
}
|
|
|
|
// Size returns the current size of the display.
|
|
func (d *DeviceOf[T]) 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))
|
|
}
|
|
|
|
// Sizes returns the current size of the display.
|
|
func (d *DeviceOf[T]) Size16() (int16, 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 *DeviceOf[T]) EnableBacklight(enable bool) {
|
|
if enable {
|
|
d.blPin.Out(gpio.High)
|
|
} else {
|
|
d.blPin.Out(gpio.Low)
|
|
}
|
|
}
|
|
|
|
// Set the sleep mode for this LCD panel. When sleeping, the panel uses a lot
|
|
// less power. The LCD won't display an image anymore, but the memory contents
|
|
// will be kept.
|
|
func (d *DeviceOf[T]) Sleep(sleepEnabled bool) error {
|
|
if sleepEnabled {
|
|
d.startWrite()
|
|
d.sendCommand(SLPIN, nil)
|
|
d.endWrite()
|
|
time.Sleep(5 * time.Millisecond) // 5ms required by the datasheet
|
|
} else {
|
|
// Turn the LCD panel back on.
|
|
d.startWrite()
|
|
d.sendCommand(SLPOUT, nil)
|
|
d.endWrite()
|
|
// Note: the st7789 documentation says that it is needed to wait at
|
|
// least 120ms before going to sleep again. Sleeping here would not be
|
|
// practical (delays turning on the screen too much), so just hope the
|
|
// screen won't need to sleep again for at least 120ms.
|
|
// In practice, it's unlikely the user will set the display to sleep
|
|
// again within 120ms.
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// InvertColors inverts the colors of the screen
|
|
func (d *DeviceOf[T]) InvertColors(invert bool) {
|
|
d.startWrite()
|
|
if invert {
|
|
d.sendCommand(INVON, nil)
|
|
} else {
|
|
d.sendCommand(INVOFF, nil)
|
|
}
|
|
d.endWrite()
|
|
}
|
|
|
|
// IsBGR changes the color mode (RGB/BGR)
|
|
func (d *DeviceOf[T]) IsBGR(bgr bool) {
|
|
d.isBGR = bgr
|
|
}
|
|
|
|
// SetScrollArea sets an area to scroll with fixed top and bottom parts of the display.
|
|
func (d *DeviceOf[T]) SetScrollArea(topFixedArea, bottomFixedArea int16) {
|
|
if d.height < 320 {
|
|
// The screen doesn't use the full 320 pixel height.
|
|
// Enlarge the bottom fixed area to fill the 320 pixel height, so that
|
|
// bottomFixedArea starts from the visible bottom of the screen.
|
|
topFixedArea += d.rowOffset
|
|
bottomFixedArea += (320 - d.height) - d.rowOffset
|
|
}
|
|
if d.rotation == display.Rotate180 {
|
|
// The screen is rotated by 180°, so we have to switch the top and
|
|
// bottom fixed area.
|
|
topFixedArea, bottomFixedArea = bottomFixedArea, topFixedArea
|
|
}
|
|
verticalScrollArea := 320 - topFixedArea - bottomFixedArea
|
|
copy(d.buf[:6], []uint8{
|
|
uint8(topFixedArea >> 8), uint8(topFixedArea),
|
|
uint8(verticalScrollArea >> 8), uint8(verticalScrollArea),
|
|
uint8(bottomFixedArea >> 8), uint8(bottomFixedArea)})
|
|
d.startWrite()
|
|
d.sendCommand(VSCRDEF, d.buf[:6])
|
|
d.endWrite()
|
|
}
|
|
|
|
// SetScroll sets the vertical scroll address of the display.
|
|
func (d *DeviceOf[T]) SetScroll(line int16) {
|
|
if d.rotation == display.Rotate180 {
|
|
// The screen is rotated by 180°, so we have to invert the scroll line
|
|
// (taking care of the RowOffset).
|
|
line = (319 - d.rowOffset) - line
|
|
}
|
|
d.buf[0] = uint8(line >> 8)
|
|
d.buf[1] = uint8(line)
|
|
d.startWrite()
|
|
d.sendCommand(VSCRSADD, d.buf[:2])
|
|
d.endWrite()
|
|
}
|
|
|
|
// StopScroll returns the display to its normal state.
|
|
func (d *DeviceOf[T]) StopScroll() {
|
|
d.startWrite()
|
|
d.sendCommand(NORON, nil)
|
|
d.endWrite()
|
|
}
|
|
|
|
func (d *DeviceOf[T]) At(x, y int) color.Color {
|
|
return color.Black
|
|
}
|
|
|
|
func (d *DeviceOf[T]) 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 *DeviceOf[T]) ColorModel() color.Model {
|
|
return color.RGBAModel
|
|
}
|
|
|
|
func (d *DeviceOf[T]) Set(x, y int, c color.Color) {
|
|
c = pixel.FromColor[T](c)
|
|
d.FillRectangle(int16(x), int16(y), 1, 1, c.(T))
|
|
}
|
|
|
|
func (d *DeviceOf[T]) Halt() error {
|
|
// Backlight off
|
|
return d.blPin.Out(gpio.Low)
|
|
}
|
|
|
|
func (DeviceOf[T]) Refresh() error {
|
|
return nil
|
|
}
|
|
|
|
func (DeviceOf[T]) String() string {
|
|
return "st7789"
|
|
}
|
|
|
|
func (d *DeviceOf[T]) DrawImage(img image.Image) error {
|
|
d.setWindow(0, 0, d.width, d.height)
|
|
|
|
var np []uint8
|
|
for y := 0; y < int(d.height); y++ {
|
|
for x := 0; x < int(d.width); x++ {
|
|
r, g, b, _ := img.At(x, y).RGBA()
|
|
c := uint16((b & 0xF800) + ((g & 0xFC00) >> 5) + ((r & 0xF800) >> 11))
|
|
np = append(np, uint8(c), uint8(c>>8))
|
|
}
|
|
}
|
|
|
|
// DC is active from setWindow call
|
|
stride := int(d.batchLength) << 1
|
|
for i := 0; i < len(np); i += stride {
|
|
if err := d.bus.Tx(np[i:i+stride], nil); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *DeviceOf[T]) DrawImageAt(x, y int, img image.Image) error {
|
|
var (
|
|
rect = img.Bounds()
|
|
size = rect.Size()
|
|
orig = image.Pt(x, y)
|
|
view = image.Rectangle{
|
|
Min: orig,
|
|
Max: orig.Add(size),
|
|
}
|
|
)
|
|
|
|
//view = view.Intersect()
|
|
|
|
if view.Max.X > int(d.width) {
|
|
view.Max.X = int(d.width)
|
|
}
|
|
if view.Max.X <= view.Min.X {
|
|
return nil // nothing to do here
|
|
}
|
|
if view.Max.Y > int(d.height) {
|
|
view.Max.Y = int(d.height)
|
|
}
|
|
|
|
d.setWindow(int16(x), int16(y), int16(view.Dx()), int16(view.Dy()))
|
|
return nil
|
|
}
|
|
|
|
// bgr565 converts a color to uint16
|
|
func bgr565(c color.Color) uint16 {
|
|
r, g, b, _ := c.RGBA()
|
|
return uint16((b & 0xF800) +
|
|
((g & 0xFC00) >> 5) +
|
|
((r & 0xF800) >> 11))
|
|
}
|
|
|
|
// rgbaTo565 converts a color.RGBA to uint16 used in the display (bits r:5, g:6, b:5)
|
|
func rgbaTo565(c color.RGBA) uint16 {
|
|
r, g, b, _ := c.RGBA()
|
|
return uint16((r & 0xF800) +
|
|
((g & 0xFC00) >> 5) +
|
|
((b & 0xF800) >> 11))
|
|
}
|
|
|
|
var _ display.Display = (*Device)(nil)
|