Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
helgibbons committed Sep 4, 2024
1 parent 38498c1 commit 6aa09a8
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 22 deletions.
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.
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)
132 changes: 132 additions & 0 deletions examples/explorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import gc
from machine import Pin, PWM
from pimoroni_i2c import PimoroniI2C
from servo import Servo
from picographics import PicoGraphics, DISPLAY_EXPLORER

# IO Pin Constants
GP0 = 0
GP1 = 1
GP2 = 2
GP3 = 3
GP4 = 4
GP5 = 5

A0 = 40
A1 = 41
A2 = 42
A3 = 43
A4 = 44
A5 = 45

GPIOS = (GP0, GP1, GP2, GP3, GP4, GP5, A0, A1, A2, A3, A4, A5)
ADCS = (A0, A1, A2, A3, A4, A5)


# Index Constants
SERVO_1 = 0
SERVO_2 = 1
SERVO_3 = 2
SERVO_4 = 3

SWITCH_A = 0
SWITCH_B = 1
SWITCH_C = 2
SWITCH_X = 3
SWITCH_Y = 4
SWITCH_Z = 5

# Count Constants
NUM_GPIOS = 6
NUM_ADCS = 6
NUM_SERVOS = 4
NUM_SWITCHES = 6


class Explorer2350():
SWITCH_A_PIN = 16
SWITCH_B_PIN = 15
SWITCH_C_PIN = 14
SWITCH_X_PIN = 17
SWITCH_Y_PIN = 18
SWITCH_Z_PIN = 19

I2C_SDA_PIN = 20
I2C_SCL_PIN = 21

USER_SW_PIN = 22

SERVO_1_PIN = 9
SERVO_2_PIN = 8
SERVO_3_PIN = 7
SERVO_4_PIN = 6

PWM_AUDIO_PIN = 12
AMP_EN_PIN = 13

AMP_CORRECTION = 4
DEFAULT_VOLUME = 0.2

def __init__(self, init_servos=True):
# Free up hardware resources
gc.collect()

self.display = PicoGraphics(display=DISPLAY_EXPLORER)

# Set up the servos, if the user wants them
self.servos = None
if init_servos:
self.servos = [Servo(self.SERVO_1_PIN - i) for i in range(4)]

# Set up the i2c for Qw/st and Breakout Garden
self.i2c = PimoroniI2C(self.I2C_SDA_PIN, self.I2C_SCL_PIN, 100000)

# Set up the amp enable
self.__amp_en = Pin(self.AMP_EN_PIN, Pin.OUT)
self.__amp_en.off()

self.audio_pwm = PWM(Pin(self.PWM_AUDIO_PIN))
self.__volume = self.DEFAULT_VOLUME

# Set up the user switch
self.__switch = Pin(self.USER_SW_PIN, Pin.IN, Pin.PULL_DOWN)

def switch_pressed(self):
return self.__switch.value()

def play_tone(self, frequency):
try:
self.audio_pwm.freq(frequency)
except ValueError:
self.play_silence()
raise ValueError("frequency of range. Expected greater than 0")

corrected_volume = (self.__volume ** 4) # Correct for RC Filter curve
self.audio_pwm.duty_u16(int(32768 * corrected_volume))
self.unmute_audio()

def play_silence(self):
self.audio_pwm.freq(44100)

corrected_volume = (self.__volume ** 4) # Correct for RC Filter curve
self.audio_pwm.duty_u16(int(32768 * corrected_volume))
self.unmute_audio()

def stop_playing(self):
self.audio_pwm.duty_u16(0)
self.mute_audio()

def volume(self, volume=None):
if volume is None:
return self.__volume

if volume < 0.01 or volume > 1.0:
raise ValueError("volume out of range. Expected 0.0 to 1.0")

self.__volume = volume

def mute_audio(self):
self.__amp_en.off()

def unmute_audio(self):
self.__amp_en.on()
58 changes: 58 additions & 0 deletions examples/multiple_servos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import time
import math
from explorer import Explorer2350

"""
Demonstrates how to control all of the servos on Explorer.
"""

# Create a new Explorer
board = Explorer2350()

# Enable all servos (this puts them at the middle)
for s in board.servos:
s.enable()
time.sleep(2)

# Go to min
for s in board.servos:
s.to_min()
time.sleep(2)

# Go to max
for s in board.servos:
s.to_max()
time.sleep(2)

# Go back to mid
for s in board.servos:
s.to_mid()
time.sleep(2)

SWEEPS = 3 # How many sweeps of the servo to perform
STEPS = 10 # The number of discrete sweep steps
STEPS_INTERVAL = 0.5 # The time in seconds between each step of the sequence
SWEEP_EXTENT = 90.0 # How far from zero to move the servo when sweeping

# Do a sine sweep
for j in range(SWEEPS):
for i in range(360):
value = math.sin(math.radians(i)) * SWEEP_EXTENT
for s in board.servos:
s.value(value)
time.sleep(0.02)

# Do a stepped sweep
for j in range(SWEEPS):
for i in range(0, STEPS):
for s in board.servos:
s.to_percent(i, 0, STEPS, 0.0 - SWEEP_EXTENT, SWEEP_EXTENT)
time.sleep(STEPS_INTERVAL)
for i in range(0, STEPS):
for s in board.servos:
s.to_percent(i, STEPS, 0, 0.0 - SWEEP_EXTENT, SWEEP_EXTENT)
time.sleep(STEPS_INTERVAL)

# Disable the servos
for s in board.servos:
s.disable()
54 changes: 54 additions & 0 deletions examples/single_servo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import time
import math
from explorer import Explorer2350, SERVO_1

"""
Demonstrates how to control a single servo on Explorer.
"""

# Create a new Explorer2350
board = Explorer2350()

# Access the servo from Explorer and enable it (this puts it at the middle)
s = board.servos[SERVO_1]

print(board.servos)
s.enable()
time.sleep(2)

# Go to min
s.to_min()
time.sleep(2)

# Go to max
s.to_max()
time.sleep(2)

# Go back to mid
s.to_mid()
time.sleep(2)


SWEEPS = 3 # How many sweeps of the servo to perform
STEPS = 10 # The number of discrete sweep steps
STEPS_INTERVAL = 0.5 # The time in seconds between each step of the sequence
SWEEP_EXTENT = 90.0 # How far from zero to move the servo when sweeping


# Do a sine sweep
for j in range(SWEEPS):
for i in range(360):
s.value(math.sin(math.radians(i)) * SWEEP_EXTENT)
time.sleep(0.02)

# Do a stepped sweep
for j in range(SWEEPS):
for i in range(0, STEPS):
s.to_percent(i, 0, STEPS, 0.0 - SWEEP_EXTENT, SWEEP_EXTENT)
time.sleep(STEPS_INTERVAL)
for i in range(0, STEPS):
s.to_percent(i, STEPS, 0, 0.0 - SWEEP_EXTENT, SWEEP_EXTENT)
time.sleep(STEPS_INTERVAL)

# Disable the servo
s.disable()
Loading

0 comments on commit 6aa09a8

Please sign in to comment.