ST7789: Port examples.

Update the Pico Display examples to use ST7789.

Since LED and Button input was also part of the Pico Display modules, replace with pimoroni.Button and pimoroni.RGBLED.
This commit is contained in:
Phil Howard 2022-05-12 12:45:25 +01:00
parent e870949a27
commit a1f7c2fc2b
5 changed files with 66 additions and 51 deletions

View File

@ -1,7 +1,13 @@
import picodisplay as display # Comment this line out to use PicoDisplay2
# import picodisplay2 as display # Uncomment this line to use PicoDisplay2
import st7789
import qrcode
# Set the display resolution
# in most cases you can swap WIDTH weith HEIGHT for portrait mode
WIDTH, HEIGHT = 240, 135 # Pico Display
# WIDTH, HEIGHT = 320, 240 # Pico Display 2.0
display = st7789.ST7789(WIDTH, HEIGHT, rotate180=False)
def measure_qr_code(size, code):
w, h = code.get_size()
@ -20,22 +26,17 @@ def draw_qr_code(ox, oy, size, code):
display.rectangle(ox + x * module_size, oy + y * module_size, module_size, module_size)
width = display.get_width()
height = display.get_height()
code = qrcode.QRCode()
code.set_text("shop.pimoroni.com")
display.init(bytearray(width * height * 2))
display.set_pen(128, 128, 128)
display.clear()
display.set_pen(0, 0, 0)
size, module_size = measure_qr_code(height, code)
left = int((width // 2) - (size // 2))
top = int((height // 2) - (size // 2))
draw_qr_code(left, top, height, code)
size, module_size = measure_qr_code(HEIGHT, code)
left = int((WIDTH // 2) - (size // 2))
top = int((HEIGHT // 2) - (size // 2))
draw_qr_code(left, top, HEIGHT, code)
display.update()

View File

@ -1,15 +1,24 @@
# This example shows you a simple, non-interrupt way of reading Pico Display's buttons with a loop that checks to see if buttons are pressed.
import picodisplay as display # Comment this line out to use PicoDisplay2
# import picodisplay2 as display # Uncomment this line to use PicoDisplay2
import st7789
import utime
from pimoroni import Button
# Initialise display with a bytearray display buffer
buf = bytearray(display.get_width() * display.get_height() * 2)
display.init(buf)
# Set the display resolution
# in most cases you can swap WIDTH weith HEIGHT for portrait mode
WIDTH, HEIGHT = 240, 135 # Pico Display
# WIDTH, HEIGHT = 320, 240 # Pico Display 2.0
display = st7789.ST7789(WIDTH, HEIGHT, rotate180=False)
display.set_backlight(0.5)
button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)
# sets up a handy function we can call to clear the screen
def clear():
display.set_pen(0, 0, 0)
@ -18,28 +27,28 @@ def clear():
while True:
if display.is_pressed(display.BUTTON_A): # if a button press is detected then...
if button_a.read(): # if a button press is detected then...
clear() # clear to black
display.set_pen(255, 255, 255) # change the pen colour
display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen
display.update() # update the display
utime.sleep(1) # pause for a sec
clear() # clear to black again
elif display.is_pressed(display.BUTTON_B):
elif button_b.read():
clear()
display.set_pen(0, 255, 255)
display.text("Button B pressed", 10, 10, 240, 4)
display.update()
utime.sleep(1)
clear()
elif display.is_pressed(display.BUTTON_X):
elif button_x.read():
clear()
display.set_pen(255, 0, 255)
display.text("Button X pressed", 10, 10, 240, 4)
display.update()
utime.sleep(1)
clear()
elif display.is_pressed(display.BUTTON_Y):
elif button_y.read():
clear()
display.set_pen(255, 255, 0)
display.text("Button Y pressed", 10, 10, 240, 4)

View File

@ -1,13 +1,12 @@
import time
import random
import picodisplay as display # Comment this line out to use PicoDisplay2
# import picodisplay2 as display # Uncomment this line to use PicoDisplay2
import st7789
width = display.get_width()
height = display.get_height()
# Set the display resolution, in most cases you can flip these for portrait mode
WIDTH, HEIGHT = 240, 135 # Pico Display
# WIDTH, HEIGHT = 320, 240 # Pico Display 2.0
display_buffer = bytearray(width * height * 2) # 2-bytes per pixel (RGB565)
display.init(display_buffer)
display = st7789.ST7789(WIDTH, HEIGHT, rotate180=False)
display.set_backlight(1.0)
@ -28,8 +27,8 @@ for i in range(0, 100):
r = random.randint(0, 10) + 3
balls.append(
Ball(
random.randint(r, r + (width - 2 * r)),
random.randint(r, r + (height - 2 * r)),
random.randint(r, r + (WIDTH - 2 * r)),
random.randint(r, r + (HEIGHT - 2 * r)),
r,
(14 - r) / 2,
(14 - r) / 2,
@ -45,9 +44,9 @@ while True:
ball.x += ball.dx
ball.y += ball.dy
xmax = width - ball.r
xmax = WIDTH - ball.r
xmin = ball.r
ymax = height - ball.r
ymax = HEIGHT - ball.r
ymin = ball.r
if ball.x < xmin or ball.x > xmax:

View File

@ -1,14 +1,19 @@
# This example borrows a CircuitPython hsv_to_rgb function to cycle through some rainbows on Pico Display's screen and RGB LED . If you're into rainbows, HSV (Hue, Saturation, Value) is very useful!
import utime
import picodisplay as display # Comment this line out to use PicoDisplay2
# import picodisplay2 as display # Uncomment this line to use PicoDisplay2
import st7789
from pimoroni import RGBLED
# Set up and initialise Pico Display
buf = bytearray(display.get_width() * display.get_height() * 2)
display.init(buf)
# Set the display resolution
# in most cases you can swap WIDTH weith HEIGHT for portrait mode
WIDTH, HEIGHT = 240, 135 # Pico Display
# WIDTH, HEIGHT = 320, 240 # Pico Display 2.0
display = st7789.ST7789(WIDTH, HEIGHT, rotate180=False)
display.set_backlight(0.8)
led = RGBLED(6, 7, 8)
# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
@ -39,7 +44,7 @@ h = 0
while True:
h += 1
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
display.set_led(r, g, b) # Set LED to a converted HSV value
led.set_rgb(r, g, b) # Set LED to a converted HSV value
display.set_pen(r, g, b) # Set pen to a converted HSV value
display.clear() # Fill the screen with the colour
display.set_pen(0, 0, 0) # Set pen to black

View File

@ -3,17 +3,21 @@
import machine
import utime
import gc
# Pico Display boilerplate
import picodisplay as display # Comment this line out to use PicoDisplay2
# import picodisplay2 as display # Uncomment this line to use PicoDisplay2
import st7789
from pimoroni import RGBLED
width = display.get_width()
height = display.get_height()
gc.collect()
display_buffer = bytearray(width * height * 2)
display.init(display_buffer)
# Set the display resolution
# in most cases you can swap WIDTH weith HEIGHT for portrait mode
WIDTH, HEIGHT = 135, 240 # Pico Display
# WIDTH, HEIGHT = 320, 240 # Pico Display 2.0
display = st7789.ST7789(WIDTH, HEIGHT, rotate180=False)
# Set the display backlight to 50%
display.set_backlight(0.5)
led = RGBLED(6, 7, 8)
# reads from Pico's temp sensor and converts it into a more manageable number
sensor_temp = machine.ADC(4)
@ -22,9 +26,6 @@ temp_min = 10
temp_max = 30
bar_width = 5
# Set the display backlight to 50%
display.set_backlight(0.5)
temperatures = []
colors = [(0, 0, 255), (0, 255, 0), (255, 255, 0), (255, 0, 0)]
@ -62,7 +63,7 @@ while True:
temperatures.append(temperature)
# shifts the temperatures history to the left by one sample
if len(temperatures) > width // bar_width:
if len(temperatures) > WIDTH // bar_width:
temperatures.pop(0)
i = 0
@ -71,14 +72,14 @@ while True:
display.set_pen(*temperature_to_color(t))
# draws the reading as a tall, thin rectangle
display.rectangle(i, height - (round(t) * 4), bar_width, height)
display.rectangle(i, HEIGHT - (round(t) * 4), bar_width, HEIGHT)
# the next tall thin rectangle needs to be drawn
# "bar_width" (default: 5) pixels to the right of the last one
i += bar_width
# heck lets also set the LED to match
display.set_led(*temperature_to_color(temperature))
led.set_rgb(*temperature_to_color(temperature))
# draws a white background for the text
display.set_pen(255, 255, 255)