MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

boochow/MicroPython-ST7735 is a modified version of GuyCarver's ST7735.py ST7735 TFT LCD driver, for micropython-esp32.

This video show steps to prepare boochow/MicroPython-ST7735 driver and font files. Tested on Espressif ESP32-C3-DevKitM-1 running MicroPython v1.19.1, to display on 1.8 inch 128x160 ST7735 SPI TFT.


Connection

VCC     3V3
GND     GND
CS      4
RESET   5
A0      9
SDA     7
SCK     6
LED     3V3
Exercise

graphicstest.py, modified to match our connection.
from ST7735 import TFT
from sysfont import sysfont
from machine import SPI,Pin
import time
import math

tft_CS = 4
tft_RESET=5
tft_A0=9
tft_SDA=7
tft_SCK=6

spi = SPI(1, baudrate=20000000, polarity=0, phase=0, miso=None)
tft=TFT(spi,tft_A0,tft_RESET,tft_CS)
tft.initr()
tft.rgb(True)

def testlines(color):
    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((0,0),(x, tft.size()[1] - 1), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((0,0),(tft.size()[0] - 1, y), color)

    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((tft.size()[0] - 1, 0), (x, tft.size()[1] - 1), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((tft.size()[0] - 1, 0), (0, y), color)

    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((0, tft.size()[1] - 1), (x, 0), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((0, tft.size()[1] - 1), (tft.size()[0] - 1,y), color)

    tft.fill(TFT.BLACK)
    for x in range(0, tft.size()[0], 6):
        tft.line((tft.size()[0] - 1, tft.size()[1] - 1), (x, 0), color)
    for y in range(0, tft.size()[1], 6):
        tft.line((tft.size()[0] - 1, tft.size()[1] - 1), (0, y), color)

def testfastlines(color1, color2):
    tft.fill(TFT.BLACK)
    for y in range(0, tft.size()[1], 5):
        tft.hline((0,y), tft.size()[0], color1)
    for x in range(0, tft.size()[0], 5):
        tft.vline((x,0), tft.size()[1], color2)

def testdrawrects(color):
    tft.fill(TFT.BLACK);
    for x in range(0,tft.size()[0],6):
        tft.rect((tft.size()[0]//2 - x//2, tft.size()[1]//2 - x/2), (x, x), color)

def testfillrects(color1, color2):
    tft.fill(TFT.BLACK);
    for x in range(tft.size()[0],0,-6):
        tft.fillrect((tft.size()[0]//2 - x//2, tft.size()[1]//2 - x/2), (x, x), color1)
        tft.rect((tft.size()[0]//2 - x//2, tft.size()[1]//2 - x/2), (x, x), color2)


def testfillcircles(radius, color):
    for x in range(radius, tft.size()[0], radius * 2):
        for y in range(radius, tft.size()[1], radius * 2):
            tft.fillcircle((x, y), radius, color)

def testdrawcircles(radius, color):
    for x in range(0, tft.size()[0] + radius, radius * 2):
        for y in range(0, tft.size()[1] + radius, radius * 2):
            tft.circle((x, y), radius, color)

def testtriangles():
    tft.fill(TFT.BLACK);
    color = 0xF800
    w = tft.size()[0] // 2
    x = tft.size()[1] - 1
    y = 0
    z = tft.size()[0]
    for t in range(0, 15):
        tft.line((w, y), (y, x), color)
        tft.line((y, x), (z, x), color)
        tft.line((z, x), (w, y), color)
        x -= 4
        y += 4
        z -= 4
        color += 100

def testroundrects():
    tft.fill(TFT.BLACK);
    color = 100
    for t in range(5):
        x = 0
        y = 0
        w = tft.size()[0] - 2
        h = tft.size()[1] - 2
        for i in range(17):
            tft.rect((x, y), (w, h), color)
            x += 2
            y += 3
            w -= 4
            h -= 6
            color += 1100
        color += 100

def tftprinttest():
    tft.fill(TFT.BLACK);
    v = 30
    tft.text((0, v), "Hello World!", TFT.RED, sysfont, 1, nowrap=True)
    v += sysfont["Height"]
    tft.text((0, v), "Hello World!", TFT.YELLOW, sysfont, 2, nowrap=True)
    v += sysfont["Height"] * 2
    tft.text((0, v), "Hello World!", TFT.GREEN, sysfont, 3, nowrap=True)
    v += sysfont["Height"] * 3
    tft.text((0, v), str(1234.567), TFT.BLUE, sysfont, 4, nowrap=True)
    time.sleep_ms(1500)
    tft.fill(TFT.BLACK);
    v = 0
    tft.text((0, v), "Hello World!", TFT.RED, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), str(math.pi), TFT.GREEN, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), " Want pi?", TFT.GREEN, sysfont)
    v += sysfont["Height"] * 2
    tft.text((0, v), hex(8675309), TFT.GREEN, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), " Print HEX!", TFT.GREEN, sysfont)
    v += sysfont["Height"] * 2
    tft.text((0, v), "Sketch has been", TFT.WHITE, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), "running for: ", TFT.WHITE, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), str(time.ticks_ms() / 1000), TFT.PURPLE, sysfont)
    v += sysfont["Height"]
    tft.text((0, v), " seconds.", TFT.WHITE, sysfont)

def test_main():
    tft.fill(TFT.BLACK)
    tft.text((0, 0), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", TFT.WHITE, sysfont, 1)
    time.sleep_ms(1000)

    tftprinttest()
    time.sleep_ms(4000)

    testlines(TFT.YELLOW)
    time.sleep_ms(500)

    testfastlines(TFT.RED, TFT.BLUE)
    time.sleep_ms(500)

    testdrawrects(TFT.GREEN)
    time.sleep_ms(500)

    testfillrects(TFT.YELLOW, TFT.PURPLE)
    time.sleep_ms(500)

    tft.fill(TFT.BLACK)
    testfillcircles(10, TFT.BLUE)
    testdrawcircles(10, TFT.WHITE)
    time.sleep_ms(500)

    testroundrects()
    time.sleep_ms(500)

    testtriangles()
    time.sleep_ms(500)

test_main()

mpyC3_tft.py, my exercise.
"""
MicroPython/Espressif ESP32-C3-DevKitM-1 exercise
+ 1.8" 128x160 TFT ST7735 SPI

using library boochow/MicroPython-ST7735
https://github.com/boochow/MicroPython-ST7735
"""
import os
import sys
import time
import random
import ST7735
from seriffont import seriffont
from sysfont import sysfont
from terminalfont import terminalfont
from machine import SPI

print("====================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("====================================")

tft_CS = 4
tft_RESET=5
tft_A0=9
tft_SDA=7
tft_SCK=6

spi = SPI(1, baudrate=20000000, polarity=0, phase=0, miso=None) # using default sck/mosi
display=ST7735.TFT(spi,tft_A0,tft_RESET,tft_CS)

displaysize = display.size()
print(displaysize)

display.initg() # or initr()/initb() according to your display

display.rotation(1)

display.fill(display.BLACK)
display.text((0,0),
             sys.implementation[0]+' '+os.uname()[3],
             display.WHITE,
             terminalfont)
time.sleep(1)
display.text((0,30),
             "run on "+os.uname()[4],
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont)
time.sleep(3)

#font test
display.fill(display.BLACK)
display.text((0,0),
             "seriffont",
             display.RED,
             seriffont)
display.text((0,10),
             "abcdefghijklmnopqrstuvwxyz",
             display.WHITE,
             seriffont)
display.text((0,30),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display.WHITE,
             seriffont)
display.text((0,50),
             "sysfont",
             display.RED,
             sysfont)
display.text((0,60),
             "abcdefghijklmnopqrstuvwxyz",
             display.WHITE,
             sysfont)
display.text((0,70),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display.WHITE,
             sysfont)
display.text((0,80),
             "terminalfont",
             display.RED,
             terminalfont)
display.text((0,90),
             "abcdefghijklmnopqrstuvwxyz",
             display.WHITE,
             terminalfont)
display.text((0,110),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display.WHITE,
             terminalfont)
time.sleep(5)

display.rotation(0)
display.fill(display.RED)
display.text((10,10),
             "RED",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

display.fill(display.GREEN)
display.text((10,10),
             "GREEN",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

display.fill(display.BLUE)
display.text((10,10),
             "BLUE",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

def sub_Disp_rot_info():
    display.fill(display.BLACK)
    display.fillrect((1,1),
                     (display.size()[0]-2, display.size()[1]-2),
                     display.WHITE)
    display.fillrect((2,2),
                     (display.size()[0]-4, display.size()[1]-4),
                     display.BLACK)
    display.text((5,5),
                 "rotate = "+str(display.rotate),
                 display.WHITE,
                 terminalfont)
    display.text((5,20),
                 str(display.size()),
                 display.WHITE,
                 terminalfont)
    print("rotate = "+str(display.rotate)+" : "+str(display.size())) 
    

#rotation test
display.rotation(0)
sub_Disp_rot_info()
time.sleep(2)

display.rotation(1)
sub_Disp_rot_info()
time.sleep(2)

display.rotation(2)
sub_Disp_rot_info()
time.sleep(2)

display.rotation(3)
sub_Disp_rot_info()
time.sleep(2)


display.fill(display.BLACK)

#Random pixel
for p in range(1000):
    x = random.randint(5, display.size()[0]-10)
    y = random.randint(5, display.size()[1]-10)
    c = ST7735.TFTColor(random.randint(0, 0xFF),
                        random.randint(0, 0xFF),
                        random.randint(0, 0xFF))
    display.pixel((x, y), c)
    
#Random line
for l in range(100):
    display.line((random.randint(5, display.size()[0]-10),
                  random.randint(5, display.size()[1]-10)),
                 (random.randint(5, display.size()[0]-10),
                  random.randint(5, display.size()[1]-10)),
                 ST7735.TFTColor(random.randint(0, 0xFF),
                        random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))

#Random circle
for l in range(20):
    display.circle((random.randint(5, display.size()[0]-10),
                    random.randint(5, display.size()[1]-10)),
                   random.randint(1, 50),
                   ST7735.TFTColor(random.randint(0, 0xFF),
                                   random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))
    
#Random fillcircle
for l in range(20):
    display.fillcircle((random.randint(5, display.size()[0]-10),
                    random.randint(5, display.size()[1]-10)),
                   random.randint(1, 50),
                   ST7735.TFTColor(random.randint(0, 0xFF),
                                   random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))

print("~ bye ~")


Comments

  1. Hi, do you know if the pin connection is the same for ESP32-S3-DevKitC-1U?

    ReplyDelete

Post a Comment

Popular posts from this blog

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module