blob: b34677fead2edf86444cc967e8dde41b11aed80f [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001#include "HAL/Utilities.hpp"
2#include <time.h>
3
4const int32_t HAL_NO_WAIT = 0;
5const int32_t HAL_WAIT_FOREVER = -1;
6
7void delayTicks(int32_t ticks)
8{
9 struct timespec test, remaining;
10 test.tv_sec = 0;
11 test.tv_nsec = ticks * 3;
12
13 /* Sleep until the requested number of ticks has passed, with additional
14 time added if nanosleep is interrupted. */
15 while(nanosleep(&test, &remaining) == -1) {
16 test = remaining;
17 }
18}
19
20void delayMillis(double ms)
21{
22 struct timespec test, remaining;
23 test.tv_sec = ms / 1000;
24 test.tv_nsec = 1000 * (((uint64_t)ms) % 1000000);
25
26 /* Sleep until the requested number of milliseconds has passed, with
27 additional time added if nanosleep is interrupted. */
28 while(nanosleep(&test, &remaining) == -1) {
29 test = remaining;
30 }
31}
32
33void delaySeconds(double s)
34{
35 struct timespec test, remaining;
36 test.tv_sec = (int)s;
37 test.tv_nsec = (s - (int)s) * 1000000000.0;
38
39 /* Sleep until the requested number of seconds has passed, with additional
40 time added if nanosleep is interrupted. */
41 while(nanosleep(&test, &remaining) == -1) {
42 test = remaining;
43 }
44}