Debian Codegraph @

bindings/python/examples/watch_line_value.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 # SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
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 # Assume a button connecting the pin to ground,
23 # so pull it up and provide some debounce.
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 # Blocks until at least one event is available
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")