From: Ben Harris Date: Sat, 24 Nov 2018 11:55:35 +0000 (+0000) Subject: Crude sysfs-compatible output mode. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=8ac5c890317756f3329fcb5086fded4d5340a622;p=clunk.git Crude sysfs-compatible output mode. For pre-libgpiod systems. --- diff --git a/clunk.c b/clunk.c index 6199b54..38a1f39 100644 --- 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)