Skip to content

Commit

Permalink
Examples: update for Pico Display 2.8"
Browse files Browse the repository at this point in the history
  • Loading branch information
helgibbons committed Jul 16, 2024
1 parent bb51ce5 commit afd652b
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 53 deletions.
2 changes: 2 additions & 0 deletions micropython/examples/pico_display/balls_demo.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions micropython/examples/pico_display/basic_qrcode.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
14 changes: 14 additions & 0 deletions micropython/examples/pico_display/button_test.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -15,6 +17,12 @@
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)
Expand All @@ -26,6 +34,7 @@
# 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()

Expand All @@ -37,33 +46,38 @@ def clear():
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
clear() # clear to black again
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)
clear()
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)
clear()
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
Binary file not shown.
26 changes: 26 additions & 0 deletions micropython/examples/pico_display/hello_world_bitmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Bitmap font demo!
Bitmap fonts are fast but blocky. They are best used for small text.
"""

from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2

display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2)

WIDTH, HEIGHT = display.get_bounds()

hue = 0.0

display.set_font("bitmap8")

for i in range(7):
# create a pen and set the drawing color
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
display.set_pen(PEN_COLOUR)
# draw text
display.text("Hello World", i * WIDTH // 12, i * HEIGHT // 7 + 6, scale=3)
# increment hue
hue += 1.0 / 7

display.update()
87 changes: 87 additions & 0 deletions micropython/examples/pico_display/hello_world_vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
Vector font demo! Vector fonts are slower but smoother. They are best used for large text.
You will need to copy the .af font files to your Pico.
Spicy Soup font originally from https://www.dafont.com/spicy-soup.font
Next Sunday font originally from https://www.dafont.com/next-sunday.font
Coolvetica font originally from https://www.dafont.com/coolvetica.font
Find out how to convert your own fonts to .af here: https://github.com/lowfatcode/alright-fonts
"""

from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_RGB565
from picovector import PicoVector, ANTIALIAS_X16
import time

display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_RGB565)
vector = PicoVector(display)

display.set_backlight(1.0)

WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)

hue = 0.0

# antialiasing draws the vector multiple times for a smoother look
vector.set_antialiasing(ANTIALIAS_X16)

TEXT = "Hello World"

while True:
# reset the hue
hue = 0.0

# clear to black
display.set_pen(BLACK)
display.clear()
# set the vector font and size
vector.set_font("spicy_soup.af", 36)
# draw the text seven times
for i in range(7):
# create a HSV (hue, value, saturation) pen and set the drawing color
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
display.set_pen(PEN_COLOUR)
# draw text
vector.text(TEXT, i * WIDTH // 20, i * HEIGHT // 7 - 5)
# increment hue
hue += 1.0 / 7
display.update()
time.sleep(5)

display.set_pen(BLACK)
display.clear()
vector.set_font("next_sunday.af", 30)
# draw the text ten times, sideways
for i in range(10):
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
display.set_pen(PEN_COLOUR)
vector.text(TEXT, i * WIDTH // 10, HEIGHT, 270)
hue += 1.0 / 10
display.update()
time.sleep(5)

display.set_pen(BLACK)
display.clear()
vector.set_font("coolvetica_rg.af", 44)
# draw the text many times
for i in range(30):
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
display.set_pen(PEN_COLOUR)
vector.text(TEXT, WIDTH // 2, HEIGHT // 2, i * 12)
hue += 1.0 / 30
display.update()
time.sleep(5)

display.set_pen(BLACK)
display.clear()
vector.set_font("coolvetica_rg.af", 72)
# draw the text many times
for i in range(36):
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
display.set_pen(PEN_COLOUR)
vector.text(TEXT, 10, i * HEIGHT // 44 - 25)
hue += 1.0 / 18
display.update()
time.sleep(5)
Binary file added micropython/examples/pico_display/next_sunday.af
Binary file not shown.
1 change: 1 addition & 0 deletions micropython/examples/pico_display/pride_stripes.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
48 changes: 33 additions & 15 deletions micropython/examples/pico_display/rainbow.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,12 +10,22 @@
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB565, rotate=0)
display.set_backlight(0.8)

led = RGBLED(6, 7, 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)

# 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
def hsv_to_rgb(h, s, v):
Expand All @@ -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)
38 changes: 38 additions & 0 deletions micropython/examples/pico_display/rainbow_wheel.py
Original file line number Diff line number Diff line change
@@ -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
46 changes: 14 additions & 32 deletions micropython/examples/pico_display/ram_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
# 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
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
Expand All @@ -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)
Binary file not shown.
Loading

0 comments on commit afd652b

Please sign in to comment.