Sound2Light auf RGB(W)-LED-Leisten
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.c 953B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * vim: sw=2 ts=2 expandtab
  3. *
  4. * THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
  5. * <cfr34k@tkolb.de> wrote this file. As long as you retain this notice you can
  6. * do whatever you want with this stuff. If we meet some day, and you think
  7. * this stuff is worth it, you can buy me a pizza in return. - Thomas Kolb
  8. */
  9. #include <errno.h>
  10. #include <time.h>
  11. #include <stdint.h>
  12. #include "utils.h"
  13. double get_hires_time(void) {
  14. struct timespec clk;
  15. clock_gettime(CLOCK_REALTIME, &clk);
  16. return clk.tv_sec + 1e-9 * clk.tv_nsec;
  17. }
  18. void fsleep(double d) {
  19. struct timespec ts;
  20. ts.tv_sec = (time_t)d;
  21. ts.tv_nsec = (long)(1e9 * (d - (long)d));
  22. nanosleep(&ts, NULL);
  23. }
  24. void sleep_until(double hires_time) {
  25. struct timespec tv;
  26. int ret;
  27. tv.tv_sec = hires_time;
  28. tv.tv_nsec = (uint64_t)(1e9 * hires_time) % 1000000000;
  29. do {
  30. ret = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &tv, NULL);
  31. } while(ret == EINTR);
  32. }