Skip to content

Commit

Permalink
add basic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
helgibbons authored and thirdr committed Sep 16, 2024
1 parent 49d7ea3 commit 7a5191a
Show file tree
Hide file tree
Showing 17 changed files with 1,184 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/ADC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Reads an analog input connected to ADC0.
Connect the middle pin of your potentiometer to ADC0, and the other two pins to 3.3V and GND.
"""

from explorer import Explorer2350
from machine import ADC
import time

board = Explorer2350()

display = board.display

# lets set up some pen colours to make drawing easier
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)

pot = ADC(0)

# alternatively, you could specify the pin number like this:
# pot = ADC(40)

while True:
display.set_pen(BLACK)
display.clear()
display.set_pen(WHITE)
# read the potentiometer value, it's a number between 0 and 65535 which represents a voltage between 0v and 3.3v
pot_value = pot.read_u16()
display.text(f"{pot_value}", 0, 0, 320, 4)
display.update()
time.sleep(0.1)
Binary file added examples/backgroundforscreen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/backgroundforscreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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)
85 changes: 85 additions & 0 deletions examples/button_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
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
18 changes: 18 additions & 0 deletions examples/display_jpg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from explorer import Explorer2350
import jpegdec

board = Explorer2350()

display = board.display

# Create a new JPEG decoder for our PicoGraphics
j = jpegdec.JPEG(display)

# Open the JPEG file
j.open_file("backgroundforscreen.jpg")

# Decode the JPEG
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=True)

# Display the result
display.update()
24 changes: 24 additions & 0 deletions examples/display_png.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Display a PNG image on Explorer 2350

from explorer import Explorer2350
import pngdec
import gc

board = Explorer2350()

display = board.display

# run garbage collection - displaying large PNGs is resource intensive
gc.collect()

# Create a new JPEG decoder for our PicoGraphics
p = pngdec.PNG(display)

# Open the PNG file
p.open_file("backgroundforscreen.png")

# Decode the PNG
p.decode(0, 0)

# Display the result
display.update()
36 changes: 36 additions & 0 deletions examples/explorer_sensor_stick_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Display readings from the multi-sensor stick on the Explorer screen
from explorer import Explorer2350
from breakout_ltr559 import BreakoutLTR559
from lsm6ds3 import LSM6DS3
from breakout_bme280 import BreakoutBME280
import time

board = Explorer2350()

ltr = BreakoutLTR559(board.i2c)
lsm = LSM6DS3(board.i2c)
bme = BreakoutBME280(board.i2c)

display = board.display

# lets set up some pen colours to make drawing easier
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(255, 0, 0)
GREY = display.create_pen(125, 125, 125)

while True:
lux, _, _, _, _, _, prox = ltr.get_reading()
ax, ay, az, gx, gy, gz = lsm.get_readings()
temperature, pressure, humidity = bme.read()
display.set_pen(BLACK)
display.clear()
display.set_pen(WHITE)
if lux is not None:
display.text(f"Lux: {lux:.0f}\nProx: {prox:.0f}", 0, 0, 320, 3)
if ax is not None:
display.text(f"Accelerometer:\nX: {ax:.0f}, Y: {ay:.0f}, \nZ: {az:.0f}\nGyro:\nX: {gx:.0f}, Y: {gy:.0f}, \nZ: {gz:.0f}", 0, 45, 320, 3)
if temperature is not None:
display.text(f"Temperature: {temperature:.2f}°C,\nHumidity: {humidity:.0f}%,\nPressure: {pressure/100:.0f}hPa", 0, 180, 320, 3)
display.update()
time.sleep(0.1)
51 changes: 51 additions & 0 deletions examples/external_buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Connect (up to) 3 external buttons to Explorer.
We're connecting our yellow button to GPIO 0, a red button to GPIO 1 and a blue button to GPIO 3. The other side of each button should be wired to ground.
Note that if you're using our square buttons, you should connect wires to two pins that are diagonally opposite each other.
"""

from machine import Pin
import time
from explorer import Explorer2350

board = Explorer2350()

display = board.display

yellow_button = Pin(0, Pin.IN, Pin.PULL_UP)
blue_button = Pin(1, Pin.IN, Pin.PULL_UP)
red_button = Pin(2, Pin.IN, Pin.PULL_UP)

# lets set up some pen colours to make drawing easier
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
YELLOW = display.create_pen(0, 255, 255)
BLUE = display.create_pen(0, 0, 255)
RED = display.create_pen(255, 0, 0)


while True:
display.set_pen(BLACK)
display.clear()
display.set_pen(WHITE)
display.text("Wire up some buttons to GP0, GP1 and GP2, and push 'em!", 0, 0, 320, 4)
# because we're using Pin.PULL_UP the logic is reversed - '0' is pushed and '1' is unpushed
if yellow_button.value() == 0:
display.set_pen(BLACK)
display.clear()
display.set_pen(YELLOW)
display.text("Yellow button pushed", 0, 0, 320, 4)
if blue_button.value() == 0:
display.set_pen(BLACK)
display.clear()
display.set_pen(BLUE)
display.text("Blue button pushed", 0, 0, 320, 4)
if red_button.value() == 0:
display.set_pen(BLACK)
display.clear()
display.set_pen(RED)
display.text("Red button pushed", 0, 0, 320, 4)
display.update()
time.sleep(0.01)
Loading

0 comments on commit 7a5191a

Please sign in to comment.