[SOLVED] timer / stopwatch program in C and ncurses
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wrote a timer program in C, but it's not too accurate. Any ideas on how to make it more accurate, like to 10 ms or 100 ms or so. It doesn't need to be too accurate, but the current one is only accurate to the nearest second or so and pausing may introduce more error.
Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <ncurses.h>
int main (int argc, char *argv[])
{
int ch;
unsigned int countdown;
time_t start;
time_t now;
if (1 == argc)
{
start = time (NULL);
}
else if (2 == argc)
{
sscanf (argv[1], "%u", &countdown);
start = time (NULL) + (countdown * 60);
}
else
{
printf ("Usage: %s <minute_countdown>\n", argv[0]);
return 1;
}
initscr();
cbreak();
noecho();
nodelay (stdscr, TRUE);
int paused = 0;
time_t prev = 0;
for (;;)
{
if (0 == paused)
{
now = time (NULL) - start;
if (now != prev)
{
mvprintw (1, 2, "%+04d:%+03d", now / 60 , now % 60);
refresh();
prev = now;
}
}
else
{
start = time (NULL) - now;
}
if (ERR != (ch = getch()))
{
if (27 == ch)
{
break;
}
else
{
if (0 == paused)
{
attron (A_STANDOUT);
paused = 1;
}
else
{
attroff (A_STANDOUT);
paused = 0;
}
mvprintw (1, 2, "%+04d:%+03d", now / 60 , now % 60);
refresh();
}
}
usleep(100000);
}
endwin();
return 0;
}
It works well, and I'll keep using it, but I'm sure there's another way. I still need to be able to pause it and resume it, as that's the most important feature, and countdown.
Oh, and it should stay mostly portable, but I only run Linux.
I was thinking of using usleep to pause for certain time, but I feel that the commands in-between will gradually add time to it and make it inaccurate, so before doing that I'm asking for other methods.
I decided to use clock_gettime as it is recommended over gettimeofday, but it still doesn't work quite right, I'll have to think this over a bit. Maybe the math is wrong.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.