├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ ├── build.yml │ ├── qa.yml │ └── test.yml ├── .gitignore ├── .stickler.yml ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.md ├── ST7735.py ├── check.sh ├── examples ├── cat.jpg ├── deployrainbows.gif ├── framerate.py ├── gif.py ├── image.py ├── scrolling-text.py └── shapes.py ├── install.sh ├── pyproject.toml ├── requirements-dev.txt ├── requirements.txt ├── st7735 └── __init__.py ├── tests ├── conftest.py ├── test_dimensions.py ├── test_features.py └── test_setup.py ├── tox.ini └── uninstall.sh /.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@v3 23 | 24 | - name: Set up Python ${{ matrix.python }} 25 | uses: actions/setup-python@v3 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@v3 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 | 14 | env: 15 | TERM: xterm-256color 16 | 17 | steps: 18 | - name: Checkout Code 19 | uses: actions/checkout@v2 20 | 21 | - name: Set up Python '3,11' 22 | uses: actions/setup-python@v3 23 | with: 24 | python-version: '3.11' 25 | 26 | - name: Install Dependencies 27 | run: | 28 | make dev-deps 29 | 30 | - name: Run Quality Assurance 31 | run: | 32 | make qa 33 | 34 | - name: Run Code Checks 35 | run: | 36 | make check 37 | -------------------------------------------------------------------------------- /.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@v3 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.0 2 | ----- 3 | 4 | * Rename module from ST7735 to st7735 5 | * Port to gpiod/gpiodevice 6 | 7 | 0.0.5 8 | ----- 9 | 10 | * Add support for choosing between BGR/RGB displays 11 | * Add methods for display power and sleep control 12 | 13 | 0.0.4-post1 14 | ----------- 15 | 16 | * Repackage with Markdown README/setup.cfg 17 | * Fix `__version__` to 0.0.4 18 | * Update dependencies in README 19 | 20 | 0.0.4 21 | ----- 22 | 23 | * Depend upon spidev==3.4.0 for stability fixes 24 | * Switch from manual data chunking to spidev.xfer3() 25 | 26 | 27 | 0.0.3 28 | ----- 29 | 30 | * Fixed backlight pin 31 | * Added `set_backlight` 32 | * Added constants BG_SPI_CS_FRONT and BG_SPI_CS_BACK 33 | * Added module `__version__` 34 | 35 | 0.0.2 36 | ----- 37 | 38 | * Support for multiple display sizes/orientations 39 | 40 | 0.0.1 41 | ----- 42 | 43 | * Initial Release 44 | -------------------------------------------------------------------------------- /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 | install: 26 | ./install.sh --unstable 27 | 28 | uninstall: 29 | ./uninstall.sh 30 | 31 | dev-deps: 32 | python3 -m pip install -r requirements-dev.txt 33 | sudo apt install dos2unix 34 | 35 | check: 36 | @bash check.sh 37 | 38 | qa: 39 | tox -e qa 40 | 41 | pytest: 42 | tox -e py 43 | 44 | nopost: 45 | @bash check.sh --nopost 46 | 47 | tag: 48 | git tag -a "v${LIBRARY_VERSION}" -m "Version ${LIBRARY_VERSION}" 49 | 50 | build: check 51 | @hatch build 52 | 53 | clean: 54 | -rm -r dist 55 | 56 | testdeploy: build 57 | twine upload --repository testpypi dist/* 58 | 59 | deploy: nopost build 60 | twine upload dist/* 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python ST7735 2 | 3 | [![Build Status](https://img.shields.io/github/actions/workflow/status/pimoroni/st7735-python/test.yml?branch=main)](https://github.com/pimoroni/st7735-python/actions/workflows/test.yml) 4 | [![Coverage Status](https://coveralls.io/repos/github/pimoroni/st7735-python/badge.svg?branch=master)](https://coveralls.io/github/pimoroni/st7735-python?branch=master) 5 | [![PyPi Package](https://img.shields.io/pypi/v/st7735.svg)](https://pypi.python.org/pypi/st7735) 6 | [![Python Versions](https://img.shields.io/pypi/pyversions/st7735.svg)](https://pypi.python.org/pypi/st7735) 7 | 8 | Python library to control an ST7735 TFT LCD display. Allows simple drawing on the display without installing a kernel module. 9 | 10 | Designed specifically to work with a ST7735 based 160x80 pixel TFT SPI display. (Specifically the 0.96" SPI LCD from Pimoroni). 11 | 12 | ## Installing 13 | 14 | ```` 15 | pip install st7735 16 | ```` 17 | 18 | See example of usage in the examples folder. 19 | 20 | # Licensing & History 21 | 22 | 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. 23 | 24 | It has been modified by Pimoroni to include support for their 160x80 SPI LCD breakout, and hopefully also generalised enough so that it will support other ST7735-powered displays. 25 | 26 | ## Modifications include: 27 | 28 | * 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)` 29 | * `width`, `height`, `rotation`, `invert`, `offset_left` and `offset_top` parameters can be passed into `__init__` for alternate displays 30 | * `Adafruit_GPIO` has been replaced with `RPi.GPIO` and `spidev` to closely align with our other software (IE: Raspberry Pi only) 31 | * Test fixtures have been added to keep this library stable 32 | 33 | 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! 34 | 35 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 36 | 37 | Modified from 'Modified from 'Adafruit Python ILI9341' written by Tony DiCola for Adafruit Industries.' written by Clement Skau. 38 | 39 | MIT license, all text above must be included in any redistribution 40 | -------------------------------------------------------------------------------- /ST7735.py: -------------------------------------------------------------------------------- 1 | from warnings import warn 2 | 3 | from st7735 import * # noqa F403 4 | 5 | warn("Using \"import ST7735\" is deprecated. Please \"import st7735\" (all lowercase)!", DeprecationWarning, stacklevel=2) 6 | -------------------------------------------------------------------------------- /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 | 10 | success() { 11 | echo -e "$(tput setaf 2)$1$(tput sgr0)" 12 | } 13 | 14 | inform() { 15 | echo -e "$(tput setaf 6)$1$(tput sgr0)" 16 | } 17 | 18 | warning() { 19 | echo -e "$(tput setaf 1)$1$(tput sgr0)" 20 | } 21 | 22 | while [[ $# -gt 0 ]]; do 23 | K="$1" 24 | case $K in 25 | -p|--nopost) 26 | NOPOST=true 27 | shift 28 | ;; 29 | *) 30 | if [[ $1 == -* ]]; then 31 | printf "Unrecognised option: $1\n"; 32 | exit 1 33 | fi 34 | POSITIONAL_ARGS+=("$1") 35 | shift 36 | esac 37 | done 38 | 39 | inform "Checking $LIBRARY_NAME $LIBRARY_VERSION\n" 40 | 41 | inform "Checking for trailing whitespace..." 42 | grep -IUrn --color "[[:blank:]]$" --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=PKG-INFO 43 | if [[ $? -eq 0 ]]; 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 | grep -lIUrn --color $'\r' --exclude-dir=dist --exclude-dir=.tox --exclude-dir=.git --exclude=Makefile 53 | if [[ $? -eq 0 ]]; then 54 | warning "DOS line-endings found!" 55 | exit 1 56 | else 57 | success "No DOS line-endings found." 58 | fi 59 | printf "\n" 60 | 61 | inform "Checking CHANGELOG.md..." 62 | cat CHANGELOG.md | grep ^${LIBRARY_VERSION} > /dev/null 2>&1 63 | if [[ $? -eq 1 ]]; then 64 | warning "Changes missing for version ${LIBRARY_VERSION}! Please update CHANGELOG.md." 65 | exit 1 66 | else 67 | success "Changes found for version ${LIBRARY_VERSION}." 68 | fi 69 | printf "\n" 70 | 71 | inform "Checking for git tag ${LIBRARY_VERSION}..." 72 | git tag -l | grep -E "${LIBRARY_VERSION}$" 73 | if [[ $? -eq 1 ]]; then 74 | warning "Missing git tag for version ${LIBRARY_VERSION}" 75 | fi 76 | printf "\n" 77 | 78 | if [[ $NOPOST ]]; then 79 | inform "Checking for .postN on library version..." 80 | if [[ "$POST_VERSION" != "" ]]; then 81 | warning "Found .$POST_VERSION on library version." 82 | inform "Please only use these for testpypi releases." 83 | exit 1 84 | else 85 | success "OK" 86 | fi 87 | fi 88 | -------------------------------------------------------------------------------- /examples/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pimoroni/st7735-python/20d6e8058f5f29964c83524d006eca859fb192b9/examples/cat.jpg -------------------------------------------------------------------------------- /examples/deployrainbows.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pimoroni/st7735-python/20d6e8058f5f29964c83524d006eca859fb192b9/examples/deployrainbows.gif -------------------------------------------------------------------------------- /examples/framerate.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Adafruit Industries 2 | # Author: Tony DiCola 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), to deal 6 | # in the Software without restriction, including without limitation the rights 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | # copies of the Software, and to permit persons to whom the Software is 9 | # furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in 12 | # all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | # THE SOFTWARE. 21 | import math 22 | import sys 23 | import time 24 | 25 | from PIL import Image, ImageDraw 26 | 27 | import st7735 28 | 29 | SPI_SPEED_MHZ = 4 # Higher speed = higher framerate 30 | 31 | if len(sys.argv) > 1: 32 | SPI_SPEED_MHZ = int(sys.argv[1]) 33 | 34 | print(f""" 35 | framerate.py - Test LCD framerate. 36 | 37 | If you're using Breakout Garden, plug the 0.96" LCD (SPI) 38 | breakout into the rear slot. 39 | 40 | Running at: {SPI_SPEED_MHZ}MHz 41 | """) 42 | 43 | # Create ST7735 LCD display class. 44 | disp = st7735.ST7735( 45 | port=0, 46 | cs=st7735.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT. BG_SPI_CS_FRONT (eg: CE1) for Enviro Plus 47 | dc="PIN21", # "GPIO9" / "PIN21". "PIN21" for a Pi 5 with Enviro Plus 48 | backlight="PIN32", # "PIN18" for back BG slot, "PIN19" for front BG slot. "PIN32" for a Pi 5 with Enviro Plus 49 | rotation=90, 50 | spi_speed_hz=4000000 51 | ) 52 | 53 | WIDTH = disp.width 54 | HEIGHT = disp.height 55 | STEPS = WIDTH * 2 56 | images = [] 57 | 58 | for step in range(STEPS): 59 | image = Image.new("RGB", (WIDTH, HEIGHT), (0, 0, 128)) 60 | draw = ImageDraw.Draw(image) 61 | 62 | if step % 2 == 0: 63 | draw.rectangle((79, 0, 159, 79), (0, 128, 0)) 64 | else: 65 | draw.rectangle((0, 0, 79, 79), (0, 128, 0)) 66 | 67 | f = math.sin((float(step) / STEPS) * math.pi) 68 | offset_left = int(f * WIDTH) 69 | draw.ellipse((offset_left, 35, offset_left + 10, 45), (255, 0, 0)) 70 | 71 | images.append(image) 72 | 73 | count = 0 74 | time_start = time.time() 75 | 76 | while True: 77 | disp.display(images[count % len(images)]) 78 | count += 1 79 | time_current = time.time() - time_start 80 | if count % 120 == 0: 81 | print(f"Time: {time_current:8.3f}, Frames: {count:6d}, FPS: {count / time_current:8.3f}") 82 | -------------------------------------------------------------------------------- /examples/gif.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Adafruit Industries 2 | # Author: Phil Howard, Tony DiCola 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), to deal 6 | # in the Software without restriction, including without limitation the rights 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | # copies of the Software, and to permit persons to whom the Software is 9 | # furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in 12 | # all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | # THE SOFTWARE. 21 | import sys 22 | import time 23 | 24 | from PIL import Image 25 | 26 | import st7735 27 | 28 | print(""" 29 | gif.py - Display a gif on the LCD. 30 | 31 | If you're using Breakout Garden, plug the 0.96" LCD (SPI) 32 | breakout into the front slot. 33 | """) 34 | 35 | if len(sys.argv) > 1: 36 | image_file = sys.argv[1] 37 | else: 38 | print(f"Usage: {sys.argv[0]} ") 39 | sys.exit(0) 40 | 41 | # Create TFT LCD display class. 42 | disp = st7735.ST7735( 43 | port=0, 44 | cs=st7735.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT. BG_SPI_CS_FRONT (eg: CE1) for Enviro Plus 45 | dc="PIN21", # "GPIO9" / "PIN21". "PIN21" for a Pi 5 with Enviro Plus 46 | backlight="PIN32", # "PIN18" for back BG slot, "PIN19" for front BG slot. "PIN32" for a Pi 5 with Enviro Plus 47 | rotation=90, 48 | spi_speed_hz=4000000 49 | ) 50 | 51 | # Initialize display. 52 | disp.begin() 53 | 54 | width = disp.width 55 | height = disp.height 56 | 57 | # Load an image. 58 | print(f"Loading gif: {image_file}...") 59 | image = Image.open(image_file) 60 | 61 | print("Drawing gif, press Ctrl+C to exit!") 62 | 63 | frame = 0 64 | 65 | while True: 66 | try: 67 | image.seek(frame) 68 | disp.display(image.resize((width, height))) 69 | frame += 1 70 | time.sleep(0.05) 71 | 72 | except EOFError: 73 | frame = 0 74 | -------------------------------------------------------------------------------- /examples/image.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Adafruit Industries 2 | # Author: Tony DiCola 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), to deal 6 | # in the Software without restriction, including without limitation the rights 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | # copies of the Software, and to permit persons to whom the Software is 9 | # furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in 12 | # all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | # THE SOFTWARE. 21 | import sys 22 | 23 | from PIL import Image 24 | 25 | import st7735 26 | 27 | print(""" 28 | image.py - Display an image on the LCD. 29 | 30 | If you're using Breakout Garden, plug the 0.96" LCD (SPI) 31 | breakout into the rear slot. 32 | """) 33 | 34 | if len(sys.argv) < 2: 35 | print(f"Usage: {sys.argv[0]} ") 36 | sys.exit(1) 37 | 38 | image_file = sys.argv[1] 39 | 40 | # Create ST7735 LCD display class. 41 | disp = st7735.ST7735( 42 | port=0, 43 | cs=st7735.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT. BG_SPI_CS_FRONT (eg: CE1) for Enviro Plus 44 | dc="PIN21", # "GPIO9" / "PIN21". "PIN21" for a Pi 5 with Enviro Plus 45 | backlight="PIN32", # "PIN18" for back BG slot, "PIN19" for front BG slot. "PIN32" for a Pi 5 with Enviro Plus 46 | rotation=90, 47 | spi_speed_hz=4000000 48 | ) 49 | 50 | WIDTH = disp.width 51 | HEIGHT = disp.height 52 | 53 | # Initialize display. 54 | disp.begin() 55 | 56 | # Load an image. 57 | print(f"Loading image: {image_file}...") 58 | image = Image.open(image_file) 59 | 60 | # Resize the image 61 | image = image.resize((WIDTH, HEIGHT)) 62 | 63 | # Draw the image on the display hardware. 64 | print("Drawing image") 65 | 66 | disp.display(image) 67 | -------------------------------------------------------------------------------- /examples/scrolling-text.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | from PIL import Image, ImageDraw, ImageFont 4 | 5 | import st7735 6 | 7 | MESSAGE = "Hello World! How are you today?" 8 | 9 | # Create ST7735 LCD display class. 10 | disp = st7735.ST7735( 11 | port=0, 12 | cs=st7735.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT. BG_SPI_CS_FRONT (eg: CE1) for Enviro Plus 13 | dc="PIN21", # "GPIO9" / "PIN21". "PIN21" for a Pi 5 with Enviro Plus 14 | backlight="PIN32", # "PIN18" for back BG slot, "PIN19" for front BG slot. "PIN32" for a Pi 5 with Enviro Plus 15 | rotation=90, 16 | spi_speed_hz=4000000 17 | ) 18 | 19 | # Initialize display. 20 | disp.begin() 21 | 22 | WIDTH = disp.width 23 | HEIGHT = disp.height 24 | 25 | 26 | img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0)) 27 | 28 | draw = ImageDraw.Draw(img) 29 | 30 | font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30) 31 | 32 | x1, y1, x2, y2 = font.getbbox(MESSAGE) 33 | size_x = x2 - x1 34 | size_y = y2 - y1 35 | 36 | text_x = 160 37 | text_y = (80 - size_y) // 2 38 | 39 | t_start = time.time() 40 | 41 | while True: 42 | x = (time.time() - t_start) * 100 43 | x %= (size_x + 160) 44 | draw.rectangle((0, 0, 160, 80), (0, 0, 0)) 45 | draw.text((int(text_x - x), text_y), MESSAGE, font=font, fill=(255, 255, 255)) 46 | disp.display(img) 47 | -------------------------------------------------------------------------------- /examples/shapes.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Adafruit Industries 2 | # Author: Tony DiCola 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), to deal 6 | # in the Software without restriction, including without limitation the rights 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | # copies of the Software, and to permit persons to whom the Software is 9 | # furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in 12 | # all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | # THE SOFTWARE. 21 | from PIL import Image, ImageDraw, ImageFont 22 | 23 | import st7735 24 | 25 | print(""" 26 | shapes.py - Display test shapes on the LCD using PIL. 27 | 28 | If you"re using Breakout Garden, plug the 0.96" LCD (SPI) 29 | breakout into the rear slot. 30 | 31 | """) 32 | 33 | # Create ST7735 LCD display class. 34 | disp = st7735.ST7735( 35 | port=0, 36 | cs=st7735.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT. BG_SPI_CS_FRONT (eg: CE1) for Enviro Plus 37 | dc="PIN21", # "GPIO9" / "PIN21". "PIN21" for a Pi 5 with Enviro Plus 38 | backlight="PIN32", # "PIN18" for back BG slot, "PIN19" for front BG slot. "PIN32" for a Pi 5 with Enviro Plus 39 | rotation=90, 40 | spi_speed_hz=4000000 41 | ) 42 | 43 | # Initialize display. 44 | disp.begin() 45 | 46 | WIDTH = disp.width 47 | HEIGHT = disp.height 48 | 49 | 50 | # Clear the display to a red background. 51 | # Can pass any tuple of red, green, blue values (from 0 to 255 each). 52 | # Get a PIL Draw object to start drawing on the display buffer. 53 | img = Image.new("RGB", (WIDTH, HEIGHT), color=(255, 0, 0)) 54 | 55 | draw = ImageDraw.Draw(img) 56 | 57 | # Draw a purple rectangle with yellow outline. 58 | draw.rectangle((10, 10, WIDTH - 10, HEIGHT - 10), outline=(255, 255, 0), fill=(255, 0, 255)) 59 | 60 | # Draw some shapes. 61 | # Draw a blue ellipse with a green outline. 62 | draw.ellipse((10, 10, WIDTH - 10, HEIGHT - 10), outline=(0, 255, 0), fill=(0, 0, 255)) 63 | 64 | # Draw a white X. 65 | draw.line((10, 10, WIDTH - 10, HEIGHT - 10), fill=(255, 255, 255)) 66 | draw.line((10, HEIGHT - 10, WIDTH - 10, 10), fill=(255, 255, 255)) 67 | 68 | # Draw a cyan triangle with a black outline. 69 | draw.polygon([(WIDTH / 2, 10), (WIDTH - 10, HEIGHT - 10), (10, HEIGHT - 10)], outline=(0, 0, 0), fill=(0, 255, 255)) 70 | 71 | # Load default font. 72 | font = ImageFont.load_default() 73 | 74 | # Alternatively load a TTF font. 75 | # Some other nice fonts to try: http://www.dafont.com/bitmap.php 76 | # font = ImageFont.truetype("Minecraftia.ttf", 16) 77 | 78 | 79 | # Define a function to create rotated text. Unfortunately PIL doesn"t have good 80 | # native support for rotated fonts, but this function can be used to make a 81 | # text image and rotate it so it"s easy to paste in the buffer. 82 | def draw_rotated_text(image, text, position, angle, font, fill=(255, 255, 255)): 83 | # Get rendered font width and height. 84 | x1, y1, x2, y2 = font.getbbox(text) 85 | width = x2 - x1 86 | height = y2 - y1 87 | # Create a new image with transparent background to store the text. 88 | textimage = Image.new("RGBA", (width, height), (0, 0, 0, 0)) 89 | # Render the text. 90 | textdraw = ImageDraw.Draw(textimage) 91 | textdraw.text((0, 0), text, font=font, fill=fill) 92 | # Rotate the text image. 93 | rotated = textimage.rotate(angle, expand=1) 94 | # Paste the text into the image, using it as a mask for transparency. 95 | image.paste(rotated, position, rotated) 96 | 97 | 98 | # Write two lines of white text on the buffer, rotated 90 degrees counter clockwise. 99 | draw_rotated_text(img, "Hello World!", (0, 0), 90, font, fill=(255, 255, 255)) 100 | draw_rotated_text(img, "This is a line of text.", (10, HEIGHT - 10), 0, font, fill=(255, 255, 255)) 101 | 102 | # Write buffer to display hardware, must be called to make things visible on the 103 | # display! 104 | disp.display(img) 105 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | LIBRARY_NAME=`grep -m 1 name pyproject.toml | awk -F" = " '{print substr($2,2,length($2)-2)}'` 3 | CONFIG_FILE=config.txt 4 | CONFIG_DIR="/boot/firmware" 5 | DATESTAMP=`date "+%Y-%m-%d-%H-%M-%S"` 6 | CONFIG_BACKUP=false 7 | APT_HAS_UPDATED=false 8 | RESOURCES_TOP_DIR=$HOME/Pimoroni 9 | VENV_BASH_SNIPPET=$RESOURCES_TOP_DIR/auto_venv.sh 10 | VENV_DIR=$HOME/.virtualenvs/pimoroni 11 | WD=`pwd` 12 | USAGE="./install.sh (--unstable)" 13 | POSITIONAL_ARGS=() 14 | FORCE=false 15 | UNSTABLE=false 16 | PYTHON="python" 17 | 18 | 19 | user_check() { 20 | if [ $(id -u) -eq 0 ]; then 21 | printf "Script should not be run as root. Try './install.sh'\n" 22 | exit 1 23 | fi 24 | } 25 | 26 | confirm() { 27 | if $FORCE; then 28 | true 29 | else 30 | read -r -p "$1 [y/N] " response < /dev/tty 31 | if [[ $response =~ ^(yes|y|Y)$ ]]; then 32 | true 33 | else 34 | false 35 | fi 36 | fi 37 | } 38 | 39 | prompt() { 40 | read -r -p "$1 [y/N] " response < /dev/tty 41 | if [[ $response =~ ^(yes|y|Y)$ ]]; then 42 | true 43 | else 44 | false 45 | fi 46 | } 47 | 48 | success() { 49 | echo -e "$(tput setaf 2)$1$(tput sgr0)" 50 | } 51 | 52 | inform() { 53 | echo -e "$(tput setaf 6)$1$(tput sgr0)" 54 | } 55 | 56 | warning() { 57 | echo -e "$(tput setaf 1)$1$(tput sgr0)" 58 | } 59 | 60 | find_config() { 61 | if [ ! -f "$CONFIG_DIR/$CONFIG_FILE" ]; then 62 | CONFIG_DIR="/boot" 63 | if [ ! -f "$CONFIG_DIR/$CONFIG_FILE"]; then 64 | warning "Could not find $CONFIG_FILE!" 65 | exit 1 66 | fi 67 | else 68 | if [ -f "/boot/$CONFIG_FILE" ] && [ ! -L "/boot/$CONFIG_FILE" ]; then 69 | warning "Oops! It looks like /boot/$CONFIG_FILE is not a link to $CONFIG_DIR/$CONFIG_FILE" 70 | warning "You might want to fix this!" 71 | fi 72 | fi 73 | inform "Using $CONFIG_FILE in $CONFIG_DIR" 74 | } 75 | 76 | venv_bash_snippet() { 77 | inform "Checking for $VENV_BASH_SNIPPET\n" 78 | if [ ! -f $VENV_BASH_SNIPPET ]; then 79 | inform "Creating $VENV_BASH_SNIPPET\n" 80 | cat << EOF > $VENV_BASH_SNIPPET 81 | # Add "source $VENV_BASH_SNIPPET" to your ~/.bashrc to activate 82 | # the Pimoroni virtual environment automagically! 83 | VENV_DIR="$VENV_DIR" 84 | if [ ! -f \$VENV_DIR/bin/activate ]; then 85 | printf "Creating user Python environment in \$VENV_DIR, please wait...\n" 86 | mkdir -p \$VENV_DIR 87 | python3 -m venv --system-site-packages \$VENV_DIR 88 | fi 89 | printf " ↓ ↓ ↓ ↓ Hello, we've activated a Python venv for you. To exit, type \"deactivate\".\n" 90 | source \$VENV_DIR/bin/activate 91 | EOF 92 | fi 93 | } 94 | 95 | venv_check() { 96 | PYTHON_BIN=`which $PYTHON` 97 | if [[ $VIRTUAL_ENV == "" ]] || [[ $PYTHON_BIN != $VIRTUAL_ENV* ]]; then 98 | printf "This script should be run in a virtual Python environment.\n" 99 | if confirm "Would you like us to create one for you?"; then 100 | if [ ! -f $VENV_DIR/bin/activate ]; then 101 | inform "Creating virtual Python environment in $VENV_DIR, please wait...\n" 102 | mkdir -p $VENV_DIR 103 | /usr/bin/python3 -m venv $VENV_DIR --system-site-packages 104 | venv_bash_snippet 105 | else 106 | inform "Found existing virtual Python environment in $VENV_DIR\n" 107 | fi 108 | inform "Activating virtual Python environment in $VENV_DIR..." 109 | inform "source $VENV_DIR/bin/activate\n" 110 | source $VENV_DIR/bin/activate 111 | 112 | else 113 | exit 1 114 | fi 115 | fi 116 | } 117 | 118 | function do_config_backup { 119 | if [ ! $CONFIG_BACKUP == true ]; then 120 | CONFIG_BACKUP=true 121 | FILENAME="config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt" 122 | inform "Backing up $CONFIG_DIR/$CONFIG_FILE to $CONFIG_DIR/$FILENAME\n" 123 | sudo cp $CONFIG_DIR/$CONFIG_FILE $CONFIG_DIR/$FILENAME 124 | mkdir -p $RESOURCES_TOP_DIR/config-backups/ 125 | cp $CONFIG_DIR/$CONFIG_FILE $RESOURCES_TOP_DIR/config-backups/$FILENAME 126 | if [ -f "$UNINSTALLER" ]; then 127 | echo "cp $RESOURCES_TOP_DIR/config-backups/$FILENAME $CONFIG_DIR/$CONFIG_FILE" >> $UNINSTALLER 128 | fi 129 | fi 130 | } 131 | 132 | function apt_pkg_install { 133 | PACKAGES=() 134 | PACKAGES_IN=("$@") 135 | for ((i = 0; i < ${#PACKAGES_IN[@]}; i++)); do 136 | PACKAGE="${PACKAGES_IN[$i]}" 137 | if [ "$PACKAGE" == "" ]; then continue; fi 138 | printf "Checking for $PACKAGE\n" 139 | dpkg -L $PACKAGE > /dev/null 2>&1 140 | if [ "$?" == "1" ]; then 141 | PACKAGES+=("$PACKAGE") 142 | fi 143 | done 144 | PACKAGES="${PACKAGES[@]}" 145 | if ! [ "$PACKAGES" == "" ]; then 146 | echo "Installing missing packages: $PACKAGES" 147 | if [ ! $APT_HAS_UPDATED ]; then 148 | sudo apt update 149 | APT_HAS_UPDATED=true 150 | fi 151 | sudo apt install -y $PACKAGES 152 | if [ -f "$UNINSTALLER" ]; then 153 | echo "apt uninstall -y $PACKAGES" >> $UNINSTALLER 154 | fi 155 | fi 156 | } 157 | 158 | function pip_pkg_install { 159 | PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring $PYTHON -m pip install --upgrade "$@" 160 | } 161 | 162 | while [[ $# -gt 0 ]]; do 163 | K="$1" 164 | case $K in 165 | -u|--unstable) 166 | UNSTABLE=true 167 | shift 168 | ;; 169 | -f|--force) 170 | FORCE=true 171 | shift 172 | ;; 173 | -p|--python) 174 | PYTHON=$2 175 | shift 176 | shift 177 | ;; 178 | *) 179 | if [[ $1 == -* ]]; then 180 | printf "Unrecognised option: $1\n"; 181 | printf "Usage: $USAGE\n"; 182 | exit 1 183 | fi 184 | POSITIONAL_ARGS+=("$1") 185 | shift 186 | esac 187 | done 188 | 189 | user_check 190 | venv_check 191 | 192 | if [ ! -f `which $PYTHON` ]; then 193 | printf "Python path $PYTHON not found!\n" 194 | exit 1 195 | fi 196 | 197 | PYTHON_VER=`$PYTHON --version` 198 | 199 | printf "$LIBRARY_NAME Python Library: Installer\n\n" 200 | 201 | inform "Checking Dependencies. Please wait..." 202 | 203 | pip_pkg_install toml 204 | 205 | CONFIG_VARS=`$PYTHON - < $UNINSTALLER 240 | printf "It's recommended you run these steps manually.\n" 241 | printf "If you want to run the full script, open it in\n" 242 | printf "an editor and remove 'exit 1' from below.\n" 243 | exit 1 244 | source $VIRTUAL_ENV/bin/activate 245 | EOF 246 | 247 | if $UNSTABLE; then 248 | warning "Installing unstable library from source.\n\n" 249 | else 250 | printf "Installing stable library from pypi.\n\n" 251 | fi 252 | 253 | inform "Installing for $PYTHON_VER...\n" 254 | apt_pkg_install "${APT_PACKAGES[@]}" 255 | if $UNSTABLE; then 256 | pip_pkg_install . 257 | else 258 | pip_pkg_install $LIBRARY_NAME 259 | fi 260 | if [ $? -eq 0 ]; then 261 | success "Done!\n" 262 | echo "$PYTHON -m pip uninstall $LIBRARY_NAME" >> $UNINSTALLER 263 | fi 264 | 265 | cd $WD 266 | 267 | find_config 268 | 269 | for ((i = 0; i < ${#SETUP_CMDS[@]}; i++)); do 270 | CMD="${SETUP_CMDS[$i]}" 271 | # Attempt to catch anything that touches config.txt and trigger a backup 272 | if [[ "$CMD" == *"raspi-config"* ]] || [[ "$CMD" == *"$CONFIG_DIR/$CONFIG_FILE"* ]] || [[ "$CMD" == *"\$CONFIG_DIR/\$CONFIG_FILE"* ]]; then 273 | do_config_backup 274 | fi 275 | eval $CMD 276 | done 277 | 278 | for ((i = 0; i < ${#CONFIG_TXT[@]}; i++)); do 279 | CONFIG_LINE="${CONFIG_TXT[$i]}" 280 | if ! [ "$CONFIG_LINE" == "" ]; then 281 | do_config_backup 282 | inform "Adding $CONFIG_LINE to $CONFIG_DIR/$CONFIG_FILE\n" 283 | sudo sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG_DIR/$CONFIG_FILE 284 | if ! grep -q "^$CONFIG_LINE" $CONFIG_DIR/$CONFIG_FILE; then 285 | printf "$CONFIG_LINE\n" | sudo tee --append $CONFIG_DIR/$CONFIG_FILE 286 | fi 287 | fi 288 | done 289 | 290 | if [ -d "examples" ]; then 291 | if confirm "Would you like to copy examples to $RESOURCES_DIR?"; then 292 | inform "Copying examples to $RESOURCES_DIR" 293 | cp -r examples/ $RESOURCES_DIR 294 | echo "rm -r $RESOURCES_DIR" >> $UNINSTALLER 295 | success "Done!" 296 | fi 297 | fi 298 | 299 | printf "\n" 300 | 301 | if confirm "Would you like to generate documentation?"; then 302 | pip_pkg_install pdoc 303 | printf "Generating documentation.\n" 304 | $PYTHON -m pdoc $LIBRARY_NAME -o $RESOURCES_DIR/docs > /dev/null 305 | if [ $? -eq 0 ]; then 306 | inform "Documentation saved to $RESOURCES_DIR/docs" 307 | success "Done!" 308 | else 309 | warning "Error: Failed to generate documentation." 310 | fi 311 | fi 312 | 313 | success "\nAll done!" 314 | inform "If this is your first time installing you should reboot for hardware changes to take effect.\n" 315 | inform "Find uninstall steps in $UNINSTALLER\n" 316 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["hatchling", "hatch-fancy-pypi-readme"] 3 | build-backend = "hatchling.build" 4 | 5 | [project] 6 | name = "st7735" 7 | dynamic = ["version", "readme"] 8 | description = "Library to control an ST7735 168x80 TFT LCD display." 9 | license = {file = "LICENSE"} 10 | requires-python = ">= 3.7" 11 | authors = [ 12 | { name = "Philip Howard", email = "phil@pimoroni.com" }, 13 | ] 14 | maintainers = [ 15 | { name = "Philip Howard", email = "phil@pimoroni.com" }, 16 | ] 17 | keywords = [ 18 | "Pi", 19 | "Raspberry", 20 | "displays" 21 | ] 22 | classifiers = [ 23 | "Development Status :: 4 - Beta", 24 | "Intended Audience :: Developers", 25 | "License :: OSI Approved :: MIT License", 26 | "Operating System :: POSIX :: Linux", 27 | "Programming Language :: Python :: 3", 28 | "Programming Language :: Python :: 3.7", 29 | "Programming Language :: Python :: 3.8", 30 | "Programming Language :: Python :: 3.9", 31 | "Programming Language :: Python :: 3.10", 32 | "Programming Language :: Python :: 3.11", 33 | "Programming Language :: Python :: 3 :: Only", 34 | "Topic :: Software Development", 35 | "Topic :: Software Development :: Libraries", 36 | "Topic :: System :: Hardware", 37 | ] 38 | dependencies = [ 39 | "spidev>=3.4", 40 | "numpy" 41 | ] 42 | 43 | [project.urls] 44 | GitHub = "https://www.github.com/pimoroni/st7735-python" 45 | Homepage = "https://www.pimoroni.com" 46 | 47 | [tool.hatch.version] 48 | path = "st7735/__init__.py" 49 | 50 | [tool.hatch.build] 51 | include = [ 52 | "st7735", 53 | "ST7735.py", 54 | "README.md", 55 | "CHANGELOG.md", 56 | "LICENSE" 57 | ] 58 | 59 | [tool.hatch.build.targets.sdist] 60 | include = [ 61 | "*" 62 | ] 63 | exclude = [ 64 | ".*", 65 | "dist" 66 | ] 67 | 68 | [tool.hatch.metadata.hooks.fancy-pypi-readme] 69 | content-type = "text/markdown" 70 | fragments = [ 71 | { path = "README.md" }, 72 | { text = "\n" }, 73 | { path = "CHANGELOG.md" } 74 | ] 75 | 76 | [tool.ruff] 77 | exclude = [ 78 | '.tox', 79 | '.egg', 80 | '.git', 81 | '__pycache__', 82 | 'build', 83 | 'dist' 84 | ] 85 | line-length = 200 86 | 87 | [tool.codespell] 88 | skip = """ 89 | ./.tox,\ 90 | ./.egg,\ 91 | ./.git,\ 92 | ./__pycache__,\ 93 | ./build,\ 94 | ./dist.\ 95 | """ 96 | 97 | [tool.isort] 98 | line_length = 200 99 | 100 | [tool.black] 101 | line-length = 200 102 | 103 | [tool.check-manifest] 104 | ignore = [ 105 | '.stickler.yml', 106 | 'boilerplate.md', 107 | 'check.sh', 108 | 'install.sh', 109 | 'uninstall.sh', 110 | 'Makefile', 111 | 'tox.ini', 112 | 'tests/*', 113 | 'examples/*', 114 | '.coveragerc', 115 | 'requirements-dev.txt' 116 | ] 117 | 118 | [tool.pimoroni] 119 | apt_packages = [] 120 | configtxt = [] 121 | commands = [ 122 | "printf \"Setting up SPI...\n\"", 123 | "sudo raspi-config nonint do_spi 0" 124 | ] 125 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | check-manifest 2 | ruff 3 | codespell 4 | isort 5 | twine 6 | hatch 7 | hatch-fancy-pypi-readme 8 | tox 9 | pdoc 10 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pillow>=10.0.0 2 | numpy>=1.26.0 3 | spidev>=3.4 4 | -------------------------------------------------------------------------------- /st7735/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Adafruit Industries 2 | # Author: Tony DiCola 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), to deal 6 | # in the Software without restriction, including without limitation the rights 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | # copies of the Software, and to permit persons to whom the Software is 9 | # furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in 12 | # all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | # THE SOFTWARE. 21 | import numbers 22 | import time 23 | 24 | import gpiod 25 | import gpiodevice 26 | import numpy as np 27 | import spidev 28 | from gpiod.line import Direction, Value 29 | 30 | __version__ = '1.0.0' 31 | 32 | OUTL = gpiod.LineSettings(direction=Direction.OUTPUT, output_value=Value.INACTIVE) 33 | 34 | BG_SPI_CS_BACK = 0 35 | BG_SPI_CS_FRONT = 1 36 | 37 | SPI_CLOCK_HZ = 16000000 38 | 39 | # Constants for interacting with display registers. 40 | ST7735_TFTWIDTH = 80 41 | ST7735_TFTHEIGHT = 160 42 | 43 | ST7735_COLS = 132 44 | ST7735_ROWS = 162 45 | 46 | ST7735_NOP = 0x00 47 | ST7735_SWRESET = 0x01 48 | ST7735_RDDID = 0x04 49 | ST7735_RDDST = 0x09 50 | 51 | ST7735_SLPIN = 0x10 52 | ST7735_SLPOUT = 0x11 53 | ST7735_PTLON = 0x12 54 | ST7735_NORON = 0x13 55 | 56 | ST7735_INVOFF = 0x20 57 | ST7735_INVON = 0x21 58 | ST7735_DISPOFF = 0x28 59 | ST7735_DISPON = 0x29 60 | 61 | ST7735_CASET = 0x2A 62 | ST7735_RASET = 0x2B 63 | ST7735_RAMWR = 0x2C 64 | ST7735_RAMRD = 0x2E 65 | 66 | ST7735_PTLAR = 0x30 67 | ST7735_MADCTL = 0x36 68 | ST7735_COLMOD = 0x3A 69 | 70 | ST7735_FRMCTR1 = 0xB1 71 | ST7735_FRMCTR2 = 0xB2 72 | ST7735_FRMCTR3 = 0xB3 73 | ST7735_INVCTR = 0xB4 74 | ST7735_DISSET5 = 0xB6 75 | 76 | 77 | ST7735_PWCTR1 = 0xC0 78 | ST7735_PWCTR2 = 0xC1 79 | ST7735_PWCTR3 = 0xC2 80 | ST7735_PWCTR4 = 0xC3 81 | ST7735_PWCTR5 = 0xC4 82 | ST7735_VMCTR1 = 0xC5 83 | 84 | ST7735_RDID1 = 0xDA 85 | ST7735_RDID2 = 0xDB 86 | ST7735_RDID3 = 0xDC 87 | ST7735_RDID4 = 0xDD 88 | 89 | ST7735_GMCTRP1 = 0xE0 90 | ST7735_GMCTRN1 = 0xE1 91 | 92 | ST7735_PWCTR6 = 0xFC 93 | 94 | # Colours for convenience 95 | ST7735_BLACK = 0x0000 # 0b 00000 000000 00000 96 | ST7735_BLUE = 0x001F # 0b 00000 000000 11111 97 | ST7735_GREEN = 0x07E0 # 0b 00000 111111 00000 98 | ST7735_RED = 0xF800 # 0b 11111 000000 00000 99 | ST7735_CYAN = 0x07FF # 0b 00000 111111 11111 100 | ST7735_MAGENTA = 0xF81F # 0b 11111 000000 11111 101 | ST7735_YELLOW = 0xFFE0 # 0b 11111 111111 00000 102 | ST7735_WHITE = 0xFFFF # 0b 11111 111111 11111 103 | 104 | 105 | def color565(r, g, b): 106 | """Convert red, green, blue components to a 16-bit 565 RGB value. Components 107 | should be values 0 to 255. 108 | """ 109 | return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3) 110 | 111 | 112 | def image_to_data(image, rotation=0): 113 | """Generator function to convert a PIL image to 16-bit 565 RGB bytes.""" 114 | # NumPy is much faster at doing this. NumPy code provided by: 115 | # Keith (https://www.blogger.com/profile/02555547344016007163) 116 | pb = np.rot90(np.array(image.convert('RGB')), rotation // 90).astype('uint16') 117 | color = ((pb[:, :, 0] & 0xF8) << 8) | ((pb[:, :, 1] & 0xFC) << 3) | (pb[:, :, 2] >> 3) 118 | return np.dstack(((color >> 8) & 0xFF, color & 0xFF)).flatten().tolist() 119 | 120 | 121 | class ST7735(object): 122 | """Representation of an ST7735 TFT LCD.""" 123 | 124 | def __init__(self, port, cs, dc, backlight=None, rst=None, width=ST7735_TFTWIDTH, 125 | height=ST7735_TFTHEIGHT, rotation=90, offset_left=None, offset_top=None, invert=True, bgr=True, spi_speed_hz=4000000): 126 | """Create an instance of the display using SPI communication. 127 | 128 | Must provide the GPIO pin label for the D/C pin and the SPI driver. 129 | 130 | Can optionally provide the GPIO pin label for the reset pin as the rst parameter. 131 | 132 | :param port: SPI port number 133 | :param cs: SPI chip-select number (0 or 1 for BCM 134 | :param backlight: Pin for controlling backlight 135 | :param rst: Reset pin for ST7735 136 | :param width: Width of display connected to ST7735 137 | :param height: Height of display connected to ST7735 138 | :param rotation: Rotation of display connected to ST7735 139 | :param offset_left: COL offset in ST7735 memory 140 | :param offset_top: ROW offset in ST7735 memory 141 | :param invert: Invert display 142 | :param spi_speed_hz: SPI speed (in Hz) 143 | 144 | """ 145 | 146 | self._spi = spidev.SpiDev(port, cs) 147 | self._spi.mode = 0 148 | self._spi.lsbfirst = False 149 | self._spi.max_speed_hz = spi_speed_hz 150 | 151 | self._width = width 152 | self._height = height 153 | self._rotation = rotation 154 | self._invert = invert 155 | self._bgr = bgr 156 | 157 | self._bl = None 158 | self._rst = None 159 | 160 | # Default left offset to center display 161 | if offset_left is None: 162 | offset_left = (ST7735_COLS - width) // 2 163 | 164 | self._offset_left = offset_left 165 | 166 | # Default top offset to center display 167 | if offset_top is None: 168 | offset_top = (ST7735_ROWS - height) // 2 169 | 170 | self._offset_top = offset_top 171 | 172 | gpiodevice.friendly_errors = True 173 | 174 | # Set DC as output. 175 | self._dc = gpiodevice.get_pin(dc, "st7735-dc", OUTL) 176 | 177 | # Setup backlight as output (if provided). 178 | if backlight is not None: 179 | self._bl = gpiodevice.get_pin(backlight, "st7735-bl", OUTL) 180 | self.set_pin(self._bl, False) 181 | time.sleep(0.1) 182 | self.set_pin(self._bl, True) 183 | 184 | # Setup reset as output (if provided). 185 | if rst is not None: 186 | self._rst = gpiodevice.get_pin(rst, "st7735-rst", OUTL) 187 | 188 | self.reset() 189 | self._init() 190 | 191 | def set_pin(self, pin, state): 192 | lines, offset = pin 193 | lines.set_value(offset, Value.ACTIVE if state else Value.INACTIVE) 194 | 195 | def send(self, data, is_data=True, chunk_size=4096): 196 | """Write a byte or array of bytes to the display. Is_data parameter 197 | controls if byte should be interpreted as display data (True) or command 198 | data (False). Chunk_size is an optional size of bytes to write in a 199 | single SPI transaction, with a default of 4096. 200 | """ 201 | # Set DC low for command, high for data. 202 | self.set_pin(self._dc, is_data) 203 | # Convert scalar argument to list so either can be passed as parameter. 204 | if isinstance(data, numbers.Number): 205 | data = [data & 0xFF] 206 | self._spi.xfer3(data) 207 | 208 | def set_backlight(self, value): 209 | """Set the backlight on/off.""" 210 | if self._bl is not None: 211 | self.set_pin(self._bl, value) 212 | 213 | def display_off(self): 214 | self.command(ST7735_DISPOFF) 215 | 216 | def display_on(self): 217 | self.command(ST7735_DISPON) 218 | 219 | def sleep(self): 220 | self.command(ST7735_SLPIN) 221 | 222 | def wake(self): 223 | self.command(ST7735_SLPOUT) 224 | 225 | @property 226 | def width(self): 227 | return self._width if self._rotation == 0 or self._rotation == 180 else self._height 228 | 229 | @property 230 | def height(self): 231 | return self._height if self._rotation == 0 or self._rotation == 180 else self._width 232 | 233 | def command(self, data): 234 | """Write a byte or array of bytes to the display as command data.""" 235 | self.send(data, False) 236 | 237 | def data(self, data): 238 | """Write a byte or array of bytes to the display as display data.""" 239 | self.send(data, True) 240 | 241 | def reset(self): 242 | """Reset the display, if reset pin is connected.""" 243 | if self._rst is not None: 244 | self.set_pin(self._rst, True) 245 | time.sleep(0.500) 246 | self.set_pin(self._rst, False) 247 | time.sleep(0.500) 248 | self.set_pin(self._rst, True) 249 | time.sleep(0.500) 250 | 251 | def _init(self): 252 | # Initialize the display. 253 | 254 | self.command(ST7735_SWRESET) # Software reset 255 | time.sleep(0.150) # delay 150 ms 256 | 257 | self.command(ST7735_SLPOUT) # Out of sleep mode 258 | time.sleep(0.500) # delay 500 ms 259 | 260 | self.command(ST7735_FRMCTR1) # Frame rate ctrl - normal mode 261 | self.data(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D) 262 | self.data(0x2C) 263 | self.data(0x2D) 264 | 265 | self.command(ST7735_FRMCTR2) # Frame rate ctrl - idle mode 266 | self.data(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D) 267 | self.data(0x2C) 268 | self.data(0x2D) 269 | 270 | self.command(ST7735_FRMCTR3) # Frame rate ctrl - partial mode 271 | self.data(0x01) # Dot inversion mode 272 | self.data(0x2C) 273 | self.data(0x2D) 274 | self.data(0x01) # Line inversion mode 275 | self.data(0x2C) 276 | self.data(0x2D) 277 | 278 | self.command(ST7735_INVCTR) # Display inversion ctrl 279 | self.data(0x07) # No inversion 280 | 281 | self.command(ST7735_PWCTR1) # Power control 282 | self.data(0xA2) 283 | self.data(0x02) # -4.6V 284 | self.data(0x84) # auto mode 285 | 286 | self.command(ST7735_PWCTR2) # Power control 287 | self.data(0x0A) # Opamp current small 288 | self.data(0x00) # Boost frequency 289 | 290 | self.command(ST7735_PWCTR4) # Power control 291 | self.data(0x8A) # BCLK/2, Opamp current small & Medium low 292 | self.data(0x2A) 293 | 294 | self.command(ST7735_PWCTR5) # Power control 295 | self.data(0x8A) 296 | self.data(0xEE) 297 | 298 | self.command(ST7735_VMCTR1) # Power control 299 | self.data(0x0E) 300 | 301 | if self._invert: 302 | self.command(ST7735_INVON) # Invert display 303 | else: 304 | self.command(ST7735_INVOFF) # Don't invert display 305 | 306 | self.command(ST7735_MADCTL) # Memory access control (directions) 307 | if self._bgr: 308 | self.data(0xC8) # row addr/col addr, bottom to top refresh; Set D3 RGB Bit to 1 for format BGR 309 | else: 310 | self.data(0xC0) # row addr/col addr, bottom to top refresh; Set D3 RGB Bit to 0 for format RGB 311 | 312 | self.command(ST7735_COLMOD) # set color mode 313 | self.data(0x05) # 16-bit color 314 | 315 | self.command(ST7735_CASET) # Column addr set 316 | self.data(0x00) # XSTART = 0 317 | self.data(self._offset_left) 318 | self.data(0x00) # XEND = ROWS - height 319 | self.data(self._width + self._offset_left - 1) 320 | 321 | self.command(ST7735_RASET) # Row addr set 322 | self.data(0x00) # XSTART = 0 323 | self.data(self._offset_top) 324 | self.data(0x00) # XEND = COLS - width 325 | self.data(self._height + self._offset_top - 1) 326 | 327 | self.command(ST7735_GMCTRP1) # Set Gamma 328 | self.data(0x02) 329 | self.data(0x1c) 330 | self.data(0x07) 331 | self.data(0x12) 332 | self.data(0x37) 333 | self.data(0x32) 334 | self.data(0x29) 335 | self.data(0x2d) 336 | self.data(0x29) 337 | self.data(0x25) 338 | self.data(0x2B) 339 | self.data(0x39) 340 | self.data(0x00) 341 | self.data(0x01) 342 | self.data(0x03) 343 | self.data(0x10) 344 | 345 | self.command(ST7735_GMCTRN1) # Set Gamma 346 | self.data(0x03) 347 | self.data(0x1d) 348 | self.data(0x07) 349 | self.data(0x06) 350 | self.data(0x2E) 351 | self.data(0x2C) 352 | self.data(0x29) 353 | self.data(0x2D) 354 | self.data(0x2E) 355 | self.data(0x2E) 356 | self.data(0x37) 357 | self.data(0x3F) 358 | self.data(0x00) 359 | self.data(0x00) 360 | self.data(0x02) 361 | self.data(0x10) 362 | 363 | self.command(ST7735_NORON) # Normal display on 364 | time.sleep(0.10) # 10 ms 365 | 366 | self.display_on() 367 | time.sleep(0.100) # 100 ms 368 | 369 | def begin(self): 370 | """Set up the display 371 | 372 | Deprecated. Included in __init__. 373 | 374 | """ 375 | pass 376 | 377 | def set_window(self, x0=0, y0=0, x1=None, y1=None): 378 | """Set the pixel address window for proceeding drawing commands. x0 and 379 | x1 should define the minimum and maximum x pixel bounds. y0 and y1 380 | should define the minimum and maximum y pixel bound. If no parameters 381 | are specified the default will be to update the entire display from 0,0 382 | to width-1,height-1. 383 | """ 384 | if x1 is None: 385 | x1 = self._width - 1 386 | 387 | if y1 is None: 388 | y1 = self._height - 1 389 | 390 | y0 += self._offset_top 391 | y1 += self._offset_top 392 | 393 | x0 += self._offset_left 394 | x1 += self._offset_left 395 | 396 | self.command(ST7735_CASET) # Column addr set 397 | self.data(x0 >> 8) 398 | self.data(x0) # XSTART 399 | self.data(x1 >> 8) 400 | self.data(x1) # XEND 401 | self.command(ST7735_RASET) # Row addr set 402 | self.data(y0 >> 8) 403 | self.data(y0) # YSTART 404 | self.data(y1 >> 8) 405 | self.data(y1) # YEND 406 | self.command(ST7735_RAMWR) # write to RAM 407 | 408 | def display(self, image): 409 | """Write the provided image to the hardware. 410 | 411 | :param image: Should be RGB format and the same dimensions as the display hardware. 412 | 413 | """ 414 | # Set address bounds to entire display. 415 | self.set_window() 416 | # Convert image to array of 16bit 565 RGB data bytes. 417 | # Unfortunate that this copy has to occur, but the SPI byte writing 418 | # function needs to take an array of bytes and PIL doesn't natively 419 | # store images in 16-bit 565 RGB format. 420 | pixelbytes = list(image_to_data(image, self._rotation)) 421 | # Write data to hardware. 422 | self.data(pixelbytes) 423 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | """Test configuration. 2 | These allow the mocking of various Python modules 3 | that might otherwise have runtime side-effects. 4 | """ 5 | import sys 6 | 7 | import mock 8 | import pytest 9 | 10 | 11 | @pytest.fixture(scope="function", autouse=False) 12 | def st7735(): 13 | import st7735 14 | yield st7735 15 | del sys.modules["st7735"] 16 | 17 | 18 | @pytest.fixture(scope="function", autouse=False) 19 | def gpiod(): 20 | """Mock gpiod module.""" 21 | sys.modules["gpiod"] = mock.MagicMock() 22 | sys.modules["gpiod.line"] = mock.MagicMock() 23 | yield sys.modules["gpiod"] 24 | del sys.modules["gpiod"] 25 | 26 | 27 | @pytest.fixture(scope="function", autouse=False) 28 | def gpiodevice(): 29 | """Mock gpiodevice module.""" 30 | sys.modules["gpiodevice"] = mock.MagicMock() 31 | sys.modules["gpiodevice"].get_pin.return_value = (mock.Mock(), 0) 32 | yield sys.modules["gpiodevice"] 33 | del sys.modules["gpiodevice"] 34 | 35 | 36 | @pytest.fixture(scope="function", autouse=False) 37 | def spidev(): 38 | """Mock spidev module.""" 39 | spidev = mock.MagicMock() 40 | sys.modules["spidev"] = spidev 41 | yield spidev 42 | del sys.modules["spidev"] 43 | 44 | 45 | @pytest.fixture(scope="function", autouse=False) 46 | def numpy(): 47 | """Mock numpy module.""" 48 | numpy = mock.MagicMock() 49 | sys.modules["numpy"] = numpy 50 | yield numpy 51 | del sys.modules["numpy"] 52 | -------------------------------------------------------------------------------- /tests/test_dimensions.py: -------------------------------------------------------------------------------- 1 | def test_128_64_0(gpiodevice, gpiod, spidev, numpy, st7735): 2 | display = st7735.ST7735(port=0, cs=0, dc=24, width=128, height=64, rotation=0) 3 | assert display.width == 128 4 | assert display.height == 64 5 | 6 | 7 | def test_128_64_90(gpiodevice, gpiod, spidev, numpy, st7735): 8 | display = st7735.ST7735(port=0, cs=0, dc=24, width=128, height=64, rotation=90) 9 | assert display.width == 64 10 | assert display.height == 128 11 | -------------------------------------------------------------------------------- /tests/test_features.py: -------------------------------------------------------------------------------- 1 | import mock 2 | 3 | 4 | def test_display(gpiodevice, gpiod, spidev, numpy, st7735): 5 | display = st7735.ST7735(port=0, cs=0, dc=24) 6 | numpy.dstack().flatten().tolist.return_value = [0xff, 0x00, 0xff, 0x00] 7 | display.display(mock.MagicMock()) 8 | 9 | spidev.SpiDev().xfer3.assert_called_with([0xff, 0x00, 0xff, 0x00]) 10 | 11 | 12 | def test_color565(gpiodevice, gpiod, spidev, numpy, st7735): 13 | assert st7735.color565(255, 255, 255) == 0xFFFF 14 | 15 | 16 | def test_image_to_data(gpiodevice, gpiod, spidev, numpy, st7735): 17 | numpy.dstack().flatten().tolist.return_value = [] 18 | assert st7735.image_to_data(mock.MagicMock()) == [] 19 | -------------------------------------------------------------------------------- /tests/test_setup.py: -------------------------------------------------------------------------------- 1 | import mock 2 | 3 | 4 | def test_setup(gpiodevice, gpiod, spidev, numpy, st7735): 5 | _ = st7735.ST7735(port=0, cs=0, dc="GPIO24") 6 | 7 | gpiodevice.get_pin.assert_has_calls([ 8 | mock.call("GPIO24", "st7735-dc", st7735.OUTL) 9 | ], any_order=True) 10 | 11 | 12 | def test_setup_no_invert(gpiodevice, gpiod, spidev, numpy, st7735): 13 | _ = st7735.ST7735(port=0, cs=0, dc="GPIO24", invert=False) 14 | 15 | 16 | def test_setup_with_backlight(gpiodevice, gpiod, spidev, numpy, st7735): 17 | display = st7735.ST7735(port=0, cs=0, dc="GPIO24", backlight="GPIO4") 18 | 19 | display.set_backlight(True) 20 | 21 | gpiodevice.get_pin.assert_has_calls([mock.call("GPIO4", "st7735-bl", st7735.OUTL)], any_order=True) 22 | 23 | 24 | def test_setup_with_reset(gpiodevice, gpiod, spidev, numpy, st7735): 25 | _ = st7735.ST7735(port=0, cs=0, dc=24, rst="GPIO4") 26 | 27 | gpiodevice.get_pin.assert_has_calls([mock.call("GPIO4", "st7735-rst", st7735.OUTL)], any_order=True) 28 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py,qa 3 | skip_missing_interpreters = True 4 | isolated_build = true 5 | minversion = 4.0.0 6 | 7 | [testenv] 8 | commands = 9 | coverage run -m pytest -v -r wsx 10 | coverage report 11 | deps = 12 | mock 13 | pytest>=3.1 14 | pytest-cov 15 | build 16 | 17 | [testenv:qa] 18 | commands = 19 | check-manifest 20 | python -m build --no-isolation 21 | python -m twine check dist/* 22 | isort --check . 23 | ruff . 24 | codespell . 25 | deps = 26 | check-manifest 27 | ruff 28 | codespell 29 | isort 30 | twine 31 | build 32 | hatch 33 | hatch-fancy-pypi-readme 34 | 35 | -------------------------------------------------------------------------------- /uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | FORCE=false 4 | LIBRARY_NAME=`grep -m 1 name pyproject.toml | awk -F" = " '{print substr($2,2,length($2)-2)}'` 5 | RESOURCES_DIR=$HOME/Pimoroni/$LIBRARY_NAME 6 | PYTHON="python" 7 | 8 | 9 | venv_check() { 10 | PYTHON_BIN=`which $PYTHON` 11 | if [[ $VIRTUAL_ENV == "" ]] || [[ $PYTHON_BIN != $VIRTUAL_ENV* ]]; then 12 | printf "This script should be run in a virtual Python environment.\n" 13 | exit 1 14 | fi 15 | } 16 | 17 | user_check() { 18 | if [ $(id -u) -eq 0 ]; then 19 | printf "Script should not be run as root. Try './uninstall.sh'\n" 20 | exit 1 21 | fi 22 | } 23 | 24 | confirm() { 25 | if $FORCE; then 26 | true 27 | else 28 | read -r -p "$1 [y/N] " response < /dev/tty 29 | if [[ $response =~ ^(yes|y|Y)$ ]]; then 30 | true 31 | else 32 | false 33 | fi 34 | fi 35 | } 36 | 37 | prompt() { 38 | read -r -p "$1 [y/N] " response < /dev/tty 39 | if [[ $response =~ ^(yes|y|Y)$ ]]; then 40 | true 41 | else 42 | false 43 | fi 44 | } 45 | 46 | success() { 47 | echo -e "$(tput setaf 2)$1$(tput sgr0)" 48 | } 49 | 50 | inform() { 51 | echo -e "$(tput setaf 6)$1$(tput sgr0)" 52 | } 53 | 54 | warning() { 55 | echo -e "$(tput setaf 1)$1$(tput sgr0)" 56 | } 57 | 58 | printf "$LIBRARY_NAME Python Library: Uninstaller\n\n" 59 | 60 | user_check 61 | venv_check 62 | 63 | printf "Uninstalling for Python 3...\n" 64 | $PYTHON -m pip uninstall $LIBRARY_NAME 65 | 66 | if [ -d $RESOURCES_DIR ]; then 67 | if confirm "Would you like to delete $RESOURCES_DIR?"; then 68 | rm -r $RESOURCES_DIR 69 | fi 70 | fi 71 | 72 | printf "Done!\n" 73 | --------------------------------------------------------------------------------