chiark / gitweb /
Minimal beginnings of an impulse clock driver.
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 3 Nov 2018 11:44:05 +0000 (11:44 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 3 Nov 2018 11:44:05 +0000 (11:44 +0000)
Currently says "tick!" every 30 seconds.

README [new file with mode: 0644]
clunk.c [new file with mode: 0644]

diff --git a/README b/README
new file mode 100644 (file)
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 (file)
index 0000000..bba7013
--- /dev/null
+++ b/clunk.c
@@ -0,0 +1,42 @@
+#include <err.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <time.h>
+
+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;
+}