From 42e4d2915774959b3cdfe593f89f796cf3d54a00 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 9 May 2023 12:10:52 +0200 Subject: [PATCH] st7789: allow changing the color format using COLMOD This is only really useful for applications that use DrawRGBBitmap8. The RGB444 format has fewer colors, but can be up to 25% faster than the default RGB565. I have tested this change on the Gopher Badge and the PineTime. Both can be substantially faster once the code is modified to output RGB444 instead of RGB565. --- st7789/registers.go | 4 ++++ st7789/st7789.go | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/st7789/registers.go b/st7789/registers.go index ab1c4d8..9e7818e 100644 --- a/st7789/registers.go +++ b/st7789/registers.go @@ -55,6 +55,10 @@ const ( VSCRDEF = 0x33 VSCRSADD = 0x37 + ColorRGB444 ColorFormat = 0b011 + ColorRGB565 ColorFormat = 0b101 + ColorRGB666 ColorFormat = 0b111 + NO_ROTATION = drivers.Rotation0 ROTATION_90 = drivers.Rotation90 // 90 degrees clock-wise rotation ROTATION_180 = drivers.Rotation180 diff --git a/st7789/st7789.go b/st7789/st7789.go index 460cfa5..8a145e4 100644 --- a/st7789/st7789.go +++ b/st7789/st7789.go @@ -21,6 +21,9 @@ import ( // Deprecated: use drivers.Rotation instead. type Rotation = drivers.Rotation +// The color format used on the display, like RGB565, RGB666, and RGB444. +type ColorFormat uint8 + // FrameRate controls the frame rate used by the display. type FrameRate uint8 @@ -134,7 +137,7 @@ func (d *Device) Configure(cfg Config) { d.sendCommand(SLPOUT, nil) // Exit sleep mode // Memory initialization - d.sendCommand(COLMOD, []byte{0x55}) // Set color mode to 16-bit color + d.setColorFormat(ColorRGB565) // Set color mode to 16-bit color time.Sleep(10 * time.Millisecond) d.setRotation(d.rotation) // Memory orientation @@ -434,6 +437,25 @@ func (d *Device) fillScreen(c color.RGBA) { } } +// 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 *Device) SetColorFormat(format ColorFormat) { + d.startWrite() + d.setColorFormat(format) + d.endWrite() +} + +func (d *Device) 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 *Device) Rotation() drivers.Rotation { return d.rotation