Debian Codegraph @

bindings/python/examples/toggle_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 toggling a single line."""
6
7 import gpiod
8 import time
9
10 from gpiod.line import Direction, Value
11
12
13 def toggle_value(value):
14 if value == Value.INACTIVE:
15 return Value.ACTIVE
16 return Value.INACTIVE
17
18
19 def toggle_line_value(chip_path, line_offset):
20 value_str = {Value.ACTIVE: "Active", Value.INACTIVE: "Inactive"}
21 value = Value.ACTIVE
22
23 with gpiod.request_lines(
24 chip_path,
25 consumer="toggle-line-value",
26 config={
27 line_offset: gpiod.LineSettings(
28 direction=Direction.OUTPUT, output_value=value
29 )
30 },
31 ) as request:
32 while True:
33 print("{}={}".format(line_offset, value_str[value]))
34 time.sleep(1)
35 value = toggle_value(value)
36 request.set_value(line_offset, value)
37
38
39 if __name__ == "__main__":
40 try:
41 toggle_line_value("/dev/gpiochip0", 5)
42 except OSError as ex:
43 print(ex, "\nCustomise the example configuration to suit your situation")