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);
131#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
156 template<
typename Rep,
typename Period>
159 const std::chrono::duration<Rep, Period>&
timeout,
160 std::move_only_function<
void()> callback
183 template<
typename Rep,
typename Period>
184 [[nodiscard]]
static timer
211 [[nodiscard]]
static timer
261 template<
typename Rep,
typename Period>
264 const std::chrono::duration<Rep, Period>& interval,
265 std::move_only_function<
void()> callback
288 template<
typename Rep,
typename Period>
289 [[nodiscard]]
static timer
290 start_periodic(
config cfg,
const std::chrono::duration<Rep, Period>& interval,
void (*callback)(
void*),
void* arg) {
315 template<
typename Rep,
typename Period>
318 const std::chrono::duration<Rep, Period>&
timeout,
319 std::move_only_function<
void()> callback
321 auto t =
make(std::move(cfg), std::move(callback));
325 auto r = t->try_start_once(
timeout);
327 return error(r.error());
351 template<
typename Rep,
typename Period>
354 auto t =
make(std::move(cfg), callback, arg);
358 auto r = t->try_start_once(
timeout);
360 return error(r.error());
385 auto t =
make(std::move(cfg), std::move(callback));
389 auto r = t->try_start_once(time);
391 return error(r.error());
416 auto t =
make(std::move(cfg), callback, arg);
420 auto r = t->try_start_once(time);
422 return error(r.error());
446 template<
typename Rep,
typename Period>
449 const std::chrono::duration<Rep, Period>& interval,
450 std::move_only_function<
void()> callback
452 auto t =
make(std::move(cfg), std::move(callback));
456 auto r = t->try_start_periodic(interval);
458 return error(r.error());
482 template<
typename Rep,
typename Period>
485 const std::chrono::duration<Rep, Period>& interval,
486 void (*callback)(
void*),
489 auto t =
make(std::move(cfg), callback, arg);
493 auto r = t->try_start_periodic(interval);
495 return error(r.error());
517 [[nodiscard]] esp_timer_handle_t
idf_handle() const noexcept {
return _handle; }
523 [[nodiscard]]
const std::string&
name() const noexcept {
return _name; }
525#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
535 template<
typename Rep,
typename Period>
569 template<
typename Rep,
typename Period>
587 template<
typename Rep,
typename Period>
589 if (_handle ==
nullptr) {
592 return wrap(esp_timer_start_once(_handle, to_us(
timeout)));
606 if (_handle ==
nullptr) {
609 auto timeout_us = std::max(int64_t{0}, (time -
clock::now()).count());
610 return wrap(esp_timer_start_once(_handle,
static_cast<uint64_t
>(timeout_us)));
645 template<
typename Rep,
typename Period>
647 if (_handle ==
nullptr) {
650 return wrap(esp_timer_start_periodic(_handle, to_us(interval)));
671#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
681 template<
typename Rep,
typename Period>
699 template<
typename Rep,
typename Period>
701 if (_handle ==
nullptr) {
704 auto err = esp_timer_restart(_handle, to_us(
timeout));
705 if (err == ESP_ERR_INVALID_STATE) {
706 return wrap(esp_timer_start_once(_handle, to_us(
timeout)));
729#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
746 if (_handle ==
nullptr) {
749 return wrap(esp_timer_stop(_handle));
769 if (_handle ==
nullptr) {
772 return esp_timer_is_active(_handle);
784 [[nodiscard]] std::chrono::microseconds
period() const noexcept {
785 if (_handle ==
nullptr) {
786 return std::chrono::microseconds{0};
788 uint64_t period_us = 0;
789 esp_timer_get_period(_handle, &period_us);
790 return std::chrono::microseconds{
static_cast<int64_t
>(period_us)};
803 if (_handle ==
nullptr) {
804 return clock::time_point::max();
807 if (esp_timer_get_expiry_time(_handle, &expiry) != ESP_OK) {
808 return clock::time_point::max();
810 if (expiry >
static_cast<uint64_t
>(std::numeric_limits<int64_t>::max())) {
811 return clock::time_point::max();
833 explicit timer(esp_timer_handle_t handle, std::string
name, context* ctx =
nullptr);
835 void _stop_and_delete() noexcept;
837 template<typename Rep, typename Period>
838 static constexpr int64_t to_us(const std::chrono::duration<Rep, Period>& d) {
839 return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
842 static void trampoline(
void* arg);
844 esp_timer_handle_t _handle =
nullptr;
846 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 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 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.
static result< 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 result< timer > make(config cfg, void(*callback)(void *), void *arg)
Creates a timer with a raw function pointer callback.
clock::time_point expiry_time() const noexcept
Returns the absolute expiry time for a one-shot timer.
static 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.
result< void > try_start_once(clock::time_point time)
Starts the timer as a one-shot timer at an absolute time.
timer & operator=(timer &&other) noexcept
esp_err_t try_restart_isr(uint64_t timeout_us)
Restarts the timer with a new timeout (ISR-compatible).
static result< 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.
esp_err_t try_start_periodic_isr(uint64_t interval_us)
Starts the timer as a periodic timer (ISR-compatible).
timer(timer &&other) noexcept
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 result< 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.
static result< timer > make(config cfg, std::move_only_function< void()> callback)
Creates a timer with a std::move_only_function callback.
static result< 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.
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 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.
result< void > try_stop()
Stops the timer.
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 result< 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.
timer & operator=(const timer &)=delete
static 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.
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< 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.
~timer()
Destroys the timer.
dispatch_method
Callback dispatch type.
@ task
Callback runs in high-priority timer task (default)
static 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.
static 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.
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.
@ invalid_state
Invalid state.
@ 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.