Moved delay mechanism into separate function

This commit is contained in:
Martin Lund 2016-05-18 08:17:07 +02:00
parent 14fc053e8b
commit 13a8bc7790
3 changed files with 13 additions and 8 deletions

View file

@ -23,5 +23,6 @@
#define TIME_H #define TIME_H
char * current_time(void); char * current_time(void);
void delay(long ms);
#endif #endif

View file

@ -23,6 +23,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "tio/error.h" #include "tio/error.h"
#include "tio/print.h"
char * current_time(void) char * current_time(void)
{ {
@ -42,3 +43,13 @@ char * current_time(void)
return time_string; return time_string;
} }
void delay(long ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}

View file

@ -32,7 +32,6 @@
#include <termios.h> #include <termios.h>
#include <stdbool.h> #include <stdbool.h>
#include <errno.h> #include <errno.h>
#include <time.h>
#include "tio/tty.h" #include "tio/tty.h"
#include "tio/print.h" #include "tio/print.h"
#include "tio/options.h" #include "tio/options.h"
@ -309,8 +308,6 @@ int connect_tty(void)
if (forward) if (forward)
{ {
struct timespec ts;
/* Send output to tty device */ /* Send output to tty device */
status = write(fd, &output_char, 1); status = write(fd, &output_char, 1);
if (status < 0) if (status < 0)
@ -324,11 +321,7 @@ int connect_tty(void)
tx_total++; tx_total++;
/* Insert output delay */ /* Insert output delay */
ts.tv_sec = 0; delay(option.output_delay);
ts.tv_nsec = option.output_delay * 1000000;
if (option.output_delay)
nanosleep(&ts, NULL);
} }
/* Save previous key */ /* Save previous key */