diff options
author | Aaron Ball <nullspoon@iohq.net> | 2016-01-28 22:40:10 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2016-01-28 22:40:10 -0700 |
commit | c16519973a0f26048716362a946ce91d5d450ed1 (patch) | |
tree | d3f85c634cd7e26e4c2d4fac505c9407045f11d9 | |
download | ctimer-c16519973a0f26048716362a946ce91d5d450ed1.tar.gz ctimer-c16519973a0f26048716362a946ce91d5d450ed1.tar.xz |
Initial commit
Currently counts up to 59 minutes and 59 seconds before resetting.
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | src/ctime.c | 21 | ||||
-rw-r--r-- | src/ctime.h | 19 | ||||
-rw-r--r-- | src/main.c | 28 |
4 files changed, 77 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cd4ee94 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +out=ctimer +warn = -Wall -Wpedantic + +all: + cc $(dbg) $(warn) -c src/ctime.c -o obj/ctime.o + cc $(dbg) $(warn) src/main.c obj/* -o $(out) + +debug: + make dbg=-g diff --git a/src/ctime.c b/src/ctime.c new file mode 100644 index 0000000..60cafb7 --- /dev/null +++ b/src/ctime.c @@ -0,0 +1,21 @@ +#include "ctime.h" + +void ctime_t_new(ctime_t* t) { + t->sec = 0; + t->min = 0; + t->hour = 0; + t->day = 0; + t->month = 0; + t->year = 0; +} + +int ctime_add_sec(ctime_t* t, int duration) { + t->sec += duration; + if(t->sec > 59) { + // Increment minute + t->min++; + // TODO: This doesn't work when duration > 1. Need better logic here. + t->sec = 0; + } + return 0; +} diff --git a/src/ctime.h b/src/ctime.h new file mode 100644 index 0000000..26c28db --- /dev/null +++ b/src/ctime.h @@ -0,0 +1,19 @@ +#ifndef ctime_time +#define ctime_time + +#include <stdlib.h> + +typedef struct ctime { + int sec; + int min; + int hour; + int day; + int month; + int year; +} ctime_t; + +void ctime_t_new(ctime_t*); + +int ctime_add_sec(ctime_t*, int); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..cc078f4 --- /dev/null +++ b/src/main.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <time.h> + +#include "ctime.h" + +int main(int argc, char* argv[]) { + int i = 1; + + // Don't override anything previous + printf("\n"); + + int sleep_sec = 1; + int sleep_usec = sleep_sec * 1000000; + + ctime_t t; + ctime_t_new(&t); + + while(0 == 0) { + printf("\r%02d:%02d", t.min, t.sec); + ctime_add_sec(&t, 1); + fflush(stdout); + i++; + usleep(sleep_usec); + } + printf("\n"); +} |