Skip to content

Commit

Permalink
Update buttons.py
Browse files Browse the repository at this point in the history
  • Loading branch information
helgibbons authored Dec 24, 2024
1 parent 86004c1 commit de5648c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions examples/7color/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@
import gpiodevice
from gpiod.line import Bias, Direction, Edge

print("""buttons.py - Detect which button has been pressed
print(
"""buttons.py - Detect which button has been pressed
This example should demonstrate how to:
1. set up gpiod to read buttons,
2. determine which button has been pressed
Press Ctrl+C to exit!
""")
"""
)

# GPIO pins for each button (from top to bottom)
# These will vary depending on platform and the ones
# below should be correct for Raspberry Pi 5.
# Run "gpioinfo" to find out what yours might be
BUTTONS = ["PIN29", "PIN31", "PIN36", "PIN18"]
# Run "gpioinfo" to find out what yours might be.
#
# Raspberry Pi 5 Header pins used by Inky Impression:
# PIN29, PIN31, PIN36, PIN18.
# These header pins correspond to BCM GPIO numbers:
# GPIO05, GPIO06, GPIO16, GPIO24.
# These GPIO numbers are what is used below and not the
# header pin numbers.
BUTTONS = [5, 6, 16, 24]

# These correspond to buttons A, B, C and D respectively
LABELS = ["A", "B", "C", "D"]
Expand All @@ -39,13 +48,14 @@
# Request the lines, *whew*
request = chip.request_lines(consumer="inky7-buttons", config=line_config)


# "handle_button" will be called every time a button is pressed
# It receives one argument: the associated gpiod event object.
def handle_button(event):
index = OFFSETS.index(event.line_offset)
pin = BUTTONS[index]
gpio_number = BUTTONS[index]
label = LABELS[index]
print(f"Button press detected on pin: {pin} label: {label}")
print(f"Button press detected on GPIO #{gpio_number} label: {label}")


while True:
Expand Down

0 comments on commit de5648c

Please sign in to comment.