2021-06-10 23:18:17 -05:00
|
|
|
struct timer {
|
2021-06-11 00:06:29 -05:00
|
|
|
int s; /* seconds */
|
|
|
|
int d; /* data storage */
|
|
|
|
void (*u)(struct timer *t); /* update function */
|
|
|
|
int (*c)(struct timer *t); /* stop check function */
|
|
|
|
int (*p)(struct timer *t); /* pause check function */
|
2021-06-10 23:18:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
void timerdec(struct timer *t) {
|
2021-06-11 00:26:49 -05:00
|
|
|
if (t->s != 0) t->s--;
|
2021-06-10 23:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void timerinc(struct timer *t) {
|
2021-06-11 00:26:49 -05:00
|
|
|
t->s++;
|
2021-06-10 23:18:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void timerupdate(struct timer *t) {
|
|
|
|
if(t->u != NULL) t->u(t);
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:06:29 -05:00
|
|
|
int timerstate(int (*f)(struct timer *t), struct timer *t) {
|
|
|
|
if(f != NULL) {
|
|
|
|
if(f(t)) return 1;
|
2021-06-10 23:18:17 -05:00
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:06:29 -05:00
|
|
|
int timerstop(struct timer *t) {
|
|
|
|
return timerstate(t->c, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
int timerpause(struct timer *t) {
|
|
|
|
return timerstate(t->p, t);
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:18:17 -05:00
|
|
|
int timerzero(struct timer *t) {
|
2021-06-11 00:26:49 -05:00
|
|
|
if(t->s == 0) return 1;
|
2021-06-10 23:18:17 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:58:16 -05:00
|
|
|
int seconds(int t) {
|
|
|
|
return t % 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
int minutes(int t) {
|
|
|
|
return (t / 60) % 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
int minute(int t) {
|
|
|
|
return t / 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hours(int t) {
|
|
|
|
return (t / 60) / 60;
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:18:17 -05:00
|
|
|
struct timer *timerinit(void) {
|
2021-06-11 00:06:29 -05:00
|
|
|
struct timer *t = malloc(sizeof *t);
|
2021-06-10 23:18:17 -05:00
|
|
|
t->s = 0;
|
2021-06-11 00:06:29 -05:00
|
|
|
t->d = 0;
|
2021-06-10 23:18:17 -05:00
|
|
|
t->u = NULL;
|
|
|
|
t->c = NULL;
|
2021-06-11 00:06:29 -05:00
|
|
|
t->p = NULL;
|
2021-06-10 23:18:17 -05:00
|
|
|
return t;
|
|
|
|
}
|