diff mbox series

[libgpiod,1/8] core: examples: consistency cleanups

Message ID 20230623043901.16764-2-warthog618@gmail.com
State New
Headers show
Series replace tool examples with use case examples | expand

Commit Message

Kent Gibson June 23, 2023, 4:38 a.m. UTC
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 mbox series

Patch

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));