core: examples: consistency cleanups
authorKent Gibson <warthog618@gmail.com>
Fri, 23 Jun 2023 04:38:54 +0000 (12:38 +0800)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Fri, 23 Jun 2023 13:55:44 +0000 (15:55 +0200)
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>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
examples/Makefile.am
examples/async_watch_line_value.c
examples/get_line_value.c
examples/toggle_line_value.c
examples/watch_line_value.c

index 4ad124be8269089abe69f47c9aca7100f66b2db3..55dfe39a93e148438bafdba94df0528be336fcbe 100644 (file)
@@ -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
index 3292dda4096689f01ecbef0efd52a71446b16d8e..f35fb1a9efc876f10fc69a6b08f5ea7edfef9a7b 100644 (file)
@@ -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));
index 08e263aef6754707d55666c5291fd66eebced618..1de99010c94357274b64479605f838eedaa9432f 100644 (file)
@@ -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;
 }
index 63d7fb9be82a318193957da7ed42d750406b47d5..6e522d6ceeff5322a2a785501b790d5b3f06be7d 100644 (file)
@@ -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);
index d962f20145cdfd50f7a100c5e06ef98bf2cc9eb2..879b09bfeb71254c03d4b758b11cd30dc92660f6 100644 (file)
@@ -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));