Skip to content

Commit d06956d

Browse files
authored
Merge pull request #973 from pimoroni/examples/pico-display-2-8
Pico Display 2.8": Update examples
2 parents a771aa9 + 1135bd5 commit d06956d

File tree

11 files changed

+180
-55
lines changed

11 files changed

+180
-55
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ We also maintain a C++/CMake boilerplate with GitHub workflows configured for te
8080
* Pico Unicorn Pack - https://shop.pimoroni.com/products/pico-unicorn-pack
8181
* Pico Audio Pack (C++ only) - https://shop.pimoroni.com/products/pico-audio-pack
8282
* Pico Wireless Pack - https://shop.pimoroni.com/products/pico-wireless-pack
83-
* Pico Display 2.0 - https://shop.pimoroni.com/products/pico-display-pack-2-0
83+
* Pico Display 2.0" - https://shop.pimoroni.com/products/pico-display-pack-2-0
8484
* Pico Enviro+ Pack - https://shop.pimoroni.com/products/pico-enviro-pack
8585
* Pico Inky Pack - https://shop.pimoroni.com/products/pico-inky-pack
8686
* Pico GFX Pack - https://shop.pimoroni.com/products/pico-gfx-pack
87+
* Pico Display 2.8" - https://shop.pimoroni.com/products/pico-display-pack-2-8
8788

8889
## SHIMs
8990

micropython/examples/pico_display/balls_demo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
2+
13
import time
24
import random
35
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P8

micropython/examples/pico_display/basic_qrcode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
2+
13
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
24
import qrcode
35

micropython/examples/pico_display/button_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# 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.
2+
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
23

34
import time
45
from pimoroni import Button
56
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4
7+
from pimoroni import RGBLED
68

79
# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
810
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0)
@@ -15,6 +17,12 @@
1517
button_x = Button(14)
1618
button_y = Button(15)
1719

20+
# Set up the RGB LED For Display Pack and Display Pack 2.0":
21+
led = RGBLED(6, 7, 8)
22+
23+
# For Display Pack 2.8" uncomment the line below and comment out the line above:
24+
# led = RGBLED(26, 27, 28)
25+
1826
WHITE = display.create_pen(255, 255, 255)
1927
BLACK = display.create_pen(0, 0, 0)
2028
CYAN = display.create_pen(0, 255, 255)
@@ -26,6 +34,7 @@
2634
# sets up a handy function we can call to clear the screen
2735
def clear():
2836
display.set_pen(BLACK)
37+
led.set_rgb(0, 0, 0)
2938
display.clear()
3039
display.update()
3140

@@ -37,33 +46,38 @@ def clear():
3746
if button_a.read(): # if a button press is detected then...
3847
clear() # clear to black
3948
display.set_pen(WHITE) # change the pen colour
49+
led.set_rgb(255, 255, 255) # set the LED colour to match
4050
display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen
4151
display.update() # update the display
4252
time.sleep(1) # pause for a sec
4353
clear() # clear to black again
4454
elif button_b.read():
4555
clear()
4656
display.set_pen(CYAN)
57+
led.set_rgb(0, 255, 255)
4758
display.text("Button B pressed", 10, 10, 240, 4)
4859
display.update()
4960
time.sleep(1)
5061
clear()
5162
elif button_x.read():
5263
clear()
5364
display.set_pen(MAGENTA)
65+
led.set_rgb(255, 0, 255)
5466
display.text("Button X pressed", 10, 10, 240, 4)
5567
display.update()
5668
time.sleep(1)
5769
clear()
5870
elif button_y.read():
5971
clear()
6072
display.set_pen(YELLOW)
73+
led.set_rgb(255, 255, 0)
6174
display.text("Button Y pressed", 10, 10, 240, 4)
6275
display.update()
6376
time.sleep(1)
6477
clear()
6578
else:
6679
display.set_pen(GREEN)
80+
led.set_rgb(0, 255, 0)
6781
display.text("Press any button!", 10, 10, 240, 4)
6882
display.update()
6983
time.sleep(0.1) # this number is how frequently the Pico checks for button presses

micropython/examples/pico_display/pride_stripes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# A customisable Pride flag. (Look in the Tufty 2040 examples for a name badge version!)
2+
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
23

34
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
45

micropython/examples/pico_display/rainbow.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# 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!
1+
# This example cycles through some rainbows on Pico Display's screen and RGB LED, using the HSV colour model.
2+
# (If you're into rainbows, HSV (Hue, Saturation, Value) is very useful)
23
# We're using a RAM intensive 64K colour palette here to get a nice smooth colour transition.
4+
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
35

46
import time
57
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB565
@@ -8,12 +10,22 @@
810
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB565, rotate=0)
911
display.set_backlight(0.8)
1012

11-
led = RGBLED(6, 7, 8)
12-
13+
# set up constants for drawing
1314
WIDTH, HEIGHT = display.get_bounds()
14-
1515
BLACK = display.create_pen(0, 0, 0)
1616

17+
# what size steps to take around the colour wheel
18+
OFFSET = 0.0025
19+
20+
# variable to keep track of the hue
21+
h = 0.0
22+
23+
# Set up the RGB LED For Display Pack and Display Pack 2.0":
24+
led = RGBLED(6, 7, 8)
25+
26+
# For Display Pack 2.8" uncomment the following line and comment out the line above:
27+
# led = RGBLED(26, 27, 28)
28+
1729

1830
# From CPython Lib/colorsys.py
1931
def hsv_to_rgb(h, s, v):
@@ -39,16 +51,22 @@ def hsv_to_rgb(h, s, v):
3951
return v, p, q
4052

4153

42-
h = 0
43-
4454
while True:
45-
h += 1
46-
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
47-
led.set_rgb(r, g, b) # Set LED to a converted HSV value
48-
RAINBOW = display.create_pen(r, g, b) # Create pen with converted HSV value
49-
display.set_pen(RAINBOW) # Set pen
50-
display.clear() # Fill the screen with the colour
51-
display.set_pen(BLACK) # Set pen to black
52-
display.text("pico disco!", 10, 10, 240, 6) # Add some text
53-
display.update() # Update the display
55+
# increment the hue each time round the loop
56+
h += OFFSET
57+
58+
# The LED needs to be set using RGB values, so convert HSV to RGB using the hsv_to_rgb() function above
59+
r, g, b = [int(255 * c) for c in hsv_to_rgb(h, 1.0, 1.0)]
60+
led.set_rgb(r, g, b)
61+
62+
# Fill the screen with the chosen hue, we can use PicoGraphics' built in HSV pen function for this
63+
RAINBOW = display.create_pen_hsv(h, 1.0, 1.0)
64+
display.set_pen(RAINBOW)
65+
display.clear()
66+
67+
# Draw some black text
68+
display.set_pen(BLACK)
69+
display.text("pico disco!", 10, 10, 240, 6)
70+
71+
display.update()
5472
time.sleep(1.0 / 60)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# A spinny rainbow wheel. Change up some of the constants below to see what happens.
2+
3+
import math
4+
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2
5+
6+
# Constants for drawing
7+
INNER_RADIUS = 40
8+
OUTER_RADIUS = 120
9+
NUMBER_OF_LINES = 24
10+
HUE_SHIFT = 0.02
11+
ROTATION_SPEED = 2
12+
LINE_THICKNESS = 2
13+
14+
# Set up the display
15+
graphics = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2)
16+
17+
WIDTH, HEIGHT = graphics.get_bounds()
18+
19+
BLACK = graphics.create_pen(0, 0, 0)
20+
21+
# Variables to keep track of rotation and hue positions
22+
r = 0
23+
t = 0
24+
25+
while True:
26+
graphics.set_pen(BLACK)
27+
graphics.clear()
28+
for i in range(0, 360, 360 // NUMBER_OF_LINES):
29+
graphics.set_pen(graphics.create_pen_hsv((i / 360) + t, 1.0, 1.0))
30+
# Draw some lines, offset by the rotation variable
31+
graphics.line(int(WIDTH / 2 + math.cos(math.radians(i + r)) * INNER_RADIUS),
32+
int(HEIGHT / 2 + math.sin(math.radians(i + r)) * INNER_RADIUS),
33+
int(WIDTH / 2 + math.cos(math.radians(i + 90 + r)) * OUTER_RADIUS),
34+
int(HEIGHT / 2 + math.sin(math.radians(i + 90 + r)) * OUTER_RADIUS),
35+
LINE_THICKNESS)
36+
graphics.update()
37+
r += ROTATION_SPEED
38+
t += HUE_SHIFT
Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
# Shows the available RAM. PEN_RGB332 is an 8 bit, fixed 256 colour palette which conserves your RAM.
2+
# Try switching the pen_type to PEN_RGB565 (16 bit, 65K colour) and see the difference!
3+
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
4+
15
import gc
26
import time
37
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB332
48

5-
# PEN_RGB332 is an 8 bit, fixed 256 colour palette which conserves your RAM.
6-
# Try switching the pen_type to PEN_RGB565 (16 bit, 65K colour) and see the difference!
7-
89
display = PicoGraphics(DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB332, rotate=0)
910

1011
# set up constants for drawing
1112
WIDTH, HEIGHT = display.get_bounds()
1213

1314
BLACK = display.create_pen(0, 0, 0)
1415

16+
# what size steps to take around the colour wheel
17+
OFFSET = 0.0025
18+
19+
# variable to keep track of the hue
20+
h = 0.0
21+
1522

1623
def free(full=False):
1724
# Calculates RAM usage
@@ -26,39 +33,14 @@ def free(full=False):
2633
return (f"Total RAM \n{T} bytes \nUnused RAM \n{F} bytes \n({P} free)")
2734

2835

29-
def hsv_to_rgb(h, s, v):
30-
# From CPython Lib/colorsys.py
31-
if s == 0.0:
32-
return v, v, v
33-
i = int(h * 6.0)
34-
f = (h * 6.0) - i
35-
p = v * (1.0 - s)
36-
q = v * (1.0 - s * f)
37-
t = v * (1.0 - s * (1.0 - f))
38-
i = i % 6
39-
if i == 0:
40-
return v, t, p
41-
if i == 1:
42-
return q, v, p
43-
if i == 2:
44-
return p, v, t
45-
if i == 3:
46-
return p, q, v
47-
if i == 4:
48-
return t, p, v
49-
if i == 5:
50-
return v, p, q
51-
52-
53-
h = 0
54-
5536
while True:
56-
h += 1
57-
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
37+
h += OFFSET
38+
5839
display.set_pen(BLACK)
59-
RAINBOW = display.create_pen(r, g, b) # Create pen with converted HSV value
40+
RAINBOW = display.create_pen_hsv(h, 1.0, 1.0)
6041
display.set_pen(RAINBOW)
6142
display.set_font("bitmap8")
6243
display.text(free(full=True), 0, 0, WIDTH, 3)
44+
6345
display.update()
6446
time.sleep(1.0 / 60)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Travel through a Windows 3.1-esque starfield, with stars growing as they get 'closer'.
2+
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
3+
4+
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
5+
import random
6+
7+
# Constants to play with
8+
NUMBER_OF_STARS = 200
9+
TRAVEL_SPEED = 1.2
10+
STAR_GROWTH = 0.12
11+
12+
# Set up our display
13+
graphics = PicoGraphics(display=DISPLAY_PICO_DISPLAY)
14+
15+
WIDTH, HEIGHT = graphics.get_bounds()
16+
17+
BLACK = graphics.create_pen(0, 0, 0)
18+
WHITE = graphics.create_pen(255, 255, 255)
19+
20+
stars = []
21+
22+
23+
def new_star():
24+
# Create a new star, with initial x, y, and size
25+
# Initial x will fall between -WIDTH / 2 and +WIDTH / 2 and y between -HEIGHT/2 and +HEIGHT/2
26+
# These are relative values for now, treating (0, 0) as the centre of the screen.
27+
star = [random.randint(0, WIDTH) - WIDTH // 2, random.randint(0, HEIGHT) - HEIGHT // 2, 0.5]
28+
return star
29+
30+
31+
for i in range(0, NUMBER_OF_STARS):
32+
stars.append(new_star())
33+
34+
while True:
35+
graphics.set_pen(BLACK)
36+
graphics.clear()
37+
graphics.set_pen(WHITE)
38+
for i in range(0, NUMBER_OF_STARS):
39+
# Load a star from the stars list
40+
s = stars[i]
41+
42+
# Update x
43+
s[0] = s[0] * TRAVEL_SPEED
44+
45+
# Update y
46+
s[1] = s[1] * TRAVEL_SPEED
47+
48+
if s[0] <= - WIDTH // 2 or s[0] >= WIDTH // 2 or s[1] <= - HEIGHT // 2 or s[1] >= HEIGHT // 2 or s[2] >= 5:
49+
# This star has fallen off the screen (or rolled dead centre and grown too big!)
50+
# Replace it with a new one
51+
s = new_star()
52+
53+
# Grow the star as it travels outward
54+
s[2] += STAR_GROWTH
55+
56+
# Save the updated star to the list
57+
stars[i] = s
58+
59+
# Draw star, adding offsets to our relative coordinates to allow for (0, 0) being in the top left corner.
60+
graphics.circle(int(s[0]) + WIDTH // 2, int(s[1]) + HEIGHT // 2, int(s[2]))
61+
graphics.update()

micropython/examples/pico_display/thermometer.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
# 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.
2-
# 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!
1+
# This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Display Pack.
2+
# It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book.
33

44
import machine
55
import time
66
from pimoroni import RGBLED
77
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
88

9-
# set up the hardware
9+
# set up the display and drawing constants
1010
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0)
11-
sensor_temp = machine.ADC(4)
12-
led = RGBLED(6, 7, 8)
1311

1412
# set the display backlight to 50%
1513
display.set_backlight(0.5)
1614

17-
# set up constants for drawing
1815
WIDTH, HEIGHT = display.get_bounds()
1916

2017
BLACK = display.create_pen(0, 0, 0)
2118
WHITE = display.create_pen(255, 255, 255)
2219

20+
# set up the internal temperature sensor
21+
sensor_temp = machine.ADC(4)
22+
23+
# Set up the RGB LED For Display Pack and Display Pack 2.0":
24+
led = RGBLED(6, 7, 8)
25+
26+
# For Display Pack 2.8" uncomment the following line and comment out the line above:
27+
# led = RGBLED(26, 27, 28)
28+
2329
conversion_factor = 3.3 / (65535) # used for calculating a temperature from the raw sensor reading
2430

2531
temp_min = 10

0 commit comments

Comments
 (0)