From 3bf64607ad45b25866a04f59ebc4352689146233 Mon Sep 17 00:00:00 2001 From: Alex Leung Date: Sun, 30 Mar 2025 21:58:36 +0800 Subject: [PATCH 1/4] Add 2 arguments: 1. bgr: bool, support for choosing bgr or rgb pixel order. 2. invert: bool, support for sending color inverse command. --- README.rst | 2 +- adafruit_st7789.py | 22 +++++++++++++++++----- examples/st7789_simpletest.py | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 1c465be..fa72f68 100644 --- a/README.rst +++ b/README.rst @@ -57,7 +57,7 @@ Usage Example display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9) - display = ST7789(display_bus, width=240, height=240, rowstart=80) + display = ST7789(display_bus, width=240, height=240, rowstart=80, bgr=True, invert=True) # Make the display context splash = displayio.Group() diff --git a/adafruit_st7789.py b/adafruit_st7789.py index 6a2ce7a..959f4c3 100755 --- a/adafruit_st7789.py +++ b/adafruit_st7789.py @@ -57,7 +57,6 @@ b"\x11\x80\xff" # _SLPOUT and Delay 500ms b"\x3a\x81\x55\x0a" # _COLMOD and Delay 10ms b"\x36\x01\x08" # _MADCTL - b"\x21\x80\x0a" # _INVON Hack and Delay 10ms b"\x13\x80\x0a" # _NORON and Delay 10ms b"\x36\x01\xc0" # _MADCTL b"\x29\x80\xff" # _DISPON and Delay 500ms @@ -66,7 +65,20 @@ # pylint: disable=too-few-public-methods class ST7789(BusDisplay): - """ST7789 driver""" - - def __init__(self, bus: FourWire, **kwargs) -> None: - super().__init__(bus, _INIT_SEQUENCE, **kwargs) + """ + ST7789 driver + + :param FourWire bus: bus that the display is connected to + :param bool bgr: (Optional) An extra init sequence to append (default=False) + :param bool invert: (Optional) Invert the colors (default=False) + """ + + def __init__(self, bus: FourWire, *, bgr: bool = False, invert: bool = False, **kwargs: Any): + init_sequence = _INIT_SEQUENCE + if bgr: + init_sequence += b"\x36\x01\xc0" # _MADCTL Default rotation plus BGR encoding + else: + init_sequence += b"\x36\x01\xc8" # _MADCTL Default rotation plus RGB encoding + if invert: + init_sequence += b"\x21\x00" # _INVON + super().__init__(bus, init_sequence, **kwargs) \ No newline at end of file diff --git a/examples/st7789_simpletest.py b/examples/st7789_simpletest.py index bfeb998..4d48e12 100644 --- a/examples/st7789_simpletest.py +++ b/examples/st7789_simpletest.py @@ -23,7 +23,7 @@ display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9) -display = ST7789(display_bus, width=240, height=240, rowstart=80) +display = ST7789(display_bus, width=240, height=240, rowstart=80, bgr=True, invert=True) # Make the display context splash = displayio.Group() From f08e3643f541eb16fef29e60011c6669f68ffe58 Mon Sep 17 00:00:00 2001 From: Alex Leung Date: Thu, 3 Apr 2025 00:08:26 +0800 Subject: [PATCH 2/4] Change the defualt value of Modify the default values of the __init__() parameters to make it compatible with the behavior of old code. --- adafruit_st7789.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/adafruit_st7789.py b/adafruit_st7789.py index 959f4c3..0042647 100755 --- a/adafruit_st7789.py +++ b/adafruit_st7789.py @@ -43,7 +43,7 @@ from busdisplay import BusDisplay try: - import typing + from typing import Any from fourwire import FourWire except ImportError: @@ -58,7 +58,6 @@ b"\x3a\x81\x55\x0a" # _COLMOD and Delay 10ms b"\x36\x01\x08" # _MADCTL b"\x13\x80\x0a" # _NORON and Delay 10ms - b"\x36\x01\xc0" # _MADCTL b"\x29\x80\xff" # _DISPON and Delay 500ms ) @@ -66,14 +65,14 @@ # pylint: disable=too-few-public-methods class ST7789(BusDisplay): """ - ST7789 driver + ST7789 driver - :param FourWire bus: bus that the display is connected to - :param bool bgr: (Optional) An extra init sequence to append (default=False) - :param bool invert: (Optional) Invert the colors (default=False) - """ + :param FourWire bus: bus that the display is connected to + :param bool bgr: (Optional) An extra init sequence to append (default=True) + :param bool invert: (Optional) Invert the colors (default=True) + """ - def __init__(self, bus: FourWire, *, bgr: bool = False, invert: bool = False, **kwargs: Any): + def __init__(self, bus: FourWire, *, bgr: bool = True, invert: bool = True, **kwargs: Any): init_sequence = _INIT_SEQUENCE if bgr: init_sequence += b"\x36\x01\xc0" # _MADCTL Default rotation plus BGR encoding From 2304e3aabadae1c8cd36586af55c48bbef15897b Mon Sep 17 00:00:00 2001 From: Alex Leung Date: Thu, 3 Apr 2025 00:30:35 +0800 Subject: [PATCH 3/4] Reformatted. --- adafruit_st7789.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/adafruit_st7789.py b/adafruit_st7789.py index 0042647..3c8019d 100755 --- a/adafruit_st7789.py +++ b/adafruit_st7789.py @@ -48,7 +48,6 @@ from fourwire import FourWire except ImportError: pass - __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ST7789.git" @@ -65,19 +64,25 @@ # pylint: disable=too-few-public-methods class ST7789(BusDisplay): """ - ST7789 driver - - :param FourWire bus: bus that the display is connected to + ST7789 driver + + :param FourWire bus: bus that the display is connected to :param bool bgr: (Optional) An extra init sequence to append (default=True) :param bool invert: (Optional) Invert the colors (default=True) """ - def __init__(self, bus: FourWire, *, bgr: bool = True, invert: bool = True, **kwargs: Any): + def __init__( + self, bus: FourWire, *, bgr: bool = True, invert: bool = True, **kwargs: Any + ): init_sequence = _INIT_SEQUENCE if bgr: - init_sequence += b"\x36\x01\xc0" # _MADCTL Default rotation plus BGR encoding + init_sequence += ( + b"\x36\x01\xc0" # _MADCTL Default rotation plus BGR encoding + ) else: - init_sequence += b"\x36\x01\xc8" # _MADCTL Default rotation plus RGB encoding + init_sequence += ( + b"\x36\x01\xc8" # _MADCTL Default rotation plus RGB encoding + ) if invert: init_sequence += b"\x21\x00" # _INVON - super().__init__(bus, init_sequence, **kwargs) \ No newline at end of file + super().__init__(bus, init_sequence, **kwargs) From 2b3fb8377fcc074ac7f66761698e46506386ab1d Mon Sep 17 00:00:00 2001 From: Alex Leung Date: Thu, 3 Apr 2025 00:58:00 +0800 Subject: [PATCH 4/4] ruff fixed. --- adafruit_st7789.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/adafruit_st7789.py b/adafruit_st7789.py index 3c8019d..8c448c7 100755 --- a/adafruit_st7789.py +++ b/adafruit_st7789.py @@ -71,18 +71,12 @@ class ST7789(BusDisplay): :param bool invert: (Optional) Invert the colors (default=True) """ - def __init__( - self, bus: FourWire, *, bgr: bool = True, invert: bool = True, **kwargs: Any - ): + def __init__(self, bus: FourWire, *, bgr: bool = True, invert: bool = True, **kwargs: Any): init_sequence = _INIT_SEQUENCE if bgr: - init_sequence += ( - b"\x36\x01\xc0" # _MADCTL Default rotation plus BGR encoding - ) + init_sequence += b"\x36\x01\xc0" # _MADCTL Default rotation plus BGR encoding else: - init_sequence += ( - b"\x36\x01\xc8" # _MADCTL Default rotation plus RGB encoding - ) + init_sequence += b"\x36\x01\xc8" # _MADCTL Default rotation plus RGB encoding if invert: init_sequence += b"\x21\x00" # _INVON super().__init__(bus, init_sequence, **kwargs)