[libgpiod][PATCH 3/4] bindings: python: examples: add dedicated examples

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add python equivalents of the core examples.

Signed-off-by: Kent Gibson <warthog618@xxxxxxxxx>
---
 .../python/examples/async_watch_line_value.py | 47 +++++++++++++++++++
 bindings/python/examples/get_line_value.py    | 26 ++++++++++
 bindings/python/examples/toggle_line_value.py | 47 +++++++++++++++++++
 bindings/python/examples/watch_line_value.py  | 42 +++++++++++++++++
 4 files changed, 162 insertions(+)
 create mode 100755 bindings/python/examples/async_watch_line_value.py
 create mode 100755 bindings/python/examples/get_line_value.py
 create mode 100755 bindings/python/examples/toggle_line_value.py
 create mode 100755 bindings/python/examples/watch_line_value.py

diff --git a/bindings/python/examples/async_watch_line_value.py b/bindings/python/examples/async_watch_line_value.py
new file mode 100755
index 0000000..031a988
--- /dev/null
+++ b/bindings/python/examples/async_watch_line_value.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@xxxxxxxxx>
+
+"""Minimal example of asynchronously watching for edges on a single line."""
+
+from datetime import timedelta
+import gpiod
+import select
+
+from gpiod.line import Bias, Edge
+
+def edge_type(event):
+    if event.event_type is event.Type.RISING_EDGE:
+        return "Rising "
+    if event.event_type is event.Type.FALLING_EDGE:
+        return "Falling"
+    return "Unknown"
+
+
+def async_watch_line_value():
+    # example configuration - customise to suit your situation
+    chip_path = '/dev/gpiochip0'
+    line_offset = 5
+
+    # assume a button connecting the pin to ground,
+    # so pull it up and provide some debounce.
+    with gpiod.request_lines(
+        chip_path,
+        consumer="async-watch-line-value",
+        config={line_offset: gpiod.LineSettings(edge_detection=Edge.BOTH,
+                                bias=Bias.PULL_UP,
+                                debounce_period=timedelta(milliseconds=10))},
+    ) as request:
+        poll = select.poll()
+        poll.register(request.fd, select.POLLIN)
+        while True:
+            # other fds could be registered with the poll and be handled
+            # separately using the return value (fd, event) from poll()
+            poll.poll()
+            for event in request.read_edge_events():
+                print("offset: %d, type: %s, event #%d" %
+                      (event.line_offset, edge_type(event), event.line_seqno))
+
+
+if __name__ == "__main__":
+    async_watch_line_value()
diff --git a/bindings/python/examples/get_line_value.py b/bindings/python/examples/get_line_value.py
new file mode 100755
index 0000000..da9d060
--- /dev/null
+++ b/bindings/python/examples/get_line_value.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@xxxxxxxxx>
+
+"""Minimal example of reading a single line."""
+
+import gpiod
+
+from gpiod.line import Direction
+
+def get_line_value():
+    # example configuration - customise to suit your situation
+    chip_path = '/dev/gpiochip0'
+    line_offset = 5
+
+    with gpiod.request_lines(
+        chip_path,
+        consumer="get-line-value",
+        config={line_offset: gpiod.LineSettings(direction=Direction.INPUT)},
+    ) as request:
+        value = request.get_value(line_offset)
+        print(value)
+
+
+if __name__ == "__main__":
+    get_line_value()
diff --git a/bindings/python/examples/toggle_line_value.py b/bindings/python/examples/toggle_line_value.py
new file mode 100755
index 0000000..ed84d5b
--- /dev/null
+++ b/bindings/python/examples/toggle_line_value.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@xxxxxxxxx>
+
+"""Minimal example of toggling a single line."""
+
+import gpiod
+import time
+
+from gpiod.line import Direction, Value
+
+def toggle_value(value):
+    if value == Value.INACTIVE:
+        return Value.ACTIVE
+    return Value.INACTIVE
+
+
+def print_value(value):
+    if value == Value.ACTIVE:
+        print("Active")
+    else:
+        print("Inactive")
+
+
+def toggle_line_value():
+    # example configuration - customise to suit your situation
+    chip_path = '/dev/gpiochip0'
+    line_offset = 5
+
+    value = Value.ACTIVE
+
+    request = gpiod.request_lines(
+        chip_path,
+        consumer="toggle-line-value",
+        config={line_offset: gpiod.LineSettings(direction=Direction.OUTPUT,
+                                                output_value=value)},
+    )
+
+    while True:
+        print_value(value)
+        time.sleep(1)
+        value = toggle_value(value)
+        request.set_value(line_offset, value)
+
+
+if __name__ == "__main__":
+    toggle_line_value()
diff --git a/bindings/python/examples/watch_line_value.py b/bindings/python/examples/watch_line_value.py
new file mode 100755
index 0000000..5747706
--- /dev/null
+++ b/bindings/python/examples/watch_line_value.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@xxxxxxxxx>
+
+"""Minimal example of watching for edges on a single line."""
+
+from datetime import timedelta
+import gpiod
+
+from gpiod.line import Bias, Edge
+
+def edge_type(event):
+    if event.event_type is event.Type.RISING_EDGE:
+        return "Rising "
+    if event.event_type is event.Type.FALLING_EDGE:
+        return "Falling"
+    return "Unknown"
+
+
+def watch_line_value():
+    # example configuration - customise to suit your situation
+    chip_path = '/dev/gpiochip0'
+    line_offset = 5
+
+    # assume a button connecting the pin to ground,
+    # so pull it up and provide some debounce.
+    with gpiod.request_lines(
+        chip_path,
+        consumer="watch-line-value",
+        config={line_offset: gpiod.LineSettings(edge_detection=Edge.BOTH,
+                                bias=Bias.PULL_UP,
+                                debounce_period=timedelta(milliseconds=10))},
+    ) as request:
+        while True:
+            # blocks until at least one event is available
+            for event in request.read_edge_events():
+                print("offset: %d, type: %s, event #%d" %
+                      (event.line_offset, edge_type(event), event.line_seqno))
+
+
+if __name__ == "__main__":
+    watch_line_value()
-- 
2.40.1




[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux