Raspberry Pi Pico 2/MicroPython display on 1.54" 240x240 ST7789 SPI IPS

Exercise of Raspberry Pi Pico 2 running MicroPython v1.24.0-preview.449 (normal and RISCV version) to display on 1.54" 240x240 ST7789 SPI IPS.


Connection:

Library:

Library russhughes/st7789py_mpy (driver for 320x240, 240x240, 135x240 and 128x128 ST7789 displays written in MicroPython) is used in this exercise, read the video to install it on Pico 2.


Exercise code:

mpy_pico2_st7789_hello.py
"""
Raspberry Pi Pico/MicroPython exercise
to display on 1.54" IPS 240x240 with SPI ST7789 driver

Hello World and color test

Using library: russhughes/st7789py_mpy,
driver for 320x240, 240x240, 135x240 and 128x128 ST7789 displays written in MicroPython.
(https://github.com/russhughes/st7789py_mpy)

Connection:
-----------
GND   GND
VCC   3V3
SCL   GP18
SDA   GP19
RES   GP20
DC    GP21
CS    GP17
BLK   GP22
      GP16 (dummy, not used)
"""

import os, sys
from machine import Pin, SPI
import time
import st7789py as st7789
import vga1_bold_16x32
# more fonts here: https://github.com/russhughes/st7789py_mpy/tree/master/romfonts

disp_sck  = 18 # default SCK of SPI(0)
disp_mosi = 19 # default MOSI of SPI(0)
disp_miso = 16  # not use
disp_res = 20
disp_dc  = 21
disp_cs  = 17
disp_blk = 22

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

DISP_WIDTH = 240
DISP_HEIGHT = 240

# it's found that:
# - cannot set baudrate
# - even I set miso=None, miso will be assigned deault GP16 or previous assigned miso
disp_spi = SPI(0, baudrate=60000000, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=None)
print(disp_spi)

display = st7789.ST7789(disp_spi,
                        DISP_WIDTH, DISP_HEIGHT,
                        reset=Pin(disp_res, Pin.OUT),
                        cs=Pin(disp_cs, Pin.OUT),
                        dc=Pin(disp_dc, Pin.OUT),
                        backlight=Pin(disp_blk, Pin.OUT))
print(st7789.__name__, display.width, "x", display.height)

display.fill(st7789.WHITE)
display.fill_rect(1, 1, display.width-2, display.height-2, st7789.BLACK)

display.text(font = vga1_bold_16x32,
             text = "Hello",
             x0 = 10,
             y0 = 10,
             color = st7789.WHITE,
             background = st7789.BLACK)
display.text(vga1_bold_16x32,
             st7789.__name__,
             10,
             42,
             st7789.WHITE,
             st7789.BLACK)

# display title on last line, center horizontally.
# for 16x32 font
title_text = "- coXXect -"
display.text(vga1_bold_16x32,
             title_text,
             int((display.width - len(title_text)*16)/2),  # center horizontally
             display.height - 32,             # last line
             st7789.WHITE,
             st7789.BLACK)

#color test
color_factor = 255/(240+240)
   
print("RED:\t", end="")
start_time = time.ticks_ms()
for y in range(display.height):
    for x in range(display.width):
        r = int((x + y) * color_factor)
        display.pixel(x, y, st7789.color565(r, 0, 0))
end_time = time.ticks_ms()
print("ticks_diff()", time.ticks_diff(end_time, start_time), "ms")
        
print("GREEN:\t", end="")
start_time = time.ticks_ms()
for y in range(display.height):
    for x in range(display.width):
        g = int((x + y) * color_factor)
        display.pixel(x, y, st7789.color565(0, g, 0))
end_time = time.ticks_ms()
print("ticks_diff()", time.ticks_diff(end_time, start_time), "ms")

print("BLUE:\t", end="")
start_time = time.ticks_ms()
for y in range(display.height):
    for x in range(display.width):
        b = int((x + y) * color_factor)
        display.pixel(x, y, st7789.color565(0, 0, b))
end_time = time.ticks_ms()
print("ticks_diff()", time.ticks_diff(end_time, start_time), "ms")

print("WHITE:\t", end="")
start_time = time.ticks_ms()
for y in range(display.height):
    for x in range(display.width):
        w = int((x + y) * color_factor)
        display.pixel(x, y, st7789.color565(w, w, w))
end_time = time.ticks_ms()
print("ticks_diff()", time.ticks_diff(end_time, start_time), "ms")

print("~ bye ~")


mpy_pico2_st7789_star.py
"""
Raspberry Pi Pico/MicroPython exercise
to display on 1.54" IPS 240x240 with SPI ST7789 driver

Display stars using polygon

Using library: russhughes/st7789py_mpy,
driver for 320x240, 240x240, 135x240 and 128x128 ST7789 displays written in MicroPython.
(https://github.com/russhughes/st7789py_mpy)

Connection:
-----------
GND   GND
VCC   3V3
SCL   GP18
SDA   GP19
RES   GP20
DC    GP21
CS    GP17
BLK   GP22
      GP16 (dummy, not used)
"""

import os, sys
from machine import Pin, SPI
import st7789py as st7789

from array import array
import math
import time

disp_sck  = 18 # default SCK of SPI(0)
disp_mosi = 19 # default MOSI of SPI(0)
disp_miso = 16  # not use
disp_res = 20
disp_dc  = 21
disp_cs  = 17
disp_blk = 22

# === myStarClass class ===
class myStarClass:
    
    def __init__(self, num_of_ends, r1, r2, angle=0):
        self.num_of_ends = num_of_ends
        self.r1 = r1
        self.r2 = r2
        self.angle = angle
        
        self.coords_list = []
        self.reCal_coords_list()
    
    def set_angle(self, angle=0):
        self.angle = angle
    
    def update_coords_list(self, num_of_ends, r1, r2):
        self.num_of_ends = num_of_ends
        self.r1 = r1
        self.r2 = r2
        
        self.coords_list.clear()
        self.reCal_coords_list()
        
    def reCal_coords_list(self):
        
        # Polygon must have at least 3 points.
        half_angle_per_end = (360/self.num_of_ends)/2
        
        for end in range(self.num_of_ends):
            angle_in_radians = math.radians((360 * end/self.num_of_ends))
            self.coords_list.append([int(self.r1 * math.sin(angle_in_radians)),
                                     int(self.r1 * math.cos(angle_in_radians))])
            
            angle_in_radians = math.radians((360 * end/self.num_of_ends) + half_angle_per_end)
            self.coords_list.append([int(self.r2 * math.sin(angle_in_radians)),
                                     int(self.r2 * math.cos(angle_in_radians))])
        self.coords_list.append(self.coords_list[0])  #close polygon
        
    def show(self, screen, offset_x=0, offset_y=0):
        screen.polygon(
            points=self.coords_list,
            x=offset_x, y=offset_y,
            color=st7789.color565(255, 255, 255),
            angle=self.angle)

# === End of myFixedPolygonClass ===

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

DISP_WIDTH = 240
DISP_HEIGHT = 240

# it's found that:
# - cannot set baudrate
# - even I set miso=None, miso will be assigned deault GP16 or previous assigned miso
disp_spi = SPI(0, baudrate=60000000, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=None)
print(disp_spi)

display = st7789.ST7789(disp_spi,
                        DISP_WIDTH, DISP_HEIGHT,
                        reset=Pin(disp_res, Pin.OUT),
                        cs=Pin(disp_cs, Pin.OUT),
                        dc=Pin(disp_dc, Pin.OUT),
                        backlight=Pin(disp_blk, Pin.OUT))
print(st7789.__name__, display.width, "x", display.height)


display.fill(st7789.WHITE)
display.fill_rect(1, 1, display.width-2, display.height-2, st7789.BLACK)

# Star animation
num_of_ends = 3
r1 = 75
r2 = 25
myStar1 = myStarClass(num_of_ends = num_of_ends,
                      r1 = r1, r2 = r2)

myStar1.show(display, int(display.width/2), int(display.height/2))

for num_of_ends in range(3, 11):
    myStar1.update_coords_list(num_of_ends, r1, r2)
    
    display.fill(0)
    myStar1.show(display, int(display.width/2), int(display.height/2))
    
    time.sleep(1)

num_of_ends = 4
myStar1.update_coords_list(num_of_ends, r1, r2)
for angle in range(0, 360, 10):
    myStar1.set_angle(math.radians(angle))
    display.fill(0)
    myStar1.show(display, int(display.width/2), int(display.height/2))
    time.sleep(0.2)

print("~ bye ~")


Next:
Raspberry Pi Pico 2/MicroPython read bmp images and draw on ST7789 display pixel-by-pixel.

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