* [libgpiod][PATCH 1/4] core: examples: add dedicated examples
2023-06-14 3:54 [libgpiod][PATCH 0/4] dedicated examples Kent Gibson
@ 2023-06-14 3:54 ` Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 2/4] bindings: cxx: " Kent Gibson
` (3 subsequent siblings)
4 siblings, 0 replies; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 3:54 UTC (permalink / raw)
To: linux-gpio, brgl; +Cc: Kent Gibson
The tools have served as example code, but have become too complicated
to serve that purpose.
Add a set of examples that have no purpose other than providing minimal
examples of common use cases.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
Makefile.am | 6 ++
configure.ac | 1 +
examples/.gitignore | 7 ++
examples/Makefile.am | 17 ++++
examples/async_watch_line_value.c | 136 ++++++++++++++++++++++++++++++
examples/get_line_value.c | 97 +++++++++++++++++++++
examples/toggle_line_value.c | 106 +++++++++++++++++++++++
examples/watch_line_value.c | 127 ++++++++++++++++++++++++++++
8 files changed, 497 insertions(+)
create mode 100644 examples/.gitignore
create mode 100644 examples/Makefile.am
create mode 100644 examples/async_watch_line_value.c
create mode 100644 examples/get_line_value.c
create mode 100644 examples/toggle_line_value.c
create mode 100644 examples/watch_line_value.c
diff --git a/Makefile.am b/Makefile.am
index 10d6a9a..941d7e8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -16,6 +16,12 @@ EXTRA_DIST = \
LICENSES/LGPL-3.0-or-later.txt \
LICENSES/BSD-3-Clause.txt
+if WITH_EXAMPLES
+
+SUBDIRS += examples
+
+endif
+
if WITH_TOOLS
SUBDIRS += tools man
diff --git a/configure.ac b/configure.ac
index c1005a9..dde2fa5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -268,6 +268,7 @@ AC_CONFIG_FILES([Makefile
lib/Makefile
lib/libgpiod.pc
contrib/Makefile
+ examples/Makefile
tools/Makefile
tests/Makefile
tests/gpiosim/Makefile
diff --git a/examples/.gitignore b/examples/.gitignore
new file mode 100644
index 0000000..bdfde9a
--- /dev/null
+++ b/examples/.gitignore
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+async_watch_line_value
+get_line_value
+toggle_line_value
+watch_line_value
diff --git a/examples/Makefile.am b/examples/Makefile.am
new file mode 100644
index 0000000..4ad124b
--- /dev/null
+++ b/examples/Makefile.am
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+AM_CFLAGS = -I$(top_srcdir)/include/ -include $(top_builddir)/config.h
+AM_CFLAGS += -Wall -Wextra -g -std=gnu89
+
+LDADD = $(top_builddir)/lib/libgpiod.la
+
+bin_PROGRAMS = async_watch_line_value get_line_value toggle_line_value watch_line_value
+
+async_watch_line_value_SOURCES = async_watch_line_value.c
+
+get_line_value_SOURCES = get_line_value.c
+
+toggle_line_valuer_SOURCES = toggle_line_value.c
+
+watch_line_value_SOURCES = watch_line_value.c
diff --git a/examples/async_watch_line_value.c b/examples/async_watch_line_value.c
new file mode 100644
index 0000000..de15c97
--- /dev/null
+++ b/examples/async_watch_line_value.c
@@ -0,0 +1,136 @@
+// 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 */
+
+#include <errno.h>
+#include <gpiod.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* request a line as input with edge detection */
+struct gpiod_line_request *request_input_line(const char *chip_path,
+ unsigned int offset,
+ const char *consumer)
+{
+ struct gpiod_line_settings *settings;
+ struct gpiod_line_request *request = NULL;
+ struct gpiod_request_config *req_cfg = NULL;
+ struct gpiod_line_config *line_cfg;
+ struct gpiod_chip *chip;
+ int ret;
+
+ chip = gpiod_chip_open(chip_path);
+ if (!chip)
+ return NULL;
+
+ settings = gpiod_line_settings_new();
+ if (!settings)
+ goto close_chip;
+
+ gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
+ gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
+ /* assume a button connecting the pin to ground, so pull it up... */
+ gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_PULL_UP);
+ /* ... and provide some debounce. */
+ gpiod_line_settings_set_debounce_period_us(settings, 10000);
+
+ line_cfg = gpiod_line_config_new();
+ if (!line_cfg)
+ goto free_settings;
+
+ ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
+ settings);
+ if (ret)
+ goto free_line_config;
+
+ if (consumer) {
+ req_cfg = gpiod_request_config_new();
+ if (!req_cfg)
+ goto free_line_config;
+
+ gpiod_request_config_set_consumer(req_cfg, consumer);
+ }
+
+ request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+
+ gpiod_request_config_free(req_cfg);
+free_line_config:
+ gpiod_line_config_free(line_cfg);
+free_settings:
+ gpiod_line_settings_free(settings);
+close_chip:
+ gpiod_chip_close(chip);
+
+ return request;
+}
+
+const char *edge_event_type_str(struct gpiod_edge_event *event)
+{
+ enum gpiod_edge_event_type eet = gpiod_edge_event_get_event_type(event);
+
+ if (eet == GPIOD_EDGE_EVENT_RISING_EDGE)
+ return "Rising ";
+ if (eet == GPIOD_EDGE_EVENT_FALLING_EDGE)
+ return "Falling";
+ return "Unknown";
+}
+
+int main(void)
+{
+ struct gpiod_line_request *request;
+ struct gpiod_edge_event_buffer *event_buffer;
+ struct gpiod_edge_event *event;
+ struct pollfd pollfd;
+ int i, ret, event_buf_size;
+ /* example configuration - customize to suit your situation */
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ request = request_input_line(chip_path, line_offset,
+ "async-watch-line-value");
+ if (!request) {
+ fprintf(stderr, "failed to request line: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ /* a larger buffer is an optimisation for reading bursts of events from
+ * the kernel, but that is not necessary in this case, so 1 is fine.
+ */
+ event_buf_size = 1;
+ event_buffer = gpiod_edge_event_buffer_new(event_buf_size);
+ if (!event_buffer) {
+ fprintf(stderr, "failed to create event buffer: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ pollfd.fd = gpiod_line_request_get_fd(request);
+ pollfd.events = POLLIN;
+ while (1) {
+ ret = poll(&pollfd, 1, -1);
+ if (ret == -1) {
+ fprintf(stderr, "error waiting for edge events: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+ ret = gpiod_line_request_read_edge_events(request, event_buffer,
+ event_buf_size);
+ if (ret == -1) {
+ fprintf(stderr, "error reading edge events: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+ for (i = 0; i < ret; i++) {
+ event = gpiod_edge_event_buffer_get_event(event_buffer,
+ i);
+ printf("offset: %d, type: %s, event #%ld\n",
+ gpiod_edge_event_get_line_offset(event),
+ edge_event_type_str(event),
+ gpiod_edge_event_get_line_seqno(event));
+ }
+ }
+}
diff --git a/examples/get_line_value.c b/examples/get_line_value.c
new file mode 100644
index 0000000..fc96a5b
--- /dev/null
+++ b/examples/get_line_value.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+/* Minimal example of reading a single line. */
+
+#include <errno.h>
+#include <gpiod.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* request a line as input */
+struct gpiod_line_request *request_input_line(const char *chip_path,
+ unsigned int offset,
+ const char *consumer)
+{
+ struct gpiod_line_settings *settings;
+ struct gpiod_line_request *request = NULL;
+ struct gpiod_request_config *req_cfg = NULL;
+ struct gpiod_line_config *line_cfg;
+ struct gpiod_chip *chip;
+ int ret;
+
+ chip = gpiod_chip_open(chip_path);
+ if (!chip)
+ return NULL;
+
+ settings = gpiod_line_settings_new();
+ if (!settings)
+ goto close_chip;
+
+ gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
+
+ line_cfg = gpiod_line_config_new();
+ if (!line_cfg)
+ goto free_settings;
+
+ ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
+ settings);
+ if (ret)
+ goto free_line_config;
+
+ if (consumer) {
+ req_cfg = gpiod_request_config_new();
+ if (!req_cfg)
+ goto free_line_config;
+
+ gpiod_request_config_set_consumer(req_cfg, consumer);
+ }
+
+ request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+
+ gpiod_request_config_free(req_cfg);
+free_line_config:
+ gpiod_line_config_free(line_cfg);
+free_settings:
+ gpiod_line_settings_free(settings);
+close_chip:
+ gpiod_chip_close(chip);
+
+ return request;
+}
+
+int print_value(enum gpiod_line_value value)
+{
+ if (value == GPIOD_LINE_VALUE_ACTIVE)
+ printf("Active\n");
+ else if (value == GPIOD_LINE_VALUE_INACTIVE) {
+ printf("Inactive\n");
+ } else {
+ fprintf(stderr, "error reading value: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
+}
+
+int main(void)
+{
+ struct gpiod_line_request *request;
+ enum gpiod_line_value value;
+ int ret;
+ /* example configuration - customize to suit your situation */
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ request = request_input_line(chip_path, line_offset, "get-line-value");
+ if (!request) {
+ fprintf(stderr, "failed to request line: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ value = gpiod_line_request_get_value(request, line_offset);
+ ret = print_value(value);
+ return ret;
+}
diff --git a/examples/toggle_line_value.c b/examples/toggle_line_value.c
new file mode 100644
index 0000000..ada163e
--- /dev/null
+++ b/examples/toggle_line_value.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+/* Minimal example of toggling a single line. */
+
+#include <errno.h>
+#include <gpiod.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+struct gpiod_line_request *request_output_line(const char *chip_path,
+ unsigned int offset,
+ enum gpiod_line_value value,
+ const char *consumer)
+{
+ struct gpiod_line_settings *settings;
+ struct gpiod_line_request *request = NULL;
+ struct gpiod_request_config *req_cfg = NULL;
+ struct gpiod_line_config *line_cfg;
+ struct gpiod_chip *chip;
+ int ret;
+
+ chip = gpiod_chip_open(chip_path);
+ if (!chip)
+ return NULL;
+
+ settings = gpiod_line_settings_new();
+ if (!settings)
+ goto close_chip;
+
+ gpiod_line_settings_set_direction(settings,
+ GPIOD_LINE_DIRECTION_OUTPUT);
+ gpiod_line_settings_set_output_value(settings, value);
+
+ line_cfg = gpiod_line_config_new();
+ if (!line_cfg)
+ goto free_settings;
+
+ ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
+ settings);
+ if (ret)
+ goto free_settings;
+
+ if (consumer) {
+ req_cfg = gpiod_request_config_new();
+ if (!req_cfg)
+ goto free_line_config;
+
+ gpiod_request_config_set_consumer(req_cfg, consumer);
+ }
+
+ request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+
+ gpiod_request_config_free(req_cfg);
+free_line_config:
+ gpiod_line_config_free(line_cfg);
+free_settings:
+ gpiod_line_settings_free(settings);
+close_chip:
+ gpiod_chip_close(chip);
+
+ return request;
+}
+
+enum gpiod_line_value toggle_line_value(enum gpiod_line_value value)
+{
+ return (value == GPIOD_LINE_VALUE_ACTIVE) ? GPIOD_LINE_VALUE_INACTIVE :
+ GPIOD_LINE_VALUE_ACTIVE;
+}
+
+void print_value(enum gpiod_line_value value)
+{
+ if (value == GPIOD_LINE_VALUE_ACTIVE)
+ printf("Active\n");
+ else
+ printf("Inactive\n");
+}
+
+int main(void)
+{
+ struct gpiod_line_request *request;
+ enum gpiod_line_value value = GPIOD_LINE_VALUE_ACTIVE;
+ /* example configuration - customize to suit your situation */
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ request = request_output_line(chip_path, line_offset, value,
+ "toggle-line-value");
+ if (!request) {
+ fprintf(stderr, "failed to request line: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ while (1) {
+ print_value(value);
+ sleep(1);
+ value = toggle_line_value(value);
+ gpiod_line_request_set_value(request, line_offset, value);
+ }
+
+ gpiod_line_request_release(request);
+ return EXIT_SUCCESS;
+}
diff --git a/examples/watch_line_value.c b/examples/watch_line_value.c
new file mode 100644
index 0000000..011943e
--- /dev/null
+++ b/examples/watch_line_value.c
@@ -0,0 +1,127 @@
+// 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. */
+
+#include <errno.h>
+#include <gpiod.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* request a line as input with edge detection */
+struct gpiod_line_request *request_input_line(const char *chip_path,
+ unsigned int offset,
+ const char *consumer)
+{
+ struct gpiod_line_settings *settings;
+ struct gpiod_line_request *request = NULL;
+ struct gpiod_request_config *req_cfg = NULL;
+ struct gpiod_line_config *line_cfg;
+ struct gpiod_chip *chip;
+ int ret;
+
+ chip = gpiod_chip_open(chip_path);
+ if (!chip)
+ return NULL;
+
+ settings = gpiod_line_settings_new();
+ if (!settings)
+ goto close_chip;
+
+ gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
+ gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
+ /* assume a button connecting the pin to ground, so pull it up... */
+ gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_PULL_UP);
+ /* ... and provide some debounce. */
+ gpiod_line_settings_set_debounce_period_us(settings, 10000);
+
+ line_cfg = gpiod_line_config_new();
+ if (!line_cfg)
+ goto free_settings;
+
+ ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
+ settings);
+ if (ret)
+ goto free_line_config;
+
+ if (consumer) {
+ req_cfg = gpiod_request_config_new();
+ if (!req_cfg)
+ goto free_line_config;
+
+ gpiod_request_config_set_consumer(req_cfg, consumer);
+ }
+
+ request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+
+ gpiod_request_config_free(req_cfg);
+free_line_config:
+ gpiod_line_config_free(line_cfg);
+free_settings:
+ gpiod_line_settings_free(settings);
+close_chip:
+ gpiod_chip_close(chip);
+
+ return request;
+}
+
+const char *edge_event_type_str(struct gpiod_edge_event *event)
+{
+ enum gpiod_edge_event_type eet = gpiod_edge_event_get_event_type(event);
+
+ if (eet == GPIOD_EDGE_EVENT_RISING_EDGE)
+ return "Rising ";
+ if (eet == GPIOD_EDGE_EVENT_FALLING_EDGE)
+ return "Falling";
+ return "Unknown";
+}
+
+int main(void)
+{
+ struct gpiod_line_request *request;
+ struct gpiod_edge_event_buffer *event_buffer;
+ struct gpiod_edge_event *event;
+ int i, ret, event_buf_size;
+ /* example configuration - customize to suit your situation */
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ request = request_input_line(chip_path, line_offset,
+ "watch-line-value");
+ if (!request) {
+ fprintf(stderr, "failed to request line: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ /* a larger buffer is an optimisation for reading bursts of events from
+ * the kernel, but that is not necessary in this case, so 1 is fine.
+ */
+ event_buf_size = 1;
+ event_buffer = gpiod_edge_event_buffer_new(event_buf_size);
+ if (!event_buffer) {
+ fprintf(stderr, "failed to create event buffer: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ while (1) {
+ /* blocks until at least one event is available */
+ ret = gpiod_line_request_read_edge_events(request, event_buffer,
+ event_buf_size);
+ if (ret == -1) {
+ fprintf(stderr, "error reading edge events: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+ for (i = 0; i < ret; i++) {
+ event = gpiod_edge_event_buffer_get_event(event_buffer,
+ i);
+ printf("offset: %d, type: %s, event #%ld\n",
+ gpiod_edge_event_get_line_offset(event),
+ edge_event_type_str(event),
+ gpiod_edge_event_get_line_seqno(event));
+ }
+ }
+}
--
2.40.1
^ permalink raw reply related [flat|nested] 16+ messages in thread* [libgpiod][PATCH 2/4] bindings: cxx: examples: add dedicated examples
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 ` Kent Gibson
2023-06-14 3:54 ` [libgpiod][PATCH 3/4] bindings: python: " Kent Gibson
` (2 subsequent siblings)
4 siblings, 0 replies; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 3:54 UTC (permalink / raw)
To: linux-gpio, brgl; +Cc: Kent Gibson
Add cxx equivalents of the core examples.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
bindings/cxx/examples/.gitignore | 4 +
bindings/cxx/examples/Makefile.am | 14 +++-
.../cxx/examples/async_watch_line_value.cpp | 78 +++++++++++++++++++
bindings/cxx/examples/get_line_value.cpp | 29 +++++++
bindings/cxx/examples/toggle_line_value.cpp | 45 +++++++++++
bindings/cxx/examples/watch_line_value.cpp | 62 +++++++++++++++
6 files changed, 231 insertions(+), 1 deletion(-)
create mode 100644 bindings/cxx/examples/async_watch_line_value.cpp
create mode 100644 bindings/cxx/examples/get_line_value.cpp
create mode 100644 bindings/cxx/examples/toggle_line_value.cpp
create mode 100644 bindings/cxx/examples/watch_line_value.cpp
diff --git a/bindings/cxx/examples/.gitignore b/bindings/cxx/examples/.gitignore
index 2209497..268b3f6 100644
--- a/bindings/cxx/examples/.gitignore
+++ b/bindings/cxx/examples/.gitignore
@@ -8,3 +8,7 @@ gpioinfocxx
gpiomoncxx
gpionotifycxx
gpiosetcxx
+async_watch_line_value
+get_line_value
+toggle_line_value
+watch_line_value
diff --git a/bindings/cxx/examples/Makefile.am b/bindings/cxx/examples/Makefile.am
index 36977ef..0213973 100644
--- a/bindings/cxx/examples/Makefile.am
+++ b/bindings/cxx/examples/Makefile.am
@@ -12,7 +12,11 @@ noinst_PROGRAMS = \
gpioinfocxx \
gpiomoncxx \
gpionotifycxx \
- gpiosetcxx
+ gpiosetcxx \
+ async_watch_line_value \
+ get_line_value \
+ toggle_line_value \
+ watch_line_value
gpiodetectcxx_SOURCES = gpiodetectcxx.cpp
@@ -27,3 +31,11 @@ gpiomoncxx_SOURCES = gpiomoncxx.cpp
gpionotifycxx_SOURCES = gpionotifycxx.cpp
gpiosetcxx_SOURCES = gpiosetcxx.cpp
+
+async_watch_line_value_SOURCES = async_watch_line_value.cpp
+
+get_line_value_SOURCES = get_line_value.cpp
+
+toggle_line_value_SOURCES = toggle_line_value.cpp
+
+watch_line_value_SOURCES = watch_line_value.cpp
diff --git a/bindings/cxx/examples/async_watch_line_value.cpp b/bindings/cxx/examples/async_watch_line_value.cpp
new file mode 100644
index 0000000..965e7dd
--- /dev/null
+++ b/bindings/cxx/examples/async_watch_line_value.cpp
@@ -0,0 +1,78 @@
+// 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. */
+
+#include <cerrno>
+#include <cstdlib>
+#include <cstring>
+#include <gpiod.hpp>
+#include <iostream>
+#include <poll.h>
+
+const char *edge_event_type_str(const ::gpiod::edge_event &event)
+{
+ switch (event.type()) {
+ case ::gpiod::edge_event::event_type::RISING_EDGE:
+ return "Rising ";
+ case ::gpiod::edge_event::event_type::FALLING_EDGE:
+ return "Falling";
+ default:
+ return "Unknown";
+ }
+}
+
+int main(void)
+{
+ // example configuration - customize to suit your situation
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ // assume a button connecting the pin to ground,
+ // so pull it up and provide some debounce.
+ auto request =
+ ::gpiod::chip(chip_path)
+ .prepare_request()
+ .set_consumer("async-watch-line-value")
+ .add_line_settings(
+ line_offset,
+ ::gpiod::line_settings()
+ .set_direction(
+ ::gpiod::line::direction::INPUT)
+ .set_edge_detection(
+ ::gpiod::line::edge::BOTH)
+ .set_bias(::gpiod::line::bias::PULL_UP)
+ .set_debounce_period(
+ std::chrono::milliseconds(10)))
+ .do_request();
+
+ // a larger buffer is an optimisation for reading bursts of events from
+ // the kernel, but that is not necessary in this case, so 1 is fine.
+ ::gpiod::edge_event_buffer buffer(1);
+
+ struct pollfd pollfd;
+ pollfd.fd = request.fd();
+ pollfd.events = POLLIN;
+
+ for (;;) {
+ // other fds could be registered with the poll and be handled
+ // separately using the pollfd.revents after poll()
+ int ret = poll(&pollfd, 1, -1);
+ if (ret == -1) {
+ ::std::cerr << "error waiting for edge events: "
+ << strerror(errno) << ::std::endl;
+
+ return EXIT_FAILURE;
+ }
+
+ request.read_edge_events(buffer);
+
+ for (const auto &event : buffer)
+ ::std::cout << "offset: " << event.line_offset()
+ << ", type: " << edge_event_type_str(event)
+ << ", event #" << event.line_seqno()
+ << ::std::endl;
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/bindings/cxx/examples/get_line_value.cpp b/bindings/cxx/examples/get_line_value.cpp
new file mode 100644
index 0000000..3e14b56
--- /dev/null
+++ b/bindings/cxx/examples/get_line_value.cpp
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+/* Minimal example of reading a single line. */
+
+#include <cstdlib>
+#include <gpiod.hpp>
+#include <iostream>
+
+int main(void)
+{
+ using ::gpiod::line_settings;
+ /* example configuration - customize to suit your situation */
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ auto request = ::gpiod::chip(chip_path)
+ .prepare_request()
+ .set_consumer("get-line-value")
+ .add_line_settings(
+ line_offset,
+ ::gpiod::line_settings().set_direction(
+ ::gpiod::line::direction::INPUT))
+ .do_request();
+
+ ::gpiod::line::value value = request.get_value(line_offset);
+ ::std::cout << value << ::std::endl;
+ return EXIT_SUCCESS;
+}
diff --git a/bindings/cxx/examples/toggle_line_value.cpp b/bindings/cxx/examples/toggle_line_value.cpp
new file mode 100644
index 0000000..2f47553
--- /dev/null
+++ b/bindings/cxx/examples/toggle_line_value.cpp
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+
+/* Minimal example of toggling a single line. */
+
+#include <gpiod.hpp>
+#include <chrono>
+#include <thread>
+#include <cstdlib>
+#include <iostream>
+
+using ::gpiod::line::value;
+
+value toggle_value(value v)
+{
+ return (v == value::ACTIVE) ? value::INACTIVE : value::ACTIVE;
+}
+
+int main(void)
+{
+ /* example configuration - customize to suit your situation */
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ value v = value::ACTIVE;
+
+ auto request =
+ ::gpiod::chip(chip_path)
+ .prepare_request()
+ .set_consumer("toggle-line-value")
+ .add_line_settings(
+ line_offset,
+ ::gpiod::line_settings().set_direction(
+ ::gpiod::line::direction::OUTPUT))
+ .do_request();
+
+ for (;;) {
+ ::std::cout << v << ::std::endl;
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ v = toggle_value(v);
+ request.set_value(line_offset, v);
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/bindings/cxx/examples/watch_line_value.cpp b/bindings/cxx/examples/watch_line_value.cpp
new file mode 100644
index 0000000..d3f2390
--- /dev/null
+++ b/bindings/cxx/examples/watch_line_value.cpp
@@ -0,0 +1,62 @@
+// 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. */
+
+#include <cstdlib>
+#include <gpiod.hpp>
+#include <iostream>
+
+const char *edge_event_type_str(const ::gpiod::edge_event &event)
+{
+ switch (event.type()) {
+ case ::gpiod::edge_event::event_type::RISING_EDGE:
+ return "Rising ";
+ case ::gpiod::edge_event::event_type::FALLING_EDGE:
+ return "Falling";
+ default:
+ return "Unknown";
+ }
+}
+
+int main(void)
+{
+ /* example configuration - customize to suit your situation */
+ const char *chip_path = "/dev/gpiochip0";
+ int line_offset = 5;
+
+ // assume a button connecting the pin to ground,
+ // so pull it up and provide some debounce.
+ auto request =
+ ::gpiod::chip(chip_path)
+ .prepare_request()
+ .set_consumer("watch-line-value")
+ .add_line_settings(
+ line_offset,
+ ::gpiod::line_settings()
+ .set_direction(
+ ::gpiod::line::direction::INPUT)
+ .set_edge_detection(
+ ::gpiod::line::edge::BOTH)
+ .set_bias(::gpiod::line::bias::PULL_UP)
+ .set_debounce_period(
+ std::chrono::milliseconds(10)))
+ .do_request();
+
+ // a larger buffer is an optimisation for reading bursts of events from
+ // the kernel, but that is not necessary in this case, so 1 is fine.
+ ::gpiod::edge_event_buffer buffer(1);
+
+ for (;;) {
+ // blocks until at least one event is available
+ request.read_edge_events(buffer);
+
+ for (const auto &event : buffer)
+ ::std::cout << "offset: " << event.line_offset()
+ << ", type: " << edge_event_type_str(event)
+ << ", event #" << event.line_seqno()
+ << ::std::endl;
+ }
+
+ return EXIT_SUCCESS;
+}
--
2.40.1
^ permalink raw reply related [flat|nested] 16+ messages in thread* [libgpiod][PATCH 3/4] bindings: python: examples: add dedicated examples
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
2023-06-14 3:54 ` [libgpiod][PATCH 4/4] bindings: rust: " Kent Gibson
2023-06-14 13:03 ` [libgpiod][PATCH 0/4] " Bartosz Golaszewski
4 siblings, 0 replies; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 3:54 UTC (permalink / raw)
To: linux-gpio, brgl; +Cc: Kent Gibson
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
^ permalink raw reply related [flat|nested] 16+ messages in thread* [libgpiod][PATCH 4/4] bindings: rust: examples: add dedicated examples
2023-06-14 3:54 [libgpiod][PATCH 0/4] dedicated examples Kent Gibson
` (2 preceding siblings ...)
2023-06-14 3:54 ` [libgpiod][PATCH 3/4] bindings: python: " Kent Gibson
@ 2023-06-14 3:54 ` Kent Gibson
2023-06-14 7:52 ` Erik Schilling
2023-06-14 13:03 ` [libgpiod][PATCH 0/4] " Bartosz Golaszewski
4 siblings, 1 reply; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 3:54 UTC (permalink / raw)
To: linux-gpio, brgl; +Cc: Kent Gibson
Add rust equivalents of the core examples.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
.../rust/libgpiod/examples/get_line_value.rs | 28 +++++++++++
.../libgpiod/examples/toggle_line_value.rs | 43 ++++++++++++++++
.../libgpiod/examples/watch_line_value.rs | 50 +++++++++++++++++++
3 files changed, 121 insertions(+)
create mode 100644 bindings/rust/libgpiod/examples/get_line_value.rs
create mode 100644 bindings/rust/libgpiod/examples/toggle_line_value.rs
create mode 100644 bindings/rust/libgpiod/examples/watch_line_value.rs
diff --git a/bindings/rust/libgpiod/examples/get_line_value.rs b/bindings/rust/libgpiod/examples/get_line_value.rs
new file mode 100644
index 0000000..732fb71
--- /dev/null
+++ b/bindings/rust/libgpiod/examples/get_line_value.rs
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
+// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+//
+// Minimal example of reading a single line.
+
+use libgpiod::line;
+
+fn main() -> libgpiod::Result<()> {
+ // example configuration - customize to suit your situation
+ let chip_path = "/dev/gpiochip0";
+ let line_offset = 5;
+
+ let mut lsettings = line::Settings::new()?;
+ lsettings.set_direction(line::Direction::Input)?;
+
+ let mut lconfig = line::Config::new()?;
+ lconfig.add_line_settings(&[line_offset], lsettings)?;
+
+ let mut rconfig = libgpiod::request::Config::new()?;
+ rconfig.set_consumer("get-line-value")?;
+
+ let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let request = chip.request_lines(Some(&rconfig), &lconfig)?;
+
+ let value = request.value(line_offset)?;
+ println!("{:?}", value);
+ Ok(())
+}
diff --git a/bindings/rust/libgpiod/examples/toggle_line_value.rs b/bindings/rust/libgpiod/examples/toggle_line_value.rs
new file mode 100644
index 0000000..cd7038e
--- /dev/null
+++ b/bindings/rust/libgpiod/examples/toggle_line_value.rs
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
+// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+//
+// Minimal example of toggling a single line.
+
+use libgpiod::line;
+use std::time::Duration;
+
+fn toggle_value(value: line::Value) -> line::Value {
+ match value {
+ line::Value::Active => line::Value::InActive,
+ line::Value::InActive => line::Value::Active,
+ }
+}
+
+fn main() -> libgpiod::Result<()> {
+ // example configuration - customize to suit your situation
+ let chip_path = "/dev/gpiochip0";
+ let line_offset = 5;
+
+ let mut value = line::Value::Active;
+
+ let mut settings = line::Settings::new()?;
+ settings
+ .set_direction(line::Direction::Output)?
+ .set_output_value(value)?;
+
+ let mut lconfig = line::Config::new()?;
+ lconfig.add_line_settings(&[line_offset], settings)?;
+
+ let mut rconfig = libgpiod::request::Config::new()?;
+ rconfig.set_consumer("toggle-line-value")?;
+
+ let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let mut req = chip.request_lines(Some(&rconfig), &lconfig)?;
+
+ loop {
+ println!("{:?}", value);
+ std::thread::sleep(Duration::from_secs(1));
+ value = toggle_value(value);
+ req.set_value(line_offset, value)?;
+ }
+}
diff --git a/bindings/rust/libgpiod/examples/watch_line_value.rs b/bindings/rust/libgpiod/examples/watch_line_value.rs
new file mode 100644
index 0000000..5a95b6a
--- /dev/null
+++ b/bindings/rust/libgpiod/examples/watch_line_value.rs
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
+// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
+//
+// Minimal example of watching for edges on a single line.
+
+use libgpiod::line;
+use std::time::Duration;
+
+fn main() -> libgpiod::Result<()> {
+ // example configuration - customize to suit your situation
+ let chip_path = "/dev/gpiochip0";
+ let line_offset = 5;
+
+ let mut lsettings = line::Settings::new()?;
+ // assume a button connecting the pin to ground,
+ // so pull it up and provide some debounce.
+ lsettings
+ .set_edge_detection(Some(line::Edge::Both))?
+ .set_bias(Some(line::Bias::PullUp))?
+ .set_debounce_period(Duration::from_millis(10));
+
+ let mut lconfig = line::Config::new()?;
+ lconfig.add_line_settings(&[line_offset], lsettings)?;
+
+ let mut rconfig = libgpiod::request::Config::new()?;
+ rconfig.set_consumer("watch-line-value")?;
+
+ let chip = libgpiod::chip::Chip::open(&chip_path)?;
+ let request = chip.request_lines(Some(&rconfig), &lconfig)?;
+
+ // a larger buffer is an optimisation for reading bursts of events from the
+ // kernel, but that is not necessary in this case, so 1 is fine.
+ let mut buffer = libgpiod::request::Buffer::new(1)?;
+ loop {
+ // blocks until at least one event is available
+ let events = request.read_edge_events(&mut buffer)?;
+ for event in events {
+ let event = event?;
+ println!(
+ "line: {}, type: {}, event #{}",
+ event.line_offset(),
+ match event.event_type()? {
+ line::EdgeKind::Rising => "Rising ",
+ line::EdgeKind::Falling => "Falling",
+ },
+ event.line_seqno()
+ );
+ }
+ }
+}
--
2.40.1
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [libgpiod][PATCH 4/4] bindings: rust: examples: add dedicated examples
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
0 siblings, 1 reply; 16+ messages in thread
From: Erik Schilling @ 2023-06-14 7:52 UTC (permalink / raw)
To: Kent Gibson, linux-gpio, brgl
On Wed Jun 14, 2023 at 5:54 AM CEST, Kent Gibson wrote:
> Add rust equivalents of the core examples.
>
> Signed-off-by: Kent Gibson <warthog618@gmail.com>
Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
Some nit-picks below, but those are a matter of taste and the change
looks ok either way.
> ---
> .../rust/libgpiod/examples/get_line_value.rs | 28 +++++++++++
> .../libgpiod/examples/toggle_line_value.rs | 43 ++++++++++++++++
> .../libgpiod/examples/watch_line_value.rs | 50 +++++++++++++++++++
> 3 files changed, 121 insertions(+)
> create mode 100644 bindings/rust/libgpiod/examples/get_line_value.rs
> create mode 100644 bindings/rust/libgpiod/examples/toggle_line_value.rs
> create mode 100644 bindings/rust/libgpiod/examples/watch_line_value.rs
>
> diff --git a/bindings/rust/libgpiod/examples/get_line_value.rs b/bindings/rust/libgpiod/examples/get_line_value.rs
> new file mode 100644
> index 0000000..732fb71
> --- /dev/null
> +++ b/bindings/rust/libgpiod/examples/get_line_value.rs
> @@ -0,0 +1,28 @@
> +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
> +// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
> +//
> +// Minimal example of reading a single line.
> +
> +use libgpiod::line;
I think one could also just import the other used modules. Or, since
this is an example anyway, just `use libgpiod::*`.
> +
> +fn main() -> libgpiod::Result<()> {
> + // example configuration - customize to suit your situation
> + let chip_path = "/dev/gpiochip0";
> + let line_offset = 5;
> +
> + let mut lsettings = line::Settings::new()?;
I think `line_settings` would still be an okish length (same below) :).
> + lsettings.set_direction(line::Direction::Input)?;
> +
> + let mut lconfig = line::Config::new()?;
> + lconfig.add_line_settings(&[line_offset], lsettings)?;
> +
> + let mut rconfig = libgpiod::request::Config::new()?;
> + rconfig.set_consumer("get-line-value")?;
> +
> + let chip = libgpiod::chip::Chip::open(&chip_path)?;
> + let request = chip.request_lines(Some(&rconfig), &lconfig)?;
> +
> + let value = request.value(line_offset)?;
> + println!("{:?}", value);
Could also be:
+ println!("{value:?}");
(same below)
> + Ok(())
> +}
> diff --git a/bindings/rust/libgpiod/examples/toggle_line_value.rs b/bindings/rust/libgpiod/examples/toggle_line_value.rs
> new file mode 100644
> index 0000000..cd7038e
> --- /dev/null
> +++ b/bindings/rust/libgpiod/examples/toggle_line_value.rs
> @@ -0,0 +1,43 @@
> +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
> +// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
> +//
> +// Minimal example of toggling a single line.
> +
> +use libgpiod::line;
> +use std::time::Duration;
> +
> +fn toggle_value(value: line::Value) -> line::Value {
> + match value {
> + line::Value::Active => line::Value::InActive,
> + line::Value::InActive => line::Value::Active,
> + }
> +}
> +
> +fn main() -> libgpiod::Result<()> {
> + // example configuration - customize to suit your situation
> + let chip_path = "/dev/gpiochip0";
> + let line_offset = 5;
> +
> + let mut value = line::Value::Active;
> +
> + let mut settings = line::Settings::new()?;
> + settings
> + .set_direction(line::Direction::Output)?
> + .set_output_value(value)?;
> +
> + let mut lconfig = line::Config::new()?;
> + lconfig.add_line_settings(&[line_offset], settings)?;
> +
> + let mut rconfig = libgpiod::request::Config::new()?;
> + rconfig.set_consumer("toggle-line-value")?;
> +
> + let chip = libgpiod::chip::Chip::open(&chip_path)?;
> + let mut req = chip.request_lines(Some(&rconfig), &lconfig)?;
> +
> + loop {
> + println!("{:?}", value);
> + std::thread::sleep(Duration::from_secs(1));
> + value = toggle_value(value);
> + req.set_value(line_offset, value)?;
> + }
> +}
> diff --git a/bindings/rust/libgpiod/examples/watch_line_value.rs b/bindings/rust/libgpiod/examples/watch_line_value.rs
> new file mode 100644
> index 0000000..5a95b6a
> --- /dev/null
> +++ b/bindings/rust/libgpiod/examples/watch_line_value.rs
> @@ -0,0 +1,50 @@
> +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
> +// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
> +//
> +// Minimal example of watching for edges on a single line.
> +
> +use libgpiod::line;
> +use std::time::Duration;
> +
> +fn main() -> libgpiod::Result<()> {
> + // example configuration - customize to suit your situation
> + let chip_path = "/dev/gpiochip0";
> + let line_offset = 5;
> +
> + let mut lsettings = line::Settings::new()?;
> + // assume a button connecting the pin to ground,
> + // so pull it up and provide some debounce.
> + lsettings
> + .set_edge_detection(Some(line::Edge::Both))?
> + .set_bias(Some(line::Bias::PullUp))?
> + .set_debounce_period(Duration::from_millis(10));
> +
> + let mut lconfig = line::Config::new()?;
> + lconfig.add_line_settings(&[line_offset], lsettings)?;
> +
> + let mut rconfig = libgpiod::request::Config::new()?;
> + rconfig.set_consumer("watch-line-value")?;
> +
> + let chip = libgpiod::chip::Chip::open(&chip_path)?;
> + let request = chip.request_lines(Some(&rconfig), &lconfig)?;
> +
> + // a larger buffer is an optimisation for reading bursts of events from the
> + // kernel, but that is not necessary in this case, so 1 is fine.
> + let mut buffer = libgpiod::request::Buffer::new(1)?;
> + loop {
> + // blocks until at least one event is available
> + let events = request.read_edge_events(&mut buffer)?;
> + for event in events {
> + let event = event?;
> + println!(
> + "line: {}, type: {}, event #{}",
> + event.line_offset(),
> + match event.event_type()? {
> + line::EdgeKind::Rising => "Rising ",
> + line::EdgeKind::Falling => "Falling",
> + },
> + event.line_seqno()
> + );
println!("{: <8}") could also be used to pad things (would allow
removing the trailing space).
> + }
> + }
> +}
> --
> 2.40.1
- Erik
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [libgpiod][PATCH 4/4] bindings: rust: examples: add dedicated examples
2023-06-14 7:52 ` Erik Schilling
@ 2023-06-14 8:18 ` Kent Gibson
2023-06-14 8:29 ` Erik Schilling
0 siblings, 1 reply; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 8:18 UTC (permalink / raw)
To: Erik Schilling; +Cc: linux-gpio, brgl
On Wed, Jun 14, 2023 at 09:52:20AM +0200, Erik Schilling wrote:
> On Wed Jun 14, 2023 at 5:54 AM CEST, Kent Gibson wrote:
> > Add rust equivalents of the core examples.
> >
> > Signed-off-by: Kent Gibson <warthog618@gmail.com>
>
> Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
>
> Some nit-picks below, but those are a matter of taste and the change
> looks ok either way.
>
> > +
> > +use libgpiod::line;
>
> I think one could also just import the other used modules. Or, since
> this is an example anyway, just `use libgpiod::*`.
>
I'm never keen on using `::*`, as subsequent changes could pull in symbols
that conflict with locals.
And as this is an example I wanted to be explicit as to where the symbols
originate, especially as there is some overlap, e.g. line::Config and
request::Config.
The general rule is, if it is only used once then use the full name.
But there are so many line attributes that using the slightly shortened
form made it more readable.
> > +
> > + let value = request.value(line_offset)?;
> > + println!("{:?}", value);
>
> Could also be:
> + println!("{value:?}");
> (same below)
>
Fair enough. I'm old school so I tend to prefer printf style.
> > + "line: {}, type: {}, event #{}",
> > + event.line_offset(),
> > + match event.event_type()? {
> > + line::EdgeKind::Rising => "Rising ",
> > + line::EdgeKind::Falling => "Falling",
> > + },
> > + event.line_seqno()
> > + );
>
> println!("{: <8}") could also be used to pad things (would allow
> removing the trailing space).
>
So add 4 chars to remove 1?
Ideally the padding would go after the comma, and then you start getting
into compound fields, so this was a case of KISS.
Cheers,
Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [libgpiod][PATCH 4/4] bindings: rust: examples: add dedicated examples
2023-06-14 8:18 ` Kent Gibson
@ 2023-06-14 8:29 ` Erik Schilling
0 siblings, 0 replies; 16+ messages in thread
From: Erik Schilling @ 2023-06-14 8:29 UTC (permalink / raw)
To: Kent Gibson; +Cc: linux-gpio, brgl
On Wed Jun 14, 2023 at 10:18 AM CEST, Kent Gibson wrote:
> On Wed, Jun 14, 2023 at 09:52:20AM +0200, Erik Schilling wrote:
> > On Wed Jun 14, 2023 at 5:54 AM CEST, Kent Gibson wrote:
> > > Add rust equivalents of the core examples.
> > >
> > > Signed-off-by: Kent Gibson <warthog618@gmail.com>
> >
> > Reviewed-by: Erik Schilling <erik.schilling@linaro.org>
> >
> > Some nit-picks below, but those are a matter of taste and the change
> > looks ok either way.
> >
> > > +
> > > +use libgpiod::line;
> >
> > I think one could also just import the other used modules. Or, since
> > this is an example anyway, just `use libgpiod::*`.
> >
>
> I'm never keen on using `::*`, as subsequent changes could pull in symbols
> that conflict with locals.
>
> And as this is an example I wanted to be explicit as to where the symbols
> originate, especially as there is some overlap, e.g. line::Config and
> request::Config.
> The general rule is, if it is only used once then use the full name.
> But there are so many line attributes that using the slightly shortened
> form made it more readable.
Yeah, I assumed that you followed this rule. Fine with me.
> > > + "line: {}, type: {}, event #{}",
> > > + event.line_offset(),
> > > + match event.event_type()? {
> > > + line::EdgeKind::Rising => "Rising ",
> > > + line::EdgeKind::Falling => "Falling",
> > > + },
> > > + event.line_seqno()
> > > + );
> >
> > println!("{: <8}") could also be used to pad things (would allow
> > removing the trailing space).
> >
>
> So add 4 chars to remove 1?
>
> Ideally the padding would go after the comma, and then you start getting
> into compound fields, so this was a case of KISS.
As I said, may be a matter of taste. I am fine with either way (thats
why I already provided the Review tag). The extra " " just jumped
to my eye since with rustfmt it is not aligned in a way that made it
immediately obvious.
- Erik
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [libgpiod][PATCH 0/4] dedicated examples
2023-06-14 3:54 [libgpiod][PATCH 0/4] dedicated examples Kent Gibson
` (3 preceding siblings ...)
2023-06-14 3:54 ` [libgpiod][PATCH 4/4] bindings: rust: " Kent Gibson
@ 2023-06-14 13:03 ` Bartosz Golaszewski
2023-06-14 13:21 ` Kent Gibson
4 siblings, 1 reply; 16+ messages in thread
From: Bartosz Golaszewski @ 2023-06-14 13:03 UTC (permalink / raw)
To: Kent Gibson; +Cc: linux-gpio
On Wed, Jun 14, 2023 at 5:54 AM Kent Gibson <warthog618@gmail.com> wrote:
>
> This series forms the basis of a proposal to rework the libgpiod
> examples.
>
> IMHO, the examples have a number of issues including:
> - no C examples other than the tools - which have become too complicated
> to be fit for that purpose.
> - bindings examples focus on simplifications of the tools rather than
> demonstrating typical use cases.
> - being more complex than necessary to demonstrate a particular use case.
> - naming that does not help new users identify the example that
> demonstrates a particular use case.
>
> To address these issues I propose adding a set of dedicated examples,
> each of which demonstrates one primary use case with as little
> additional fat as possible. Each example should be named after the use
> case it demonstrates. Each examples should be implemented for all
> supported languages, to provide examples for users considering
> language choice and for developers to compare the APIs and identify
> areas for improvement.
>
> The examples are explicitly intended to answer "how do I use libgpiod to
> do xxx" questions, with as few distractions as possible.
>
> The examples may demonstrate ancillary features, as long as that is
> relevant to the primary use case. e.g. the watch_line_value examples
> set the line bias and debounce.
>
> The examples should hard code input parameters, not pull them from the
> command line or environment, as that is distracting and frequently more
> complicated than the example itself.
>
> The examples should be standalone - they should not share code. Large
> chunks of shared code are an indicator that libgpiod probably needs to
> provide additional functionality to simplify use cases.
>
> The examples are not intended to be distributed in binary form, but
> could be incorporated into the documentation.
>
> The set of examples provided is a starter set that answer the following
> questions:
> - how do you get the value of a line?
> - how do you set the value of a line?
> - how do you watch for edge events on a line?
> - how do you watch for edge events asynchronously (e.g. using poll())?
>
> I didn't add the latter in Rust as that should be done using one of the
> many available async reactors and I didn't want to pick one or pull in
> any dependencies.
>
> I expect that this proposal may be contraversial, and that is one of the
> reasons I've kept the starter set small.
>
> Cheers,
> Kent.
>
> Kent Gibson (4):
> core: examples: add dedicated examples
> bindings: cxx: examples: add dedicated examples
> bindings: python: examples: add dedicated examples
> bindings: rust: examples: add dedicated examples
>
> Makefile.am | 6 +
> bindings/cxx/examples/.gitignore | 4 +
> bindings/cxx/examples/Makefile.am | 14 +-
> .../cxx/examples/async_watch_line_value.cpp | 78 ++++++++++
> bindings/cxx/examples/get_line_value.cpp | 29 ++++
> bindings/cxx/examples/toggle_line_value.cpp | 45 ++++++
> bindings/cxx/examples/watch_line_value.cpp | 62 ++++++++
> .../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 ++++++
> .../rust/libgpiod/examples/get_line_value.rs | 28 ++++
> .../libgpiod/examples/toggle_line_value.rs | 43 ++++++
> .../libgpiod/examples/watch_line_value.rs | 50 +++++++
> configure.ac | 1 +
> examples/.gitignore | 7 +
> examples/Makefile.am | 17 +++
> examples/async_watch_line_value.c | 136 ++++++++++++++++++
> examples/get_line_value.c | 97 +++++++++++++
> examples/toggle_line_value.c | 106 ++++++++++++++
> examples/watch_line_value.c | 127 ++++++++++++++++
> 21 files changed, 1011 insertions(+), 1 deletion(-)
> create mode 100644 bindings/cxx/examples/async_watch_line_value.cpp
> create mode 100644 bindings/cxx/examples/get_line_value.cpp
> create mode 100644 bindings/cxx/examples/toggle_line_value.cpp
> create mode 100644 bindings/cxx/examples/watch_line_value.cpp
> 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
> create mode 100644 bindings/rust/libgpiod/examples/get_line_value.rs
> create mode 100644 bindings/rust/libgpiod/examples/toggle_line_value.rs
> create mode 100644 bindings/rust/libgpiod/examples/watch_line_value.rs
> create mode 100644 examples/.gitignore
> create mode 100644 examples/Makefile.am
> create mode 100644 examples/async_watch_line_value.c
> create mode 100644 examples/get_line_value.c
> create mode 100644 examples/toggle_line_value.c
> create mode 100644 examples/watch_line_value.c
>
> --
> 2.40.1
>
Great idea! I applied patches 1-3 with some tweaks (coding style, C++
comments, statify functions, put local functions into anonymous
namespaces for C++, apply black to python code etc. etc.).
You can respin the rust patch separately.
Thanks
Bart
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [libgpiod][PATCH 0/4] dedicated examples
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
0 siblings, 1 reply; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 13:21 UTC (permalink / raw)
To: Bartosz Golaszewski; +Cc: linux-gpio
On Wed, Jun 14, 2023 at 03:03:51PM +0200, Bartosz Golaszewski wrote:
> >
>
> Great idea! I applied patches 1-3 with some tweaks (coding style, C++
> comments, statify functions, put local functions into anonymous
> namespaces for C++, apply black to python code etc. etc.).
>
Ok. Would've been nice to get review comments before having changes
pulled straight in, but whatever.
Requiring C style comments in C++ is just weird.
> You can respin the rust patch separately.
>
And what would you like respun there?
Cheers,
Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [libgpiod][PATCH 0/4] dedicated examples
2023-06-14 13:21 ` Kent Gibson
@ 2023-06-14 13:26 ` Bartosz Golaszewski
2023-06-14 13:57 ` Kent Gibson
0 siblings, 1 reply; 16+ messages in thread
From: Bartosz Golaszewski @ 2023-06-14 13:26 UTC (permalink / raw)
To: Kent Gibson; +Cc: linux-gpio
On Wed, Jun 14, 2023 at 3:21 PM Kent Gibson <warthog618@gmail.com> wrote:
>
> On Wed, Jun 14, 2023 at 03:03:51PM +0200, Bartosz Golaszewski wrote:
> > >
> >
> > Great idea! I applied patches 1-3 with some tweaks (coding style, C++
> > comments, statify functions, put local functions into anonymous
> > namespaces for C++, apply black to python code etc. etc.).
> >
>
> Ok. Would've been nice to get review comments before having changes
> pulled straight in, but whatever.
>
No functional changes. I wanted to avoid bikeshedding over coding style etc.
> Requiring C style comments in C++ is just weird.
>
> > You can respin the rust patch separately.
> >
>
> And what would you like respun there?
>
I saw comments from Erik and though you'd need a v2 but as it's mostly
a matter of taste, I'll apply it as is.
Bart
> Cheers,
> Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [libgpiod][PATCH 0/4] dedicated examples
2023-06-14 13:26 ` Bartosz Golaszewski
@ 2023-06-14 13:57 ` Kent Gibson
2023-06-14 15:11 ` Bartosz Golaszewski
0 siblings, 1 reply; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 13:57 UTC (permalink / raw)
To: Bartosz Golaszewski; +Cc: linux-gpio
On Wed, Jun 14, 2023 at 03:26:54PM +0200, Bartosz Golaszewski wrote:
> On Wed, Jun 14, 2023 at 3:21 PM Kent Gibson <warthog618@gmail.com> wrote:
> >
> > On Wed, Jun 14, 2023 at 03:03:51PM +0200, Bartosz Golaszewski wrote:
> > > >
> > >
> > > Great idea! I applied patches 1-3 with some tweaks (coding style, C++
> > > comments, statify functions, put local functions into anonymous
> > > namespaces for C++, apply black to python code etc. etc.).
> > >
Any opinions on where to go next?
Other use cases to add - e.g. multi-line versions?
Convert the tool examples to use case examples? Or do you still want
to distribute the binaries for those?
Any functionality to add to libgpiod?
Cheers,
Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [libgpiod][PATCH 0/4] dedicated examples
2023-06-14 13:57 ` Kent Gibson
@ 2023-06-14 15:11 ` Bartosz Golaszewski
2023-06-14 16:00 ` Kent Gibson
0 siblings, 1 reply; 16+ messages in thread
From: Bartosz Golaszewski @ 2023-06-14 15:11 UTC (permalink / raw)
To: Kent Gibson; +Cc: linux-gpio
On Wed, Jun 14, 2023 at 3:57 PM Kent Gibson <warthog618@gmail.com> wrote:
>
> On Wed, Jun 14, 2023 at 03:26:54PM +0200, Bartosz Golaszewski wrote:
> > On Wed, Jun 14, 2023 at 3:21 PM Kent Gibson <warthog618@gmail.com> wrote:
> > >
> > > On Wed, Jun 14, 2023 at 03:03:51PM +0200, Bartosz Golaszewski wrote:
> > > > >
> > > >
> > > > Great idea! I applied patches 1-3 with some tweaks (coding style, C++
> > > > comments, statify functions, put local functions into anonymous
> > > > namespaces for C++, apply black to python code etc. etc.).
> > > >
>
>
> Any opinions on where to go next?
> Other use cases to add - e.g. multi-line versions?
Sure, I don't see why not.
> Convert the tool examples to use case examples? Or do you still want
> to distribute the binaries for those?
No, the examples are built but not installed. And yes, if you have the
time and will to convert them to something more useful, please do!
> Any functionality to add to libgpiod?
>
I don't think so at the moment. Do you see anything obvious? I know,
we spoke about putting the line resolver into libgpiod but I'm not
sure we really want it. At least in the core library anyway. The GLib
layer on top of libgpiod is a place that would be a good target for
such a functionality IMO.
Other than that, I think libgpiod now has everything it needs to cover
all use-cases for the uAPI.
Bart
> Cheers,
> Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [libgpiod][PATCH 0/4] dedicated examples
2023-06-14 15:11 ` Bartosz Golaszewski
@ 2023-06-14 16:00 ` Kent Gibson
2023-06-15 15:16 ` Bartosz Golaszewski
0 siblings, 1 reply; 16+ messages in thread
From: Kent Gibson @ 2023-06-14 16:00 UTC (permalink / raw)
To: Bartosz Golaszewski; +Cc: linux-gpio
On Wed, Jun 14, 2023 at 05:11:32PM +0200, Bartosz Golaszewski wrote:
> On Wed, Jun 14, 2023 at 3:57 PM Kent Gibson <warthog618@gmail.com> wrote:
> >
> > Any functionality to add to libgpiod?
> >
>
> I don't think so at the moment. Do you see anything obvious? I know,
> we spoke about putting the line resolver into libgpiod but I'm not
> sure we really want it. At least in the core library anyway. The GLib
> layer on top of libgpiod is a place that would be a good target for
> such a functionality IMO.
>
Yeah, making the line resolver generally available is a can of worms.
Not prepared to take that one on at the moment.
I'm reasonably content to leave that to the user - as long as they can
readily iterate over the chips and lines themselves.
Maybe provide an iterator for all the lines in the system
available to the user?
> Other than that, I think libgpiod now has everything it needs to cover
> all use-cases for the uAPI.
>
The point isn't that coverage is missing, it is to find ways to make
common tasks simpler.
The ones that spring to mind so far are:
- C: requesting a single line as output
- C: requesting a single line as input
- providing a toggle function for line_value, as it is an enum which is
a bit awkward.
- the chip iterator in the python tools helpers.py
- streaming operators for the enums where they are not automatically
provided
The C ones are specifically for simple sysfs-like equivalence, as telling
users they need to replace a single write to a file with ~100 lines of C
is really hard to sell.
The config options would be as minimal as possible.
I was going to suggest the user could always reconfigure the line later
if they need extra features, but there is no function to return the
existing line config :-(.
Cheers,
Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [libgpiod][PATCH 0/4] dedicated examples
2023-06-14 16:00 ` Kent Gibson
@ 2023-06-15 15:16 ` Bartosz Golaszewski
2023-06-15 15:39 ` Kent Gibson
0 siblings, 1 reply; 16+ messages in thread
From: Bartosz Golaszewski @ 2023-06-15 15:16 UTC (permalink / raw)
To: Kent Gibson; +Cc: linux-gpio
On Wed, Jun 14, 2023 at 6:00 PM Kent Gibson <warthog618@gmail.com> wrote:
>
> On Wed, Jun 14, 2023 at 05:11:32PM +0200, Bartosz Golaszewski wrote:
> > On Wed, Jun 14, 2023 at 3:57 PM Kent Gibson <warthog618@gmail.com> wrote:
> > >
> > > Any functionality to add to libgpiod?
> > >
> >
> > I don't think so at the moment. Do you see anything obvious? I know,
> > we spoke about putting the line resolver into libgpiod but I'm not
> > sure we really want it. At least in the core library anyway. The GLib
> > layer on top of libgpiod is a place that would be a good target for
> > such a functionality IMO.
> >
>
> Yeah, making the line resolver generally available is a can of worms.
> Not prepared to take that one on at the moment.
> I'm reasonably content to leave that to the user - as long as they can
> readily iterate over the chips and lines themselves.
> Maybe provide an iterator for all the lines in the system
> available to the user?
>
> > Other than that, I think libgpiod now has everything it needs to cover
> > all use-cases for the uAPI.
> >
>
> The point isn't that coverage is missing, it is to find ways to make
> common tasks simpler.
>
> The ones that spring to mind so far are:
> - C: requesting a single line as output
> - C: requesting a single line as input
> - providing a toggle function for line_value, as it is an enum which is
> a bit awkward.
> - the chip iterator in the python tools helpers.py
> - streaming operators for the enums where they are not automatically
> provided
>
>
> The C ones are specifically for simple sysfs-like equivalence, as telling
> users they need to replace a single write to a file with ~100 lines of C
> is really hard to sell.
You don't really need 100 LOC for a trivial request in C (it's a bit
over-dramatic :) ) but my thinking is: whether it's 5 lines or 10 or
100 - it doesn't change the fact that it is a fundamental change from
sysfs in that you need to write the code, compile it, link it against
the right libraries etc. etc. It will be so much more work no matter
how much you simplify the API and that is already enough to scare away
a lot of folks used to just writing to a bunch of files.
This is why I'm proposing the DBus API as a way of replacing several
features of sysfs that are so beloved by users: central authority over
GPIOs, easy to use from shell scripts (just replace "echo 223 >
export; echo output > 223/direction" etc. with "gdbus call --system
--dest io.gpiod1 --object-path /io/gpiod1/gpiochip2 --method
io.gpiod1.Chip.RequestLines <args>" which is just a tiny bit more
verbose but achieves the same goal and exposes all uAPI v2 features)
and only requires including the dbus daemon in your system which would
be packaged by most distros shipping libgpiod eventually. DBus has the
advantage of being usable from any language you fancy and still being
relatively fast.
In other words, I'm thinking that packing a lot of "helper" features
into libgpiod will only lead to feature creep but not achieve the goal
of pulling people away from sysfs.
Bart
> The config options would be as minimal as possible.
> I was going to suggest the user could always reconfigure the line later
> if they need extra features, but there is no function to return the
> existing line config :-(.
>
> Cheers,
> Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [libgpiod][PATCH 0/4] dedicated examples
2023-06-15 15:16 ` Bartosz Golaszewski
@ 2023-06-15 15:39 ` Kent Gibson
0 siblings, 0 replies; 16+ messages in thread
From: Kent Gibson @ 2023-06-15 15:39 UTC (permalink / raw)
To: Bartosz Golaszewski; +Cc: linux-gpio
On Thu, Jun 15, 2023 at 05:16:04PM +0200, Bartosz Golaszewski wrote:
> On Wed, Jun 14, 2023 at 6:00 PM Kent Gibson <warthog618@gmail.com> wrote:
> >
> > The C ones are specifically for simple sysfs-like equivalence, as telling
> > users they need to replace a single write to a file with ~100 lines of C
> > is really hard to sell.
>
> You don't really need 100 LOC for a trivial request in C (it's a bit
> over-dramatic :) ) but my thinking is: whether it's 5 lines or 10 or
> 100 - it doesn't change the fact that it is a fundamental change from
> sysfs in that you need to write the code, compile it, link it against
> the right libraries etc. etc. It will be so much more work no matter
> how much you simplify the API and that is already enough to scare away
> a lot of folks used to just writing to a bunch of files.
>
Sure - those using scripts would probably go for Python anyway.
But no one in their right mind would elect to use the C API given the
alternatives, which makes it basically pointless.
> This is why I'm proposing the DBus API as a way of replacing several
> features of sysfs that are so beloved by users: central authority over
> GPIOs, easy to use from shell scripts (just replace "echo 223 >
> export; echo output > 223/direction" etc. with "gdbus call --system
> --dest io.gpiod1 --object-path /io/gpiod1/gpiochip2 --method
> io.gpiod1.Chip.RequestLines <args>" which is just a tiny bit more
> verbose but achieves the same goal and exposes all uAPI v2 features)
> and only requires including the dbus daemon in your system which would
> be packaged by most distros shipping libgpiod eventually. DBus has the
> advantage of being usable from any language you fancy and still being
> relatively fast.
>
> In other words, I'm thinking that packing a lot of "helper" features
> into libgpiod will only lead to feature creep but not achieve the goal
> of pulling people away from sysfs.
>
And you think I'm exaggerating.
Removing a few pain points is not "packing a lot of "helper" features".
Cheers,
Kent.
^ permalink raw reply [flat|nested] 16+ messages in thread