86#ifndef COMPONENT_HANDLER_PCA9685_HANDLER_H_
87#define COMPONENT_HANDLER_PCA9685_HANDLER_H_
93#include "base/BaseGpio.h"
94#include "base/BasePwm.h"
95#include "base/BaseI2c.h"
96#include "core/hf-core-drivers/external/hf-pca9685-driver/inc/pca9685.hpp"
154 bool Write(uint8_t addr, uint8_t reg,
const uint8_t* data,
size_t len)
noexcept;
167 bool Read(uint8_t addr, uint8_t reg, uint8_t* data,
size_t len)
noexcept;
254 hf_pwm_err_t
EnableChannel(hf_channel_id_t channel_id) noexcept override;
261 hf_pwm_err_t
DisableChannel(hf_channel_id_t channel_id) noexcept override;
277 float duty_cycle) noexcept override;
286 hf_u32_t raw_value) noexcept override;
300 hf_frequency_hz_t frequency_hz) noexcept override;
314 float phase_shift_degrees) noexcept override;
320 hf_pwm_err_t
StartAll() noexcept override;
326 hf_pwm_err_t
StopAll() noexcept override;
332 hf_pwm_err_t
UpdateAll() noexcept override;
339 hf_channel_id_t complementary_channel,
340 hf_u32_t deadtime_ns) noexcept override;
347 float GetDutyCycle(hf_channel_id_t channel_id) const noexcept override;
354 hf_frequency_hz_t
GetFrequency(hf_channel_id_t channel_id) const noexcept override;
418 hf_gpio_active_state_t active_state =
419 hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH)
noexcept;
452 hf_gpio_err_t
SetDirectionImpl(hf_gpio_direction_t direction) noexcept override;
455 hf_gpio_err_t
GetDirectionImpl(hf_gpio_direction_t& direction) const noexcept override;
461 hf_gpio_err_t
GetOutputModeImpl(hf_gpio_output_mode_t& mode) const noexcept override;
464 hf_gpio_err_t
SetPullModeImpl(hf_gpio_pull_mode_t mode) noexcept override;
474 hf_gpio_err_t
SetPinLevelImpl(hf_gpio_level_t level) noexcept override;
479 hf_gpio_err_t
GetPinLevelImpl(hf_gpio_level_t& level) noexcept override;
490 hf_gpio_level_t::HF_GPIO_LEVEL_LOW;
639 hf_pin_num_t channel,
640 hf_gpio_active_state_t active_state =
641 hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH,
642 bool allow_existing = true) noexcept;
649 std::shared_ptr<BaseGpio>
GetGpioPin(hf_pin_num_t channel) noexcept;
728 bool SetDuty(uint8_t channel,
float duty)
noexcept;
729 bool SetPwm(uint8_t channel, uint16_t on_time, uint16_t off_time)
noexcept;
730 bool SetAllPwm(uint16_t on_time, uint16_t off_time)
noexcept;
733 bool Sleep() noexcept;
734 bool Wake() noexcept;
Concrete I2C communication adapter for PCA9685 using BaseI2c.
Definition Pca9685Handler.h:130
RtosMutex i2c_mutex_
Thread safety for I2C operations.
Definition Pca9685Handler.h:179
bool Read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) noexcept
Read data from a device register.
Definition Pca9685Handler.cpp:57
BaseI2c & i2c_device_
I2C device interface (not owned).
Definition Pca9685Handler.h:178
bool EnsureInitialized() noexcept
Ensure the I2C bus is initialized and ready.
Definition Pca9685Handler.cpp:68
HalI2cPca9685Comm(BaseI2c &i2c_device) noexcept
Construct the I2C communication adapter.
Definition Pca9685Handler.cpp:32
bool Write(uint8_t addr, uint8_t reg, const uint8_t *data, size_t len) noexcept
Write data to a device register.
Definition Pca9685Handler.cpp:35
BaseGpio adapter for a single PCA9685 channel used as a digital output.
Definition Pca9685Handler.h:408
hf_pin_num_t pin_
Channel number (0-15).
Definition Pca9685Handler.h:486
hf_gpio_err_t SetPinLevelImpl(hf_gpio_level_t level) noexcept override
Write pin level via the PCA9685 driver.
Definition Pca9685Handler.cpp:664
hf_gpio_level_t cached_level_
Cached output level.
Definition Pca9685Handler.h:489
Pca9685Handler * parent_handler_
Owning handler (not owned).
Definition Pca9685Handler.h:487
bool IsPinAvailable() const noexcept override
Check if this channel number is valid (0-15).
Definition Pca9685Handler.cpp:616
Pca9685GpioPin(hf_pin_num_t pin, Pca9685Handler *parent_handler, hf_gpio_active_state_t active_state=hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH) noexcept
Construct a PCA9685 GPIO pin wrapper.
Definition Pca9685Handler.cpp:583
hf_gpio_pull_mode_t GetPullModeImpl() const noexcept override
Returns FLOATING (no pull resistors on PCA9685).
Definition Pca9685Handler.cpp:660
char description_[32]
Human-readable description.
Definition Pca9685Handler.h:488
const char * GetDescription() const noexcept override
Returns a description string like "PCA9685_CH_5".
Definition Pca9685Handler.cpp:620
hf_gpio_err_t GetDirectionImpl(hf_gpio_direction_t &direction) const noexcept override
Get direction (always OUTPUT).
Definition Pca9685Handler.cpp:635
bool Initialize() noexcept override
Initialize the GPIO pin (sets channel to full-off by default).
Definition Pca9685Handler.cpp:598
hf_gpio_err_t GetPinLevelImpl(hf_gpio_level_t &level) noexcept override
Read pin level (returns cached state since PCA9685 is output-only).
Definition Pca9685Handler.cpp:682
hf_gpio_err_t GetOutputModeImpl(hf_gpio_output_mode_t &mode) const noexcept override
Get output mode (tracked internally).
Definition Pca9685Handler.cpp:648
hf_gpio_err_t SetDirectionImpl(hf_gpio_direction_t direction) noexcept override
Set pin direction. Only OUTPUT is supported.
Definition Pca9685Handler.cpp:626
~Pca9685GpioPin() noexcept override=default
Default destructor.
hf_gpio_err_t SetPullModeImpl(hf_gpio_pull_mode_t mode) noexcept override
Not supported – returns GPIO_ERR_UNSUPPORTED_OPERATION.
Definition Pca9685Handler.cpp:654
hf_u8_t GetMaxPins() const noexcept override
Returns 16 (total channels on the PCA9685).
Definition Pca9685Handler.h:437
hf_gpio_err_t SetOutputModeImpl(hf_gpio_output_mode_t mode) noexcept override
Set output mode. Only push-pull is natively supported (global setting).
Definition Pca9685Handler.cpp:641
bool Deinitialize() noexcept override
Deinitialize (marks as uninitialized; channel state persists).
Definition Pca9685Handler.cpp:611
Unified, non-templated handler for a single PCA9685 PWM controller.
Definition Pca9685Handler.h:529
bool EnsureInitialized() noexcept
Ensure the handler is initialized (lazy initialization).
Definition Pca9685Handler.cpp:85
bool SetDuty(uint8_t channel, float duty) noexcept
Definition Pca9685Handler.cpp:166
bool initialized_
Initialization state.
Definition Pca9685Handler.h:747
bool SetAllPwm(uint16_t on_time, uint16_t off_time) noexcept
Definition Pca9685Handler.cpp:181
std::unique_ptr< HalI2cPca9685Comm > i2c_adapter_
I2C adapter (created in init).
Definition Pca9685Handler.h:745
std::shared_ptr< BasePwm > GetPwmAdapter() noexcept
Get or create the BasePwm adapter for all 16 channels.
Definition Pca9685Handler.cpp:243
std::shared_ptr< BaseGpio > CreateGpioPin(hf_pin_num_t channel, hf_gpio_active_state_t active_state=hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH, bool allow_existing=true) noexcept
Create or retrieve a BaseGpio pin wrapper for a channel.
Definition Pca9685Handler.cpp:256
bool SetChannelFullOff(uint8_t channel) noexcept
Definition Pca9685Handler.cpp:194
bool SetOutputInvert(bool invert) noexcept
Definition Pca9685Handler.cpp:221
std::shared_ptr< Pca9685PwmAdapter > pwm_adapter_
PWM adapter (lazy).
Definition Pca9685Handler.h:753
std::array< std::shared_ptr< Pca9685GpioPin >, 16 > gpio_registry_
GPIO pin registry.
Definition Pca9685Handler.h:754
RtosMutex handler_mutex_
Thread safety for all operations.
Definition Pca9685Handler.h:748
std::shared_ptr< BaseGpio > GetGpioPin(hf_pin_num_t channel) noexcept
Get an existing GPIO pin wrapper by channel number.
Definition Pca9685Handler.cpp:280
Pca9685Handler(BaseI2c &i2c_device) noexcept
Construct a handler for a PCA9685 device.
Definition Pca9685Handler.cpp:76
void ClearErrorFlags(uint16_t mask=0xFFFF) noexcept
Clear driver error flags.
Definition Pca9685Handler.cpp:300
static constexpr uint8_t ChannelCount() noexcept
Total number of PWM channels.
Definition Pca9685Handler.h:608
bool validateChannel(uint8_t channel) const noexcept
Validate channel number (0-15).
Definition Pca9685Handler.h:724
hf_pwm_err_t deinitializeInternal() noexcept
Internal deinitialization.
Definition Pca9685Handler.cpp:138
Pca9685Driver * GetDriver() noexcept
Get the underlying PCA9685 driver for advanced register-level operations.
Definition Pca9685Handler.cpp:369
bool Sleep() noexcept
Definition Pca9685Handler.cpp:205
pca9685::PCA9685< HalI2cPca9685Comm > Pca9685Driver
Typed PCA9685 driver using our I2C adapter.
Definition Pca9685Handler.h:537
bool SetPwm(uint8_t channel, uint16_t on_time, uint16_t off_time) noexcept
Definition Pca9685Handler.cpp:173
bool SetOutputDriverMode(bool totem_pole) noexcept
Definition Pca9685Handler.cpp:227
bool IsPinCreated(hf_pin_num_t channel) const noexcept
Check if a GPIO pin wrapper has been created for a channel.
Definition Pca9685Handler.cpp:286
std::unique_ptr< Pca9685Driver > pca9685_driver_
Typed driver (created in init).
Definition Pca9685Handler.h:746
hf_pwm_err_t initializeInternal() noexcept
Internal initialization (called under mutex).
Definition Pca9685Handler.cpp:106
uint8_t GetI2cAddress() const noexcept
Get the I2C address of the underlying device.
Definition Pca9685Handler.cpp:237
bool ensureInitializedLocked() noexcept
Check init under already-held handler_mutex_.
Definition Pca9685Handler.cpp:101
bool Wake() noexcept
Definition Pca9685Handler.cpp:211
bool EnsureDeinitialized() noexcept
Deinitialize the handler and release resources.
Definition Pca9685Handler.cpp:93
uint16_t GetErrorFlags() const noexcept
Get the driver's error flags.
Definition Pca9685Handler.cpp:296
~Pca9685Handler() noexcept=default
Destructor. Releases driver, adapter, and all wrappers.
bool SetFrequency(float freq_hz) noexcept
Definition Pca9685Handler.cpp:160
bool SetChannelFullOn(uint8_t channel) noexcept
Definition Pca9685Handler.cpp:187
void DumpDiagnostics() const noexcept
Dump comprehensive diagnostics to the system log.
Definition Pca9685Handler.cpp:310
BaseI2c & i2c_device_
I2C device reference (not owned).
Definition Pca9685Handler.h:744
bool IsInitialized() const noexcept
Check if the handler has been initialized.
Definition Pca9685Handler.h:597
BasePwm adapter for all 16 PCA9685 PWM channels.
Definition Pca9685Handler.h:214
hf_pwm_err_t SetComplementaryOutput(hf_channel_id_t primary_channel, hf_channel_id_t complementary_channel, hf_u32_t deadtime_ns) noexcept override
Not supported by PCA9685.
Definition Pca9685Handler.cpp:562
std::array< uint16_t, kMaxChannels > on_time_cache_
Cached on-time values (phase offset).
Definition Pca9685Handler.h:367
hf_pwm_err_t EnableChannel(hf_channel_id_t channel_id) noexcept override
Enable a PWM channel (restore its duty cycle from cache).
Definition Pca9685Handler.cpp:410
hf_pwm_err_t UpdateAll() noexcept override
No-op (PCA9685 updates immediately on register write).
Definition Pca9685Handler.cpp:557
static constexpr uint32_t kMinFrequencyHz
Minimum PWM frequency in Hz.
Definition Pca9685Handler.h:226
hf_pwm_err_t SetDutyCycle(hf_channel_id_t channel_id, float duty_cycle) noexcept override
Set duty cycle for a channel (0.0-1.0).
Definition Pca9685Handler.cpp:449
bool validateChannel(hf_channel_id_t channel_id) const noexcept
Validate channel ID is in range 0-15.
Definition Pca9685Handler.h:374
hf_pwm_err_t Initialize() noexcept override
Initialize the PWM adapter (ensures parent handler is initialized).
Definition Pca9685Handler.cpp:391
static constexpr uint16_t kMaxRawValue
Maximum raw PWM value (2^12 - 1 = 4095).
Definition Pca9685Handler.h:223
bool IsChannelEnabled(hf_channel_id_t channel_id) const noexcept override
Check if a channel is enabled.
Definition Pca9685Handler.cpp:444
std::array< float, kMaxChannels > duty_cache_
Cached duty cycles (0.0-1.0).
Definition Pca9685Handler.h:366
hf_pwm_err_t SetPhaseShift(hf_channel_id_t channel_id, float phase_shift_degrees) noexcept override
Set phase shift for a channel (via PCA9685 on-time offset).
Definition Pca9685Handler.cpp:514
Pca9685Handler * parent_handler_
Owning handler (not owned).
Definition Pca9685Handler.h:362
hf_pwm_err_t StopAll() noexcept override
Put the PCA9685 into sleep mode (stop all outputs).
Definition Pca9685Handler.cpp:550
hf_pwm_err_t SetFrequency(hf_channel_id_t channel_id, hf_frequency_hz_t frequency_hz) noexcept override
Set PWM frequency (global – affects ALL channels).
Definition Pca9685Handler.cpp:498
static constexpr uint32_t kMaxFrequencyHz
Maximum PWM frequency in Hz.
Definition Pca9685Handler.h:229
hf_pwm_err_t StartAll() noexcept override
Wake the PCA9685 from sleep (start all channels).
Definition Pca9685Handler.cpp:543
hf_frequency_hz_t GetFrequency(hf_channel_id_t channel_id) const noexcept override
Get the current PWM frequency (global).
Definition Pca9685Handler.cpp:574
~Pca9685PwmAdapter() noexcept override=default
Default destructor.
static constexpr uint8_t kResolutionBits
PWM resolution in bits.
Definition Pca9685Handler.h:220
hf_pwm_err_t Deinitialize() noexcept override
Deinitialize (marks adapter as uninitialized).
Definition Pca9685Handler.cpp:402
float GetDutyCycle(hf_channel_id_t channel_id) const noexcept override
Get the cached duty cycle for a channel.
Definition Pca9685Handler.cpp:569
hf_pwm_err_t DisableChannel(hf_channel_id_t channel_id) noexcept override
Disable a PWM channel (set to full-off).
Definition Pca9685Handler.cpp:431
std::array< bool, kMaxChannels > channel_enabled_
Channel enable state.
Definition Pca9685Handler.h:368
hf_frequency_hz_t current_frequency_hz_
Current global frequency.
Definition Pca9685Handler.h:371
hf_pwm_err_t SetDutyCycleRaw(hf_channel_id_t channel_id, hf_u32_t raw_value) noexcept override
Set raw PWM tick value for a channel.
Definition Pca9685Handler.cpp:480
static constexpr uint8_t kMaxChannels
Number of PWM channels on the PCA9685.
Definition Pca9685Handler.h:217
Pca9685PwmAdapter(Pca9685Handler *parent_handler) noexcept
Construct the PWM adapter.
Definition Pca9685Handler.cpp:384