bindings: python: examples: add gpionotify.py
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Fri, 10 Mar 2023 09:59:47 +0000 (10:59 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Sun, 12 Mar 2023 13:44:27 +0000 (14:44 +0100)
Add a new Python code examples - gpionotify.py - which is a simplified
reimplementation of the gpionotify tool.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/examples/Makefile.am
bindings/python/examples/gpionotify.py [new file with mode: 0755]

index f42b80e900a86abe9d4da6a762a6214e259cb7c5..9463a9fba2deb0c71fb89e8a874dac4d8fb0e958 100644 (file)
@@ -7,4 +7,5 @@ EXTRA_DIST = \
        gpioget.py \
        gpioinfo.py \
        gpiomon.py \
+       gpionotify.py \
        gpioset.py
diff --git a/bindings/python/examples/gpionotify.py b/bindings/python/examples/gpionotify.py
new file mode 100755 (executable)
index 0000000..4e50515
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
+
+"""Simplified reimplementation of the gpionotify tool in Python."""
+
+import gpiod
+import sys
+
+if __name__ == "__main__":
+    if len(sys.argv) < 3:
+        raise TypeError("usage: gpionotify.py <gpiochip> <offset1> <offset2> ...")
+
+    path = sys.argv[1]
+
+    with gpiod.Chip(path) as chip:
+        for line in sys.argv[2:]:
+            chip.watch_line_info(line)
+
+        while True:
+            print(chip.read_info_event())