BellUtils.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef EUPHONIUM_BELL_UTILS
  2. #define EUPHONIUM_BELL_UTILS
  3. #include <stdint.h> // for int32_t, int64_t
  4. #include <string.h> // for NULL
  5. #ifdef _WIN32
  6. #include <WinSock2.h>
  7. #else
  8. #include <sys/time.h> // for timeval, gettimeofday
  9. #include <unistd.h> // for usleep
  10. #endif
  11. #include <cmath> // for floor
  12. #include <string> // for string
  13. #ifdef ESP_PLATFORM
  14. #include "esp_system.h"
  15. #endif
  16. namespace bell {
  17. std::string generateRandomUUID();
  18. void freeAndNull(void*& ptr);
  19. std::string getMacAddress();
  20. struct tv {
  21. tv() {}
  22. tv(timeval tv) : sec(tv.tv_sec), usec(tv.tv_usec){};
  23. tv(int32_t _sec, int32_t _usec) : sec(_sec), usec(_usec){};
  24. static tv now() {
  25. tv timestampNow;
  26. #if _WIN32
  27. static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
  28. SYSTEMTIME system_time;
  29. FILETIME file_time;
  30. uint64_t time;
  31. GetSystemTime(&system_time);
  32. SystemTimeToFileTime(&system_time, &file_time);
  33. time = ((uint64_t)file_time.dwLowDateTime);
  34. time += ((uint64_t)file_time.dwHighDateTime) << 32;
  35. timestampNow.sec = (long)((time - EPOCH) / 10000000L);
  36. timestampNow.usec = (long)(system_time.wMilliseconds * 1000);
  37. #else
  38. timeval t;
  39. gettimeofday(&t, NULL);
  40. timestampNow.sec = t.tv_sec;
  41. timestampNow.usec = t.tv_usec;
  42. #endif
  43. return timestampNow;
  44. }
  45. int32_t sec;
  46. int32_t usec;
  47. int64_t ms() {
  48. return (sec * (int64_t)1000) + (usec / 1000);
  49. }
  50. tv operator+(const tv& other) const {
  51. tv result(*this);
  52. result.sec += other.sec;
  53. result.usec += other.usec;
  54. if (result.usec > 1000000) {
  55. result.sec += result.usec / 1000000;
  56. result.usec %= 1000000;
  57. }
  58. return result;
  59. }
  60. tv operator/(const int& other) const {
  61. tv result(*this);
  62. int64_t millis = result.ms();
  63. millis = millis / other;
  64. result.sec = std::floor(millis / 1000.0);
  65. result.usec = (int32_t)((int64_t)(millis * 1000) % 1000000);
  66. return result;
  67. }
  68. tv operator-(const tv& other) const {
  69. tv result(*this);
  70. result.sec -= other.sec;
  71. result.usec -= other.usec;
  72. while (result.usec < 0) {
  73. result.sec -= 1;
  74. result.usec += 1000000;
  75. }
  76. return result;
  77. }
  78. };
  79. } // namespace bell
  80. #ifdef ESP_PLATFORM
  81. #include <freertos/FreeRTOS.h>
  82. #define BELL_SLEEP_MS(ms) vTaskDelay(ms / portTICK_PERIOD_MS)
  83. #define BELL_YIELD() taskYIELD()
  84. #elif defined(_WIN32)
  85. #define BELL_SLEEP_MS(ms) Sleep(ms)
  86. #define BELL_YIELD() ;
  87. #else
  88. #define BELL_SLEEP_MS(ms) usleep(ms * 1000)
  89. #define BELL_YIELD() ;
  90. #endif
  91. #endif