CircuitPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI

This video demo the following exercise code tested on NodeMCU ESP-C3-32S-Kit running CircuitPython 7.3.2. to display on 1.8 inch 128x160 ST7735 SPI TFT.


Libraries needed:
- adafruit_st7735r.mpy
- adafruit_display_text folder
- adafruit_display_shapes folder

(if you don't know how to download and install CircuitPython Library, read HERE)

Connection:


VCC     3V3
GND     GND
CS      IO1
RESET   IO2
A0      IO6
SDA     IO7
SCK     IO8
LED     3V3

		     ST7735 SPI TFT
		     +------------
    +----------------| VCC
    | +--------------| GND
    | |          +---| CS
    | |        +-|---| RESET
    | |      +-|-|---| A0
    | |    +-|-|-|---| SDA
    | |  +-|-|-|-|---| SCK
    +-|--|-|-|-|-|---| LED
    | |  | | | | |   +--------------
    | |  | | | | |
    | |  | | | | |   NodeMCU ESP-C3-32S-Kit
    | |  | | | | |   +------------------+
    | |  | | | | |   |			|
    | |  | | | | +---| IO1		|
    | |  | | | +-----| IO2		|
    | |  | | |       |			|
    | |  | | |       |			|
    | |  | | |       |			|
    | |  | | +-------| IO6		|
    | |  | +---------| IO7		|
    | |  +-----------| IO8		|
    | +--------------| GND		|
    +----------------| 3V3		|
                     |			|
                     |			|
                     |			|
                     |			|
                     +------------------+
					 


Exercise code:

cpyESP-C3-32S_st7735r_128x160_simpletest.py
modified from CircuitPython Library Example st7735r_128x160_simpletest.py
to match our connection.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text.
"""

import board
import terminalio
import displayio
from adafruit_display_text import label
from adafruit_st7735r import ST7735R
import busio

# Release any resources currently in use for the displays
displayio.release_displays()

#--- init ST7735 ---
disp_width=160
disp_height=128

#connection
tft_CS=board.IO1
tft_RESET=board.IO2
tft_A0=board.IO6
tft_SDA=board.IO7
tft_SCK=board.IO8

tft_spi = busio.SPI(clock=tft_SCK, MOSI=tft_SDA)
display_bus = displayio.FourWire(
    tft_spi, command=tft_A0, chip_select=tft_CS, reset=tft_RESET
)
display = ST7735R(display_bus,
                  width=disp_width, height=disp_height,
                  rotation=90,
                  bgr=True)
#------------------------

# Make the display context
splash = displayio.Group()
display.show(splash)

color_bitmap = displayio.Bitmap(160, 128, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x00FF00  # Bright Green

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(150, 118, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0xAA0088  # Purple
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=5, y=5)
splash.append(inner_sprite)

# Draw a label
text_group = displayio.Group(scale=2, x=11, y=64)
text = "Hello World!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00)
text_group.append(text_area)  # Subgroup for text scaling
splash.append(text_group)

while True:
    pass


cpyESP-C3-32S_st7735r_128x160_colored_labels.py
modified from CircuitPython Library Example st7735r_128x160_colored_labels.py
to match our connection.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This test will initialize the display using displayio and draw 8 colored
labels. Useful for testing the color settings on an unknown display.
"""

import board
import terminalio
import displayio
from adafruit_display_text import label
from adafruit_st7735r import ST7735R
import busio

# Release any resources currently in use for the displays
displayio.release_displays()
#--- init ST7735 ---
disp_width=160
disp_height=128

#connection
tft_CS=board.IO1
tft_RESET=board.IO2
tft_A0=board.IO6
tft_SDA=board.IO7
tft_SCK=board.IO8

tft_spi = busio.SPI(clock=tft_SCK, MOSI=tft_SDA)
display_bus = displayio.FourWire(
    tft_spi, command=tft_A0, chip_select=tft_CS, reset=tft_RESET
)
display = ST7735R(display_bus,
                  width=disp_width, height=disp_height,
                  rotation=90,
                  bgr=True)
#------------------------

# Make the display context
splash = displayio.Group()
display.show(splash)

color_bitmap = displayio.Bitmap(160, 80, 1)
color_palette = displayio.Palette(1)
# write some text in each font color, rgb, cmyk
color_palette[0] = 0x111111  # light grey

text_group_left = displayio.Group(scale=1, x=0, y=6)
text_area_red = label.Label(terminalio.FONT, text="RED", color=0xFF0000)
text_area_green = label.Label(terminalio.FONT, text="\nGREEN", color=0x00FF00)
text_area_blue = label.Label(terminalio.FONT, text="\n\nBLUE", color=0x0000FF)
text_area_white = label.Label(terminalio.FONT, text="\n\n\nWHITE", color=0xFFFFFF)
text_group_left.append(text_area_red)
text_group_left.append(text_area_green)
text_group_left.append(text_area_blue)
text_group_left.append(text_area_white)
splash.append(text_group_left)

text_group_right = displayio.Group(scale=1, x=80, y=6)
text_area_cyan = label.Label(terminalio.FONT, text="CYAN", color=0x00FFFF)
text_group_right.append(text_area_cyan)
text_area_magenta = label.Label(terminalio.FONT, text="\nMAGENTA", color=0xFF00FF)
text_group_right.append(text_area_magenta)
text_area_yellow = label.Label(terminalio.FONT, text="\n\nYELLOW", color=0xFFFF00)
text_group_right.append(text_area_yellow)
text_area_black = label.Label(terminalio.FONT, text="\n\n\nBLACK", color=0x000000)
text_group_right.append(text_area_black)
splash.append(text_group_right)

while True:
    pass


cpyESP-C3-32S_st7735r_128x160_color.py
"""
CircuitPython/NodeMCU ESP-C3-32S-Kit exercise
+ 1.8" 128x160 TFT ST7735 SPI

color demo by changing palette

ported from my exercise cpyS3_ili9341_color.py in
https://coxxect.blogspot.com/2022/08/320x240-ili9341-spi-tft-drived-with.html
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_st7735r
import busio
import time

# Release any resources currently in use for the displays
displayio.release_displays()

print(adafruit_st7735r.__name__,
      adafruit_st7735r.__version__)

#--- init ST7735 ---
disp_width=160
disp_height=128

#connection
tft_CS=board.IO1
tft_RESET=board.IO2
tft_A0=board.IO6
tft_SDA=board.IO7
tft_SCK=board.IO8

tft_spi = busio.SPI(clock=tft_SCK, MOSI=tft_SDA)
display_bus = displayio.FourWire(
    tft_spi, command=tft_A0, chip_select=tft_CS, reset=tft_RESET
)
display = adafruit_st7735r.ST7735R(display_bus,
                                   width=disp_width, height=disp_height,
                                   rotation=90,
                                   bgr=True)
#------------------------

# Make the display context
splash = displayio.Group()
display.show(splash)

NUM_OF_DIV = 8
COLOR_WHITE = 0xFFFFFF
COLOR_BLACK = 0x000000

num_of_color = NUM_OF_DIV*NUM_OF_DIV

# Draw a green background
color_bitmap = displayio.Bitmap(disp_width, disp_height, num_of_color)
color_palette = displayio.Palette(num_of_color)
#color_palette[0] = COLOR_BLACK

col_per_div = int(256/NUM_OF_DIV)

div_width = int(disp_width/NUM_OF_DIV)
div_height = int(disp_height/NUM_OF_DIV)

# prepare color_palette - GREEN x BLUE
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(i*col_per_div*0x0100
                                             + j*col_per_div)

#prepare color_bitmap
for j in range(NUM_OF_DIV):
    for i in range(NUM_OF_DIV):
        for y in range(5, div_height-5):
            for x in range(5, div_width-5):
                p = j*NUM_OF_DIV + i
                #print(i, j, p)
                color_bitmap[i*div_width+x, j*div_height+y] = p

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)

splash.append(bg_sprite)

time.sleep(3)
# change color_palette - RED x GREEN
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(i*col_per_div*0x010000
                                             + j*col_per_div*0x0100)

time.sleep(3)
# change color_palette - BLUE x RED
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(i*col_per_div
                                             + j*col_per_div*0x010000)

time.sleep(3)
# change color_palette - RED only
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div*0x010000)
        
time.sleep(3)
# change color_palette - GREEN only
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div*0x0100)
       
time.sleep(3)
# change color_palette - BLUE only
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div)
       
time.sleep(3)
# change color_palette - gray
for i in range(NUM_OF_DIV):
    for j in range(NUM_OF_DIV):
        color_palette[i*NUM_OF_DIV + j] =int(j*col_per_div*0x010000 +
                                             j*col_per_div*0x0100 +
                                             j*col_per_div)       
       
time.sleep(3)

#while True:
#    pass


cpyESP-C3-32S_st7735r_128x160_aniCircle_2.py
"""
CircuitPython/NodeMCU ESP-C3-32S-Kit exercise
+ 1.8" 128x160 TFT ST7735 SPI

Libs needed:
- adafruit_st7735r.mpy
- adafruit_display_text folder
- adafruit_display_shapes folder

ported from my exercise cpy_rpiPico_displayio_ssd1306_aniCircle_2.py in
https://coxxect.blogspot.com/2022/08/ssd1306-spi-oled-on-raspberry-pi.html
* I'm lazy, just keep it in Black and White:)
"""

import board
import displayio
import terminalio
from adafruit_display_text import label
from adafruit_display_shapes.circle import Circle
import adafruit_st7735r
import time
import random
import busio

displayio.release_displays()

print(adafruit_st7735r.__name__,
      adafruit_st7735r.__version__)

#--- init ST7735 ---


#connection
tft_CS=board.IO1
tft_RESET=board.IO2
tft_A0=board.IO6
tft_SDA=board.IO7
tft_SCK=board.IO8

tft_spi = busio.SPI(clock=tft_SCK, MOSI=tft_SDA)
display_bus = displayio.FourWire(
    tft_spi, command=tft_A0, chip_select=tft_CS, reset=tft_RESET
)
"""
DISP_WIDTH=160
DISP_HEIGHT=128
display = adafruit_st7735r.ST7735R(display_bus,
                                   width=DISP_WIDTH, height=DISP_HEIGHT,
                                   rotation=90,
                                   bgr=True)
"""
DISP_WIDTH=128
DISP_HEIGHT=160
display = adafruit_st7735r.ST7735R(display_bus,
                                   width=DISP_WIDTH, height=DISP_HEIGHT,
                                   rotation=180,
                                   bgr=True)

#------------------------

BORDER = 5

# Make the display context
splash = displayio.Group()
display.show(splash)

bg_bitmap = displayio.Bitmap(DISP_WIDTH, DISP_HEIGHT, 2)
bg_palette = displayio.Palette(2)
bg_palette[0] = 0x000000  # Black
bg_palette[1] = 0xFFFFFF  # White

bg_sprite = displayio.TileGrid(
    bg_bitmap,
    pixel_shader=bg_palette,
    x=0, y=0)

#===========================================
#prepare Label of title
title = "    coXXect    "
group_title = displayio.Group(scale=1)

label_title = label.Label(terminalio.FONT,
                        text=title,
                        color=0xFFFFFF)
label_title.anchor_point = (0.0, 0.0)
label_title.anchored_position = (0, 0)
label_title_width = label_title.bounding_box[2]
label_title_height = label_title.bounding_box[3]
shape_title_r = label_title_width//2

shape_title = Circle(x0=label_title_width//2,
                     y0=label_title_height//2,
                     r=shape_title_r,
                     fill=0x000000,
                     outline=0xFFFFFF, stroke=1)

group_title.x = (DISP_WIDTH-label_title_width)//2
group_title.y = (DISP_HEIGHT-label_title_height)//2
group_title.append(shape_title)
group_title.append(label_title)
#===========================================
splash.append(bg_sprite)
splash.append(group_title)
#===========================================
def background_random():
    global bg_bitmap
    x = random.randrange(DISP_WIDTH)
    y = random.randrange(DISP_HEIGHT)
    c = bg_bitmap[x, y]
    c = not c
    bg_bitmap[x, y] = c


aniXMove = +1
aniYMove = +1
aniXLim = DISP_WIDTH - 1 - shape_title.width

aniYdelta = (DISP_HEIGHT-1)//2 - shape_title_r
aniYLowLim = group_title.y - aniYdelta
aniYUppLim = group_title.y + aniYdelta

TOGGLE_TITLE_NUM = 5
toggle_title_cnt = TOGGLE_TITLE_NUM
TOG_TITLE = [["    coXXect    ", 10],
             ["      ESP32-C3 ", 2],
             ["     ESP32-C3  ", 2],
             ["    ESP32-C3   ", 2],
             ["   ESP32-C3    ", 2],
             ["  ESP32-C3     ", 2],
             [" ESP32-C3      ", 2],
             [" CircuitPython ", 10],
             [" ST7735 TFT    ", 10],
             [" 1.8\" 128x160  ",10]
             ]

toggle_title_st = 0

def title_animation():
    global group_title
    global aniXMove
    global aniYMove
    global aniXLim
    global aniYLim
    
    global label_title
    global toggle_title_cnt
    global toggle_title_st
    
    #Move Title group
    x = group_title.x + aniXMove
    group_title.x = x
    if aniXMove > 0:
        if x >= aniXLim:
            aniXMove = -1
    else:
        if x <= 0:
            aniXMove = +1
            
    y = group_title.y + aniYMove
    group_title.y = y
    if aniYMove > 0:
        if y >= aniYUppLim:
            aniYMove = -1
    else:
        if y <= aniYLowLim:
            aniYMove = +1
            
    toggle_title_cnt = toggle_title_cnt-1
    if toggle_title_cnt <= 0:
        
        toggle_title_st = toggle_title_st+1
        if toggle_title_st >= len(TOG_TITLE):
            toggle_title_st = 0
        label_title.text = TOG_TITLE[toggle_title_st][0]
        toggle_title_cnt = TOG_TITLE[toggle_title_st][1]

BG_CHANGE_DURATION = 0.05
bg_change_nx = time.monotonic() + BG_CHANGE_DURATION
TITLE_CHANGE_DURATION = 0.25
title_change_nx = time.monotonic() + TITLE_CHANGE_DURATION
while True:
    
    now = time.monotonic()
    
    if now >= bg_change_nx:
        bg_change_nx = now+BG_CHANGE_DURATION
        background_random()
    
    if now >= title_change_nx:
        title_change_nx = now+TITLE_CHANGE_DURATION
        title_animation()
    
    time.sleep(0.01)


Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator