Skip to content

Commit

Permalink
Some slot IO examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Oct 10, 2023
1 parent 7785bda commit bd2e3b3
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
61 changes: 61 additions & 0 deletions examples/yukon/read_slot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import time
from machine import Pin
from pimoroni_yukon import Yukon
from pimoroni_yukon import SLOT1 as SLOT

"""
Read the IO pins of a single Yukon slot.
"""

# Constants
SLEEP = 0.5 # The time to sleep between each reading

# Variables
yukon = Yukon() # A new Yukon object

# Configure the slot pins as inputs
SLOT.FAST1.init(Pin.IN)
SLOT.FAST2.init(Pin.IN)
SLOT.FAST3.init(Pin.IN)
SLOT.FAST4.init(Pin.IN)
SLOT.SLOW1.init(Pin.IN)
SLOT.SLOW1.init(Pin.IN)
SLOT.SLOW1.init(Pin.IN)

# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt)
try:
# Loop until the BOOT/USER button is pressed
while not yukon.is_boot_pressed():

# Read the slot's four 'Fast' pins
state_f1 = SLOT.FAST1.value()
state_f2 = SLOT.FAST2.value()
state_f3 = SLOT.FAST3.value()
state_f4 = SLOT.FAST4.value()

# Read the slot's three 'Slow' pins. These have internal
# pull-ups so will read 1 if left disconnected
state_s1 = SLOT.SLOW1.value()
state_s2 = SLOT.SLOW2.value()
state_s3 = SLOT.SLOW3.value()

# Read the slot's two analog pins
adc1 = yukon.read_slot_adc1(SLOT)
adc2 = yukon.read_slot_adc2(SLOT)

# Print out the pin states in a nice format
print(f"F1 = {state_f1}", end=", ")
print(f"F2 = {state_f2}", end=", ")
print(f"F3 = {state_f3}", end=", ")
print(f"F4 = {state_f4}", end=", ")
print(f"S1 = {state_s1}", end=", ")
print(f"S2 = {state_s2}", end=", ")
print(f"S3 = {state_s3}", end=", ")
print(f"A1 = {adc1}", end=", ")
print(f"A2 = {adc2}")

time.sleep(SLEEP) # Sleep for a number of seconds

finally:
# Put the board back into a safe state, regardless of how the program may have ended
yukon.reset()
2 changes: 1 addition & 1 deletion examples/yukon/set_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pimoroni_yukon import Yukon, GP26, GP27, LCD_CS, LCD_DC, LCD_BL

"""
Read the IO pins on Yukon's expansion header.
Initialise the IO pins on Yukon's expansion header as outputs and set them.
"""

# Constants
Expand Down
63 changes: 63 additions & 0 deletions examples/yukon/set_slot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import time
from machine import Pin
from pimoroni_yukon import Yukon
from pimoroni_yukon import SLOT1 as SLOT

"""
Initialise the IO pins on a Yukon slot as outputs and set them.
"""

# Constants
SLEEP = 0.5 # The time to sleep between each reading

# Variables
yukon = Yukon() # A new Yukon object

# Configure the slot pins as outputs with a low state
SLOT.FAST1.init(Pin.OUT, value=False)
SLOT.FAST2.init(Pin.OUT, value=False)
SLOT.FAST3.init(Pin.OUT, value=False)
SLOT.FAST4.init(Pin.OUT, value=False)
SLOT.SLOW1.init(Pin.OUT, value=False)
SLOT.SLOW2.init(Pin.OUT, value=False)
SLOT.SLOW3.init(Pin.OUT, value=False)

# Store the slot pins in a list for easier access
pins = [SLOT.FAST1, SLOT.FAST2, SLOT.FAST3, SLOT.FAST4,
SLOT.SLOW1, SLOT.SLOW2, SLOT.SLOW3]
current_pin = 0

# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt)
try:
# Loop until the BOOT/USER button is pressed
while not yukon.is_boot_pressed():

# Toggle the current slot pin
pins[current_pin].toggle()

# Advance the current pin, and wrap when the end of the list is reached
current_pin = (current_pin + 1) % len(pins)

# Read the output states of the slot pins
state_f1 = SLOT.FAST1.value()
state_f2 = SLOT.FAST2.value()
state_f3 = SLOT.FAST3.value()
state_f4 = SLOT.FAST4.value()
state_s1 = SLOT.SLOW1.value()
state_s2 = SLOT.SLOW2.value()
state_s3 = SLOT.SLOW3.value()

# Print out the pin states in a nice format
print(f"F1 = {state_f1}", end=", ")
print(f"F2 = {state_f2}", end=", ")
print(f"F3 = {state_f3}", end=", ")
print(f"F4 = {state_f4}", end=", ")
print(f"S1 = {state_s1}", end=", ")
print(f"S2 = {state_s2}", end=", ")
print(f"S3 = {state_s3}")

time.sleep(SLEEP) # Sleep for a number of seconds

finally:
# Put the board back into a safe state, regardless of how the program may have ended
yukon.reset()

0 comments on commit bd2e3b3

Please sign in to comment.