st7789py Reference

MIT License

Copyright (c) 2020-2023 Russ Hughes

Copyright (c) 2019 Ivan Belokobylskiy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The driver is based on devbis’ st7789py_mpy module from https://github.com/devbis/st7789py_mpy.

This driver supports:

  • 320x240, 240x240, 135x240 and 128x128 pixel displays

  • Display rotation

  • RGB and BGR color orders

  • Hardware based scrolling

  • Drawing text using 8 and 16 bit wide bitmap fonts with heights that are multiples of 8. Included are 12 bitmap fonts derived from classic pc BIOS text mode fonts.

  • Drawing text using converted TrueType fonts.

  • Drawing converted bitmaps

  • Named color constants

    • BLACK

    • BLUE

    • RED

    • GREEN

    • CYAN

    • MAGENTA

    • YELLOW

    • WHITE

st7789py.color565(red, green=0, blue=0)[source]

Convert red, green and blue values (0-255) into a 16-bit 565 encoding.

class st7789py.ST7789(spi, width, height, reset=None, dc=None, cs=None, backlight=None, rotation=0, color_order=BGR, custom_init=None, custom_rotations=None)[source]

ST7789 driver class

Parameters:
  • spi (spi) – spi object Required

  • width (int) – display width Required

  • height (int) – display height Required

  • reset (pin) – reset pin

  • dc (pin) – dc pin Required

  • cs (pin) – cs pin

  • backlight (pin) – backlight pin

  • rotation (int) –

    • 0-Portrait

    • 1-Landscape

    • 2-Inverted Portrait

    • 3-Inverted Landscape

  • color_order (int) –

    • RGB: Red, Green Blue, default

    • BGR: Blue, Green, Red

  • custom_init (tuple) –

    custom initialization commands

    • ((b’command’, b’data’, delay_ms), …)

  • custom_rotations (tuple) –

    custom rotation definitions

    • ((width, height, xstart, ystart, madctl, needs_swap), …)

init(commands)[source]

Initialize display.

hard_reset()[source]

Hard reset display.

soft_reset()[source]

Soft reset display.

sleep_mode(value)[source]

Enable or disable display sleep mode.

Parameters:
  • value (bool) – if True enable sleep mode. if False disable sleep

  • mode

inversion_mode(value)[source]

Enable or disable display inversion mode.

Parameters:
  • value (bool) – if True enable inversion mode. if False disable

  • mode (inversion) –

rotation(rotation)[source]

Set display rotation.

Parameters:
  • rotation (int) –

    • 0-Portrait

    • 1-Landscape

    • 2-Inverted Portrait

    • 3-Inverted Landscape

  • rotations (custom_rotations can have any number of) –

vline(x, y, length, color)[source]

Draw vertical line at the given location and color.

Parameters:
  • x (int) – x coordinate

  • Y (int) – y coordinate

  • length (int) – length of line

  • color (int) – 565 encoded color

hline(x, y, length, color)[source]

Draw horizontal line at the given location and color.

Parameters:
  • x (int) – x coordinate

  • Y (int) – y coordinate

  • length (int) – length of line

  • color (int) – 565 encoded color

pixel(x, y, color)[source]

Draw a pixel at the given location and color.

Parameters:
  • x (int) – x coordinate

  • Y (int) – y coordinate

  • color (int) – 565 encoded color

blit_buffer(buffer, x, y, width, height)[source]

Copy buffer to display at the given location.

Parameters:
  • buffer (bytes) – Data to copy to display

  • x (int) – Top left corner x coordinate

  • Y (int) – Top left corner y coordinate

  • width (int) – Width

  • height (int) – Height

rect(x, y, w, h, color)[source]

Draw a rectangle at the given location, size and color.

Parameters:
  • x (int) – Top left corner x coordinate

  • y (int) – Top left corner y coordinate

  • width (int) – Width in pixels

  • height (int) – Height in pixels

  • color (int) – 565 encoded color

fill_rect(x, y, width, height, color)[source]

Draw a rectangle at the given location, size and filled with color.

Parameters:
  • x (int) – Top left corner x coordinate

  • y (int) – Top left corner y coordinate

  • width (int) – Width in pixels

  • height (int) – Height in pixels

  • color (int) – 565 encoded color

fill(color)[source]

Fill the entire FrameBuffer with the specified color.

Parameters:

color (int) – 565 encoded color

line(x0, y0, x1, y1, color)[source]

Draw a single pixel wide line starting at x0, y0 and ending at x1, y1.

Parameters:
  • x0 (int) – Start point x coordinate

  • y0 (int) – Start point y coordinate

  • x1 (int) – End point x coordinate

  • y1 (int) – End point y coordinate

  • color (int) – 565 encoded color

vscrdef(tfa, vsa, bfa)[source]

Set Vertical Scrolling Definition.

To scroll a 135x240 display these values should be 40, 240, 40. There are 40 lines above the display that are not shown followed by 240 lines that are shown followed by 40 more lines that are not shown. You could write to these areas off display and scroll them into view by changing the TFA, VSA and BFA values.

Parameters:
  • tfa (int) – Top Fixed Area

  • vsa (int) – Vertical Scrolling Area

  • bfa (int) – Bottom Fixed Area

vscsad(vssa)[source]

Set Vertical Scroll Start Address of RAM.

Defines which line in the Frame Memory will be written as the first line after the last line of the Top Fixed Area on the display

Example

for line in range(40, 280, 1):

tft.vscsad(line) utime.sleep(0.01)

Parameters:

vssa (int) – Vertical Scrolling Start Address

text(font, text, x0, y0, color=WHITE, background=BLACK)[source]

Draw text on display in specified font and colors. 8 and 16 bit wide fonts are supported.

Parameters:
  • font (module) – font module to use.

  • text (str) – text to write

  • x0 (int) – column to start drawing at

  • y0 (int) – row to start drawing at

  • color (int) – 565 encoded color to use for characters

  • background (int) – 565 encoded color to use for background

bitmap(bitmap, x, y, index=0)[source]

Draw a bitmap on display at the specified column and row

Parameters:
  • bitmap (bitmap_module) – The module containing the bitmap to draw

  • x (int) – column to start drawing at

  • y (int) – row to start drawing at

  • index (int) – Optional index of bitmap to draw from multiple bitmap module

pbitmap(bitmap, x, y, index=0)[source]

Draw a bitmap on display at the specified column and row one row at a time

Parameters:
  • bitmap (bitmap_module) – The module containing the bitmap to draw

  • x (int) – column to start drawing at

  • y (int) – row to start drawing at

  • index (int) – Optional index of bitmap to draw from multiple bitmap module

write(font, string, x, y, fg=WHITE, bg=BLACK)[source]

Write a string using a converted true-type font on the display starting at the specified column and row

Parameters:
  • font (font) – The module containing the converted true-type font

  • s (string) – The string to write

  • x (int) – column to start writing

  • y (int) – row to start writing

  • fg (int) – foreground color, optional, defaults to WHITE

  • bg (int) – background color, optional, defaults to BLACK

write_width(font, string)[source]

Returns the width in pixels of the string if it was written with the specified font

Parameters:
  • font (font) – The module containing the converted true-type font

  • string (string) – The string to measure

Returns:

The width of the string in pixels

Return type:

int

polygon(points, x, y, color, angle=0, center_x=0, center_y=0)[source]

Draw a polygon on the display.

Parameters:
  • points (list) – List of points to draw.

  • x (int) – X-coordinate of the polygon’s position.

  • y (int) – Y-coordinate of the polygon’s position.

  • color (int) – 565 encoded color.

  • angle (float) – Rotation angle in radians (default: 0).

  • center_x (int) – X-coordinate of the rotation center (default: 0).

  • center_y (int) – Y-coordinate of the rotation center (default: 0).

Raises:

ValueError – If the polygon has less than 3 points.