80x160 ST7735S SPI TFT with Raspberry Pi Pico W/CircuitPython 8 Beta 5
Display on
0.96" 80x160 (RGB) IPS with ST7735S SPI Driver
with Raspberry Pi Pico W/CircuitPython 8.0.0 Beta 5
Prepare Libraries:
Visit https://circuitpython.org/libraries, download and extract
Bundle for Version 8.x matching with your CircuitPython version.
Libraries
need:
adafruit_display_shapes folder
adafruit_display_text folder
adafruit_st7735r.mpy
adafruit_ticks.mpy
Copy
to CircuitPython device's /lib
ST7735 Raspberry Pi Pico W
---------------------------
BLK 3V3
CS GP15
DC GP14
RES GP13
SDA GP11
SCL GP10
VCC 3V3
GND GND
Exercise Code:
cpyPicoW_st7735r_simpletest.py
modified from example st7735r_simpletest.py
"""
Copy from CircuitPython Lib example st7735r_simpletest.py
"""
# 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()
"""
spi = board.SPI()
tft_cs = board.D5
tft_dc = board.D6
display_bus = displayio.FourWire(
spi, command=tft_dc, chip_select=tft_cs, reset=board.D9
)
display = ST7735R(display_bus, width=128, height=128, colstart=2, rowstart=1)
"""
# --- Setup st7789 display---
# Define GPIOs for ST7789
tft_SCL = board.GP10
tft_SDA = board.GP11
tft_RES = board.GP13
tft_DC = board.GP14
tft_CS = board.GP15
tft_width = 160
tft_height = 80
tft_spi = busio.SPI(clock=tft_SCL, MOSI=tft_SDA)
display_bus = displayio.FourWire(tft_spi, command=tft_DC, chip_select=tft_CS, reset=tft_RES)
display = ST7735R(display_bus,
width=tft_width, height=tft_height,
rotation=270,
colstart=26, rowstart=1,
invert=True
)
# ----------------------------
# Make the display context
splash = displayio.Group()
display.show(splash)
color_bitmap = displayio.Bitmap(display.width, display.height, 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(display.width-10, display.height-10, 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 = "Hello World!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=40, y=30)
splash.append(text_area)
while True:
pass
cpyPicoW_st7735r_80x160_hello.py
for color checking
"""
Test on Raspberry Pi Pico W
with CircuitPython 8.0.0 Beta 5
display on 0.96 80x160 IPS with ST7735S SPI driver
for color checking
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_st7735r
import busio
import time
import os
# Release any resources currently in use for the displays
displayio.release_displays()
# --- Setup st7735 display---
# Define GPIOs for ST7735
tft_SCL = board.GP10
tft_SDA = board.GP11
tft_RES = board.GP13
tft_DC = board.GP14
tft_CS = board.GP15
tft_width = 160
tft_height = 80
tft_spi = busio.SPI(clock=tft_SCL, MOSI=tft_SDA)
display_bus = displayio.FourWire(tft_spi, command=tft_DC, chip_select=tft_CS, reset=tft_RES)
#I get the parameters colstart/rowstart by guessing and trying
display = adafruit_st7735r.ST7735R(display_bus,
width=tft_width, height=tft_height,
rotation=270,
colstart=26, rowstart=1,
invert=True
)
# ----------------------------
# Make the display context
splash = displayio.Group()
display.show(splash)
colorset = ((0x000000, "BLACK"),
(0xFF0000, "RED"),
(0x00FF00, "GREEN"),
(0x0000FF, "BLUE"),
(0xFFFFFF, "WHITE"),
)
color_bitmap = displayio.Bitmap(display.width, display.height, 5)
color_palette = displayio.Palette(5)
for i in range(len(colorset)):
print("color_palette[", i, "] =", hex(colorset[i][0]))
color_palette[i] = colorset[i][0]
print("--- start ---")
color_bitmap.fill(0)
bg_sprite = displayio.TileGrid(color_bitmap,
pixel_shader=color_palette,
x=0, y=0)
splash.append(bg_sprite)
# Draw a label
#text = "Hello\nWorld!"
text = os.uname()[4] + "\n" + os.uname()[3] + "\n" + \
adafruit_st7735r.__name__ + " " + adafruit_st7735r.__version__ + "\n" + \
str(display.width) + "x" + str(display.height)
text_area = label.Label(terminalio.FONT,
text=text,
color=0x505050,
x=0, y=10)
splash.append(text_area)
time.sleep(5)
idx = 0
while True:
time.sleep(2)
idx = idx+1
if idx == len(colorset):
idx = 0
newcolor = colorset[idx][0]
newcolorname = colorset[idx][1]
print("idx:", idx, " = ",
"{0:#0{1}x}".format(newcolor,8),
newcolorname)
color_bitmap.fill(idx)
text_area.text=newcolorname
cpyPicoW_st7735r_80x160_ani.py
animate something
"""
Test on Raspberry Pi Pico W
with CircuitPython 8.0.0 Beta 5
display on 0.96 80x160 IPS with ST7735S SPI driver
animate something
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_st7735r
import busio
import time
import os
from adafruit_display_shapes.polygon import Polygon
from adafruit_display_shapes.circle import Circle
import math
import adafruit_ticks
# Release any resources currently in use for the displays
displayio.release_displays()
# --- Setup st7735 display---
# Define GPIOs for ST7735
tft_SCL = board.GP10
tft_SDA = board.GP11
tft_RES = board.GP13
tft_DC = board.GP14
tft_CS = board.GP15
tft_width = 160
tft_height = 80
tft_spi = busio.SPI(clock=tft_SCL, MOSI=tft_SDA)
display_bus = displayio.FourWire(tft_spi, command=tft_DC, chip_select=tft_CS, reset=tft_RES)
#I get the parameters colstart/rowstart by guessing and trying
display = adafruit_st7735r.ST7735R(display_bus,
width=tft_width, height=tft_height,
rotation=270,
colstart=26, rowstart=1,
invert=True
)
# ----------------------------
# Make the display context
splash = displayio.Group()
display.show(splash)
# background with one color only
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000
print("--- start ---")
color_bitmap.fill(0)
bg_sprite = displayio.TileGrid(color_bitmap,
pixel_shader=color_palette,
x=0, y=0)
splash.append(bg_sprite)
# Draw a label
#text = "Hello\nWorld!"
text = os.uname()[4] + "\n" + os.uname()[3] + "\n" + \
adafruit_st7735r.__name__ + " " + adafruit_st7735r.__version__ + "\n" + \
str(display.width) + "x" + str(display.height)
text_area = label.Label(terminalio.FONT,
text=text,
color=0xFFFFFF,
x=0, y=10)
splash.append(text_area)
time.sleep(3)
splash.remove(text_area)
# prepare sharps
center_x = int(display.width/2)
center_y = int(display.height/2)
circle_r = 25
print("center", center_x, ":", center_y)
circle = Circle(center_x, center_y, circle_r,
fill=0xA00000, outline=0xFFFFFF)
#.x & .y point to the upper-left corner
#.x0 & .y0 point to the center
print("circle.x=", circle.x, "\t", "circle.y=", circle.y)
print("circle.x0=", circle.x0, "\t", "circle.y0=", circle.y0)
splash.append(circle)
time.sleep(0.5)
def getPolPt(x, y, r, deg):
rad=math.radians(deg)
px = int(r*math.cos(rad)+center_x)
py = int(r*math.sin(rad)+center_y)
return(int(px),
int(py))
pol=(getPolPt(center_x, center_y, circle_r, 0),
getPolPt(center_x, center_y, circle_r, 72),
getPolPt(center_x, center_y, circle_r, 144),
getPolPt(center_x, center_y, circle_r, 216),
getPolPt(center_x, center_y, circle_r, 288)
)
polygon = Polygon(points=pol,
outline=0xFFFFFF)
splash.append(polygon)
#prepare animation
dur_aniBackground = 250
dur_aniCircle = 50
cur_tick = adafruit_ticks.ticks_ms()
nextTick_aniBackground = adafruit_ticks.ticks_add(cur_tick, dur_aniBackground)
nextTick_aniCircle = adafruit_ticks.ticks_add(cur_tick, dur_aniCircle)
colpart = 0x00
cstep = 3
colStep = +cstep
cirStepX = +3
cirStepY = +2
while True:
cur_tick = adafruit_ticks.ticks_ms()
if adafruit_ticks.ticks_diff(cur_tick, nextTick_aniBackground)>0:
nextTick_aniBackground = adafruit_ticks.ticks_add(cur_tick, dur_aniBackground)
#animate background color by changing color_palette[0]
if colpart<=0x00:
colStep = +cstep
elif colpart>=0xFF:
colStep = -cstep
colpart = colpart+colStep
part = colpart & 0xFF
color_palette[0] = part + part*0x100 + part*0x10000
if adafruit_ticks.ticks_diff(cur_tick, nextTick_aniCircle)>0:
nextTick_aniCircle = adafruit_ticks.ticks_add(cur_tick, dur_aniCircle)
circle.x0 = circle.x0+cirStepX
circle.y0 = circle.y0+cirStepY
if (circle.x0<=0) or (circle.x0>=display.width):
cirStepX = cirStepX * -1
if (circle.y0<=0) or (circle.y0>=display.height):
cirStepY = cirStepY * -1
cpyPicoW_st7735r_80x160_temp.py
Read and display RP2040 internal temperature
"""
Test on Raspberry Pi Pico W
with CircuitPython 8.0.0 Beta 5
display on 0.96 80x160 IPS with ST7735S SPI driver
Read and display RP2040 internal temperature
"""
import board
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_st7735r
import busio
import time
import os
from adafruit_display_shapes.polygon import Polygon
from adafruit_display_shapes.circle import Circle
import math
import adafruit_ticks
import microcontroller
conversion_factor = 3.3 / (65535) # for temperature reading from ADC(4)
# Release any resources currently in use for the displays
displayio.release_displays()
# --- Setup st7735 display---
# Define GPIOs for ST7735
tft_SCL = board.GP10
tft_SDA = board.GP11
tft_RES = board.GP13
tft_DC = board.GP14
tft_CS = board.GP15
tft_width = 160
tft_height = 80
tft_spi = busio.SPI(clock=tft_SCL, MOSI=tft_SDA)
display_bus = displayio.FourWire(tft_spi, command=tft_DC, chip_select=tft_CS, reset=tft_RES)
#I get the parameters colstart/rowstart by guessing and trying
display = adafruit_st7735r.ST7735R(display_bus,
width=tft_width, height=tft_height,
rotation=270,
colstart=26, rowstart=1,
invert=True
)
# ----------------------------
# Make the display context
splash = displayio.Group()
display.show(splash)
# background with one color only
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000
print("--- start ---")
color_bitmap.fill(0)
bg_sprite = displayio.TileGrid(color_bitmap,
pixel_shader=color_palette,
x=0, y=0)
splash.append(bg_sprite)
# Draw a label
text = os.uname()[4] + "\n" + os.uname()[3] + "\n" + \
adafruit_st7735r.__name__ + " " + adafruit_st7735r.__version__ + "\n" + \
str(display.width) + "x" + str(display.height)
text_area = label.Label(terminalio.FONT,
text=text,
color=0xB0B0B0,
x=0, y=10)
splash.append(text_area)
time.sleep(3)
text_area.text = "Read RP2040\ninternal temperature"
temp_area = label.Label(terminalio.FONT,
text="waiting",
color=0xB0B000,
scale=3,
x=10, y=50)
splash.append(temp_area)
dur_readTemp = 1000
cur_tick = adafruit_ticks.ticks_ms()
nextTick_readTemp = adafruit_ticks.ticks_add(cur_tick, dur_readTemp)
while True:
cur_tick = adafruit_ticks.ticks_ms()
if adafruit_ticks.ticks_diff(cur_tick, nextTick_readTemp)>0:
nextTick_readTemp = adafruit_ticks.ticks_add(cur_tick, dur_readTemp)
#read internal temperature, in Celsius
temp_area.text = str(microcontroller.cpu.temperature)
Related:
~ Raspberry Pi Pico W/MicroPython x 0.96" 80x160 ST7735 SPI IPS
Comments
Post a Comment