From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod][PATCH 1/8] core: examples: consistency cleanups
Date: Fri, 23 Jun 2023 12:38:54 +0800 [thread overview]
Message-ID: <20230623043901.16764-2-warthog618@gmail.com> (raw)
In-Reply-To: <20230623043901.16764-1-warthog618@gmail.com>
A collection of minor cleanups to make the examples more consistent and
ease the addition of more examples:
- reformat Makefile.am to simplify adding more examples
- add line offset to value output
- remove commas from edge event output
- replace while(1) with for (;;)
- fix a typo in Makefile.am
- fix an error handling goto in toggle_line_value.c
Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
examples/Makefile.am | 8 ++++++--
examples/async_watch_line_value.c | 6 +++---
examples/get_line_value.c | 11 +++++++----
examples/toggle_line_value.c | 17 ++++++++++-------
examples/watch_line_value.c | 6 +++---
5 files changed, 29 insertions(+), 19 deletions(-)
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 4ad124b..55dfe39 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -6,12 +6,16 @@ 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
+noinst_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
+toggle_line_value_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
index 3292dda..f35fb1a 100644
--- a/examples/async_watch_line_value.c
+++ b/examples/async_watch_line_value.c
@@ -74,7 +74,7 @@ static const char *edge_event_type_str(struct gpiod_edge_event *event)
{
switch (gpiod_edge_event_get_event_type(event)) {
case GPIOD_EDGE_EVENT_RISING_EDGE:
- return "Rising ";
+ return "Rising";
case GPIOD_EDGE_EVENT_FALLING_EDGE:
return "Falling";
default:
@@ -117,7 +117,7 @@ int main(void)
pollfd.fd = gpiod_line_request_get_fd(request);
pollfd.events = POLLIN;
- while (1) {
+ for (;;) {
ret = poll(&pollfd, 1, -1);
if (ret == -1) {
fprintf(stderr, "error waiting for edge events: %s\n",
@@ -134,7 +134,7 @@ int main(void)
for (i = 0; i < ret; i++) {
event = gpiod_edge_event_buffer_get_event(event_buffer,
i);
- printf("offset: %d, type: %s, event #%ld\n",
+ printf("offset: %d type: %-7s 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
index 08e263a..1de9901 100644
--- a/examples/get_line_value.c
+++ b/examples/get_line_value.c
@@ -64,12 +64,12 @@ close_chip:
return request;
}
-static int print_value(enum gpiod_line_value value)
+static int print_value(unsigned int offset, enum gpiod_line_value value)
{
if (value == GPIOD_LINE_VALUE_ACTIVE)
- printf("Active\n");
+ printf("%d=Active\n", offset);
else if (value == GPIOD_LINE_VALUE_INACTIVE) {
- printf("Inactive\n");
+ printf("%d=Inactive\n", offset);
} else {
fprintf(stderr, "error reading value: %s\n",
strerror(errno));
@@ -97,7 +97,10 @@ int main(void)
}
value = gpiod_line_request_get_value(request, line_offset);
- ret = print_value(value);
+ ret = print_value(line_offset, value);
+
+ /* not strictly required here, but if the app wasn't exiting... */
+ gpiod_line_request_release(request);
return ret;
}
diff --git a/examples/toggle_line_value.c b/examples/toggle_line_value.c
index 63d7fb9..6e522d6 100644
--- a/examples/toggle_line_value.c
+++ b/examples/toggle_line_value.c
@@ -40,7 +40,7 @@ request_output_line(const char *chip_path, unsigned int offset,
ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
settings);
if (ret)
- goto free_settings;
+ goto free_line_config;
if (consumer) {
req_cfg = gpiod_request_config_new();
@@ -72,12 +72,15 @@ static enum gpiod_line_value toggle_line_value(enum gpiod_line_value value)
GPIOD_LINE_VALUE_ACTIVE;
}
-static void print_value(enum gpiod_line_value value)
+static const char * value_str(enum gpiod_line_value value)
{
if (value == GPIOD_LINE_VALUE_ACTIVE)
- printf("Active\n");
- else
- printf("Inactive\n");
+ return "Active";
+ else if (value == GPIOD_LINE_VALUE_INACTIVE) {
+ return "Inactive";
+ } else {
+ return "Unknown";
+ }
}
int main(void)
@@ -97,8 +100,8 @@ int main(void)
return EXIT_FAILURE;
}
- while (1) {
- print_value(value);
+ for (;;) {
+ printf("%d=%s\n", line_offset, value_str(value));
sleep(1);
value = toggle_line_value(value);
gpiod_line_request_set_value(request, line_offset, value);
diff --git a/examples/watch_line_value.c b/examples/watch_line_value.c
index d962f20..879b09b 100644
--- a/examples/watch_line_value.c
+++ b/examples/watch_line_value.c
@@ -73,7 +73,7 @@ static const char *edge_event_type_str(struct gpiod_edge_event *event)
{
switch (gpiod_edge_event_get_event_type(event)) {
case GPIOD_EDGE_EVENT_RISING_EDGE:
- return "Rising ";
+ return "Rising";
case GPIOD_EDGE_EVENT_FALLING_EDGE:
return "Falling";
default:
@@ -112,7 +112,7 @@ int main(void)
return EXIT_FAILURE;
}
- while (1) {
+ for (;;) {
/* Blocks until at least one event is available. */
ret = gpiod_line_request_read_edge_events(request, event_buffer,
event_buf_size);
@@ -124,7 +124,7 @@ int main(void)
for (i = 0; i < ret; i++) {
event = gpiod_edge_event_buffer_get_event(event_buffer,
i);
- printf("offset: %d, type: %s, event #%ld\n",
+ printf("offset: %d type: %-7s event #%ld\n",
gpiod_edge_event_get_line_offset(event),
edge_event_type_str(event),
gpiod_edge_event_get_line_seqno(event));
--
2.41.0
next prev parent reply other threads:[~2023-06-23 4:39 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 ` Kent Gibson [this message]
2023-06-23 4:38 ` [libgpiod][PATCH 2/8] core: examples: add more " 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 ` [libgpiod][PATCH 5/8] bindings: python: examples: consistency cleanup Kent Gibson
2023-06-23 4:38 ` [libgpiod][PATCH 6/8] bindings: python: examples: replace tools examples with use case examples 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-2-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).