aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorAndreas Eversberg <jolly@eversberg.eu>2017-01-14 08:05:30 +0100
committerAndreas Eversberg <jolly@eversberg.eu>2017-02-18 21:01:03 +0100
commit799b96c801567c1cd0c6a1281511bca90eeac1db (patch)
treec948b718962509986c28e88a1970e65d54085ee8 /src/common
parenta7e47f83d71c32d0afbabc8840669aaabc9f6cd5 (diff)
Fixed sleep interval in main loop
The elapsed time is calculated, so that it is removed from the sleep time. This way the loop interval matches the given target.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/main_common.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/common/main_common.c b/src/common/main_common.c
index 38c01ad..b11d497 100644
--- a/src/common/main_common.c
+++ b/src/common/main_common.c
@@ -425,7 +425,7 @@ void main_common(int *quit, int latency, int interval, void (*myhandler)(void))
{
int latspl;
sender_t *sender;
- double last_time = 0, now;
+ double last_time_call = 0, begin_time, now, sleep;
struct termios term, term_orig;
int c;
@@ -460,6 +460,8 @@ void main_common(int *quit, int latency, int interval, void (*myhandler)(void))
signal(SIGPIPE, sighandler);
while(!(*quit)) {
+ begin_time = get_time();
+
/* process sound of all transceivers */
for (sender = sender_head; sender; sender = sender->next) {
/* do not process audio for an audio slave, since it is done by audio master */
@@ -474,10 +476,10 @@ void main_common(int *quit, int latency, int interval, void (*myhandler)(void))
/* process audio for mncc call instances */
now = get_time();
- if (now - last_time >= 0.1)
- last_time = now;
- if (now - last_time >= 0.020) {
- last_time += 0.020;
+ if (now - last_time_call >= 0.1)
+ last_time_call = now;
+ if (now - last_time_call >= 0.020) {
+ last_time_call += 0.020;
/* call clock every 20ms */
call_mncc_clock();
}
@@ -513,8 +515,15 @@ next_char:
if (myhandler)
myhandler();
- /* sleep a while */
- usleep(interval * 1000);
+ now = get_time();
+
+ /* sleep interval */
+ sleep = ((double)interval / 1000.0) - (now - begin_time);
+ if (sleep > 0)
+ usleep(sleep * 1000000.0);
+
+// now = get_time();
+// printf("duration =%.6f\n", now - begin_time);
}
/* reset signals */