Merge pull request #973 from pimoroni/examples/pico-display-2-8

Pico Display 2.8": Update examples
pull/979/head
Hel Gibbons 2024-08-01 14:42:27 +01:00 zatwierdzone przez GitHub
commit d06956dd8d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
11 zmienionych plików z 180 dodań i 55 usunięć

Wyświetl plik

@ -80,10 +80,11 @@ We also maintain a C++/CMake boilerplate with GitHub workflows configured for te
* Pico Unicorn Pack - https://shop.pimoroni.com/products/pico-unicorn-pack
* Pico Audio Pack (C++ only) - https://shop.pimoroni.com/products/pico-audio-pack
* Pico Wireless Pack - https://shop.pimoroni.com/products/pico-wireless-pack
* Pico Display 2.0 - https://shop.pimoroni.com/products/pico-display-pack-2-0
* Pico Display 2.0" - https://shop.pimoroni.com/products/pico-display-pack-2-0
* Pico Enviro+ Pack - https://shop.pimoroni.com/products/pico-enviro-pack
* Pico Inky Pack - https://shop.pimoroni.com/products/pico-inky-pack
* Pico GFX Pack - https://shop.pimoroni.com/products/pico-gfx-pack
* Pico Display 2.8" - https://shop.pimoroni.com/products/pico-display-pack-2-8
## SHIMs

Wyświetl plik

@ -1,3 +1,5 @@
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
import time
import random
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P8

Wyświetl plik

@ -1,3 +1,5 @@
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
import qrcode

Wyświetl plik

@ -1,8 +1,10 @@
# 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.
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
import time
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4
from pimoroni import RGBLED
# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0)
@ -15,6 +17,12 @@ button_b = Button(13)
button_x = Button(14)
button_y = Button(15)
# Set up the RGB LED For Display Pack and Display Pack 2.0":
led = RGBLED(6, 7, 8)
# For Display Pack 2.8" uncomment the line below and comment out the line above:
# led = RGBLED(26, 27, 28)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
CYAN = display.create_pen(0, 255, 255)
@ -26,6 +34,7 @@ GREEN = display.create_pen(0, 255, 0)
# sets up a handy function we can call to clear the screen
def clear():
display.set_pen(BLACK)
led.set_rgb(0, 0, 0)
display.clear()
display.update()
@ -37,6 +46,7 @@ while True:
if button_a.read(): # if a button press is detected then...
clear() # clear to black
display.set_pen(WHITE) # change the pen colour
led.set_rgb(255, 255, 255) # set the LED colour to match
display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen
display.update() # update the display
time.sleep(1) # pause for a sec
@ -44,6 +54,7 @@ while True:
elif button_b.read():
clear()
display.set_pen(CYAN)
led.set_rgb(0, 255, 255)
display.text("Button B pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
@ -51,6 +62,7 @@ while True:
elif button_x.read():
clear()
display.set_pen(MAGENTA)
led.set_rgb(255, 0, 255)
display.text("Button X pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
@ -58,12 +70,14 @@ while True:
elif button_y.read():
clear()
display.set_pen(YELLOW)
led.set_rgb(255, 255, 0)
display.text("Button Y pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
clear()
else:
display.set_pen(GREEN)
led.set_rgb(0, 255, 0)
display.text("Press any button!", 10, 10, 240, 4)
display.update()
time.sleep(0.1) # this number is how frequently the Pico checks for button presses

Wyświetl plik

@ -1,4 +1,5 @@
# A customisable Pride flag. (Look in the Tufty 2040 examples for a name badge version!)
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY

Wyświetl plik

@ -1,5 +1,7 @@
# 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!
# This example cycles through some rainbows on Pico Display's screen and RGB LED, using the HSV colour model.
# (If you're into rainbows, HSV (Hue, Saturation, Value) is very useful)
# We're using a RAM intensive 64K colour palette here to get a nice smooth colour transition.
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
import time
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB565
@ -8,11 +10,21 @@ from pimoroni import RGBLED
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB565, rotate=0)
display.set_backlight(0.8)
# set up constants for drawing
WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
# what size steps to take around the colour wheel
OFFSET = 0.0025
# variable to keep track of the hue
h = 0.0
# Set up the RGB LED For Display Pack and Display Pack 2.0":
led = RGBLED(6, 7, 8)
WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
# For Display Pack 2.8" uncomment the following line and comment out the line above:
# led = RGBLED(26, 27, 28)
# From CPython Lib/colorsys.py
@ -39,16 +51,22 @@ def hsv_to_rgb(h, s, v):
return v, p, q
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
led.set_rgb(r, g, b) # Set LED to a converted HSV value
RAINBOW = display.create_pen(r, g, b) # Create pen with converted HSV value
display.set_pen(RAINBOW) # Set pen
display.clear() # Fill the screen with the colour
display.set_pen(BLACK) # Set pen to black
display.text("pico disco!", 10, 10, 240, 6) # Add some text
display.update() # Update the display
# increment the hue each time round the loop
h += OFFSET
# The LED needs to be set using RGB values, so convert HSV to RGB using the hsv_to_rgb() function above
r, g, b = [int(255 * c) for c in hsv_to_rgb(h, 1.0, 1.0)]
led.set_rgb(r, g, b)
# Fill the screen with the chosen hue, we can use PicoGraphics' built in HSV pen function for this
RAINBOW = display.create_pen_hsv(h, 1.0, 1.0)
display.set_pen(RAINBOW)
display.clear()
# Draw some black text
display.set_pen(BLACK)
display.text("pico disco!", 10, 10, 240, 6)
display.update()
time.sleep(1.0 / 60)

Wyświetl plik

@ -0,0 +1,38 @@
# A spinny rainbow wheel. Change up some of the constants below to see what happens.
import math
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2
# Constants for drawing
INNER_RADIUS = 40
OUTER_RADIUS = 120
NUMBER_OF_LINES = 24
HUE_SHIFT = 0.02
ROTATION_SPEED = 2
LINE_THICKNESS = 2
# Set up the display
graphics = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2)
WIDTH, HEIGHT = graphics.get_bounds()
BLACK = graphics.create_pen(0, 0, 0)
# Variables to keep track of rotation and hue positions
r = 0
t = 0
while True:
graphics.set_pen(BLACK)
graphics.clear()
for i in range(0, 360, 360 // NUMBER_OF_LINES):
graphics.set_pen(graphics.create_pen_hsv((i / 360) + t, 1.0, 1.0))
# Draw some lines, offset by the rotation variable
graphics.line(int(WIDTH / 2 + math.cos(math.radians(i + r)) * INNER_RADIUS),
int(HEIGHT / 2 + math.sin(math.radians(i + r)) * INNER_RADIUS),
int(WIDTH / 2 + math.cos(math.radians(i + 90 + r)) * OUTER_RADIUS),
int(HEIGHT / 2 + math.sin(math.radians(i + 90 + r)) * OUTER_RADIUS),
LINE_THICKNESS)
graphics.update()
r += ROTATION_SPEED
t += HUE_SHIFT

Wyświetl plik

@ -1,10 +1,11 @@
# Shows the available RAM. PEN_RGB332 is an 8 bit, fixed 256 colour palette which conserves your RAM.
# Try switching the pen_type to PEN_RGB565 (16 bit, 65K colour) and see the difference!
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
import gc
import time
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB332
# PEN_RGB332 is an 8 bit, fixed 256 colour palette which conserves your RAM.
# Try switching the pen_type to PEN_RGB565 (16 bit, 65K colour) and see the difference!
display = PicoGraphics(DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB332, rotate=0)
# set up constants for drawing
@ -12,6 +13,12 @@ WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
# what size steps to take around the colour wheel
OFFSET = 0.0025
# variable to keep track of the hue
h = 0.0
def free(full=False):
# Calculates RAM usage
@ -26,39 +33,14 @@ def free(full=False):
return (f"Total RAM \n{T} bytes \nUnused RAM \n{F} bytes \n({P} free)")
def hsv_to_rgb(h, s, v):
# From CPython Lib/colorsys.py
if s == 0.0:
return v, v, v
i = int(h * 6.0)
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
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
h += OFFSET
display.set_pen(BLACK)
RAINBOW = display.create_pen(r, g, b) # Create pen with converted HSV value
RAINBOW = display.create_pen_hsv(h, 1.0, 1.0)
display.set_pen(RAINBOW)
display.set_font("bitmap8")
display.text(free(full=True), 0, 0, WIDTH, 3)
display.update()
time.sleep(1.0 / 60)

Wyświetl plik

@ -0,0 +1,61 @@
# Travel through a Windows 3.1-esque starfield, with stars growing as they get 'closer'.
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
import random
# Constants to play with
NUMBER_OF_STARS = 200
TRAVEL_SPEED = 1.2
STAR_GROWTH = 0.12
# Set up our display
graphics = PicoGraphics(display=DISPLAY_PICO_DISPLAY)
WIDTH, HEIGHT = graphics.get_bounds()
BLACK = graphics.create_pen(0, 0, 0)
WHITE = graphics.create_pen(255, 255, 255)
stars = []
def new_star():
# Create a new star, with initial x, y, and size
# Initial x will fall between -WIDTH / 2 and +WIDTH / 2 and y between -HEIGHT/2 and +HEIGHT/2
# These are relative values for now, treating (0, 0) as the centre of the screen.
star = [random.randint(0, WIDTH) - WIDTH // 2, random.randint(0, HEIGHT) - HEIGHT // 2, 0.5]
return star
for i in range(0, NUMBER_OF_STARS):
stars.append(new_star())
while True:
graphics.set_pen(BLACK)
graphics.clear()
graphics.set_pen(WHITE)
for i in range(0, NUMBER_OF_STARS):
# Load a star from the stars list
s = stars[i]
# Update x
s[0] = s[0] * TRAVEL_SPEED
# Update y
s[1] = s[1] * TRAVEL_SPEED
if s[0] <= - WIDTH // 2 or s[0] >= WIDTH // 2 or s[1] <= - HEIGHT // 2 or s[1] >= HEIGHT // 2 or s[2] >= 5:
# This star has fallen off the screen (or rolled dead centre and grown too big!)
# Replace it with a new one
s = new_star()
# Grow the star as it travels outward
s[2] += STAR_GROWTH
# Save the updated star to the list
stars[i] = s
# Draw star, adding offsets to our relative coordinates to allow for (0, 0) being in the top left corner.
graphics.circle(int(s[0]) + WIDTH // 2, int(s[1]) + HEIGHT // 2, int(s[2]))
graphics.update()

Wyświetl plik

@ -1,25 +1,31 @@
# This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Display Pack, along with a little pixelly graph.
# It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book, which is a great read if you're a beginner!
# This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Display Pack.
# It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book.
import machine
import time
from pimoroni import RGBLED
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
# set up the hardware
# set up the display and drawing constants
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0)
sensor_temp = machine.ADC(4)
led = RGBLED(6, 7, 8)
# set the display backlight to 50%
display.set_backlight(0.5)
# set up constants for drawing
WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
WHITE = display.create_pen(255, 255, 255)
# set up the internal temperature sensor
sensor_temp = machine.ADC(4)
# Set up the RGB LED For Display Pack and Display Pack 2.0":
led = RGBLED(6, 7, 8)
# For Display Pack 2.8" uncomment the following line and comment out the line above:
# led = RGBLED(26, 27, 28)
conversion_factor = 3.3 / (65535) # used for calculating a temperature from the raw sensor reading
temp_min = 10

Wyświetl plik

@ -57,7 +57,7 @@ Bear in mind that MicroPython has only 192K of RAM available- a 320x240 pixel di
### Supported Displays
* Pico Display - 240x135 SPI LCD - `DISPLAY_PICO_DISPLAY`
* Pico Display 2 - 320x240 SPI LCD - `DISPLAY_PICO_DISPLAY_2`
* Pico Display 2.0" / 2.8" - 320x240 SPI LCD - `DISPLAY_PICO_DISPLAY_2`
* Tufty 2040 - 320x240 Parallel LCD - `DISPLAY_TUFTY_2040`
* Pico Explorer - 240x240 SPI LCD - `DISPLAY_PICO_EXPLORER`
* Enviro Plus - 240x240 SPI LCD - `DISPLAY_ENVIRO_PLUS`