add generalized timer functions to timerlib.h

This commit is contained in:
randomuser 2021-06-10 23:18:17 -05:00
parent 6d6eec17c7
commit 85f3d82f15
2 changed files with 49 additions and 48 deletions

View File

@ -4,12 +4,7 @@
#include <poll.h> #include <poll.h>
#include <string.h> #include <string.h>
struct timer { #include "timerlib.h"
int m;
int s;
void (*u)(struct timer *t);
int (*c)(struct timer *t);
};
struct settings { struct settings {
int e:1; /* use escape (v assumed) */ int e:1; /* use escape (v assumed) */
@ -29,39 +24,6 @@ struct settings {
.s = 0 .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) { int timerissettings(struct timer *t) {
if(t->m == 0 && t->s == 0) return 0; if(t->m == 0 && t->s == 0) return 0;
if(t->m == settings.m && if(t->m == settings.m &&
@ -77,15 +39,6 @@ char *timerdisp(struct timer *t) {
return str; 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() { void timerloop() {
struct timer *t = timerinit(); struct timer *t = timerinit();
if(settings.d) { if(settings.d) {

48
progs/timerlib.h Normal file
View File

@ -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;
}