From: Ben Harris Date: Fri, 23 Nov 2018 23:26:59 +0000 (+0000) Subject: Enforce a minimum "low" time for the output. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=ca37924239304e9d1c86347f6a717c6beb245da2;p=clunk.git Enforce a minimum "low" time for the output. By sleeping for 250 ms after each pulse, we ensure that even if CLOCK_REALTIME is suddenly put forward by 29.7 seconds we won't start a new pulse before the dial has recovered from the last one. --- diff --git a/clunk.c b/clunk.c index 7f10dc9..cb31782 100644 --- a/clunk.c +++ b/clunk.c @@ -17,6 +17,11 @@ */ static long const pulsewidth = 250000000; /* nanoseconds */ +/* + * Minimum gap between pulses. + */ +static long const gapwidth = 250000000; /* nanoseconds */ + /* * Maximum error to correct by advancing the clock rather than * stopping it, in seconds. The point where it's faster to stop the @@ -39,6 +44,9 @@ static sigset_t signals_to_block, timing_signals; static timer_t main_timer; +#define TS_SEC(s) ((struct timespec){.tv_sec = s, .tv_nsec = 0}) +#define TS_NSEC(s) ((struct timespec){.tv_sec = 0, .tv_nsec = s}) + static void dummy_out(bool state) { @@ -121,17 +129,21 @@ record_tick_finished(void) static void pulse() { - struct timespec const ts = { .tv_sec = 0, .tv_nsec = pulsewidth }; sigset_t saved_mask; if (sigprocmask(SIG_BLOCK, &signals_to_block, &saved_mask) != 0) err(1, "sigprocmask(block)"); (*outfn)(true); record_tick(); - errno = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); + errno = clock_nanosleep(CLOCK_MONOTONIC, 0, + &TS_NSEC(pulsewidth), NULL); if (errno != 0) err(1, "clock_nanosleep"); (*outfn)(false); + errno = clock_nanosleep(CLOCK_MONOTONIC, 0, + &TS_NSEC(gapwidth), NULL); + if (errno != 0) + err(1, "clock_nanosleep"); record_tick_finished(); if (sigprocmask(SIG_SETMASK, &saved_mask, NULL) != 0) err(1, "sigprocmask(restore)");