Skip to content

Commit afd652b

Browse files
committed
Examples: update for Pico Display 2.8"
1 parent bb51ce5 commit afd652b

14 files changed

+290
-53
lines changed

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
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Bitmap font demo!
3+
4+
Bitmap fonts are fast but blocky. They are best used for small text.
5+
"""
6+
7+
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2
8+
9+
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2)
10+
11+
WIDTH, HEIGHT = display.get_bounds()
12+
13+
hue = 0.0
14+
15+
display.set_font("bitmap8")
16+
17+
for i in range(7):
18+
# create a pen and set the drawing color
19+
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
20+
display.set_pen(PEN_COLOUR)
21+
# draw text
22+
display.text("Hello World", i * WIDTH // 12, i * HEIGHT // 7 + 6, scale=3)
23+
# increment hue
24+
hue += 1.0 / 7
25+
26+
display.update()
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
Vector font demo! Vector fonts are slower but smoother. They are best used for large text.
3+
4+
You will need to copy the .af font files to your Pico.
5+
6+
Spicy Soup font originally from https://www.dafont.com/spicy-soup.font
7+
Next Sunday font originally from https://www.dafont.com/next-sunday.font
8+
Coolvetica font originally from https://www.dafont.com/coolvetica.font
9+
10+
Find out how to convert your own fonts to .af here: https://github.com/lowfatcode/alright-fonts
11+
"""
12+
13+
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_RGB565
14+
from picovector import PicoVector, ANTIALIAS_X16
15+
import time
16+
17+
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_RGB565)
18+
vector = PicoVector(display)
19+
20+
display.set_backlight(1.0)
21+
22+
WIDTH, HEIGHT = display.get_bounds()
23+
BLACK = display.create_pen(0, 0, 0)
24+
25+
hue = 0.0
26+
27+
# antialiasing draws the vector multiple times for a smoother look
28+
vector.set_antialiasing(ANTIALIAS_X16)
29+
30+
TEXT = "Hello World"
31+
32+
while True:
33+
# reset the hue
34+
hue = 0.0
35+
36+
# clear to black
37+
display.set_pen(BLACK)
38+
display.clear()
39+
# set the vector font and size
40+
vector.set_font("spicy_soup.af", 36)
41+
# draw the text seven times
42+
for i in range(7):
43+
# create a HSV (hue, value, saturation) pen and set the drawing color
44+
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
45+
display.set_pen(PEN_COLOUR)
46+
# draw text
47+
vector.text(TEXT, i * WIDTH // 20, i * HEIGHT // 7 - 5)
48+
# increment hue
49+
hue += 1.0 / 7
50+
display.update()
51+
time.sleep(5)
52+
53+
display.set_pen(BLACK)
54+
display.clear()
55+
vector.set_font("next_sunday.af", 30)
56+
# draw the text ten times, sideways
57+
for i in range(10):
58+
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
59+
display.set_pen(PEN_COLOUR)
60+
vector.text(TEXT, i * WIDTH // 10, HEIGHT, 270)
61+
hue += 1.0 / 10
62+
display.update()
63+
time.sleep(5)
64+
65+
display.set_pen(BLACK)
66+
display.clear()
67+
vector.set_font("coolvetica_rg.af", 44)
68+
# draw the text many times
69+
for i in range(30):
70+
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
71+
display.set_pen(PEN_COLOUR)
72+
vector.text(TEXT, WIDTH // 2, HEIGHT // 2, i * 12)
73+
hue += 1.0 / 30
74+
display.update()
75+
time.sleep(5)
76+
77+
display.set_pen(BLACK)
78+
display.clear()
79+
vector.set_font("coolvetica_rg.af", 72)
80+
# draw the text many times
81+
for i in range(36):
82+
PEN_COLOUR = display.create_pen_hsv(hue, 1.0, 1.0)
83+
display.set_pen(PEN_COLOUR)
84+
vector.text(TEXT, 10, i * HEIGHT // 44 - 25)
85+
hue += 1.0 / 18
86+
display.update()
87+
time.sleep(5)
11.6 KB
Binary file not shown.

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

0 commit comments

Comments
 (0)