├── .coveragerc ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ ├── build.yml │ ├── qa.yml │ └── test.yml ├── .gitignore ├── .stickler.yml ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.md ├── ST7789.py ├── check.sh ├── examples ├── 320x240.py ├── LICENSE.txt ├── cat.jpg ├── deployrainbows.gif ├── framerate.py ├── gif.py ├── image.py ├── round.py ├── scrolling-text.py └── shapes.py ├── install.sh ├── pyproject.toml ├── requirements-dev.txt ├── square-lcd-breakout-1.gif ├── st7789-combined.jpg ├── st7789 └── __init__.py ├── tests ├── conftest.py ├── test_dimensions.py ├── test_display.py └── test_setup.py ├── tox.ini └── uninstall.sh /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | source = st7789 3 | omit = 4 | .tox/* 5 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Pimoroni Python library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.pimoroni.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Python/Raspberry Pi projects check these very common issues to ensure they don't apply**: 17 | 18 | - If you are receiving an **ImportError: No module named...** error then a 19 | library the code depends on is not installed. Check the tutorial/guide or 20 | README to ensure you have installed the necessary libraries. Usually the 21 | missing library can be installed with the `pip` tool, but check the tutorial/guide 22 | for the exact command. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and power in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | If you're sure this issue is a defect in the code and checked the steps above 33 | please fill in the following fields to provide enough troubleshooting information. 34 | You may delete the guideline and text above to just leave the following details: 35 | 36 | - Platform/operating system (i.e. Raspberry Pi with Raspbian operating system, 37 | Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.): **INSERT PLATFORM/OPERATING 38 | SYSTEM HERE** 39 | 40 | - Python version (run `python -version` or `python3 -version`): **INSERT PYTHON 41 | VERSION HERE** 42 | 43 | - Error message you are receiving, including any Python exception traces: **INSERT 44 | ERROR MESAGE/EXCEPTION TRACES HERE*** 45 | 46 | - List the steps to reproduce the problem below (if possible attach code or commands 47 | to run): **LIST REPRO STEPS BELOW** 48 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Pimoroni's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. You should install tox (`pip install tox`) and run it in the `library` 15 | folder to execute the tests and run Python linting. 16 | 17 | Thank you again for contributing! We will try to test and integrate the change 18 | as soon as we can, but be aware we have many GitHub repositories to manage and 19 | can't immediately respond to every request. There is no need to bump or check in 20 | on a pull request (it will clutter the discussion of the request). 21 | 22 | Also don't be worried if the request is closed or not integrated--sometimes the 23 | priorities of Pimoroni's GitHub code (education, ease of use) might not match the 24 | priorities of the pull request. Don't fret, the open source community thrives on 25 | forks and GitHub makes it easy to keep your changes in a forked repo. 26 | 27 | After reviewing the guidelines above you can delete this text from the pull request. 28 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | 9 | jobs: 10 | test: 11 | name: Python ${{ matrix.python }} 12 | runs-on: ubuntu-latest 13 | strategy: 14 | matrix: 15 | python: ['3.9', '3.10', '3.11'] 16 | 17 | env: 18 | RELEASE_FILE: ${{ github.event.repository.name }}-${{ github.event.release.tag_name || github.sha }}-py${{ matrix.python }} 19 | 20 | steps: 21 | - name: Checkout Code 22 | uses: actions/checkout@v4 23 | 24 | - name: Set up Python ${{ matrix.python }} 25 | uses: actions/setup-python@v5 26 | with: 27 | python-version: ${{ matrix.python }} 28 | 29 | - name: Install Dependencies 30 | run: | 31 | make dev-deps 32 | 33 | - name: Build Packages 34 | run: | 35 | make build 36 | 37 | - name: Upload Packages 38 | uses: actions/upload-artifact@v4 39 | with: 40 | name: ${{ env.RELEASE_FILE }} 41 | path: dist/ 42 | -------------------------------------------------------------------------------- /.github/workflows/qa.yml: -------------------------------------------------------------------------------- 1 | name: QA 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | 9 | jobs: 10 | test: 11 | name: linting & spelling 12 | runs-on: ubuntu-latest 13 | env: 14 | TERM: xterm-256color 15 | 16 | steps: 17 | - name: Checkout Code 18 | uses: actions/checkout@v4 19 | 20 | - name: Set up Python '3,11' 21 | uses: actions/setup-python@v5 22 | with: 23 | python-version: '3.11' 24 | 25 | - name: Install Dependencies 26 | run: | 27 | make dev-deps 28 | 29 | - name: Run Quality Assurance 30 | run: | 31 | make qa 32 | 33 | - name: Run Code Checks 34 | run: | 35 | make check 36 | 37 | - name: Run Bash Code Checks 38 | run: | 39 | make shellcheck 40 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | 9 | jobs: 10 | test: 11 | name: Python ${{ matrix.python }} 12 | runs-on: ubuntu-latest 13 | strategy: 14 | matrix: 15 | python: ['3.9', '3.10', '3.11'] 16 | 17 | steps: 18 | - name: Checkout Code 19 | uses: actions/checkout@v3 20 | 21 | - name: Set up Python ${{ matrix.python }} 22 | uses: actions/setup-python@v5 23 | with: 24 | python-version: ${{ matrix.python }} 25 | 26 | - name: Install Dependencies 27 | run: | 28 | make dev-deps 29 | 30 | - name: Run Tests 31 | run: | 32 | make pytest 33 | 34 | - name: Coverage 35 | if: ${{ matrix.python == '3.9' }} 36 | env: 37 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 38 | run: | 39 | python -m pip install coveralls 40 | coveralls --service=github 41 | 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | _build/ 3 | *.o 4 | *.so 5 | *.a 6 | *.py[cod] 7 | *.egg-info 8 | dist/ 9 | __pycache__ 10 | .DS_Store 11 | *.deb 12 | *.dsc 13 | *.build 14 | *.changes 15 | *.orig.* 16 | packaging/*tar.xz 17 | library/debian/ 18 | .coverage 19 | .pytest_cache 20 | .tox 21 | -------------------------------------------------------------------------------- /.stickler.yml: -------------------------------------------------------------------------------- 1 | --- 2 | linters: 3 | flake8: 4 | python: 3 5 | max-line-length: 160 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 1.0.1 2 | ----- 3 | 4 | * Add spidev and numpy dependencies. 5 | 6 | 1.0.0 7 | ----- 8 | 9 | * Repackage to hatch/pyproject.toml 10 | * Port to gpiod/gpiodevice 11 | 12 | 0.0.4 13 | ----- 14 | 15 | * Add support for 320x240 2.0" LCD (Display HAT Mini) 16 | * Add support for 240x135 1.14" LCD (@slabua) 17 | * Rework numpy RGB888 to RGB565 18 | * Support displaying numpy arrays (@zecktos) 19 | 20 | 0.0.3 21 | ----- 22 | 23 | * Add support for RLCD 24 | * Brought back `offset_left` and `offset_top` parameters 25 | 26 | 0.0.2 27 | ----- 28 | 29 | * Fix for image retention 30 | * Drop defunct parameters 31 | 32 | 0.0.1 33 | ----- 34 | 35 | * Initial Release 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Adafruit Industries 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | LIBRARY_NAME := $(shell hatch project metadata name 2> /dev/null) 2 | LIBRARY_VERSION := $(shell hatch version 2> /dev/null) 3 | 4 | .PHONY: usage install uninstall check pytest qa build-deps check tag wheel sdist clean dist testdeploy deploy 5 | usage: 6 | ifdef LIBRARY_NAME 7 | @echo "Library: ${LIBRARY_NAME}" 8 | @echo "Version: ${LIBRARY_VERSION}\n" 9 | else 10 | @echo "WARNING: You should 'make dev-deps'\n" 11 | endif 12 | @echo "Usage: make , where target is one of:\n" 13 | @echo "install: install the library locally from source" 14 | @echo "uninstall: uninstall the local library" 15 | @echo "dev-deps: install Python dev dependencies" 16 | @echo "check: perform basic integrity checks on the codebase" 17 | @echo "qa: run linting and package QA" 18 | @echo "pytest: run Python test fixtures" 19 | @echo "clean: clean Python build and dist directories" 20 | @echo "build: build Python distribution files" 21 | @echo "testdeploy: build and upload to test PyPi" 22 | @echo "deploy: build and upload to PyPi" 23 | @echo "tag: tag the repository with the current version\n" 24 | 25 | version: 26 | @hatch version 27 | 28 | install: 29 | ./install.sh --unstable 30 | 31 | uninstall: 32 | ./uninstall.sh 33 | 34 | dev-deps: 35 | python3 -m pip install -r requirements-dev.txt 36 | sudo apt install dos2unix shellcheck 37 | 38 | check: 39 | @bash check.sh 40 | 41 | shellcheck: 42 | shellcheck *.sh 43 | 44 | qa: 45 | tox -e qa 46 | 47 | pytest: 48 | tox -e py 49 | 50 | nopost: 51 | @bash check.sh --nopost 52 | 53 | tag: version 54 | git tag -a "v${LIBRARY_VERSION}" -m "Version ${LIBRARY_VERSION}" 55 | 56 | build: check 57 | @hatch build 58 | 59 | clean: 60 | -rm -r dist 61 | 62 | testdeploy: build 63 | twine upload --repository testpypi dist/* 64 | 65 | deploy: nopost build 66 | twine upload dist/* 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python ST7789 2 | 3 | [![Build Status](https://img.shields.io/github/actions/workflow/status/pimoroni/st7789-python/test.yml?branch=main)](https://github.com/pimoroni/st7789-python/actions/workflows/test.yml) 4 | [![Coverage Status](https://coveralls.io/repos/github/pimoroni/st7789-python/badge.svg?branch=main)](https://coveralls.io/github/pimoroni/st7789-python?branch=main) 5 | [![PyPi Package](https://img.shields.io/pypi/v/st7789.svg)](https://pypi.python.org/pypi/st7789) 6 | [![Python Versions](https://img.shields.io/pypi/pyversions/st7789.svg)](https://pypi.python.org/pypi/st7789) 7 | 8 | Python library to control an ST7789 TFT LCD display 9 | 10 | 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)). 11 | 12 | ![Animated GIF showing the ST7789 SPI LCD displaying Deploy/Rainbows in alternating frames](https://raw.githubusercontent.com/pimoroni/st7789-python/master/square-lcd-breakout-1.gif) 13 | 14 | # Installation 15 | 16 | Make sure you have the following dependencies: 17 | 18 | ```` 19 | sudo apt-get update 20 | sudo apt-get install python-rpi.gpio python-spidev python-pip python-pil python-numpy 21 | ```` 22 | 23 | Install this library by running: 24 | 25 | ```` 26 | sudo pip install st7789 27 | ```` 28 | 29 | You might also need to enable I2C and SPI in raspi-config. See example of usage in the examples folder. 30 | 31 | 32 | # Licensing & History 33 | 34 | This library is a modification of a modification of code originally written by Tony DiCola for Adafruit Industries, and modified to work with the ST7735 by Clement Skau. 35 | 36 | To create this ST7789 driver, it has been hard-forked from st7735-python which was originally modified by Pimoroni to include support for their 160x80 SPI LCD breakout. 37 | 38 | ## Modifications include: 39 | 40 | * PIL/Pillow has been removed from the underlying display driver to separate concerns- you should create your own PIL image and display it using `display(image)` 41 | * `width`, `height`, `rotation`, `invert`, `offset_left` and `offset_top` parameters can be passed into `__init__` for alternate displays 42 | * `Adafruit_GPIO` has been replaced with `RPi.GPIO` and `spidev` to closely align with our other software (IE: Raspberry Pi only) 43 | * Test fixtures have been added to keep this library stable 44 | 45 | Pimoroni invests time and resources forking and modifying this open source code, please support Pimoroni and open-source software by purchasing products from us, too! 46 | 47 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 48 | 49 | Modified from 'Modified from 'Adafruit Python ILI9341' written by Tony DiCola for Adafruit Industries.' written by Clement Skau. 50 | 51 | MIT license, all text above must be included in any redistribution 52 | -------------------------------------------------------------------------------- /ST7789.py: -------------------------------------------------------------------------------- 1 | from warnings import warn 2 | 3 | from st7789 import * # noqa F403 4 | 5 | warn( 6 | 'Using "import ST7789" is deprecated. Please "import st7789" (all lowercase)!', 7 | DeprecationWarning, 8 | stacklevel=2, 9 | ) 10 | -------------------------------------------------------------------------------- /check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script handles some basic QA checks on the source 4 | 5 | NOPOST=$1 6 | LIBRARY_NAME=$(hatch project metadata name) 7 | LIBRARY_VERSION=$(hatch version | awk -F "." '{print $1"."$2"."$3}') 8 | POST_VERSION=$(hatch version | awk -F "." '{print substr($4,0,length($4))}') 9 | TERM=${TERM:="xterm-256color"} 10 | 11 | success() { 12 | echo -e "$(tput setaf 2)$1$(tput sgr0)" 13 | } 14 | 15 | inform() { 16 | echo -e "$(tput setaf 6)$1$(tput sgr0)" 17 | } 18 | 19 | warning() { 20 | echo -e "$(tput setaf 1)$1$(tput sgr0)" 21 | } 22 | 23 | while [[ $# -gt 0 ]]; do 24 | K="$1" 25 | case $K in 26 | -p|--nopost) 27 | NOPOST=true 28 | shift 29 | ;; 30 | *) 31 | if [[ $1 == -* ]]; then 32 | printf "Unrecognised option: %s\n" "$1"; 33 | exit 1 34 | fi 35 | POSITIONAL_ARGS+=("$1") 36 | shift 37 | esac 38 | done 39 | 40 | inform "Checking $LIBRARY_NAME $LIBRARY_VERSION\n" 41 | 42 | inform "Checking for trailing whitespace..." 43 | if grep -IUrn --color "[[:blank:]]$" --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=PKG-INFO; then 44 | warning "Trailing whitespace found!" 45 | exit 1 46 | else 47 | success "No trailing whitespace found." 48 | fi 49 | printf "\n" 50 | 51 | inform "Checking for DOS line-endings..." 52 | if grep -lIUrn --color $'\r' --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=Makefile; then 53 | warning "DOS line-endings found!" 54 | exit 1 55 | else 56 | success "No DOS line-endings found." 57 | fi 58 | printf "\n" 59 | 60 | inform "Checking CHANGELOG.md..." 61 | if ! grep "^${LIBRARY_VERSION}" CHANGELOG.md > /dev/null 2>&1; then 62 | warning "Changes missing for version ${LIBRARY_VERSION}! Please update CHANGELOG.md." 63 | exit 1 64 | else 65 | success "Changes found for version ${LIBRARY_VERSION}." 66 | fi 67 | printf "\n" 68 | 69 | inform "Checking for git tag ${LIBRARY_VERSION}..." 70 | if ! git tag -l | grep -E "${LIBRARY_VERSION}$"; then 71 | warning "Missing git tag for version ${LIBRARY_VERSION}" 72 | fi 73 | printf "\n" 74 | 75 | if [[ $NOPOST ]]; then 76 | inform "Checking for .postN on library version..." 77 | if [[ "$POST_VERSION" != "" ]]; then 78 | warning "Found .$POST_VERSION on library version." 79 | inform "Please only use these for testpypi releases." 80 | exit 1 81 | else 82 | success "OK" 83 | fi 84 | fi 85 | -------------------------------------------------------------------------------- /examples/320x240.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import time 3 | 4 | from PIL import Image, ImageDraw 5 | 6 | from st7789 import ST7789 7 | 8 | # Buttons 9 | BUTTON_A = 5 10 | BUTTON_B = 6 11 | BUTTON_X = 16 12 | BUTTON_Y = 24 13 | 14 | # Onboard RGB LED 15 | LED_R = 17 16 | LED_G = 27 17 | LED_B = 22 18 | 19 | # General 20 | SPI_PORT = 0 21 | SPI_CS = 1 22 | SPI_DC = 9 23 | BACKLIGHT = 13 24 | 25 | # Screen dimensions 26 | WIDTH = 320 27 | HEIGHT = 240 28 | 29 | buffer = Image.new("RGB", (WIDTH, HEIGHT)) 30 | draw = ImageDraw.Draw(buffer) 31 | 32 | draw.rectangle((0, 0, 50, 50), (255, 0, 0)) 33 | draw.rectangle((320 - 50, 0, 320, 50), (0, 255, 0)) 34 | draw.rectangle((0, 240 - 50, 50, 240), (0, 0, 255)) 35 | draw.rectangle((320 - 50, 240 - 50, 320, 240), (255, 255, 0)) 36 | 37 | display = ST7789( 38 | port=SPI_PORT, 39 | cs=SPI_CS, 40 | dc=SPI_DC, 41 | backlight=BACKLIGHT, 42 | width=WIDTH, 43 | height=HEIGHT, 44 | rotation=180, 45 | spi_speed_hz=60 * 1000 * 1000, 46 | ) 47 | 48 | while True: 49 | display.display(buffer) 50 | time.sleep(1.0 / 60) 51 | -------------------------------------------------------------------------------- /examples/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Examples originally modified from Adafruit_Python_ILI9341 2 | https://github.com/adafruit/Adafruit_Python_ILI9341 3 | 4 | Copyright (c 2014 Adafruit Industries 5 | Author: Tony DiCola 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /examples/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pimoroni/st7789-python/2e0df2861ef65eceb76a7ea771bf9580e1bc6156/examples/cat.jpg -------------------------------------------------------------------------------- /examples/deployrainbows.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pimoroni/st7789-python/2e0df2861ef65eceb76a7ea771bf9580e1bc6156/examples/deployrainbows.gif -------------------------------------------------------------------------------- /examples/framerate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import math 3 | import sys 4 | import time 5 | 6 | from PIL import Image, ImageDraw 7 | 8 | import st7789 9 | 10 | # Higher SPI bus speed = higher framerate 11 | try: 12 | SPI_SPEED_MHZ = int(sys.argv[1]) 13 | except ValueError: 14 | sys.exit(1) 15 | except IndexError: 16 | SPI_SPEED_MHZ = 80 17 | 18 | try: 19 | display_type = sys.argv[2] 20 | except IndexError: 21 | display_type = "square" 22 | 23 | print( 24 | f""" 25 | framerate.py - Test LCD framerate. 26 | 27 | If you're using Breakout Garden, plug the 1.3" LCD (SPI) 28 | breakout into the front slot. 29 | 30 | Usage: {sys.argv[0]} 31 | 32 | Where is one of: 33 | * square - 240x240 1.3" Square LCD 34 | * round - 240x240 1.3" Round LCD (applies an offset) 35 | * rect - 240x135 1.14" Rectangular LCD (applies an offset) 36 | * dhmini - 320x240 2.0" Rectangular LCD 37 | 38 | Running at: {SPI_SPEED_MHZ}MHz on a {display_type} display. 39 | """ 40 | ) 41 | 42 | try: 43 | width, height, rotation, backlight, offset_left, offset_top = { 44 | "square": (240, 240, 90, 19, 0, 0), 45 | "round": (240, 240, 90, 19, 40, 0), 46 | "rect": (240, 135, 0, 19, 40, 53), 47 | "dhmini": (320, 240, 180, 13, 0, 0), 48 | }[display_type] 49 | except IndexError: 50 | raise RuntimeError(f"Unsupported display type: {display_type}") 51 | 52 | # Create ST7789 LCD display class. 53 | disp = st7789.ST7789( 54 | width=width, 55 | height=height, 56 | rotation=rotation, 57 | port=0, 58 | cs=st7789.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT 59 | dc=9, 60 | backlight=19, # Breakout Garden: 18 for back slot, 19 for front slot. 61 | # NOTE: Change this to 13 for Pirate Audio boards 62 | spi_speed_hz=SPI_SPEED_MHZ * 1000000, 63 | offset_left=offset_left, 64 | offset_top=offset_top, 65 | ) 66 | 67 | WIDTH = disp.width 68 | HEIGHT = disp.height 69 | STEPS = WIDTH * 2 70 | images = [] 71 | 72 | for step in range(STEPS): 73 | image = Image.new("RGB", (WIDTH, HEIGHT), (0, 0, 128)) 74 | draw = ImageDraw.Draw(image) 75 | 76 | if step % 2 == 0: 77 | draw.rectangle((WIDTH / 2, int(HEIGHT / 2), WIDTH, HEIGHT), (0, 128, 0)) 78 | else: 79 | draw.rectangle((0, 0, WIDTH / 2 - 1, int(HEIGHT / 2) - 1), (0, 128, 0)) 80 | 81 | f = math.sin((float(step) / STEPS) * math.pi) 82 | offset_left = int(f * WIDTH) 83 | draw.ellipse((offset_left, 35, offset_left + 10, 45), (255, 0, 0)) 84 | 85 | images.append(image) 86 | 87 | count = 0 88 | time_start = time.time() 89 | 90 | while True: 91 | disp.display(images[count % len(images)]) 92 | count += 1 93 | time_current = time.time() - time_start 94 | if count % 120 == 0: 95 | print( 96 | f"Time: {time_current:8.3f}, Frames: {count:6d}, FPS: {count / time_current:8.3f}" 97 | ) 98 | -------------------------------------------------------------------------------- /examples/gif.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import sys 3 | import time 4 | 5 | from PIL import Image 6 | 7 | import st7789 8 | 9 | print( 10 | """ 11 | gif.py - Display a gif on the LCD. 12 | 13 | If you're using Breakout Garden, plug the 1.3" LCD (SPI) 14 | breakout into the front slot. 15 | 16 | """ 17 | ) 18 | 19 | if len(sys.argv) < 2: 20 | print( 21 | f"""Usage: {sys.argv[0]} 22 | 23 | Where is a .gif file. 24 | Hint: {sys.argv[0]} deployrainbows.gif 25 | 26 | And is one of: 27 | * square - 240x240 1.3" Square LCD 28 | * round - 240x240 1.3" Round LCD (applies an offset) 29 | * rect - 240x135 1.14" Rectangular LCD (applies an offset) 30 | * dhmini - 320x240 2.0" Display HAT Mini 31 | """ 32 | ) 33 | sys.exit(1) 34 | 35 | image_file = sys.argv[1] 36 | 37 | try: 38 | display_type = sys.argv[2] 39 | except IndexError: 40 | display_type = "square" 41 | 42 | # Create ST7789 LCD display class. 43 | 44 | if display_type in ("square", "rect", "round"): 45 | disp = st7789.ST7789( 46 | height=135 if display_type == "rect" else 240, 47 | rotation=0 if display_type == "rect" else 90, 48 | port=0, 49 | cs=st7789.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT 50 | dc=9, 51 | backlight=19, # Breakout Garden: 18 for back slot, 19 for front slot. 52 | # NOTE: Change this to 13 for Pirate Audio boards 53 | spi_speed_hz=80 * 1000 * 1000, 54 | offset_left=0 if display_type == "square" else 40, 55 | offset_top=53 if display_type == "rect" else 0, 56 | ) 57 | 58 | elif display_type == "dhmini": 59 | disp = st7789.ST7789( 60 | height=240, 61 | width=320, 62 | rotation=180, 63 | port=0, 64 | cs=1, 65 | dc=9, 66 | backlight=13, 67 | spi_speed_hz=60 * 1000 * 1000, 68 | offset_left=0, 69 | offset_top=0, 70 | ) 71 | 72 | else: 73 | print("Invalid display type!") 74 | 75 | # Initialize display. 76 | disp.begin() 77 | 78 | width = disp.width 79 | height = disp.height 80 | 81 | # Load an image. 82 | print(f"Loading gif: {image_file}...") 83 | image = Image.open(image_file) 84 | 85 | print("Drawing gif, press Ctrl+C to exit!") 86 | 87 | frame = 0 88 | 89 | while True: 90 | try: 91 | image.seek(frame) 92 | disp.display(image.resize((width, height))) 93 | frame += 1 94 | time.sleep(0.05) 95 | 96 | except EOFError: 97 | frame = 0 98 | -------------------------------------------------------------------------------- /examples/image.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import sys 3 | 4 | from PIL import Image 5 | 6 | import st7789 7 | 8 | print( 9 | """ 10 | image.py - Display an image on the LCD. 11 | 12 | If you're using Breakout Garden, plug the 1.3" LCD (SPI) 13 | breakout into the front slot. 14 | 15 | """ 16 | ) 17 | 18 | if len(sys.argv) < 2: 19 | print( 20 | f"""Usage: {sys.argv[0]} 21 | 22 | Where is one of: 23 | * square - 240x240 1.3" Square LCD 24 | * round - 240x240 1.3" Round LCD (applies an offset) 25 | * rect - 240x135 1.14" Rectangular LCD (applies an offset) 26 | * dhmini - 320x240 2.0" Display HAT Mini 27 | """ 28 | ) 29 | sys.exit(1) 30 | 31 | image_file = sys.argv[1] 32 | 33 | try: 34 | display_type = sys.argv[2] 35 | except IndexError: 36 | display_type = "square" 37 | 38 | # Create ST7789 LCD display class. 39 | 40 | if display_type in ("square", "rect", "round"): 41 | disp = st7789.ST7789( 42 | height=135 if display_type == "rect" else 240, 43 | rotation=0 if display_type == "rect" else 90, 44 | port=0, 45 | cs=st7789.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT 46 | dc=9, 47 | backlight=19, # Breakout Garden: 18 for back slot, 19 for front slot. 48 | # NOTE: Change this to 13 for Pirate Audio boards 49 | spi_speed_hz=80 * 1000 * 1000, 50 | offset_left=0 if display_type == "square" else 40, 51 | offset_top=53 if display_type == "rect" else 0, 52 | ) 53 | 54 | elif display_type == "dhmini": 55 | disp = st7789.ST7789( 56 | height=240, 57 | width=320, 58 | rotation=180, 59 | port=0, 60 | cs=1, 61 | dc=9, 62 | backlight=13, 63 | spi_speed_hz=60 * 1000 * 1000, 64 | offset_left=0, 65 | offset_top=0, 66 | ) 67 | 68 | else: 69 | print("Invalid display type!") 70 | 71 | WIDTH = disp.width 72 | HEIGHT = disp.height 73 | 74 | # Initialize display. 75 | disp.begin() 76 | 77 | # Load an image. 78 | print(f"Loading image: {image_file}...") 79 | image = Image.open(image_file) 80 | 81 | # Resize the image 82 | image = image.resize((WIDTH, HEIGHT)) 83 | 84 | # Draw the image on the display hardware. 85 | print("Drawing image") 86 | 87 | disp.display(image) 88 | -------------------------------------------------------------------------------- /examples/round.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import colorsys 3 | import math 4 | import sys 5 | import time 6 | 7 | from PIL import Image, ImageDraw 8 | 9 | import st7789 10 | 11 | print( 12 | f""" 13 | round.py - Shiny shiny round LCD! 14 | 15 | If you're using Breakout Garden, plug a 1.3" ROUND LCD 16 | (SPI) breakout into the front slot. 17 | 18 | Usage: {sys.argv[0]}