2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Sitronix ST7789 display driver
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2020-03-08 21:47:19 +01:00
|
|
|
|
2020-05-14 22:41:02 +02:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Although the ST7789 supports a variety of communication protocols currently
|
|
|
|
this driver only has support for SPI interfaces. However it is structured
|
|
|
|
such that other serial protocols can easily be added.
|
2020-03-08 21:47:19 +01:00
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
|
2020-01-31 20:36:55 +01:00
|
|
|
import micropython
|
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
from micropython import const
|
|
|
|
from time import sleep_ms
|
|
|
|
|
|
|
|
# register definitions
|
|
|
|
_SWRESET = const(0x01)
|
2020-02-01 21:20:30 +01:00
|
|
|
_SLPIN = const(0x10)
|
2020-01-23 19:59:43 +01:00
|
|
|
_SLPOUT = const(0x11)
|
|
|
|
_NORON = const(0x13)
|
|
|
|
_INVOFF = const(0x20)
|
|
|
|
_INVON = const(0x21)
|
2020-03-08 21:47:19 +01:00
|
|
|
_DISPOFF = const(0x28)
|
2020-01-23 19:59:43 +01:00
|
|
|
_DISPON = const(0x29)
|
|
|
|
_CASET = const(0x2a)
|
|
|
|
_RASET = const(0x2b)
|
|
|
|
_RAMWR = const(0x2c)
|
|
|
|
_COLMOD = const(0x3a)
|
|
|
|
_MADCTL = const(0x36)
|
2020-01-21 23:10:50 +01:00
|
|
|
|
|
|
|
class ST7789(object):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Sitronix ST7789 display driver
|
|
|
|
|
|
|
|
.. automethod:: __init__
|
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
def __init__(self, width, height):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Configure the size of the display.
|
|
|
|
|
|
|
|
:param int width: Display width, in pixels
|
|
|
|
:param int height: Display height in pixels
|
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.linebuffer = bytearray(2 * width)
|
|
|
|
self.init_display()
|
2020-01-21 23:10:50 +01:00
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
def init_display(self):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Reset and initialize the display."""
|
2020-01-21 23:10:50 +01:00
|
|
|
self.reset()
|
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
self.write_cmd(_SLPOUT)
|
|
|
|
sleep_ms(10)
|
|
|
|
|
|
|
|
for cmd in (
|
|
|
|
(_COLMOD, b'\x05'), # MCU will send 16-bit RGB565
|
|
|
|
(_MADCTL, b'\x00'), # Left to right, top to bottom
|
|
|
|
#(_INVOFF, None), # Results in odd palette
|
|
|
|
(_INVON, None),
|
|
|
|
(_NORON, None),
|
|
|
|
):
|
|
|
|
self.write_cmd(cmd[0])
|
|
|
|
if cmd[1]:
|
|
|
|
self.write_data(cmd[1])
|
|
|
|
self.fill(0)
|
|
|
|
self.write_cmd(_DISPON)
|
2020-01-28 22:19:36 +01:00
|
|
|
|
|
|
|
# From the point we sent the SLPOUT there must be a
|
|
|
|
# 120ms gap before any subsequent SLPIN. In most cases
|
2020-05-14 22:41:02 +02:00
|
|
|
# (i.e. when the SPI baud rate is slower than 8M then
|
2020-01-28 22:19:36 +01:00
|
|
|
# that time already elapsed as we zeroed the RAM).
|
|
|
|
#sleep_ms(125)
|
2020-01-23 19:59:43 +01:00
|
|
|
|
|
|
|
def poweroff(self):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Put the display into sleep mode."""
|
2020-02-01 21:20:30 +01:00
|
|
|
self.write_cmd(_SLPIN)
|
|
|
|
sleep_ms(125)
|
2020-01-23 19:59:43 +01:00
|
|
|
|
|
|
|
def poweron(self):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Wake the display and leave sleep mode."""
|
2020-02-01 21:20:30 +01:00
|
|
|
self.write_cmd(_SLPOUT)
|
|
|
|
sleep_ms(125)
|
2020-01-23 19:59:43 +01:00
|
|
|
|
|
|
|
def invert(self, invert):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Invert the display.
|
|
|
|
|
|
|
|
:param bool invert: True to invert the display, False for normal mode.
|
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
if invert:
|
|
|
|
self.write_cmd(_INVON)
|
2020-01-21 23:10:50 +01:00
|
|
|
else:
|
2020-01-23 19:59:43 +01:00
|
|
|
self.write_cmd(_INVOFF)
|
2020-01-21 23:10:50 +01:00
|
|
|
|
2020-03-08 21:47:19 +01:00
|
|
|
def mute(self, mute):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Mute the display.
|
|
|
|
|
|
|
|
When muted the display will be entirely black.
|
|
|
|
|
|
|
|
:param bool mute: True to mute the display, False for normal mode.
|
|
|
|
"""
|
2020-03-08 21:47:19 +01:00
|
|
|
if mute:
|
|
|
|
self.write_cmd(_DISPOFF)
|
|
|
|
else:
|
|
|
|
self.write_cmd(_DISPON)
|
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
def set_window(self, x=0, y=0, width=None, height=None):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Set the clipping rectangle.
|
|
|
|
|
|
|
|
All writes to the display will be wrapped at the edges of the rectangle.
|
|
|
|
|
|
|
|
:param x: X coordinate of the left-most pixels of the rectangle
|
|
|
|
:param y: Y coordinate of the top-most pixels of the rectangle
|
|
|
|
:param w: Width of the rectangle, defaults to None (which means select
|
|
|
|
the right-most pixel of the display)
|
|
|
|
:param h: Height of the rectangle, defaults to None (which means select
|
|
|
|
the bottom-most pixel of the display)
|
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
if not width:
|
|
|
|
width = self.width
|
|
|
|
if not height:
|
|
|
|
height = self.height
|
2020-01-21 23:10:50 +01:00
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
xp = x + width - 1
|
|
|
|
yp = y + height - 1
|
2020-01-21 23:10:50 +01:00
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
self.write_cmd(_CASET)
|
|
|
|
self.write_data(bytearray([x >> 8, x & 0xff, xp >> 8, xp & 0xff]))
|
|
|
|
self.write_cmd(_RASET)
|
|
|
|
self.write_data(bytearray([y >> 8, y & 0xff, yp >> 8, yp & 0xff]))
|
|
|
|
self.write_cmd(_RAMWR)
|
2020-01-21 23:10:50 +01:00
|
|
|
|
2020-02-19 20:47:51 +01:00
|
|
|
def rawblit(self, buf, x, y, width, height):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Blit raw pixels to the display.
|
|
|
|
|
|
|
|
:param buf: Pixel buffer
|
|
|
|
:param x: X coordinate of the left-most pixels of the rectangle
|
|
|
|
:param y: Y coordinate of the top-most pixels of the rectangle
|
|
|
|
:param w: Width of the rectangle, defaults to None (which means select
|
|
|
|
the right-most pixel of the display)
|
|
|
|
:param h: Height of the rectangle, defaults to None (which means select
|
|
|
|
the bottom-most pixel of the display)
|
|
|
|
"""
|
2020-02-19 20:47:51 +01:00
|
|
|
self.set_window(x, y, width, height)
|
|
|
|
self.write_data(buf)
|
|
|
|
|
2020-02-03 23:34:54 +01:00
|
|
|
def fill(self, bg, x=0, y=0, w=None, h=None):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Draw a solid colour rectangle.
|
|
|
|
|
|
|
|
If no arguments a provided the whole display will be filled with
|
|
|
|
the background colour (typically black).
|
|
|
|
|
|
|
|
:param bg: Background colour (in RGB565 format)
|
|
|
|
:param x: X coordinate of the left-most pixels of the rectangle
|
|
|
|
:param y: Y coordinate of the top-most pixels of the rectangle
|
|
|
|
:param w: Width of the rectangle, defaults to None (which means select
|
|
|
|
the right-most pixel of the display)
|
|
|
|
:param h: Height of the rectangle, defaults to None (which means select
|
|
|
|
the bottom-most pixel of the display)
|
|
|
|
"""
|
2020-02-03 23:34:54 +01:00
|
|
|
if not w:
|
|
|
|
w = self.width - x
|
|
|
|
if not h:
|
|
|
|
h = self.height - y
|
|
|
|
self.set_window(x, y, w, h)
|
2020-01-21 23:10:50 +01:00
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
# Populate the line buffer
|
2020-02-03 23:34:54 +01:00
|
|
|
buf = memoryview(self.linebuffer)[0:2*w]
|
|
|
|
for xi in range(0, 2*w, 2):
|
|
|
|
buf[xi] = bg >> 8
|
|
|
|
buf[xi+1] = bg & 0xff
|
|
|
|
|
|
|
|
# Do the fill
|
|
|
|
for yi in range(h):
|
|
|
|
self.write_data(buf)
|
2020-01-21 23:10:50 +01:00
|
|
|
|
2020-01-23 19:59:43 +01:00
|
|
|
class ST7789_SPI(ST7789):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""
|
|
|
|
.. method:: quick_write(buf)
|
|
|
|
|
|
|
|
Send data to the display as part of an optimized write sequence.
|
|
|
|
|
|
|
|
:param bytearray buf: Data, must be in a form that can be directly
|
|
|
|
consumed by the SPI bus.
|
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
def __init__(self, width, height, spi, cs, dc, res=None, rate=8000000):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Configure the display.
|
|
|
|
|
|
|
|
:param int width: Width of the display
|
|
|
|
:param int height: Height of the display
|
|
|
|
:param machine.SPI spi: SPI controller
|
|
|
|
:param machine.Pin cs: Pin (or signal) to use as the chip select
|
|
|
|
:param machine.Pin cs: Pin (or signal) to use to switch between data
|
|
|
|
and command mode.
|
|
|
|
:param machine.Pin res: Pin (or signal) to, optionally, use to reset
|
|
|
|
the display.
|
|
|
|
:param int rate: SPI bus frequency
|
|
|
|
"""
|
2020-04-08 22:50:42 +02:00
|
|
|
self.quick_write = spi.write
|
|
|
|
self.cs = cs.value
|
|
|
|
self.dc = dc.value
|
2020-01-23 19:59:43 +01:00
|
|
|
self.res = res
|
|
|
|
self.rate = rate
|
|
|
|
|
2020-04-08 22:50:42 +02:00
|
|
|
#spi.init(baudrate=self.rate, polarity=1, phase=1)
|
2020-01-23 19:59:43 +01:00
|
|
|
cs.init(cs.OUT, value=1)
|
|
|
|
dc.init(dc.OUT, value=0)
|
|
|
|
if res:
|
|
|
|
res.init(res.OUT, value=0)
|
|
|
|
|
|
|
|
super().__init__(width, height)
|
|
|
|
|
|
|
|
def reset(self):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Reset the display.
|
|
|
|
|
|
|
|
Uses the hardware reset pin if there is one, otherwise it will issue
|
|
|
|
a software reset command.
|
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
if self.res:
|
|
|
|
self.res(0)
|
|
|
|
sleep_ms(10)
|
|
|
|
self.res(1)
|
|
|
|
else:
|
|
|
|
self.write_cmd(_SWRESET)
|
2020-02-01 21:20:30 +01:00
|
|
|
sleep_ms(125)
|
2020-01-23 19:59:43 +01:00
|
|
|
|
|
|
|
def write_cmd(self, cmd):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Send a command opcode to the display.
|
|
|
|
|
|
|
|
:param sequence cmd: Command, will be automatically converted so it can
|
|
|
|
be issued to the SPI bus.
|
|
|
|
"""
|
2020-04-08 22:50:42 +02:00
|
|
|
dc = self.dc
|
|
|
|
cs = self.cs
|
|
|
|
|
|
|
|
dc(0)
|
|
|
|
cs(0)
|
|
|
|
self.quick_write(bytearray([cmd]))
|
|
|
|
cs(1)
|
|
|
|
dc(1)
|
2020-01-23 19:59:43 +01:00
|
|
|
|
|
|
|
def write_data(self, buf):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Send data to the display.
|
|
|
|
|
|
|
|
:param bytearray buf: Data, must be in a form that can be directly
|
|
|
|
consumed by the SPI bus.
|
|
|
|
"""
|
2020-04-08 22:50:42 +02:00
|
|
|
cs = self.cs
|
|
|
|
cs(0)
|
|
|
|
self.quick_write(buf)
|
|
|
|
cs(1)
|
|
|
|
|
|
|
|
def quick_start(self):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Prepare for an optimized write sequence.
|
|
|
|
|
|
|
|
Optimized write sequences allow applications to produce data in chunks
|
|
|
|
without having any overhead managing the chip select.
|
|
|
|
"""
|
2020-01-23 19:59:43 +01:00
|
|
|
self.cs(0)
|
2020-04-08 22:50:42 +02:00
|
|
|
|
|
|
|
def quick_end(self):
|
2020-05-14 22:41:02 +02:00
|
|
|
"""Complete an optimized write sequence."""
|
2020-01-23 19:59:43 +01:00
|
|
|
self.cs(1)
|