From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod][PATCH 3/4] bindings: python: examples: add dedicated examples
Date: Wed, 14 Jun 2023 11:54:25 +0800 [thread overview]
Message-ID: <20230614035426.15097-4-warthog618@gmail.com> (raw)
In-Reply-To: <20230614035426.15097-1-warthog618@gmail.com>
Add python equivalents of the core examples.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
.../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@gmail.com>
+
+"""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@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 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@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 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@gmail.com>
+
+"""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
next prev parent reply other threads:[~2023-06-14 3:55 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-14 3:54 [libgpiod][PATCH 0/4] dedicated examples Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 1/4] core: examples: add " Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 2/4] bindings: cxx: " Kent Gibson
2023-06-14 3:54 ` Kent Gibson [this message]
2023-06-14 3:54 ` [libgpiod][PATCH 4/4] bindings: rust: " Kent Gibson
2023-06-14 7:52 ` Erik Schilling
2023-06-14 8:18 ` Kent Gibson
2023-06-14 8:29 ` Erik Schilling
2023-06-14 13:03 ` [libgpiod][PATCH 0/4] " Bartosz Golaszewski
2023-06-14 13:21 ` Kent Gibson
2023-06-14 13:26 ` Bartosz Golaszewski
2023-06-14 13:57 ` Kent Gibson
2023-06-14 15:11 ` Bartosz Golaszewski
2023-06-14 16:00 ` Kent Gibson
2023-06-15 15:16 ` Bartosz Golaszewski
2023-06-15 15:39 ` Kent Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230614035426.15097-4-warthog618@gmail.com \
--to=warthog618@gmail.com \
--cc=brgl@bgdev.pl \
--cc=linux-gpio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).