new file mode 100644
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
+
+EXTRA_DIST = \
+ gpiodetect.py \
+ gpiofind.py \
+ gpioget.py \
+ gpioinfo.py \
+ gpiomon.py \
+ gpioset.py
new file mode 100755
@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
+
+"""Reimplementation of the gpiodetect tool in Python."""
+
+import gpiod
+import os
+
+if __name__ == "__main__":
+ for entry in os.scandir("/dev/"):
+ if gpiod.is_gpiochip_device(entry.path):
+ with gpiod.Chip(entry.path) as chip:
+ info = chip.get_info()
+ print(
+ "{} [{}] ({} lines)".format(info.name, info.label, info.num_lines)
+ )
new file mode 100755
@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
+
+"""Reimplementation of the gpiofind tool in Python."""
+
+import gpiod
+import os
+import sys
+
+if __name__ == "__main__":
+ for entry in os.scandir("/dev/"):
+ if gpiod.is_gpiochip_device(entry.path):
+ with gpiod.Chip(entry.path) as chip:
+ offset = chip.get_line_offset_from_name(sys.argv[1])
+ if offset is not None:
+ print("{} {}".format(chip.get_info().name, offset))
+ sys.exit(0)
+
+ sys.exit(1)
new file mode 100755
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
+
+"""Simplified reimplementation of the gpioget tool in Python."""
+
+import gpiod
+import sys
+
+Direction = gpiod.Line.Direction
+Value = gpiod.Line.Value
+
+if __name__ == "__main__":
+ if len(sys.argv) < 3:
+ raise TypeError("usage: gpioget.py <gpiochip> <offset1> <offset2> ...")
+
+ path = sys.argv[1]
+ offsets = []
+ for off in sys.argv[2:]:
+ offsets.append(int(off))
+
+ with gpiod.request_lines(
+ path,
+ gpiod.RequestConfig(offsets=offsets, consumer="gpioget.py"),
+ gpiod.LineConfig(direction=Direction.INPUT),
+ ) as request:
+ vals = request.get_values()
+
+ for val in vals:
+ print("0" if val == Value.INACTIVE else "1", end=" ")
+ print()
new file mode 100755
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
+
+"""Simplified reimplementation of the gpioinfo tool in Python."""
+
+import gpiod
+import os
+
+if __name__ == "__main__":
+ for entry in os.scandir("/dev/"):
+ if gpiod.is_gpiochip_device(entry.path):
+ with gpiod.Chip(entry.path) as chip:
+ cinfo = chip.get_info()
+ print("{} - {} lines:".format(cinfo.name, cinfo.num_lines))
+
+ for offset in range(0, cinfo.num_lines):
+ linfo = chip.get_line_info(offset)
+ offset = linfo.offset
+ name = linfo.name
+ consumer = linfo.consumer
+ direction = linfo.direction
+ active_low = linfo.active_low
+
+ print(
+ "\tline {:>3}: {:>18} {:>12} {:>8} {:>10}".format(
+ offset,
+ "unnamed" if name is None else name,
+ "unused" if consumer is None else consumer,
+ "input"
+ if direction == gpiod.Line.Direction.INPUT
+ else "output",
+ "active-low" if active_low else "active-high",
+ )
+ )
new file mode 100755
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
+
+"""Simplified reimplementation of the gpiomon tool in Python."""
+
+import gpiod
+import sys
+
+Edge = gpiod.Line.Edge
+
+if __name__ == "__main__":
+ if len(sys.argv) < 3:
+ raise TypeError("usage: gpiomon.py <gpiochip> <offset1> <offset2> ...")
+
+ path = sys.argv[1]
+ offsets = []
+ for off in sys.argv[2:]:
+ offsets.append(int(off))
+
+ buf = gpiod.EdgeEventBuffer()
+
+ with gpiod.request_lines(
+ path,
+ gpiod.RequestConfig(offsets=offsets, consumer="gpiomon.py"),
+ gpiod.LineConfig(edge_detection=Edge.BOTH),
+ ) as request:
+ while True:
+ request.read_edge_event(buf)
+ for event in buf:
+ print(event)
new file mode 100755
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
+
+"""Simplified reimplementation of the gpioset tool in Python."""
+
+import gpiod
+import sys
+
+Value = gpiod.Line.Value
+
+if __name__ == "__main__":
+ if len(sys.argv) < 3:
+ raise TypeError("usage: gpioset.py <gpiochip> <offset1>=<value1> ...")
+
+ path = sys.argv[1]
+ values = dict()
+ for arg in sys.argv[2:]:
+ arg = arg.split("=")
+ key = int(arg[0])
+ val = int(arg[1])
+
+ if val == 1:
+ values[key] = Value.ACTIVE
+ elif val == 0:
+ values[key] = Value.INACTIVE
+ else:
+ raise ValueError("{} is an invalid value for GPIO lines".format(val))
+
+ with gpiod.request_lines(
+ path,
+ gpiod.RequestConfig(offsets=values.keys(), consumer="gpioset.py"),
+ gpiod.LineConfig(direction=gpiod.Line.Direction.OUTPUT, output_values=values),
+ ) as request:
+ input()
This adds the usual set of reimplementations of gpio-tools using the new python module. Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl> --- bindings/python/examples/Makefile.am | 10 ++++++++ bindings/python/examples/gpiodetect.py | 17 +++++++++++++ bindings/python/examples/gpiofind.py | 20 +++++++++++++++ bindings/python/examples/gpioget.py | 31 +++++++++++++++++++++++ bindings/python/examples/gpioinfo.py | 35 ++++++++++++++++++++++++++ bindings/python/examples/gpiomon.py | 31 +++++++++++++++++++++++ bindings/python/examples/gpioset.py | 35 ++++++++++++++++++++++++++ 7 files changed, 179 insertions(+) create mode 100644 bindings/python/examples/Makefile.am create mode 100755 bindings/python/examples/gpiodetect.py create mode 100755 bindings/python/examples/gpiofind.py create mode 100755 bindings/python/examples/gpioget.py create mode 100755 bindings/python/examples/gpioinfo.py create mode 100755 bindings/python/examples/gpiomon.py create mode 100755 bindings/python/examples/gpioset.py