Debian Codegraph @

bindings/python/examples/reconfigure_input_to_output.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 """Example of a bi-directional line requested as input and then switched to output."""
6
7 import gpiod
8
9 from gpiod.line import Direction, Value
10
11
12 def reconfigure_input_to_output(chip_path, line_offset):
13 # request the line initially as an input
14 with gpiod.request_lines(
15 chip_path,
16 consumer="reconfigure-input-to-output",
17 config={line_offset: gpiod.LineSettings(direction=Direction.INPUT)},
18 ) as request:
19 # read the current line value
20 value = request.get_value(line_offset)
21 print("{}={} (input)".format(line_offset, value))
22 # switch the line to an output and drive it low
23 request.reconfigure_lines(
24 config={
25 line_offset: gpiod.LineSettings(
26 direction=Direction.OUTPUT, output_value=Value.INACTIVE
27 )
28 }
29 )
30 # report the current driven value
31 value = request.get_value(line_offset)
32 print("{}={} (output)".format(line_offset, value))
33
34
35 if __name__ == "__main__":
36 try:
37 reconfigure_input_to_output("/dev/gpiochip0", 5)
38 except OSError as ex:
39 print(ex, "\nCustomise the example configuration to suit your situation")