Table of contents
Overview
- Name: ST7735_TFT_RPI
- Description:
- C++ Library for a TFT SPI LCD, ST7735 Driver
- Dynamic install-able Raspberry Pi C++ library.
- Inverse colour, rotate, sleep, idle & vertical scroll modes supported.
- 8 fonts
- Graphics + print class included.
- 24 bit colour , 16 bit color & bi-color Bitmaps supported.
- Hardware and Software SPI
- Dependency: bcm2835 Library
- Author: Gavin Lyons
- Port of my PIC library at github link.
- Created: May 2021
- Developed on
- Raspberry PI 3 model b
- C++ g++ (Raspbian 8.3.0-6+rpi1) 8.3.0
- Raspbian 10 Buster OS , armv7l Linux 5.10.63-v7
- bcm2835 Library 1.68
Installation
-
Make sure SPI bus is enabled on your raspberry PI
-
Install the dependency bcm2835 Library if not installed (at time of writing latest version is 1.68.)
- The bcm2835 library provides SPI bus, delays and GPIO control.
- Install the C libraries of bcm2835, Installation instructions here
-
Download the STT735_TFT_RPI library
- Open a Terminal in a folder where you want to download,build & test library
- Run following command to download from github.
curl -sL https://codeload.github.com/amrka/ST7735_TFT_RPI/tar.gz/refs/tags/1.6 | tar xz
- Install library. Run "make" to run the makefile to install library, it will be installed to usr/lib and usr/include
cd ST7735_TFT_RPI-1.6
sudo make
Test
- Next step is to test TFT and installed library with the included main.cpp test example file. Wire up your TFT. Next enter the example folder and run the makefile in THAT folder, This second makefile builds the examples file using the just installed library, and creates a test exe file in "bin". Make run will use "sudo" as the bcm2835 requires root permissions by default see here for details you should now see the test routines in that file running on the display. The example file is setup by default for a 128x128 red tab TFT.
cd example/
make
make run
- There are three examples files. The default example file is Tests. To decide which one the makefile(In examples folder) builds simply edit "SRC" variable at top of the makefile. In the "User SRC directory Option Section" at top of file. Pick an example "SRC" directory path and ONE ONLY.
# | example file name | Desc |
---|---|---|
1 | src/Tests | Tests text , fonts, bitmap ,functions |
2 | src/Hello_world | basic use case |
3 | src/Frame_rate_test | FPS speed testing |
Software
API
The API (application programming interface) documentation is at link hosted on github pages and generated by Doxygen.
USER OPTIONS
In the example files. There are 3 sections in "Setup()" function where user can make adjustments to select for SPI type used, PCB type used and screen size.
- USER OPTION 1 GPIO/SPI TYPE
- USER OPTION 2 SCREEN SECTION
- USER OPTION 3 PCB_TYPE, SPI SPEED , SPI_CE_PIN
USER OPTION 1 SPI TYPE / GPIO
This library supports both Hardware SPI and software SPI. The parameters set for TFTSetupGPIO define which is used, by default its Hardware. to switch between the two: set SCLK, SDIN and CS to -1 for HW SPI set them to valid GPIO numbers for SW SPI. HW SPI is far faster and more reliable than SW SPI If user is having reliability issues with SW SPI in some setups. The user can adjust the TFT_HIGHFREQ_DELAY setting in ST7735_TFT.h file, this is a microsecond delay in SW SPI loop. It is set to 0 by default, Increasing it will slow down SW SPI further.
USER OPTION 2 Screen size + Offsets
User can adjust screen pixel height, screen pixel width and x & y screen offsets. These offsets can be used in the event of screen damage or manufacturing errors around edge such as cropped data or defective pixels. The function TFTInitScreenSize sets them.
USER OPTION 3 PCB Version, SPI SPEED , SPI_CE_PIN
TFTInitPCBType method
- Param1 PCB_TYPE
In the main.cpp in USER OPTION 3 PCB_TYPE select your display type. By passing an enum type to function TFTInitPCBType. Default is "TFT_ST7735R_Red". There are 4 types of the ST7735 TFT display supported. If your display works but RGB colors are inverted you may have chosen wrong display type.
Number | Description | Enum label | Tested |
---|---|---|---|
1 | ST7735B controller | TFT_ST7735B | n/a |
2 | ST7735R Green Tab | TFT_ST7735R_Green | n/a |
3 | ST7735R Red Tab | TFT_ST7735R_Red | RED PCB v1.1, 1.44, 128x128 pixels |
4 | ST7735S Black Tab | TFT_ST7735S_Black | RED PCB v1.2, 1.8, 128 x 160 pixels |
- Param2 SPI_Speed (HW SPI Only)
Here the user can pass the SPI Bus freq in Hertz, Maximum 125 Mhz , Minimum 30Khz, The default in file is 8Mhz If you set to 0 .Speed is set to bcm2835 constant BCM2835_SPI_CLOCK_DIVIDER_32. If using SW spi, ignore.
- Param3 SPI_CE_PIN (HW SPI Only)
Which Chip enable pin to use two choices. If using SW spi, ignore. * SPICE0 = 0 * SPICE1 = 1
Files
In example folder: The Main.cpp file contains tests showing library functions. A bitmap data file contains data for bi-color bitmaps and icons tests. The color bitmaps used in testing are in bitmap folder, 3 16-bit and 3 24-bit images.
There are two makefiles
1. At root directory builds and installs library at a system level.
2. Example directory builds example file + installed library to an executable.
Fonts
8 fonts available :
num | name | size xbyy | ASCII range |
---|---|---|---|
1 | Default | 5x8 | Full Extended ASCII 0x00 - 0xFF |
2 | Thick | 7x8 | ASCII 0x20 - 0x5A , no lowercase letters |
3 | Seven segment | 4x8 | ASCII 0x20 - 0x7A |
4 | Wide | 8x8 | ASCII 0x20 - 0x5A , no lowercase letters |
5 | Tiny | 3x8 | ASCII 0x20 - 0x7A |
6 | Homespun | 7x8 | ASCII 0x20 - 0x7A |
7 | Big Nums | 16x32 | ASCII 0x2E-0x3A , Numbers + : . only |
8 | Med Nums | 16x16 | ASCII 0x2E-0x3A , Numbers + : . only |
The fonts 1-6 are byte high(at text size 1) scale-able fonts, The large numerical Fonts, 7 & 8 cannot be scaled.
Bitmap
There are four functions to support drawing bitmaps
Function Name | Colour support | Pixel size | Note |
---|---|---|---|
TFTdrawIcon | bi-colour | 8 x(0-127) | Data vertically addressed |
TFTdrawBitmap | bi-colour | 2048 bytes max | Data horizontally addressed |
TFTdrawBitmap16 | 16 bit color 565 BMP files | 32K max | ------ |
TFTdrawBitmap24 | 24 bit color BMP files | 48K max | Converted by software to 16-bit color |
- Bitmap size in kiloBytes = (screenWidth * screenHeight * bitsPerPixel)/(1024 * 8)
- Pixel size column assumes 128 by 128 screen.
Hardware
Connections as setup in main.cpp test file.
TFT PinNum | Pindesc | RPI HW SPI | RPI SW SPI |
---|---|---|---|
1 | LED | VCC | VCC |
2 | SCLK | SPI_CLK | GPIO5 |
3 | SDA | SPI_MOSI | GPIO6 |
4 | A0/DC | GPIO24 | GPIO24 |
5 | RESET | GPI025 | GPIO25 |
6 | SS/CS | SPI_CE0 | GPIO8 |
7 | GND | GND | GND |
8 | VCC | VCC | VCC |
- Connect LED backlight pin 1 thru a 150R/220R ohm resistor to 3.3/5V VCC.
- This is a 3.3V logic device do NOT connect the I/O logic lines to 5V logic device.
- You can connect VCC to 5V if there is a 3.3 volt regulator on back of TFT module.
- Pick any GPIO you want for SW SPI for HW SPI: reset and DC lines are flexible.
- User can select SPI_CE0 or SPI_CE1 for HW SPI
- Backlight control is left to user.
Output
Output of some of the test routine's. Left to right, top to bottom.
- Different defined colors at default font size 1. Full 16 bit colour 565 available
- Different sizes of default font. Size 2 3 4 & 5 shown.
- Different Fonts at font size 2, fonts 1-5. Are these fonts are scale-able, font 6 not shown.
- Shapes
- More Shapes!
- Bitmap (bi-color) A background and a foreground.
- Clock Demo showing icons, small bitmaps and font 7 "BigNums"
- 24-bit color bitmap test image
- 16-bit color bitmap test image