aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKent Gibson <warthog618@gmail.com>2023-06-14 11:54:25 +0800
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>2023-06-14 15:02:07 +0200
commit953dbd6af794e528e05a22ba4bed4b02e49a46fd (patch)
treee8de927dfa44741312cc6e9c9b91b81e36c10cf3
parent53663aeb5f81283de4fc0bde8395e7bb94568cf1 (diff)
downloadlibgpiod-953dbd6af794e528e05a22ba4bed4b02e49a46fd.tar.gz
bindings: python: examples: add dedicated examples
Add python equivalents of the core examples. Signed-off-by: Kent Gibson <warthog618@gmail.com> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
-rwxr-xr-xbindings/python/examples/async_watch_line_value.py54
-rwxr-xr-xbindings/python/examples/get_line_value.py27
-rwxr-xr-xbindings/python/examples/toggle_line_value.py51
-rwxr-xr-xbindings/python/examples/watch_line_value.py49
4 files changed, 181 insertions, 0 deletions
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 00000000..ed09ec93
--- /dev/null
+++ b/bindings/python/examples/async_watch_line_value.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+"""Minimal example of asynchronously watching for edges on a single line."""
+
+import gpiod
+import select
+
+from datetime import timedelta
+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 00000000..ab733df3
--- /dev/null
+++ b/bindings/python/examples/get_line_value.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+"""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 00000000..46e52f91
--- /dev/null
+++ b/bindings/python/examples/toggle_line_value.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+"""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 00000000..42fc0bda
--- /dev/null
+++ b/bindings/python/examples/watch_line_value.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+"""Minimal example of watching for edges on a single line."""
+
+import gpiod
+
+from datetime import timedelta
+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()