Add espchrono and tickchrono implementations

This commit is contained in:
2021-02-24 17:57:03 +01:00
parent 212b12e403
commit 0b9e5dc550
3 changed files with 48 additions and 1 deletions

View File

@ -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)

25
src/espchrono_impl.cpp Normal file
View File

@ -0,0 +1,25 @@
// local includes
#include "espchrono.h"
// system includes
#include <time.h>
// esp-idf inlcludes
#include <esp_timer.h>
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<duration>(std::chrono::microseconds{esp_timer_get_time()})};
}

20
src/tickchrono.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
// system includes
#include <chrono>
#include <ratio>
// esp-idf includes
#include <freertos/FreeRTOS.h>
#include <freertos/projdefs.h>
namespace espcpputils {
using ticks = std::chrono::duration<TickType_t, std::ratio<1, configTICK_RATE_HZ>>;
static_assert(pdMS_TO_TICKS(1000) == ticks(std::chrono::seconds(1)).count());
static_assert(pdMS_TO_TICKS(100) == std::chrono::floor<ticks>(std::chrono::milliseconds(100)).count());
static_assert(pdMS_TO_TICKS(10) == std::chrono::floor<ticks>(std::chrono::milliseconds(10)).count());
} // namespace espcpputils