From 3f2af32442b9384d316651d42ad35945533dc39e Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Mon, 11 Nov 2024 23:26:33 +0100 Subject: Add timespec utils --- src/block.c | 2 +- src/comet.c | 2 +- src/util.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- src/util.h | 17 ++++++++++++++++- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/block.c b/src/block.c index d0b66c9..29a8394 100644 --- a/src/block.c +++ b/src/block.c @@ -9,7 +9,7 @@ void block_update(block_t *block) if (block->update_cb != NULL) { struct timespec now, diff; timespec_get(&now, TIME_UTC); - diff = timespec_diff(block->update_last, now); + diff = timespec_diff(now, block->update_last); if (timespec_greater(diff, block->update_interval)) { log_value_trace("Updating block", diff --git a/src/comet.c b/src/comet.c index 6fccb3d..a539015 100644 --- a/src/comet.c +++ b/src/comet.c @@ -124,7 +124,7 @@ int main(int argc, char **argv) layout_free(&layout); timespec_get(&end, TIME_UTC); - diff = timespec_diff(timespec_diff(end, start), rate); + diff = timespec_diff(rate, timespec_diff(end, start)); nanosleep(&diff, NULL); } diff --git a/src/util.c b/src/util.c index 805d397..669e031 100644 --- a/src/util.c +++ b/src/util.c @@ -28,14 +28,46 @@ void color_print(FILE *stream, color_t *color) fprintf(stream, "#%02x%02x%02x%02x", r, g, b, a); } +const struct timespec timespec_from_ms(long ms) +{ + struct timespec ts = { + .tv_sec = ms / 1000, + .tv_nsec = (ms % 1000) * 1000000ul, + }; + return ts; +} + +const long timespec_to_ms(struct timespec ts) +{ + return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000ul); +} + struct timespec timespec_diff(struct timespec a, struct timespec b) { - bool over = (b.tv_nsec - a.tv_nsec) < 0; - struct timespec diff = { - .tv_sec = b.tv_sec - a.tv_sec - over, - .tv_nsec = b.tv_nsec - a.tv_nsec + over * 1000000000ul, + bool over = (a.tv_nsec - b.tv_nsec) < 0; + struct timespec ts = { + .tv_sec = a.tv_sec - b.tv_sec - over, + .tv_nsec = a.tv_nsec - b.tv_nsec + over * 1000000000ul, }; - return diff; + return ts; +} + +struct timespec timespec_add(struct timespec a, struct timespec b) +{ + bool over = (a.tv_nsec + b.tv_nsec) > 1000000000ul; + struct timespec ts = { + .tv_sec = a.tv_sec + b.tv_sec + over, + .tv_nsec = a.tv_nsec + b.tv_nsec - over * 1000000000ul, + }; + return ts; +} + +struct timespec timespec_div(struct timespec ts, int n) +{ + ts.tv_nsec /= n; + ts.tv_nsec += ((ts.tv_sec % n) * 1000000000ul) / n; + ts.tv_sec /= n; + return ts; } bool timespec_greater(struct timespec a, struct timespec b) @@ -44,6 +76,11 @@ bool timespec_greater(struct timespec a, struct timespec b) || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec); } +bool timespec_zero(struct timespec ts) +{ + return ts.tv_sec == 0 && ts.tv_nsec == 0; +} + void timespec_print(FILE *stream, struct timespec *ts) { fprintf(stream, "%ld.%.9ld", ts->tv_sec, ts->tv_nsec); diff --git a/src/util.h b/src/util.h index 770790d..d7eafb5 100644 --- a/src/util.h +++ b/src/util.h @@ -6,7 +6,7 @@ #include #include -#define unreachable() log_panic("Unreachable code"); +#define unreachable() log_panic("The impossible happened"); // Color representation normalized to [0, 1] // @@ -25,14 +25,29 @@ static inline color_t color_rgb(int r, int g, int b) return color_rgba(r, g, b, 255); } +static inline color_t color_hex(unsigned int h) +{ + return color_rgb((h >> 16) & 0xff, (h >> 8) & 0xff, h & 0xff); +} + char *color_to_string(color_t *color); void color_print(FILE *stream, color_t *color); +const struct timespec timespec_from_ms(long ms); + +const long timespec_to_ms(struct timespec ts); + struct timespec timespec_diff(struct timespec a, struct timespec b); +struct timespec timespec_add(struct timespec a, struct timespec b); + +struct timespec timespec_div(struct timespec ts, int n); + bool timespec_greater(struct timespec a, struct timespec b); +bool timespec_zero(struct timespec ts); + void timespec_print(FILE *stream, struct timespec *ts); // Check if point (px, py) is inside a rectangle in (x, y), (x+w, y), (x, y+h) and (w+h, y+h) -- cgit v1.2.3