Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![PyPi Package](https://img.shields.io/pypi/v/st7789.svg)](https://pypi.python.org/pypi/st7789)
[![Python Versions](https://img.shields.io/pypi/pyversions/st7789.svg)](https://pypi.python.org/pypi/st7789)

Python library to control an ST7789 TFT LCD display
Python library to control an ST7789 or ST7789V TFT LCD display

Designed specifically to work with a ST7789 based 240x240 pixel TFT SPI display. (Specifically the [1.3" SPI LCD from Pimoroni](https://shop.pimoroni.com/products/1-3-spi-colour-lcd-240x240-breakout)).

Expand Down
123 changes: 93 additions & 30 deletions st7789/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
ST7789_PWCTR6 = 0xFC


VARIANT_ST7789 = 1
VARIANT_ST7789V = 2

class ST7789(object):
"""Representation of an ST7789 TFT LCD."""

Expand All @@ -107,6 +110,7 @@ def __init__(
spi_speed_hz=4000000,
offset_left=0,
offset_top=0,
variant=VARIANT_ST7789,
):
"""Create an instance of the display using SPI communication.

Expand All @@ -123,6 +127,7 @@ def __init__(
:param rotation: Rotation of display connected to ST7789
:param invert: Invert display
:param spi_speed_hz: SPI speed (in Hz)
:param variant: Display variant [ST7789, ST7789V]

"""
if rotation not in [0, 90, 180, 270]:
Expand Down Expand Up @@ -171,6 +176,10 @@ def __init__(
self._rst = ST7789.get_rst_pin(rst)
self.reset()

if variant not in [VARIANT_ST7789, VARIANT_ST7789V]:
raise ValueError(f"Invalid variant {variant}")
self._variant = variant

self._init()

def set_pin(self, pin, state):
Expand Down Expand Up @@ -239,7 +248,10 @@ def _init(self):
time.sleep(0.150) # delay 150 ms

self.command(ST7789_MADCTL)
self.data(0x70)
if self._variant == VARIANT_ST7789:
self.data(0x70)
elif self._variant == VARIANT_ST7789V:
self.data(0x00)

self.command(ST7789_FRMCTR2) # Frame rate ctrl - idle mode
self.data(0x0C)
Expand All @@ -255,7 +267,10 @@ def _init(self):
self.data(0x14)

self.command(ST7789_VCOMS)
self.data(0x37)
if self._variant == VARIANT_ST7789:
self.data(0x37)
elif self._variant == VARIANT_ST7789V:
self.data(0x1F)

self.command(ST7789_LCMCTRL) # Power control
self.data(0x2C)
Expand All @@ -277,36 +292,81 @@ def _init(self):
self.data(0x0F)

self.command(ST7789_GMCTRP1) # Set Gamma
self.data(0xD0)
self.data(0x04)
self.data(0x0D)
self.data(0x11)
self.data(0x13)
self.data(0x2B)
self.data(0x3F)
self.data(0x54)
self.data(0x4C)
self.data(0x18)
self.data(0x0D)
self.data(0x0B)
self.data(0x1F)
self.data(0x23)
if self._variant == VARIANT_ST7789:
self.data(0x04)
self.data(0xD0)
self.data(0x0D)
self.data(0x04)
self.data(0x11)
self.data(0x0D)
self.data(0x13)
self.data(0x11)
self.data(0x2B)
self.data(0x13)
self.data(0x3F)
self.data(0x2B)
self.data(0x54)
self.data(0x3F)
self.data(0x4C)
self.data(0x54)
self.data(0x18)
self.data(0x4C)
self.data(0x0D)
self.data(0x18)
self.data(0x0B)
self.data(0x0D)
self.data(0x1F)
self.data(0x0B)
self.data(0x23)
self.data(0x1F)
self.data(0x23)
elif self._variant == VARIANT_ST7789V:
self.data(0xD0)
self.data(0x08)
self.data(0x11)
self.data(0x08)
self.data(0x0C)
self.data(0x15)
self.data(0x39)
self.data(0x33)
self.data(0x50)
self.data(0x36)
self.data(0x13)
self.data(0x14)
self.data(0x29)
self.data(0x2D)

self.command(ST7789_GMCTRN1) # Set Gamma
self.data(0xD0)
self.data(0x04)
self.data(0x0C)
self.data(0x11)
self.data(0x13)
self.data(0x2C)
self.data(0x3F)
self.data(0x44)
self.data(0x51)
self.data(0x2F)
self.data(0x1F)
self.data(0x1F)
self.data(0x20)
self.data(0x23)
if self._variant == VARIANT_ST7789:
self.data(0xD0)
self.data(0x04)
self.data(0x0C)
self.data(0x11)
self.data(0x13)
self.data(0x2C)
self.data(0x3F)
self.data(0x44)
self.data(0x51)
self.data(0x2F)
self.data(0x1F)
self.data(0x1F)
self.data(0x20)
self.data(0x23)
elif self._variant == VARIANT_ST7789V:
self.data(0xD0)
self.data(0x08)
self.data(0x10)
self.data(0x08)
self.data(0x06)
self.data(0x06)
self.data(0x39)
self.data(0x44)
self.data(0x51)
self.data(0x0B)
self.data(0x16)
self.data(0x14)
self.data(0x2F)
self.data(0x31)

if self._invert:
self.command(ST7789_INVON) # Invert display
Expand Down Expand Up @@ -363,6 +423,9 @@ def display(self, image):
:param image: Should be RGB format and the same dimensions as the display hardware.

"""
if self._variant == VARIANT_ST7789V:
self.command(ST7789_MADCTL)
self.data(0x70)
# Set address bounds to entire display.
self.set_window()

Expand Down