|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
A lightweight identifier for a hardware PWM timer. More...
Classes | |
| struct | config |
| Timer configuration parameters. More... | |
Public Member Functions | |
| timer (const timer &)=default | |
| timer & | operator= (const timer &)=default |
| timer (timer &&)=default | |
| timer & | operator= (timer &&)=default |
| constexpr bool | operator== (const timer &other) const =default |
| constexpr enum speed_mode | speed_mode () const noexcept |
| Returns the speed mode. | |
| void | configure (const struct config &cfg) |
| Configures the timer with the given parameters. | |
| void | configure (freq::hertz frequency, uint8_t resolution_bits=13) |
| Configures the timer with frequency and resolution. | |
| result< void > | try_configure (const struct config &cfg) |
| Configures the timer with the given parameters. | |
| result< void > | try_configure (freq::hertz frequency, uint8_t resolution_bits=13) |
| Configures the timer with frequency and resolution. | |
| bool | is_configured () const noexcept |
| Returns true if the timer has been configured. | |
| uint8_t | resolution_bits () const noexcept |
| Returns the configured resolution in bits. | |
| uint32_t | ticks_max () const noexcept |
| Returns the maximum duty ticks for the configured resolution. | |
| freq::hertz | frequency () const |
| Returns the current timer frequency. | |
| std::chrono::nanoseconds | period () const |
| Returns the PWM period (1 / frequency) as a duration. | |
| std::chrono::nanoseconds | tick_period () const |
| Returns the duration of a single timer tick. | |
| void | set_frequency (freq::hertz frequency) |
| Changes the timer frequency. | |
| result< void > | try_set_frequency (freq::hertz frequency) |
| Changes the timer frequency. | |
| template<typename Rep , typename Period > | |
| void | set_period (const std::chrono::duration< Rep, Period > &period) |
| Changes the timer period. | |
| template<typename Rep , typename Period > | |
| result< void > | try_set_period (const std::chrono::duration< Rep, Period > &period) |
| Changes the timer period. | |
| void | pause () |
| Pauses the timer counter. | |
| result< void > | try_pause () |
| Pauses the timer counter. | |
| void | resume () |
| Resumes the timer counter. | |
| result< void > | try_resume () |
| Resumes the timer counter. | |
| void | reset () |
| Resets the timer counter. | |
| result< void > | try_reset () |
| Resets the timer counter. | |
Friends | |
| template<int N, speed_mode M> | |
| struct | timer_constant |
| std::optional< timer > | get_timer (enum channel, enum speed_mode) |
| Returns the timer associated with an active channel, if any. | |
| result< output > | try_start (idfxx::gpio, const config &, const output_config &) |
| Starts PWM output with automatic allocation and custom output configuration. | |
A lightweight identifier for a hardware PWM timer.
Timers are fixed hardware resources, like GPIO pins. This class provides a copyable, lightweight handle for configuring and controlling a specific timer unit. Use the predefined constants (timer_0 through timer_3).
Call configure() to set up the timer's frequency and resolution before creating any output bound to it.
|
default |
Configures the timer with the given parameters.
Sets up the timer's frequency, resolution, and clock source. Must be called before creating any output bound to this timer.
| cfg | Timer configuration. |
| std::system_error | on failure. |
Definition at line 202 of file pwm.hpp.
References try_configure(), and idfxx::unwrap().
Configures the timer with frequency and resolution.
Convenience overload using automatic clock source selection.
| frequency | PWM frequency. |
| resolution_bits | Duty resolution in bits (default: 13). |
| std::system_error | on failure. |
Definition at line 214 of file pwm.hpp.
References frequency(), resolution_bits(), try_configure(), and idfxx::unwrap().
| freq::hertz idfxx::pwm::timer::frequency | ( | ) | const |
Returns the current timer frequency.
The actual frequency may differ slightly from the requested value due to rounding.
Referenced by configure(), set_frequency(), and try_configure().
|
noexcept |
Returns true if the timer has been configured.
This reflects global state — any copy of the same timer identifier will see the same configured status.
|
inline |
Pauses the timer counter.
| std::system_error | on failure. |
Definition at line 381 of file pwm.hpp.
References try_pause(), and idfxx::unwrap().
| std::chrono::nanoseconds idfxx::pwm::timer::period | ( | ) | const |
Returns the PWM period (1 / frequency) as a duration.
Returns zero if the timer is not configured.
Referenced by set_period(), and try_set_period().
|
inline |
Resets the timer counter.
| std::system_error | on failure. |
Definition at line 415 of file pwm.hpp.
References try_reset(), and idfxx::unwrap().
|
noexcept |
Returns the configured resolution in bits.
Returns 0 if the timer has not been configured via configure().
Referenced by configure(), and try_configure().
|
inline |
Resumes the timer counter.
| std::system_error | on failure. |
Definition at line 398 of file pwm.hpp.
References try_resume(), and idfxx::unwrap().
|
inline |
Changes the timer frequency.
The timer must have been configured first via configure().
| frequency | New PWM frequency. |
| std::system_error | on failure. |
Definition at line 313 of file pwm.hpp.
References frequency(), try_set_frequency(), and idfxx::unwrap().
Changes the timer period.
The timer must have been configured first via configure().
| Rep | Duration representation type. |
| Period | Duration period type. |
| period | New PWM period. |
| std::system_error | on failure. |
Definition at line 347 of file pwm.hpp.
References period(), try_set_period(), and idfxx::unwrap().
|
inlineconstexprnoexcept |
| std::chrono::nanoseconds idfxx::pwm::timer::tick_period | ( | ) | const |
Returns the duration of a single timer tick.
Equal to period() / ticks_max(). Returns zero if the timer is not configured.
|
noexcept |
Returns the maximum duty ticks for the configured resolution.
Equal to 1 << resolution_bits(). Returns 1 if not configured.
Configures the timer with the given parameters.
Sets up the timer's frequency, resolution, and clock source. Must be called before creating any output bound to this timer.
| cfg | Timer configuration. |
| invalid_arg | Invalid frequency (zero) or resolution. |
| fail | Requested frequency cannot be achieved. |
Referenced by configure(), configure(), and try_configure().
|
inline |
Configures the timer with frequency and resolution.
Convenience overload using automatic clock source selection.
| frequency | PWM frequency. |
| resolution_bits | Duty resolution in bits (default: 13). |
Definition at line 242 of file pwm.hpp.
References frequency(), resolution_bits(), and try_configure().
Changes the timer frequency.
The timer must have been configured first via configure().
| frequency | New PWM frequency. |
| invalid_arg | Invalid frequency or timer not configured. |
| fail | Requested frequency cannot be achieved. |
Referenced by set_frequency(), and try_set_period().
|
inline |
Changes the timer period.
The timer must have been configured first via configure().
| Rep | Duration representation type. |
| Period | Duration period type. |
| period | New PWM period. |
| invalid_arg | Invalid period (zero or negative). |
| fail | Requested period cannot be achieved. |
Definition at line 366 of file pwm.hpp.
References idfxx::error(), idfxx::invalid_arg, period(), and try_set_frequency().
Referenced by set_period().
Returns the timer associated with an active channel, if any.
| ch | Channel to check. |
| mode | Speed mode group (defaults to low_speed). |
|
friend |
|
friend |
Starts PWM output with automatic allocation and custom output configuration.
| gpio | GPIO pin for the output. |
| cfg | Timer configuration (frequency, resolution, clock source). |
| out_cfg | Output configuration (initial duty, hpoint, inversion, sleep mode). |
| not_found | No free timer or channel available. |
| invalid_arg | GPIO not connected or invalid configuration. |