diff --git a/micropython/examples/pico_display/balls_demo.py b/micropython/examples/pico_display/balls_demo.py index 71c79a50..7b6ce682 100644 --- a/micropython/examples/pico_display/balls_demo.py +++ b/micropython/examples/pico_display/balls_demo.py @@ -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 diff --git a/micropython/examples/pico_display/basic_qrcode.py b/micropython/examples/pico_display/basic_qrcode.py index 01025987..8183bcad 100644 --- a/micropython/examples/pico_display/basic_qrcode.py +++ b/micropython/examples/pico_display/basic_qrcode.py @@ -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 diff --git a/micropython/examples/pico_display/button_test.py b/micropython/examples/pico_display/button_test.py index d55d8efb..fe47ce08 100644 --- a/micropython/examples/pico_display/button_test.py +++ b/micropython/examples/pico_display/button_test.py @@ -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 diff --git a/micropython/examples/pico_display/coolvetica_rg.af b/micropython/examples/pico_display/coolvetica_rg.af new file mode 100644 index 00000000..4a9d9ed3 Binary files /dev/null and b/micropython/examples/pico_display/coolvetica_rg.af differ diff --git a/micropython/examples/pico_display/hello_world_bitmap.py b/micropython/examples/pico_display/hello_world_bitmap.py new file mode 100644 index 00000000..a499d4e2 --- /dev/null +++ b/micropython/examples/pico_display/hello_world_bitmap.py @@ -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() diff --git a/micropython/examples/pico_display/hello_world_vector.py b/micropython/examples/pico_display/hello_world_vector.py new file mode 100644 index 00000000..dd249c3d --- /dev/null +++ b/micropython/examples/pico_display/hello_world_vector.py @@ -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) diff --git a/micropython/examples/pico_display/next_sunday.af b/micropython/examples/pico_display/next_sunday.af new file mode 100644 index 00000000..f6536266 Binary files /dev/null and b/micropython/examples/pico_display/next_sunday.af differ diff --git a/micropython/examples/pico_display/pride_stripes.py b/micropython/examples/pico_display/pride_stripes.py index e6846fd5..cfc7857a 100644 --- a/micropython/examples/pico_display/pride_stripes.py +++ b/micropython/examples/pico_display/pride_stripes.py @@ -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 diff --git a/micropython/examples/pico_display/rainbow.py b/micropython/examples/pico_display/rainbow.py index 38305fbf..fdd35582 100644 --- a/micropython/examples/pico_display/rainbow.py +++ b/micropython/examples/pico_display/rainbow.py @@ -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) diff --git a/micropython/examples/pico_display/rainbow_wheel.py b/micropython/examples/pico_display/rainbow_wheel.py new file mode 100644 index 00000000..6a384786 --- /dev/null +++ b/micropython/examples/pico_display/rainbow_wheel.py @@ -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 diff --git a/micropython/examples/pico_display/ram_test.py b/micropython/examples/pico_display/ram_test.py index 213e3c6b..3fcbd461 100644 --- a/micropython/examples/pico_display/ram_test.py +++ b/micropython/examples/pico_display/ram_test.py @@ -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) diff --git a/micropython/examples/pico_display/spicy_soup.af b/micropython/examples/pico_display/spicy_soup.af new file mode 100644 index 00000000..b7be16b6 Binary files /dev/null and b/micropython/examples/pico_display/spicy_soup.af differ diff --git a/micropython/examples/pico_display/starfield.py b/micropython/examples/pico_display/starfield.py new file mode 100644 index 00000000..a314711e --- /dev/null +++ b/micropython/examples/pico_display/starfield.py @@ -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() diff --git a/micropython/examples/pico_display/thermometer.py b/micropython/examples/pico_display/thermometer.py index b3ffa0e5..cf831139 100644 --- a/micropython/examples/pico_display/thermometer.py +++ b/micropython/examples/pico_display/thermometer.py @@ -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