Files
fleet_sports_app_common/inc/app_clock.hpp
T
crosstyanandClaude Fable 5 c948c7565c feat: unify app_utils/app_clock headers shared by the fleet
Merged from the three drifted hand-copies in TrackBackFwd, healthy-band-nrf
and zephyr_gateway_fwd:
- as_u8s(T&) overloads and deferrer (band/hub side)
- hexdump, add_overflow, constrain_value_in_range_static, to_mac_string
  (TrackBackFwd side)
- add_overflow now returns std::expected directly instead of the app_result
  alias; to_mac_string guarded by __cpp_lib_format for GCC 12 toolchains
- app_clock.hpp now() selects k_uptime_ticks / esp_timer_get_time via
  __ZEPHYR__ / ESP_PLATFORM; fixed doc comment (microseconds, not ms)
- dropped vestigial esp_err.h/esp_system.h/FreeRTOS.h includes

Dual build glue: ESP-IDF component (root CMakeLists) + Zephyr module (zephyr/).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 15:30:28 +08:00

39 lines
1.2 KiB
C++

#pragma once
#include <chrono>
#include <cstdint>
#include <ratio>
#if defined(__ZEPHYR__)
#include <zephyr/kernel.h>
#elif defined(ESP_PLATFORM)
#include <esp_timer.h>
#else
#error "app_clock.hpp: unsupported platform (expected Zephyr or ESP-IDF)"
#endif
namespace app::utils {
/**
* @brief a monotonic clock with microsecond resolution
* @see https://en.cppreference.com/w/cpp/named_req/TrivialClock.html
* @see https://en.cppreference.com/w/cpp/named_req/Clock.html
*/
struct clock_t {
using rep = int64_t; // signed arithmetic type for tick count
using period = std::micro; // tick period in seconds (1/1000000)
using duration = std::chrono::duration<rep, period>;
using time_point = std::chrono::time_point<clock_t>;
static constexpr bool is_steady = true;
static time_point now() {
#if defined(__ZEPHYR__)
auto t = k_uptime_ticks();
auto dur = k_ticks_to_us_floor64(t);
return time_point(duration(static_cast<rep>(dur)));
#else
return time_point(duration(static_cast<rep>(esp_timer_get_time())));
#endif
}
};
using milliseconds = std::chrono::duration<int32_t, std::milli>;
}