change timer to deal only in seconds

This commit is contained in:
randomuser 2021-06-11 00:26:49 -05:00
parent 3ad3002985
commit dcc2e07295
2 changed files with 11 additions and 20 deletions

View File

@ -25,17 +25,16 @@ struct settings {
}; };
int timerissettings(struct timer *t) { int timerissettings(struct timer *t) {
if(t->m == 0 && t->s == 0) return 0; if(t->s == 0) return 0;
if(t->m == settings.m && if(settings.m * 60 + settings.s == t->s) return 1;
t->s == settings.s) return 1;
return 0; return 0;
} }
char *timerdisp(struct timer *t) { char *timerdisp(struct timer *t) {
char *str = malloc(20); char *str = malloc(20);
if(settings.f) snprintf(str, 20, "%02i:%02i:%02i", if(settings.f) snprintf(str, 20, "%02i:%02i:%02i",
(t->m / 60), (t->m % 60), t->s); ((t->s / 60) / 60), (t->s / 60) % 60, t->s % 60);
else snprintf(str, 20, "%02i:%02i", t->m, t->s); else snprintf(str, 20, "%02i:%02i", t->s / 60, t->s % 60);
return str; return str;
} }
@ -43,8 +42,7 @@ void timerloop() {
struct timer *t = timerinit(); struct timer *t = timerinit();
if(settings.d) { if(settings.d) {
t->u = timerdec; t->u = timerdec;
t->m = settings.m; t->s = settings.s + (settings.m * 60);
t->s = settings.s;
t->c = timerzero; t->c = timerzero;
} else { } else {
t->u = timerinc; t->u = timerinc;
@ -72,10 +70,13 @@ void timerloop() {
} }
else if(settings.v) printf("%s\n", c); else if(settings.v) printf("%s\n", c);
if(timerstop(t)) break; if(timerstop(t)) break;
free(c);
timerupdate(t); timerupdate(t);
sleep(1); sleep(1);
} }
if(settings.b) putchar('\a'); if(settings.b) putchar('\a');
free(t);
free(c);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {

View File

@ -1,5 +1,4 @@
struct timer { struct timer {
int m; /* minutes */
int s; /* seconds */ int s; /* seconds */
int d; /* data storage */ int d; /* data storage */
void (*u)(struct timer *t); /* update function */ void (*u)(struct timer *t); /* update function */
@ -8,19 +7,11 @@ struct timer {
}; };
void timerdec(struct timer *t) { void timerdec(struct timer *t) {
if(t->s > 0) t->s--; if (t->s != 0) t->s--;
else if(t->s == 0) {
t->s = 59;
t->m--;
}
} }
void timerinc(struct timer *t) { void timerinc(struct timer *t) {
if(t->s < 59) t->s++; t->s++;
else if(t->s == 59) {
t->s = 0;
t->m++;
}
} }
void timerupdate(struct timer *t) { void timerupdate(struct timer *t) {
@ -44,13 +35,12 @@ int timerpause(struct timer *t) {
} }
int timerzero(struct timer *t) { int timerzero(struct timer *t) {
if(t->m == 0 && t->s == 0) return 1; if(t->s == 0) return 1;
return 0; return 0;
} }
struct timer *timerinit(void) { struct timer *timerinit(void) {
struct timer *t = malloc(sizeof *t); struct timer *t = malloc(sizeof *t);
t->m = 0;
t->s = 0; t->s = 0;
t->d = 0; t->d = 0;
t->u = NULL; t->u = NULL;