| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Minimal example of watching for edges on a single line.""" |
| 6 |
|
| 7 |
import gpiod |
| 8 |
|
| 9 |
from datetime import timedelta |
| 10 |
from gpiod.line import Bias, Edge |
| 11 |
|
| 12 |
|
| 13 |
def edge_type_str(event): |
| 14 |
if event.event_type is event.Type.RISING_EDGE: |
| 15 |
return "Rising" |
| 16 |
if event.event_type is event.Type.FALLING_EDGE: |
| 17 |
return "Falling" |
| 18 |
return "Unknown" |
| 19 |
|
| 20 |
|
| 21 |
def watch_line_value(chip_path, line_offset): |
| 22 |
|
| 23 |
|
| 24 |
with gpiod.request_lines( |
| 25 |
chip_path, |
| 26 |
consumer="watch-line-value", |
| 27 |
config={ |
| 28 |
line_offset: gpiod.LineSettings( |
| 29 |
edge_detection=Edge.BOTH, |
| 30 |
bias=Bias.PULL_UP, |
| 31 |
debounce_period=timedelta(milliseconds=10), |
| 32 |
) |
| 33 |
}, |
| 34 |
) as request: |
| 35 |
while True: |
| 36 |
|
| 37 |
for event in request.read_edge_events(): |
| 38 |
print( |
| 39 |
"line: {} type: {:<7} event #{}".format( |
| 40 |
event.line_offset, edge_type_str(event), event.line_seqno |
| 41 |
) |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
if __name__ == "__main__": |
| 46 |
try: |
| 47 |
watch_line_value("/dev/gpiochip0", 5) |
| 48 |
except OSError as ex: |
| 49 |
print(ex, "\nCustomise the example configuration to suit your situation") |