chiark / gitweb /
Add rough support for select libgpiod lines by number.
authorBen Harris <bjh21@bjh21.me.uk>
Mon, 26 Nov 2018 00:32:47 +0000 (00:32 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 8 Dec 2018 22:30:09 +0000 (22:30 +0000)
clunk.c

diff --git a/clunk.c b/clunk.c
index 38a1f39eceb9ef3b74f2cccde5e9b86ba5dcc7bc..0031e09b12a4ddaff0e8b3252eb06dae038aa1f2 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -208,14 +208,31 @@ init_statefile(char const *statefilename)
 
 #ifdef WITH_LIBGPIOD
 static void
-init_libgpiod(char const *gpio_name)
+init_libgpiod(char const *chip_name, char const *line_name, int line_num)
 {
-
-       libgpiod_line = gpiod_line_find(gpio_name);
-       if (libgpiod_line == NULL)
-               err(1, "GPIO line '%s'", gpio_name);
+       struct gpiod_chip * chip;
+
+       if (chip_name != NULL) {
+               chip = gpiod_chip_open_lookup(chip_name);
+               if (chip == NULL)
+                       err(1, "GPIO chip '%s'", chip_name);
+               if (line_name != NULL) {
+                       libgpiod_line = gpiod_chip_find_line(chip, line_name);
+                       if (libgpiod_line == NULL)
+                               err(1, "GPIO line '%s'", line_name);
+                       
+               } else {
+                       libgpiod_line = gpiod_chip_get_line(chip, line_num);
+                       if (libgpiod_line == NULL)
+                               err(1, "GPIO line %d", line_num);
+               }
+       } else {
+               libgpiod_line = gpiod_line_find(line_name);
+               if (libgpiod_line == NULL)
+                       err(1, "GPIO line '%s'", line_name);
+       }
        if (gpiod_line_request_output(libgpiod_line, "clunk", false) == -1)
-               err(1, "requesting '%s'", gpio_name);
+               err(1, "requesting GPIO line");
        outfn = &libgpiod_out;
 }
 #endif /* WITH_LIBGPIOD */
@@ -239,6 +256,10 @@ init(int argc, char **argv)
                .sigev_signo = SIGALRM
        };
        char *statefile = NULL, *statestr = NULL;
+#ifdef WITH_LIBGPIOD
+       char *gpiod_chip = NULL, *gpiod_line_name = NULL;
+       int gpiod_line_num = -1;
+#endif
        int opt;
 
        tzset();
@@ -257,7 +278,7 @@ init(int argc, char **argv)
 
        while ((opt = getopt(argc, argv, "f:s:Y:"
 #ifdef WITH_LIBGPIOD
-                            "G:"
+                            "G:C:L:"
 #endif /* WITH_LIBGPIOD */
                        )) != -1) {
                switch (opt) {
@@ -269,7 +290,13 @@ init(int argc, char **argv)
                        break;
 #ifdef WITH_LIBGPIOD
                case 'G':
-                       init_libgpiod(optarg);
+                       gpiod_line_name = optarg;
+                       break;
+               case 'C':
+                       gpiod_chip = optarg;
+                       break;
+               case 'L':
+                       gpiod_line_num = atoi(optarg);
                        break;
 #endif /* WITH_LIBGPIOD */
                case 'Y':
@@ -289,6 +316,10 @@ init(int argc, char **argv)
                        err(1, "localtime_r");
                displayed.tm_sec = (displayed.tm_sec >= 30) ? 30 : 0;
        }
+#ifdef WITH_LIBGPIOD
+       if (gpiod_line_name != NULL || gpiod_chip != NULL)
+               init_libgpiod(gpiod_chip, gpiod_line_name, gpiod_line_num);
+#endif
 }
 
 enum need_adjust { STOP, TICK, ADVANCE };