diff --git a/CMakeLists.txt b/CMakeLists.txt index fca7680..3f5903a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set(headers src/lockingqueue.h src/recursivelockhelper.h src/taskutils.h + src/tickchrono.h src/wrappers/binary_semaphore.h src/wrappers/counting_semaphore.h src/wrappers/event_group.h @@ -19,7 +20,8 @@ set(headers set(sources src/asyncudplistener.cpp + src/espchrono_impl.cpp src/taskutils.cpp ) -idf_component_register(INCLUDE_DIRS src SRCS ${headers} ${sources} REQUIRES freertos esp_system esp_http_client esp_websocket_client cpputils lwip) +idf_component_register(INCLUDE_DIRS src SRCS ${headers} ${sources} REQUIRES freertos esp_system esp_http_client esp_websocket_client lwip cpputils espchrono) diff --git a/src/espchrono_impl.cpp b/src/espchrono_impl.cpp new file mode 100644 index 0000000..48d4509 --- /dev/null +++ b/src/espchrono_impl.cpp @@ -0,0 +1,25 @@ +// local includes +#include "espchrono.h" + +// system includes +#include + +// esp-idf inlcludes +#include + +using namespace std::chrono_literals; + +// actual implementations used on the ESP32 + +auto espchrono::utc_clock::now() noexcept -> time_point +{ + timeval tv; + gettimeofday(&tv, NULL); + seconds32 seconds{tv.tv_sec}; + return time_point{seconds}; +} + +auto espchrono::millis_clock::now() noexcept -> time_point +{ + return time_point{std::chrono::floor(std::chrono::microseconds{esp_timer_get_time()})}; +} diff --git a/src/tickchrono.h b/src/tickchrono.h new file mode 100644 index 0000000..f883ea4 --- /dev/null +++ b/src/tickchrono.h @@ -0,0 +1,20 @@ +#pragma once + +// system includes +#include +#include + +// esp-idf includes +#include +#include + +namespace espcpputils { + +using ticks = std::chrono::duration>; + +static_assert(pdMS_TO_TICKS(1000) == ticks(std::chrono::seconds(1)).count()); +static_assert(pdMS_TO_TICKS(100) == std::chrono::floor(std::chrono::milliseconds(100)).count()); +static_assert(pdMS_TO_TICKS(10) == std::chrono::floor(std::chrono::milliseconds(10)).count()); + +} // namespace espcpputils +