From 85f3d82f15bfd491e120c185180410ef44a0373f Mon Sep 17 00:00:00 2001 From: randomuser Date: Thu, 10 Jun 2021 23:18:17 -0500 Subject: [PATCH] add generalized timer functions to timerlib.h --- progs/timer.c | 49 +----------------------------------------------- progs/timerlib.h | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 progs/timerlib.h diff --git a/progs/timer.c b/progs/timer.c index 9a6297b..b97f4de 100644 --- a/progs/timer.c +++ b/progs/timer.c @@ -4,12 +4,7 @@ #include #include -struct timer { - int m; - int s; - void (*u)(struct timer *t); - int (*c)(struct timer *t); -}; +#include "timerlib.h" struct settings { int e:1; /* use escape (v assumed) */ @@ -29,39 +24,6 @@ struct settings { .s = 0 }; -void timerdec(struct timer *t) { - if(t->s > 0) t->s--; - else if(t->s == 0) { - t->s = 59; - t->m--; - } -} - -void timerinc(struct timer *t) { - if(t->s < 59) t->s++; - else if(t->s == 59) { - t->s = 0; - t->m++; - } -} - -void timerupdate(struct timer *t) { - if(t->u != NULL) t->u(t); -} - -int timerstate(struct timer *t) { - if(t->c != NULL) { - if(t->c(t)) return 1; - else return 0; - } - return 0; -} - -int timerzero(struct timer *t) { - if(t->m == 0 && t->s == 0) return 1; - return 0; -} - int timerissettings(struct timer *t) { if(t->m == 0 && t->s == 0) return 0; if(t->m == settings.m && @@ -77,15 +39,6 @@ char *timerdisp(struct timer *t) { return str; } -struct timer *timerinit(void) { - struct timer *t = malloc(sizeof t); - t->m = 0; - t->s = 0; - t->u = NULL; - t->c = NULL; - return t; -} - void timerloop() { struct timer *t = timerinit(); if(settings.d) { diff --git a/progs/timerlib.h b/progs/timerlib.h new file mode 100644 index 0000000..df5aeb3 --- /dev/null +++ b/progs/timerlib.h @@ -0,0 +1,48 @@ +struct timer { + int m; + int s; + void (*u)(struct timer *t); + int (*c)(struct timer *t); +}; + +void timerdec(struct timer *t) { + if(t->s > 0) t->s--; + else if(t->s == 0) { + t->s = 59; + t->m--; + } +} + +void timerinc(struct timer *t) { + if(t->s < 59) t->s++; + else if(t->s == 59) { + t->s = 0; + t->m++; + } +} + +void timerupdate(struct timer *t) { + if(t->u != NULL) t->u(t); +} + +int timerstate(struct timer *t) { + if(t->c != NULL) { + if(t->c(t)) return 1; + else return 0; + } + return 0; +} + +int timerzero(struct timer *t) { + if(t->m == 0 && t->s == 0) return 1; + return 0; +} + +struct timer *timerinit(void) { + struct timer *t = malloc(sizeof t); + t->m = 0; + t->s = 0; + t->u = NULL; + t->c = NULL; + return t; +}