74 task = ESP_TIMER_TASK,
75#if CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD || __DOXYGEN__
84 std::string_view
name =
"";
89#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
100 [[nodiscard]]
explicit timer(
const config& cfg, std::move_only_function<
void()> callback);
111 [[nodiscard]]
explicit timer(
const config& cfg,
void (*callback)(
void*),
void* arg);
142#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
167 template<
typename Rep,
typename Period>
170 const std::chrono::duration<Rep, Period>&
timeout,
171 std::move_only_function<
void()> callback
199 template<
typename Rep,
typename Period>
200 [[nodiscard]]
static std::unique_ptr<timer>
227 [[nodiscard]]
static std::unique_ptr<timer>
253 [[nodiscard]]
static std::unique_ptr<timer>
282 template<
typename Rep,
typename Period>
285 const std::chrono::duration<Rep, Period>& interval,
286 std::move_only_function<
void()> callback
314 template<
typename Rep,
typename Period>
315 [[nodiscard]]
static std::unique_ptr<timer>
316 start_periodic(
config cfg,
const std::chrono::duration<Rep, Period>& interval,
void (*callback)(
void*),
void* arg) {
340 template<
typename Rep,
typename Period>
343 const std::chrono::duration<Rep, Period>&
timeout,
344 std::move_only_function<
void()> callback
346 auto t =
make(std::move(cfg), std::move(callback));
350 auto r = (*t)->try_start_once(
timeout);
352 return error(r.error());
376 template<
typename Rep,
typename Period>
379 auto t =
make(std::move(cfg), callback, arg);
383 auto r = (*t)->try_start_once(
timeout);
385 return error(r.error());
410 auto t =
make(std::move(cfg), std::move(callback));
414 auto r = (*t)->try_start_once(time);
416 return error(r.error());
441 auto t =
make(std::move(cfg), callback, arg);
445 auto r = (*t)->try_start_once(time);
447 return error(r.error());
471 template<
typename Rep,
typename Period>
474 const std::chrono::duration<Rep, Period>& interval,
475 std::move_only_function<
void()> callback
477 auto t =
make(std::move(cfg), std::move(callback));
481 auto r = (*t)->try_start_periodic(interval);
483 return error(r.error());
507 template<
typename Rep,
typename Period>
510 const std::chrono::duration<Rep, Period>& interval,
511 void (*callback)(
void*),
514 auto t =
make(std::move(cfg), callback, arg);
518 auto r = (*t)->try_start_periodic(interval);
520 return error(r.error());
543 [[nodiscard]] esp_timer_handle_t
idf_handle() const noexcept {
return _handle; }
549 [[nodiscard]]
const std::string&
name() const noexcept {
return _name; }
551#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
561 template<
typename Rep,
typename Period>
595 template<
typename Rep,
typename Period>
613 template<
typename Rep,
typename Period>
615 return wrap(esp_timer_start_once(_handle, to_us(
timeout)));
629 auto timeout_us = std::max(int64_t{0}, (time -
clock::now()).count());
630 return wrap(esp_timer_start_once(_handle,
static_cast<uint64_t
>(timeout_us)));
665 template<
typename Rep,
typename Period>
667 return wrap(esp_timer_start_periodic(_handle, to_us(interval)));
688#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
698 template<
typename Rep,
typename Period>
716 template<
typename Rep,
typename Period>
718 auto err = esp_timer_restart(_handle, to_us(
timeout));
719 if (err == ESP_ERR_INVALID_STATE) {
720 return wrap(esp_timer_start_once(_handle, to_us(
timeout)));
743#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
777 [[nodiscard]]
bool is_active() const noexcept {
return esp_timer_is_active(_handle); }
788 [[nodiscard]] std::chrono::microseconds
period() const noexcept {
789 uint64_t period_us = 0;
790 esp_timer_get_period(_handle, &period_us);
791 return std::chrono::microseconds{
static_cast<int64_t
>(period_us)};
805 if (esp_timer_get_expiry_time(_handle, &expiry) != ESP_OK) {
806 return clock::time_point::max();
808 if (expiry >
static_cast<uint64_t
>(std::numeric_limits<int64_t>::max())) {
809 return clock::time_point::max();
831 explicit timer(esp_timer_handle_t handle, std::string
name, context* ctx);
832 explicit timer(esp_timer_handle_t handle, std::string
name);
834 template<
typename Rep,
typename Period>
835 static constexpr int64_t to_us(
const std::chrono::duration<Rep, Period>& d) {
836 return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
839 static void trampoline(
void* arg);
841 esp_timer_handle_t _handle;
843 context* _context =
nullptr;
Task lifecycle management.
High-resolution timer with microsecond precision.
timer(const config &cfg, std::move_only_function< void()> callback)
Creates a timer with a std::move_only_function callback.
const std::string & name() const noexcept
Returns the timer name.
esp_err_t try_stop_isr()
Stops the timer (ISR-compatible).
static result< std::unique_ptr< timer > > try_start_once(config cfg, const std::chrono::duration< Rep, Period > &timeout, void(*callback)(void *), void *arg)
Creates and starts a one-shot timer with a raw function pointer callback.
static result< std::unique_ptr< timer > > make(config cfg, void(*callback)(void *), void *arg)
Creates a timer with a raw function pointer callback.
static clock::time_point next_alarm()
Returns the time of the next scheduled timer event.
result< void > try_start_once(const std::chrono::duration< Rep, Period > &timeout)
Starts the timer as a one-shot timer.
std::chrono::microseconds period() const noexcept
Returns the period of a periodic timer.
void start_once(clock::time_point time)
Starts the timer as a one-shot timer at an absolute time.
static std::unique_ptr< timer > start_once(config cfg, const std::chrono::duration< Rep, Period > &timeout, void(*callback)(void *), void *arg)
Creates and starts a one-shot timer with a raw function pointer callback.
static result< std::unique_ptr< timer > > try_start_once(config cfg, clock::time_point time, std::move_only_function< void()> callback)
Creates and starts a one-shot timer at an absolute time with a std::move_only_function callback.
timer(const timer &)=delete
static std::unique_ptr< timer > start_once(config cfg, clock::time_point time, std::move_only_function< void()> callback)
Creates and starts a one-shot timer at an absolute time with a std::move_only_function callback.
clock::time_point expiry_time() const noexcept
Returns the absolute expiry time for a one-shot timer.
static result< std::unique_ptr< timer > > try_start_periodic(config cfg, const std::chrono::duration< Rep, Period > &interval, std::move_only_function< void()> callback)
Creates and starts a periodic timer with a std::move_only_function callback.
result< void > try_start_once(clock::time_point time)
Starts the timer as a one-shot timer at an absolute time.
esp_err_t try_restart_isr(uint64_t timeout_us)
Restarts the timer with a new timeout (ISR-compatible).
esp_err_t try_start_periodic_isr(uint64_t interval_us)
Starts the timer as a periodic timer (ISR-compatible).
esp_err_t try_start_once_isr(uint64_t timeout_us)
Starts the timer as a one-shot timer (ISR-compatible).
void start_periodic(const std::chrono::duration< Rep, Period > &interval)
Starts the timer as a periodic timer.
static std::unique_ptr< timer > start_periodic(config cfg, const std::chrono::duration< Rep, Period > &interval, void(*callback)(void *), void *arg)
Creates and starts a periodic timer with a raw function pointer callback.
void start_once(const std::chrono::duration< Rep, Period > &timeout)
Starts the timer as a one-shot timer.
esp_timer_handle_t idf_handle() const noexcept
Returns the underlying ESP-IDF timer handle.
static std::unique_ptr< timer > start_once(config cfg, const std::chrono::duration< Rep, Period > &timeout, std::move_only_function< void()> callback)
Creates and starts a one-shot timer with a std::move_only_function callback.
static result< std::unique_ptr< timer > > try_start_once(config cfg, clock::time_point time, void(*callback)(void *), void *arg)
Creates and starts a one-shot timer at an absolute time with a raw function pointer callback.
result< void > try_stop()
Stops the timer.
static std::unique_ptr< timer > start_periodic(config cfg, const std::chrono::duration< Rep, Period > &interval, std::move_only_function< void()> callback)
Creates and starts a periodic timer with a std::move_only_function callback.
bool is_active() const noexcept
Checks if the timer is currently running.
void stop()
Stops the timer.
void restart(const std::chrono::duration< Rep, Period > &timeout)
Restarts the timer with a new timeout.
static std::unique_ptr< timer > start_once(config cfg, clock::time_point time, void(*callback)(void *), void *arg)
Creates and starts a one-shot timer at an absolute time with a raw function pointer callback.
timer & operator=(timer &&)=delete
timer(const config &cfg, void(*callback)(void *), void *arg)
Creates a timer with a raw function pointer callback.
timer & operator=(const timer &)=delete
static result< std::unique_ptr< timer > > try_start_periodic(config cfg, const std::chrono::duration< Rep, Period > &interval, void(*callback)(void *), void *arg)
Creates and starts a periodic timer with a raw function pointer callback.
result< void > try_start_periodic(const std::chrono::duration< Rep, Period > &interval)
Starts the timer as a periodic timer.
result< void > try_restart(const std::chrono::duration< Rep, Period > &timeout)
Restarts the timer with a new timeout.
static result< std::unique_ptr< timer > > make(config cfg, std::move_only_function< void()> callback)
Creates a timer with a std::move_only_function callback.
~timer()
Destroys the timer.
static result< std::unique_ptr< timer > > try_start_once(config cfg, const std::chrono::duration< Rep, Period > &timeout, std::move_only_function< void()> callback)
Creates and starts a one-shot timer with a std::move_only_function callback.
dispatch_method
Callback dispatch type.
@ task
Callback runs in high-priority timer task (default)
constexpr std::unexpected< std::error_code > error(E e) noexcept
Creates an unexpected error from an error code enum.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
@ timeout
Operation timed out.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
result< void > wrap(esp_err_t e)
Wraps an esp_err_t into a result<void>.
Monotonic clock based on boot time.
static constexpr bool is_steady
std::chrono::time_point< clock > time_point
static time_point now() noexcept
Returns the current time.
std::chrono::microseconds duration
Timer configuration parameters.
bool skip_unhandled_events
Skip events if callback busy.
enum dispatch_method dispatch
Callback dispatch type.
std::string_view name
Timer name for debugging.