|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
High-resolution timer with microsecond precision. More...
Classes | |
| struct | clock |
| Monotonic clock based on boot time. More... | |
| struct | config |
| Timer configuration parameters. More... | |
Public Types | |
| enum class | dispatch_method : int { task = ESP_TIMER_TASK } |
| Callback dispatch type. More... | |
Public Member Functions | |
| timer (const config &cfg, std::move_only_function< void()> callback) | |
| Creates a timer with a std::move_only_function callback. | |
| timer (const config &cfg, void(*callback)(void *), void *arg) | |
| Creates a timer with a raw function pointer callback. | |
| ~timer () | |
| Destroys the timer. | |
| timer (const timer &)=delete | |
| timer & | operator= (const timer &)=delete |
| timer (timer &&)=delete | |
| timer & | operator= (timer &&)=delete |
| esp_timer_handle_t | idf_handle () const noexcept |
| Returns the underlying ESP-IDF timer handle. | |
| const std::string & | name () const noexcept |
| Returns the timer name. | |
| template<typename Rep , typename Period > | |
| void | start_once (const std::chrono::duration< Rep, Period > &timeout) |
| Starts the timer as a one-shot timer. | |
| void | start_once (clock::time_point time) |
| Starts the timer as a one-shot timer at an absolute time. | |
| template<typename Rep , typename Period > | |
| void | start_periodic (const std::chrono::duration< Rep, Period > &interval) |
| Starts the timer as a periodic timer. | |
| template<typename Rep , typename Period > | |
| result< void > | try_start_once (const std::chrono::duration< Rep, Period > &timeout) |
| Starts the timer as a one-shot timer. | |
| 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_start_once_isr (uint64_t timeout_us) |
| Starts the timer as a one-shot timer (ISR-compatible). | |
| template<typename Rep , typename Period > | |
| result< void > | try_start_periodic (const std::chrono::duration< Rep, Period > &interval) |
| Starts the timer as a periodic timer. | |
| esp_err_t | try_start_periodic_isr (uint64_t interval_us) |
| Starts the timer as a periodic timer (ISR-compatible). | |
| template<typename Rep , typename Period > | |
| void | restart (const std::chrono::duration< Rep, Period > &timeout) |
| Restarts the timer with a new timeout. | |
| template<typename Rep , typename Period > | |
| result< void > | try_restart (const std::chrono::duration< Rep, Period > &timeout) |
| Restarts the timer with a new timeout. | |
| esp_err_t | try_restart_isr (uint64_t timeout_us) |
| Restarts the timer with a new timeout (ISR-compatible). | |
| void | stop () |
| Stops the timer. | |
| result< void > | try_stop () |
| Stops the timer. | |
| esp_err_t | try_stop_isr () |
| Stops the timer (ISR-compatible). | |
| bool | is_active () const noexcept |
| Checks if the timer is currently running. | |
| std::chrono::microseconds | period () const noexcept |
| Returns the period of a periodic timer. | |
| clock::time_point | expiry_time () const noexcept |
| Returns the absolute expiry time for a one-shot timer. | |
Static Public Member Functions | |
| 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. | |
| static result< std::unique_ptr< timer > > | make (config cfg, void(*callback)(void *), void *arg) |
| Creates a timer with a raw function pointer callback. | |
| template<typename Rep , typename Period > | |
| 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. | |
| template<typename Rep , typename Period > | |
| 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 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. | |
| 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. | |
| template<typename Rep , typename Period > | |
| 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. | |
| template<typename Rep , typename Period > | |
| 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. | |
| template<typename Rep , typename Period > | |
| 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. | |
| template<typename Rep , typename Period > | |
| 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 > > | 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. | |
| 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. | |
| template<typename Rep , typename Period > | |
| 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. | |
| template<typename Rep , typename Period > | |
| 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. | |
| static clock::time_point | next_alarm () |
| Returns the time of the next scheduled timer event. | |
High-resolution timer with microsecond precision.
Supports both one-shot and periodic timers with callbacks dispatched either from a dedicated timer task or directly from ISR context.
Timers are non-copyable and non-movable. Use the factory method make() for result-based construction or the throwing constructor when exceptions are enabled.
|
strong |
Callback dispatch type.
Determines whether the timer callback runs in a high-priority timer task or directly in ISR context.
| Enumerator | |
|---|---|
| task | Callback runs in high-priority timer task (default) |
|
explicit |
Creates a timer with a std::move_only_function callback.
| cfg | Timer configuration. |
| callback | Function to call when timer fires. |
| std::system_error | on failure. |
|
explicit |
Creates a timer with a raw function pointer callback.
| cfg | Timer configuration. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| std::system_error | on failure. |
| idfxx::timer::~timer | ( | ) |
Destroys the timer.
Stops the timer if running and waits for any in-flight callback to complete before releasing resources.
|
delete |
|
delete |
|
inlinenoexcept |
Returns the absolute expiry time for a one-shot timer.
For periodic timers, returns clock::time_point::max() as a sentinel value since periodic timers do not have a single expiry time.
| clock::time_point::max() | For periodic timers. |
|
inlinenoexcept |
|
inlinenoexcept |
|
static |
Creates a timer with a std::move_only_function callback.
| cfg | Timer configuration. |
| callback | Function to call when timer fires. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration. |
Referenced by try_start_once(), try_start_once(), try_start_once(), try_start_once(), try_start_periodic(), and try_start_periodic().
|
static |
Creates a timer with a raw function pointer callback.
| cfg | Timer configuration. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration. |
|
inlinenoexcept |
|
inlinestatic |
|
inlinenoexcept |
|
inline |
Restarts the timer with a new timeout.
| Rep | Duration representation type. |
| Period | Duration period type. |
| timeout | Time until the callback is invoked. |
| std::system_error | on failure. |
Definition at line 699 of file timer.hpp.
References idfxx::timeout, try_restart(), and idfxx::unwrap().
|
inline |
Starts the timer as a one-shot timer at an absolute time.
The callback will be invoked once at the specified time point. If the time point is in the past, the timer fires as soon as possible.
| time | Absolute time at which the callback should be invoked. |
| std::system_error | on failure. |
Definition at line 584 of file timer.hpp.
References try_start_once(), and idfxx::unwrap().
|
inlinestatic |
Creates and starts a one-shot timer at an absolute time with a std::move_only_function callback.
Combines timer creation and one-shot start into a single operation. The timer fires at the specified time point. If the time point is in the past, the timer fires as soon as possible.
| cfg | Timer configuration. |
| time | Absolute time at which the callback should be invoked. |
| callback | Function to call when timer fires. |
| std::system_error | on failure. |
Definition at line 228 of file timer.hpp.
References try_start_once(), and idfxx::unwrap().
|
inlinestatic |
Creates and starts a one-shot timer at an absolute time with a raw function pointer callback.
Combines timer creation and one-shot start into a single operation. The timer fires at the specified time point. If the time point is in the past, the timer fires as soon as possible.
| cfg | Timer configuration. |
| time | Absolute time at which the callback should be invoked. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| std::system_error | on failure. |
Definition at line 254 of file timer.hpp.
References try_start_once(), and idfxx::unwrap().
|
inlinestatic |
Creates and starts a one-shot timer with a std::move_only_function callback.
Combines timer creation and one-shot start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| timeout | Time until the callback is invoked. |
| callback | Function to call when timer fires. |
| std::system_error | on failure. |
Definition at line 168 of file timer.hpp.
References idfxx::timeout, try_start_once(), and idfxx::unwrap().
|
inlinestatic |
Creates and starts a one-shot timer with a raw function pointer callback.
Combines timer creation and one-shot start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| timeout | Time until the callback is invoked. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| std::system_error | on failure. |
Definition at line 201 of file timer.hpp.
References idfxx::timeout, try_start_once(), and idfxx::unwrap().
|
inline |
Starts the timer as a one-shot timer.
| Rep | Duration representation type. |
| Period | Duration period type. |
| timeout | Time until the callback is invoked. |
| std::system_error | on failure. |
Definition at line 562 of file timer.hpp.
References idfxx::timeout, try_start_once(), and idfxx::unwrap().
|
inlinestatic |
Creates and starts a periodic timer with a std::move_only_function callback.
Combines timer creation and periodic start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| interval | Time between callback invocations. |
| callback | Function to call when timer fires. |
| std::system_error | on failure. |
Definition at line 283 of file timer.hpp.
References try_start_periodic(), and idfxx::unwrap().
|
inlinestatic |
Creates and starts a periodic timer with a raw function pointer callback.
Combines timer creation and periodic start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| interval | Time between callback invocations. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| std::system_error | on failure. |
Definition at line 316 of file timer.hpp.
References try_start_periodic(), and idfxx::unwrap().
|
inline |
Starts the timer as a periodic timer.
| Rep | Duration representation type. |
| Period | Duration period type. |
| interval | Time between callback invocations. |
| std::system_error | on failure. |
Definition at line 596 of file timer.hpp.
References try_start_periodic(), and idfxx::unwrap().
|
inline |
Stops the timer.
| std::system_error | on failure. |
Definition at line 750 of file timer.hpp.
References try_stop(), and idfxx::unwrap().
|
inline |
Restarts the timer with a new timeout.
If the timer is running, it will be stopped and restarted. If not running, it will be started as a one-shot timer.
| Rep | Duration representation type. |
| Period | Duration period type. |
| timeout | Time until the callback is invoked. |
| invalid_arg | Invalid timeout value. |
Definition at line 717 of file timer.hpp.
References idfxx::timeout, and idfxx::wrap().
Referenced by restart().
| esp_err_t idfxx::timer::try_restart_isr | ( | uint64_t | timeout_us | ) |
Restarts the timer with a new timeout (ISR-compatible).
This method returns the underlying ESP-IDF esp_err_t directly to avoid flash-resident code paths. When CONFIG_ESP_TIMER_IN_IRAM is enabled, this method is placed in IRAM and can be called from ISR context.
| timeout_us | Timeout in microseconds. |
|
inline |
Starts the timer as a one-shot timer at an absolute time.
The callback will be invoked once at the specified time point. If the time point is in the past, the timer fires as soon as possible.
| time | Absolute time at which the callback should be invoked. |
| invalid_state | Timer is already running. |
Definition at line 628 of file timer.hpp.
References idfxx::timer::clock::now(), and idfxx::wrap().
|
inlinestatic |
Creates and starts a one-shot timer at an absolute time with a std::move_only_function callback.
Combines timer creation and one-shot start into a single operation. The timer fires at the specified time point. If the time point is in the past, the timer fires as soon as possible.
| cfg | Timer configuration. |
| time | Absolute time at which the callback should be invoked. |
| callback | Function to call when timer fires. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration. |
| invalid_state | Timer could not be started. |
Definition at line 409 of file timer.hpp.
References idfxx::error(), and make().
|
inlinestatic |
Creates and starts a one-shot timer at an absolute time with a raw function pointer callback.
Combines timer creation and one-shot start into a single operation. The timer fires at the specified time point. If the time point is in the past, the timer fires as soon as possible.
| cfg | Timer configuration. |
| time | Absolute time at which the callback should be invoked. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration. |
| invalid_state | Timer could not be started. |
Definition at line 440 of file timer.hpp.
References idfxx::error(), and make().
|
inlinestatic |
Creates and starts a one-shot timer with a std::move_only_function callback.
Combines timer creation and one-shot start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| timeout | Time until the callback is invoked. |
| callback | Function to call when timer fires. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration or timeout value. |
| invalid_state | Timer could not be started. |
Definition at line 341 of file timer.hpp.
References idfxx::error(), make(), and idfxx::timeout.
Referenced by start_once(), start_once(), start_once(), start_once(), start_once(), and start_once().
|
inlinestatic |
Creates and starts a one-shot timer with a raw function pointer callback.
Combines timer creation and one-shot start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| timeout | Time until the callback is invoked. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration or timeout value. |
| invalid_state | Timer could not be started. |
Definition at line 378 of file timer.hpp.
References idfxx::error(), make(), and idfxx::timeout.
|
inline |
Starts the timer as a one-shot timer.
The callback will be invoked once after the specified timeout.
| Rep | Duration representation type. |
| Period | Duration period type. |
| timeout | Time until the callback is invoked. |
| invalid_state | Timer is already running. |
| invalid_arg | Invalid timeout value. |
Definition at line 614 of file timer.hpp.
References idfxx::timeout, and idfxx::wrap().
| esp_err_t idfxx::timer::try_start_once_isr | ( | uint64_t | timeout_us | ) |
Starts the timer as a one-shot timer (ISR-compatible).
This method returns the underlying ESP-IDF esp_err_t directly to avoid flash-resident code paths. When CONFIG_ESP_TIMER_IN_IRAM is enabled, this method is placed in IRAM and can be called from ISR context.
| timeout_us | Timeout in microseconds. |
| ESP_ERR_INVALID_STATE | Timer is already running. |
| ESP_ERR_INVALID_ARG | Invalid timeout value. |
|
inlinestatic |
Creates and starts a periodic timer with a std::move_only_function callback.
Combines timer creation and periodic start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| interval | Time between callback invocations. |
| callback | Function to call when timer fires. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration or interval value. |
| invalid_state | Timer could not be started. |
Definition at line 472 of file timer.hpp.
References idfxx::error(), and make().
Referenced by start_periodic(), start_periodic(), and start_periodic().
|
inlinestatic |
Creates and starts a periodic timer with a raw function pointer callback.
Combines timer creation and periodic start into a single operation. If creation succeeds but starting fails, the timer is automatically cleaned up via RAII.
| Rep | Duration representation type. |
| Period | Duration period type. |
| cfg | Timer configuration. |
| interval | Time between callback invocations. |
| callback | Function to call when timer fires. |
| arg | Argument passed to the callback. |
| no_mem | Memory allocation failed. |
| invalid_arg | Invalid configuration or interval value. |
| invalid_state | Timer could not be started. |
Definition at line 508 of file timer.hpp.
References idfxx::error(), and make().
|
inline |
Starts the timer as a periodic timer.
The callback will be invoked repeatedly at the specified interval.
| Rep | Duration representation type. |
| Period | Duration period type. |
| interval | Time between callback invocations. |
| invalid_state | Timer is already running. |
| invalid_arg | Invalid interval value. |
Definition at line 666 of file timer.hpp.
References idfxx::wrap().
| esp_err_t idfxx::timer::try_start_periodic_isr | ( | uint64_t | interval_us | ) |
Starts the timer as a periodic timer (ISR-compatible).
This method returns the underlying ESP-IDF esp_err_t directly to avoid flash-resident code paths. When CONFIG_ESP_TIMER_IN_IRAM is enabled, this method is placed in IRAM and can be called from ISR context.
| interval_us | Interval in microseconds. |
|
inline |
Stops the timer.
| invalid_state | Timer is not running. |
Definition at line 759 of file timer.hpp.
References idfxx::wrap().
Referenced by stop().
| esp_err_t idfxx::timer::try_stop_isr | ( | ) |
Stops the timer (ISR-compatible).
This method returns the underlying ESP-IDF esp_err_t directly to avoid flash-resident code paths. When CONFIG_ESP_TIMER_IN_IRAM is enabled, this method is placed in IRAM and can be called from ISR context.
| ESP_ERR_INVALID_STATE | Timer is not running. |