chiark / gitweb /
Crude sysfs-compatible output mode.
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 24 Nov 2018 11:55:35 +0000 (11:55 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 8 Dec 2018 22:30:09 +0000 (22:30 +0000)
For pre-libgpiod systems.

clunk.c

diff --git a/clunk.c b/clunk.c
index 6199b549878f2f0d5566a1d58ec830a8b0d41a0c..38a1f39eceb9ef3b74f2cccde5e9b86ba5dcc7bc 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -78,6 +78,19 @@ libgpiod_out(bool state)
 }
 #endif /* WITH_LIBGPIOD */
 
+static int sysfsfd;
+
+static void sysfs_out(bool state)
+{
+       ssize_t ret;
+
+       ret = pwrite(sysfsfd, state ? "1\n" : "0\n", 2, 0);
+       if (ret == -1)
+               err(1, "write to sysfs");
+       if (ret != 2)
+               errx(1, "short write to sysfs");
+}
+
 static void (*outfn)(bool) = &dummy_out;
 
 static void
@@ -207,6 +220,16 @@ init_libgpiod(char const *gpio_name)
 }
 #endif /* WITH_LIBGPIOD */
 
+static void
+init_sysfs(char const *path)
+{
+
+       sysfsfd = open(path, O_WRONLY);
+       if (sysfsfd == -1)
+               err(1, "%s", path);
+       outfn = &sysfs_out;
+}
+
 static void
 init(int argc, char **argv)
 {
@@ -232,7 +255,7 @@ init(int argc, char **argv)
        if (timer_create(CLOCK_REALTIME, &sev, &main_timer) != 0)
                err(1, "timer_create");
 
-       while ((opt = getopt(argc, argv, "f:s:"
+       while ((opt = getopt(argc, argv, "f:s:Y:"
 #ifdef WITH_LIBGPIOD
                             "G:"
 #endif /* WITH_LIBGPIOD */
@@ -249,6 +272,9 @@ init(int argc, char **argv)
                        init_libgpiod(optarg);
                        break;
 #endif /* WITH_LIBGPIOD */
+               case 'Y':
+                       init_sysfs(optarg);
+                       break;
                }
        }
        if (statefile != NULL)