ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
pwm.hpp File Reference

Simple PWM output wrapper with period and duty cycle configuration. More...

#include <cstdint>
#include "non_copyable.hpp"
Include dependency graph for pwm.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  c7222::PwmOut
 Output-only PWM wrapper with period and duty configuration. More...
 
struct  c7222::PwmOut::Config
 Configuration structure for PWM output setup. More...
 

Namespaces

namespace  c7222
 C7222 course abstractions namespace.
 

Detailed Description

Simple PWM output wrapper with period and duty cycle configuration.

This class provides a minimal PWM interface focused on:

  • Period (microseconds)
  • Duty cycle (0.0 to 1.0)

Platform-specific behavior (Pico):

  • The Pico backend configures the PWM slice/channel associated with the given GPIO and computes TOP + divider from the requested period.
  • Ownership is enforced per GPIO pin. Constructing two PwmOut objects for the same pin asserts in debug builds.
  • When PWM is disabled (via Enable(false)), the pin function is returned to GPIO_FUNC_SIO so it can be used as a normal GPIO again.
  • When the object is destroyed, the pin is also returned to GPIO_FUNC_SIO and ownership is released.

Edge cases and usage notes:

  • A GPIO cannot be driven by PWM and normal GPIO (e.g., Led) at the same time. To switch from PWM to LED control, call Enable(false) (or destroy the PwmOut) before constructing a Led on the same pin.
  • If you re-enable PWM after using the pin as GPIO, call Enable(true) or Configure(...) to reapply the PWM configuration.

Example: basic PWM output

c7222::PwmOut pwm(15);
pwm.SetPeriodUs(1000.0f); // 1 kHz
pwm.SetDutyCycle(0.25f); // 25%
pwm.SetActiveLow(false);
Output-only PWM wrapper with period and duty configuration.
Definition pwm.hpp:63

Example: release PWM and reconfigure LED on same pin

c7222::PwmOut pwm(20, {20, 2000.0f, 0.5f, true, true});
pwm.Enable(false); // return pin to GPIO
c7222::Led led(20);
led.Reconfigure(true, c7222::GpioDriveStrength::mA4, true);
Output-only GPIO wrapper intended for LEDs.
Definition led.hpp:39
void Enable(bool on)
Enable or disable the PWM output.