Skip to content

Commit

Permalink
basic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
helgibbons committed Aug 15, 2024
1 parent e109d7b commit 7237372
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/balls_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import time
import random
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_P8

display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_P8)

WIDTH, HEIGHT = display.get_bounds()

# We're creating 100 balls with their own individual colour and 1 BG colour
# for a total of 101 colours, which will all fit in the custom 256 entry palette!


class Ball:
def __init__(self, x, y, r, dx, dy, pen):
self.x = x
self.y = y
self.r = r
self.dx = dx
self.dy = dy
self.pen = pen


# initialise shapes
balls = []
for i in range(0, 100):
r = random.randint(0, 10) + 3
balls.append(
Ball(
random.randint(r, r + (WIDTH - 2 * r)),
random.randint(r, r + (HEIGHT - 2 * r)),
r,
(14 - r) / 2,
(14 - r) / 2,
display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
)
)

BG = display.create_pen(40, 40, 40)

while True:
display.set_pen(BG)
display.clear()

for ball in balls:
ball.x += ball.dx
ball.y += ball.dy

xmax = WIDTH - ball.r
xmin = ball.r
ymax = HEIGHT - ball.r
ymin = ball.r

if ball.x < xmin or ball.x > xmax:
ball.dx *= -1

if ball.y < ymin or ball.y > ymax:
ball.dy *= -1

display.set_pen(ball.pen)
display.circle(int(ball.x), int(ball.y), int(ball.r))

display.update()
time.sleep(0.01)
83 changes: 83 additions & 0 deletions examples/button_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# This example shows you a simple, non-interrupt way of reading Pico Explorer's buttons with a loop that checks to see if buttons are pressed.

import time
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_P4
from machine import Pin

# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_P4)

button_a = Pin(16, Pin.IN, Pin.PULL_UP)
button_b = Pin(15, Pin.IN, Pin.PULL_UP)
button_c = Pin(14, Pin.IN, Pin.PULL_UP)
button_x = Pin(17, Pin.IN, Pin.PULL_UP)
button_y = Pin(18, Pin.IN, Pin.PULL_UP)
button_z = Pin(19, Pin.IN, Pin.PULL_UP)

WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
CYAN = display.create_pen(0, 255, 255)
MAGENTA = display.create_pen(255, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
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)
display.clear()
display.update()


# set up
clear()
display.set_font("bitmap8")

while True:
if button_a.value() == 0: # if a button press is detected then...
clear() # clear to black
display.set_pen(WHITE) # change the pen colour
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.value() == 0:
clear()
display.set_pen(CYAN)
display.text("Button B pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
clear()
elif button_c.value() == 0:
clear()
display.set_pen(CYAN)
display.text("Button C pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
clear()
elif button_x.value() == 0:
clear()
display.set_pen(MAGENTA)
display.text("Button X pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
clear()
elif button_y.value() == 0:
clear()
display.set_pen(YELLOW)
display.text("Button Y pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
clear()
elif button_z.value() == 0:
clear()
display.set_pen(YELLOW)
display.text("Button Z pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
clear()
else:
display.set_pen(GREEN)
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
51 changes: 51 additions & 0 deletions examples/rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This example borrows a CircuitPython hsv_to_rgb function to cycle through some rainbows on Pico Explorer's screen. 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.

import time
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_RGB565

display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_RGB565)

WIDTH, HEIGHT = display.get_bounds()

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


# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
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
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.text("\\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/", 25, 120, 240, 4) # and some more text
display.text("oontz oontz oontz", 25, 220, 240, 2) # and a bit more tiny text
display.update() # Update the display
time.sleep(1.0 / 60)

0 comments on commit 7237372

Please sign in to comment.