24 lines
No EOL
997 B
C
24 lines
No EOL
997 B
C
//
|
|
// Created by Hannah on 31/01/2026.
|
|
//
|
|
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
|
|
int delayTickrate(int ticksPerSecond, long int period) {
|
|
int requiredNanoseconds = 10e9/ticksPerSecond;
|
|
struct timeval currentTime;
|
|
gettimeofday(¤tTime, NULL);
|
|
printf("Nanoseconds: %ld\nCurrent:%ld\n", requiredNanoseconds,currentTime.tv_usec*1000 - period*1000);
|
|
if (currentTime.tv_usec*1000 - period*1000 < requiredNanoseconds) {
|
|
struct timespec remainingTime;
|
|
remainingTime.tv_sec = 0;
|
|
remainingTime.tv_nsec = requiredNanoseconds - (currentTime.tv_usec*1000 - period*1000);
|
|
printf("Sleeping for %dnano and %dsec with %f effective sec\n", remainingTime.tv_nsec, remainingTime.tv_sec, remainingTime.tv_nsec/10e9);
|
|
clock_nanosleep(CLOCK_MONOTONIC, 0, &remainingTime, NULL);
|
|
return 0;
|
|
}
|
|
printf("Can't delay, %d larger than required %d", currentTime.tv_usec*1000 - period*1000, requiredNanoseconds);
|
|
return 1;
|
|
} |