|
4 | 4 | import gpiodevice
|
5 | 5 | from gpiod.line import Bias, Direction, Edge
|
6 | 6 |
|
7 |
| -print("""buttons.py - Detect which button has been pressed |
| 7 | +print( |
| 8 | + """buttons.py - Detect which button has been pressed |
8 | 9 |
|
9 | 10 | This example should demonstrate how to:
|
10 | 11 | 1. set up gpiod to read buttons,
|
11 | 12 | 2. determine which button has been pressed
|
12 | 13 |
|
13 | 14 | Press Ctrl+C to exit!
|
14 | 15 |
|
15 |
| -""") |
| 16 | +""" |
| 17 | +) |
16 | 18 |
|
17 | 19 | # GPIO pins for each button (from top to bottom)
|
18 | 20 | # These will vary depending on platform and the ones
|
19 | 21 | # below should be correct for Raspberry Pi 5.
|
20 |
| -# Run "gpioinfo" to find out what yours might be |
21 |
| -BUTTONS = ["PIN29", "PIN31", "PIN36", "PIN18"] |
| 22 | +# Run "gpioinfo" to find out what yours might be. |
| 23 | +# |
| 24 | +# Raspberry Pi 5 Header pins used by Inky Impression: |
| 25 | +# PIN29, PIN31, PIN36, PIN18. |
| 26 | +# These header pins correspond to BCM GPIO numbers: |
| 27 | +# GPIO05, GPIO06, GPIO16, GPIO24. |
| 28 | +# These GPIO numbers are what is used below and not the |
| 29 | +# header pin numbers. |
| 30 | +BUTTONS = [5, 6, 16, 24] |
22 | 31 |
|
23 | 32 | # These correspond to buttons A, B, C and D respectively
|
24 | 33 | LABELS = ["A", "B", "C", "D"]
|
|
39 | 48 | # Request the lines, *whew*
|
40 | 49 | request = chip.request_lines(consumer="inky7-buttons", config=line_config)
|
41 | 50 |
|
| 51 | + |
42 | 52 | # "handle_button" will be called every time a button is pressed
|
43 | 53 | # It receives one argument: the associated gpiod event object.
|
44 | 54 | def handle_button(event):
|
45 | 55 | index = OFFSETS.index(event.line_offset)
|
46 |
| - pin = BUTTONS[index] |
| 56 | + gpio_number = BUTTONS[index] |
47 | 57 | label = LABELS[index]
|
48 |
| - print(f"Button press detected on pin: {pin} label: {label}") |
| 58 | + print(f"Button press detected on GPIO #{gpio_number} label: {label}") |
49 | 59 |
|
50 | 60 |
|
51 | 61 | while True:
|
|
0 commit comments