From: Ben Harris Date: Sat, 3 Nov 2018 11:44:05 +0000 (+0000) Subject: Minimal beginnings of an impulse clock driver. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=e0641c13b7d36ddab823c1e699f63cac645af8a2;p=clunk.git Minimal beginnings of an impulse clock driver. Currently says "tick!" every 30 seconds. --- e0641c13b7d36ddab823c1e699f63cac645af8a2 diff --git a/README b/README new file mode 100644 index 0000000..5849875 --- /dev/null +++ b/README @@ -0,0 +1,6 @@ +Clunk might one day be a program for driving an impulse electric clock +from a GPIO line. + +The general plan is to generate a pulse every 30 s (at 0 s and 30 s +past each minute), with extra magic to adjust the clock at startup and +at the start and end of summer time. diff --git a/clunk.c b/clunk.c new file mode 100644 index 0000000..bba7013 --- /dev/null +++ b/clunk.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +static void +pulse() +{ + /* Dummy implementation */ + printf("tick!\n"); +} + +static void +run() +{ + struct timespec ts; + struct tm tm; + + while (true) { + if (clock_gettime(CLOCK_REALTIME, &ts) != 0) + err(1, "clock_gettime"); + if (localtime_r(&ts.tv_sec, &tm) == NULL) + err(1, "localtime_r"); + /* Round down to the last 30 s. */ + ts.tv_nsec = 0; + ts.tv_sec -= tm.tm_sec % 30; + /* Choose when next tick will be. */ + ts.tv_sec += 30; + if (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL) + != 0) + err(1, "clock_nanosleep"); + pulse(); + } +} + +int +main(int argc, char **argv) +{ + + run(); + return 0; +}