All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod][PATCH 5/8] bindings: python: examples: consistency cleanup
Date: Fri, 23 Jun 2023 12:38:58 +0800	[thread overview]
Message-ID: <20230623043901.16764-6-warthog618@gmail.com> (raw)
In-Reply-To: <20230623043901.16764-1-warthog618@gmail.com>

A collection of minor changes to be more consistent with other examples:
 - capitalize comments
 - add line offset to value outputs
 - drop comma from edge event outputs
 - improve behaviour if run on a platform that does not match the
   example configuration
 - use with to cleanup request in toggle_line_value.py

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 .../python/examples/async_watch_line_value.py | 15 ++++----
 bindings/python/examples/get_line_value.py    | 13 ++++---
 bindings/python/examples/toggle_line_value.py | 34 +++++++------------
 bindings/python/examples/watch_line_value.py  | 19 +++++------
 4 files changed, 35 insertions(+), 46 deletions(-)

diff --git a/bindings/python/examples/async_watch_line_value.py b/bindings/python/examples/async_watch_line_value.py
index ed09ec9..ea8314f 100755
--- a/bindings/python/examples/async_watch_line_value.py
+++ b/bindings/python/examples/async_watch_line_value.py
@@ -19,12 +19,8 @@ def edge_type(event):
     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,
+def async_watch_line_value(chip_path, line_offset):
+    # Assume a button connecting the pin to ground,
     # so pull it up and provide some debounce.
     with gpiod.request_lines(
         chip_path,
@@ -40,7 +36,7 @@ def async_watch_line_value():
         poll = select.poll()
         poll.register(request.fd, select.POLLIN)
         while True:
-            # other fds could be registered with the poll and be handled
+            # 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():
@@ -51,4 +47,7 @@ def async_watch_line_value():
 
 
 if __name__ == "__main__":
-    async_watch_line_value()
+    try:
+        async_watch_line_value("/dev/gpiochip0", 5)
+    except OSError as ex:
+        print(ex, "\nCustomise the example configuration to suit your situation")
diff --git a/bindings/python/examples/get_line_value.py b/bindings/python/examples/get_line_value.py
index ab733df..f3ca13b 100755
--- a/bindings/python/examples/get_line_value.py
+++ b/bindings/python/examples/get_line_value.py
@@ -9,19 +9,18 @@ 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
-
+def get_line_value(chip_path, line_offset):
     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)
+        print("{}={}".format(line_offset, value))
 
 
 if __name__ == "__main__":
-    get_line_value()
+    try:
+        get_line_value("/dev/gpiochip0", 5)
+    except OSError as ex:
+        print(ex, "\nCustomise the example configuration to suit your situation")
diff --git a/bindings/python/examples/toggle_line_value.py b/bindings/python/examples/toggle_line_value.py
index 46e52f9..e0de8fb 100755
--- a/bindings/python/examples/toggle_line_value.py
+++ b/bindings/python/examples/toggle_line_value.py
@@ -16,21 +16,11 @@ def toggle_value(value):
     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
-
+def toggle_line_value(chip_path, line_offset):
+    value_str = {Value.ACTIVE: "Active", Value.INACTIVE: "Inactive"}
     value = Value.ACTIVE
 
-    request = gpiod.request_lines(
+    with gpiod.request_lines(
         chip_path,
         consumer="toggle-line-value",
         config={
@@ -38,14 +28,16 @@ def toggle_line_value():
                 direction=Direction.OUTPUT, output_value=value
             )
         },
-    )
-
-    while True:
-        print_value(value)
-        time.sleep(1)
-        value = toggle_value(value)
-        request.set_value(line_offset, value)
+    ) as request:
+        while True:
+            print("{}={}".format(line_offset, value_str[value]))
+            time.sleep(1)
+            value = toggle_value(value)
+            request.set_value(line_offset, value)
 
 
 if __name__ == "__main__":
-    toggle_line_value()
+    try:
+        toggle_line_value("/dev/gpiochip0", 5)
+    except OSError as ex:
+        print(ex, "\nCustomise the example configuration to suit your situation")
diff --git a/bindings/python/examples/watch_line_value.py b/bindings/python/examples/watch_line_value.py
index 42fc0bd..841bf40 100755
--- a/bindings/python/examples/watch_line_value.py
+++ b/bindings/python/examples/watch_line_value.py
@@ -12,18 +12,14 @@ from gpiod.line import Bias, Edge
 
 def edge_type(event):
     if event.event_type is event.Type.RISING_EDGE:
-        return "Rising "
+        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,
+def watch_line_value(chip_path, line_offset):
+    # Assume a button connecting the pin to ground,
     # so pull it up and provide some debounce.
     with gpiod.request_lines(
         chip_path,
@@ -37,13 +33,16 @@ def watch_line_value():
         },
     ) as request:
         while True:
-            # blocks until at least one event is available
+            # Blocks until at least one event is available
             for event in request.read_edge_events():
                 print(
-                    "offset: %d, type: %s, event #%d"
+                    "line: %d  type: %-7s  event #%d"
                     % (event.line_offset, edge_type(event), event.line_seqno)
                 )
 
 
 if __name__ == "__main__":
-    watch_line_value()
+    try:
+        watch_line_value("/dev/gpiochip0", 5)
+    except OSError as ex:
+        print(ex, "\nCustomise the example configuration to suit your situation")
-- 
2.41.0


  parent reply	other threads:[~2023-06-23  4:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-23  4:38 [libgpiod][PATCH 0/8] replace tool examples with use case examples Kent Gibson
2023-06-23  4:38 ` [libgpiod][PATCH 1/8] core: examples: consistency cleanups Kent Gibson
2023-06-23  4:38 ` [libgpiod][PATCH 2/8] core: examples: add more use case examples Kent Gibson
2023-06-23  4:38 ` [libgpiod][PATCH 3/8] bindings: cxx: examples: consistency cleanup Kent Gibson
2023-06-23  4:38 ` [libgpiod][PATCH 4/8] bindings: cxx: examples: replace tools examples with use case examples Kent Gibson
2023-06-23  4:38 ` Kent Gibson [this message]
2023-06-23  4:38 ` [libgpiod][PATCH 6/8] bindings: python: " Kent Gibson
2023-06-23 19:31   ` Bartosz Golaszewski
2023-06-23  4:39 ` [libgpiod][PATCH 7/8] bindings: rust: examples: consistency cleanup Kent Gibson
2023-06-23  7:58   ` Erik Schilling
2023-06-23  4:39 ` [libgpiod][PATCH 8/8] bindings: rust: examples: replace tools examples with use case examples Kent Gibson
2023-06-23  7:57   ` Erik Schilling
2023-06-23  8:26     ` Kent Gibson
2023-06-23 19:35 ` [libgpiod][PATCH 0/8] replace tool " Bartosz Golaszewski

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=20230623043901.16764-6-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.