Raspberry Pi Pico 2/CircuitPython 9 display on 1.54" 240x240 ST7789 SPI IPS
CircuitPython 9.2.0 run on
Raspberry Pi Pico 2 (tested work on Pico 2 W also) to display on
1.54" 240x240 ST7789 SPI IPS.
Connection:
Code:
cpy_pico2_st7789.py, with color test and scrolling text.
"""
Raspberry Pi Pico 2/CircuitPython 9
to display on 1.54" 240x240 ST7789 SPI IPS
Connection:
-----------
GND GND
VCC 3V3
SCL GP18
SDA GP19
RES GP20
DC GP21
CS GP17
BLK GP22
CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy
- adafruit_display_text folder
"""
import os, sys
import microcontroller
import board
import busio
import displayio
import terminalio
import adafruit_st7789
from adafruit_display_text import label
import time
disp_sck = board.GP18
disp_mosi = board.GP19
disp_res = board.GP20
disp_dc = board.GP21
disp_cs = board.GP17
disp_blk = board.GP22
# Release any resources currently in use for the displays
displayio.release_displays()
disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)
display_bus = displayio.FourWire(spi_bus=disp_spi,
command=disp_dc,
chip_select=disp_cs,
reset=disp_res)
display = adafruit_st7789.ST7789(display_bus,
width=240,
height=240,
rowstart=80,
rotation=180,
backlight_pin=disp_blk)
sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
"\nrun on " + os.uname()[4] +\
"\n" + adafruit_st7789.__name__ + " " + adafruit_st7789.__version__
print("====================================")
print(sys_info_text)
print("====================================")
print()
# Make the display context
bgGroup = displayio.Group()
display.root_group = bgGroup
bg_bitmap = displayio.Bitmap(display.width, display.height, 1) # with one color
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)
colorSet = ((0xFF0000, "RED"),
(0x00FF00, "GREEN"),
(0x0000FF, "BLUE"),
(0xFFFFFF, "WHITE"),
(0x000000, "BLACK"))
color_bitmap = displayio.Bitmap(display.width-2, display.height-2, 1) # with one color
color_palette = displayio.Palette(1)
color_palette[0] = 0x000000 # BLACK
color_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=1, y=1)
bgGroup.append(color_sprite)
color_text_group = displayio.Group(scale=3, x=20, y=120)
color_text_area = label.Label(terminalio.FONT, text="", color=0xFFFFFF)
color_text_group.append(color_text_area)
bgGroup.append(color_text_group)
# Color Test
time.sleep(0.5)
for i in colorSet:
time.sleep(2)
color_palette[0] = i[0]
color_text_area.text = i[1]
color_text_area.color = i[0] ^ 0xFFFFFF
time.sleep(2)
bgGroup.remove(color_text_group)
# scrolling text
# ref:
# https://learn.adafruit.com/rgb-led-matrices-matrix-panels-with-circuitpython/example-simple-two-line-text-scroller
def scroll(line):
line.x = line.x - 1
line_width = line.bounding_box[2]
if line.x < -line_width:
line.x = display.width
def reverse_scroll(line):
line.x = line.x + 1
line_width = line.bounding_box[2]
if line.x >= display.width:
line.x = -line_width
# Scrolling text
sys_info_group = displayio.Group(scale=3, x=0, y=20)
sys_info_area = label.Label(terminalio.FONT, text=sys_info_text, color=0xFFFFFF)
sys_info_group.append(sys_info_area)
bgGroup.append(sys_info_group)
info1_group = displayio.Group(scale=2, x=0, y=220)
info1_text = "coXXect.blogspot.com"
info1_area = label.Label(terminalio.FONT, text=info1_text, color=0xFFFF00)
info1_group.append(info1_area)
bgGroup.append(info1_group)
scroll_end = time.monotonic() + 30 # Scroll text for 15 second
#while time.monotonic() < scroll_end:
while True:
scroll(sys_info_area)
reverse_scroll(info1_area)
display.refresh(minimum_frames_per_second=0)
bgGroup.remove(sys_info_group)
bgGroup.remove(info1_group)
cpy_pico2_st7789_star.py, draw animated star using vectorio.Polygon.
"""
Raspberry Pi Pico 2/CircuitPython 9
to display on 1.54" 240x240 ST7789 SPI IPS
Draw stars using vectorio.Polygon
vectorio – Lightweight 2D shapes for displays,
provide simple filled drawing primitives for use with displayio.
https://docs.circuitpython.org/en/latest/shared-bindings/vectorio/
Connection:
-----------
GND GND
VCC 3V3
SCL GP18
SDA GP19
RES GP20
DC GP21
CS GP17
BLK GP22
CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy
"""
import os, sys
import microcontroller
import board
import busio
import displayio
import terminalio
import adafruit_st7789
import time
import math
import vectorio
disp_sck = board.GP18
disp_mosi = board.GP19
disp_res = board.GP20
disp_dc = board.GP21
disp_cs = board.GP17
disp_blk = board.GP22
def gen_star_coords_list(num_of_ends, r1, r2, angle=0):
# Polygon needs at least 3 points
# so num_of_ends at least 2
half_angle_per_end = (360/num_of_ends)/2
coords_list = []
for end in range(num_of_ends):
angle_in_radians = math.radians((360 * end/num_of_ends) + angle)
coords_list.append((int(r1 * math.sin(angle_in_radians)),
int(r1 * math.cos(angle_in_radians))))
angle_in_radians = math.radians((360 * end/num_of_ends) + half_angle_per_end + angle)
coords_list.append((int(r2 * math.sin(angle_in_radians)),
int(r2 * math.cos(angle_in_radians))))
return coords_list
# Release any resources currently in use for the displays
displayio.release_displays()
disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)
display_bus = displayio.FourWire(spi_bus=disp_spi,
command=disp_dc,
chip_select=disp_cs,
reset=disp_res)
display = adafruit_st7789.ST7789(display_bus,
width=240,
height=240,
rowstart=80,
rotation=180,
backlight_pin=disp_blk)
sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
"\nrun on " + os.uname()[4] +\
"\n" + adafruit_st7789.__name__ + " " + adafruit_st7789.__version__
print("====================================")
print(sys_info_text)
print("====================================")
print()
# Make the display context
bgGroup = displayio.Group()
display.root_group = bgGroup
num_of_ends = 6
min_r1 = 50
max_r1 = 110
r1_step = +1
r1 = min_r1
r2 = 30
angle = 0
angle_step = 5
color_min = 0x10
color_max = 0xF0
r_step = +5
g_step = -5
b_step = -5
r = color_min
g = 0x00 #color_max
b = color_max
center_x = int(display.width/2)
center_y = int(display.height/2)
palette = displayio.Palette(1)
palette[0] = r * 0x010000 + g * 0x0100 + b
# Generate the 1st star
star_points= gen_star_coords_list(num_of_ends, r1, r2, angle)
star_polygon = vectorio.Polygon(pixel_shader=palette, points=star_points, x=center_x, y=center_y)
bgGroup.append(star_polygon)
time.sleep(1)
while True:
angle = angle + angle_step
if angle >= 360:
angle = angle - 360
r1 = r1 + r1_step
if (r1 >= max_r1) or (r1 < min_r1):
r1_step = r1_step * -1 # reverse
r = r + r_step
if (r >= color_max) or (r < color_min):
r_step = r_step * -1 # reverse
"""
g = g + g_step
if (g >= color_max) or (g < color_min):
g_step = g_step * -1 # reverse
"""
b = b + b_step
if (b >= color_max) or (b < color_min):
b_step = b_step * -1 # reverse
palette[0] = r * 0x010000 + g * 0x0100 + b
star_polygon.points= gen_star_coords_list(num_of_ends, r1, r2, angle)
time.sleep(0.2)
print("~ bye ~")
cpy_pico2_st7789_star2.py, animated star, with adafruit_display_text and adafruit_display_shapes.roundrect added.
"""
Raspberry Pi Pico 2/CircuitPython 9
to display on 1.54" 240x240 ST7789 SPI IPS
Draw stars using vectorio.Polygon,
adafruit_display_text and adafruit_display_shapes.roundrect added.
Connection:
-----------
GND GND
VCC 3V3
SCL GP18
SDA GP19
RES GP20
DC GP21
CS GP17
BLK GP22
CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy
- adafruit_display_text folder
- adafruit_display_shapes folder
"""
import os, sys
import microcontroller
import board
import busio
import displayio
import terminalio
import adafruit_st7789
import time
import math
import vectorio
from adafruit_display_text import label
from adafruit_display_shapes.roundrect import RoundRect
disp_sck = board.GP18
disp_mosi = board.GP19
disp_res = board.GP20
disp_dc = board.GP21
disp_cs = board.GP17
disp_blk = board.GP22
def gen_star_coords_list(num_of_ends, r1, r2, angle=0):
# Polygon needs at least 3 points
# so num_of_ends at least 2
half_angle_per_end = (360/num_of_ends)/2
coords_list = []
for end in range(num_of_ends):
angle_in_radians = math.radians((360 * end/num_of_ends) + angle)
coords_list.append((int(r1 * math.sin(angle_in_radians)),
int(r1 * math.cos(angle_in_radians))))
angle_in_radians = math.radians((360 * end/num_of_ends) + half_angle_per_end + angle)
coords_list.append((int(r2 * math.sin(angle_in_radians)),
int(r2 * math.cos(angle_in_radians))))
return coords_list
# Release any resources currently in use for the displays
displayio.release_displays()
disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)
display_bus = displayio.FourWire(spi_bus=disp_spi,
command=disp_dc,
chip_select=disp_cs,
reset=disp_res)
display = adafruit_st7789.ST7789(display_bus,
width=240,
height=240,
rowstart=80,
rotation=180,
backlight_pin=disp_blk)
sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
"\nrun on " + os.uname()[4] +\
"\n" + adafruit_st7789.__name__ + " " + adafruit_st7789.__version__
print("====================================")
print(sys_info_text)
print("====================================")
print()
# Make the display context
bgGroup = displayio.Group()
display.root_group = bgGroup
# Prepare a title label inside a RoundRect
scale = 4
group_title = displayio.Group(scale=scale)
label_title = label.Label(terminalio.FONT, text="coXXect", 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 = RoundRect(x=-2, y=-2,
width=label_title_width+4,
height=label_title_height+4,
r=5,
fill=0x000000,
outline=0xFFFFFF, stroke=1)
group_title.x = (display.width-(label_title_width*scale))//2 # center horizontal
group_title.y = 50
group_title.append(shape_title)
group_title.append(label_title)
bgGroup.append(group_title)
# End of title label
num_of_ends = 5
min_r1 = 50
max_r1 = 110
r1_step = +1
r1 = min_r1
r2 = 30
angle = 0
angle_step = 5
color_min = 0x10
color_max = 0xF0
r_step = +5
g_step = -5
b_step = -5
r = color_min
g = 0xFF #color_max
b = color_max
center_x = int(display.width/2) + 30
center_y = int(display.height/2) + 30
palette = displayio.Palette(1)
palette[0] = r * 0x010000 + g * 0x0100 + b
# Generate the 1st star
star_points= gen_star_coords_list(num_of_ends, r1, r2, angle)
star_polygon = vectorio.Polygon(pixel_shader=palette, points=star_points, x=center_x, y=center_y)
bgGroup.append(star_polygon)
time.sleep(1)
while True:
angle = angle + angle_step
if angle >= 360:
angle = angle - 360
r1 = r1 + r1_step
if (r1 >= max_r1) or (r1 < min_r1):
r1_step = r1_step * -1 # reverse
r = r + r_step
if (r >= color_max) or (r < color_min):
r_step = r_step * -1 # reverse
"""
g = g + g_step
if (g >= color_max) or (g < color_min):
g_step = g_step * -1 # reverse
"""
b = b + b_step
if (b >= color_max) or (b < color_min):
b_step = b_step * -1 # reverse
palette[0] = r * 0x010000 + g * 0x0100 + b
star_polygon.points= gen_star_coords_list(num_of_ends, r1, r2, angle)
time.sleep(0.2)
print("~ bye ~")
cpy_pico2_st7789_circle.py, visual illusion circles.
"""
Raspberry Pi Pico 2/CircuitPython 9
to display on 1.54" 240x240 ST7789 SPI IPS
Draw visual illusion circle
Connection:
-----------
GND GND
VCC 3V3
SCL GP18
SDA GP19
RES GP20
DC GP21
CS GP17
BLK GP22
CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy
- adafruit_display_shapes folder
"""
import os, sys
import microcontroller
import board
import busio
import displayio
import adafruit_st7789
import time
from adafruit_display_shapes.circle import Circle
disp_sck = board.GP18
disp_mosi = board.GP19
disp_res = board.GP20
disp_dc = board.GP21
disp_cs = board.GP17
disp_blk = board.GP22
# Release any resources currently in use for the displays
displayio.release_displays()
disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)
display_bus = displayio.FourWire(spi_bus=disp_spi,
command=disp_dc,
chip_select=disp_cs,
reset=disp_res)
display = adafruit_st7789.ST7789(display_bus,
width=240,
height=240,
rowstart=80,
rotation=180,
backlight_pin=disp_blk)
sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
"\nrun on " + os.uname()[4] +\
"\n" + adafruit_st7789.__name__ + " " + adafruit_st7789.__version__
print("====================================")
print(sys_info_text)
print("====================================")
print("All the circles have the same gray level. What you see?")
print()
# Make the display context
bgGroup = displayio.Group()
display.root_group = bgGroup
num_of_color = 240
bg_bitmap = displayio.Bitmap(display.width, display.height, num_of_color)
bg_palette = displayio.Palette(num_of_color)
for i in range(num_of_color):
bg_palette[i] = i*0x010000 + i*0x0100 + i
for y in range(display.height):
for x in range(display.width):
bg_bitmap[x, y] = (x+y) >> 1
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)
all_circle_the_same_gray = 0x808080
bgGroup.append(Circle(30, 30, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(120, 30, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(210, 30, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(30, 120, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(120, 120, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(210, 120, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(30, 210, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(120, 210, 20, fill=all_circle_the_same_gray))
bgGroup.append(Circle(210, 210, 20, fill=all_circle_the_same_gray))
time.sleep(2)
for i in range(num_of_color):
bg_palette[num_of_color-i-1] = i*0x010000 + i*0x0100 + i
time.sleep(2)
for i in range(num_of_color):
bg_palette[i] = i*0x010000 + i*0x0100 + i
time.sleep(2)
for change_dir in range(num_of_color, 0, -1):
for i in range(change_dir):
bg_palette[i] = (bg_palette[i]+0x010101) & 0xFFFFFF
for i in range(change_dir, num_of_color):
bg_palette[i] = (bg_palette[i]-0x010101) & 0xFFFFFF
print("~ bye ~")
Corresponding exercise run on MicroPython:
~ Raspberry Pi Pico 2/MicroPython display on 1.54" 240x240 ST7789 SPI IPS
Next:
~ Raspberry Pi Pico 2/CircuitPython 9 display bmp on ST7789 LCD using adafruit_imageload/adafruit_slideshow
Comments
Post a Comment