82#ifndef COMPONENT_HANDLER_PCAL95555_HANDLER_H_
83#define COMPONENT_HANDLER_PCAL95555_HANDLER_H_
90#include "base/BaseGpio.h"
91#include "base/BaseI2c.h"
92#include "core/hf-core-drivers/external/hf-pcal95555-driver/inc/pcal95555.hpp"
151 bool Write(uint8_t addr, uint8_t reg,
const uint8_t* data,
size_t len)
noexcept;
164 bool Read(uint8_t addr, uint8_t reg, uint8_t* data,
size_t len)
noexcept;
239 hf_gpio_direction_t direction = hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT,
240 hf_gpio_active_state_t active_state = hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH,
241 hf_gpio_output_mode_t output_mode = hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL,
242 hf_gpio_pull_mode_t pull_mode = hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING)
noexcept;
286 InterruptCallback callback =
nullptr,
287 void* user_data =
nullptr) noexcept override;
326 hf_gpio_err_t
SetDirectionImpl(hf_gpio_direction_t direction) noexcept override;
332 hf_gpio_err_t
SetPullModeImpl(hf_gpio_pull_mode_t mode) noexcept override;
338 hf_gpio_err_t
SetPinLevelImpl(hf_gpio_level_t level) noexcept override;
341 hf_gpio_err_t
GetPinLevelImpl(hf_gpio_level_t& level) noexcept override;
344 hf_gpio_err_t
GetDirectionImpl(hf_gpio_direction_t& direction) const noexcept override;
347 hf_gpio_err_t
GetOutputModeImpl(hf_gpio_output_mode_t& mode) const noexcept override;
364 hf_gpio_interrupt_trigger_t::HF_GPIO_INTERRUPT_TRIGGER_NONE;
438 explicit Pcal95555Handler(BaseI2c& i2c_device, BaseGpio* interrupt_pin =
nullptr) noexcept;
501 hf_gpio_err_t
SetDirection(uint8_t pin, hf_gpio_direction_t direction)
noexcept;
509 hf_gpio_err_t
SetOutput(uint8_t pin,
bool active)
noexcept;
517 hf_gpio_err_t
ReadInput(uint8_t pin,
bool& active)
noexcept;
524 hf_gpio_err_t
Toggle(uint8_t pin)
noexcept;
540 hf_gpio_err_t
SetPullMode(uint8_t pin, hf_gpio_pull_mode_t pull_mode)
noexcept;
548 hf_gpio_err_t
GetPullMode(uint8_t pin, hf_gpio_pull_mode_t& pull_mode)
noexcept;
564 hf_gpio_err_t
SetDirections(uint16_t pin_mask, hf_gpio_direction_t direction)
noexcept;
572 hf_gpio_err_t
SetOutputs(uint16_t pin_mask,
bool active)
noexcept;
581 hf_gpio_err_t
SetPullModes(uint16_t pin_mask, hf_gpio_pull_mode_t pull_mode)
noexcept;
641 static constexpr uint8_t
PinCount() noexcept {
return 16; }
666 hf_gpio_direction_t direction = hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT,
667 hf_gpio_active_state_t active_state = hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH,
668 hf_gpio_output_mode_t output_mode = hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL,
669 hf_gpio_pull_mode_t pull_mode = hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING,
670 bool allow_existing = true) noexcept;
677 std::shared_ptr<BaseGpio>
GetGpioPin(hf_pin_num_t pin) noexcept;
746 hf_gpio_err_t
SetDriveStrength(hf_pin_num_t pin, DriveStrength level) noexcept;
764 hf_gpio_err_t
SetOutputMode(
bool port0_open_drain,
bool port1_open_drain) noexcept;
846 hf_gpio_interrupt_trigger_t trigger,
847 InterruptCallback callback,
848 void* user_data)
noexcept;
870 hf_gpio_interrupt_trigger_t trigger,
871 void* user_data) noexcept;
Concrete I2C communication adapter for PCAL95555 using BaseI2c.
Definition Pcal95555Handler.h:126
bool Read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) noexcept
Read data from a device register.
Definition Pcal95555Handler.cpp:56
bool Write(uint8_t addr, uint8_t reg, const uint8_t *data, size_t len) noexcept
Write data to a device register.
Definition Pcal95555Handler.cpp:34
std::function< void()> interrupt_handler_
Stored interrupt handler from driver.
Definition Pcal95555Handler.h:195
bool RegisterInterruptHandler(std::function< void()> handler) noexcept
Register an interrupt handler to be called when the INT pin fires.
Definition Pcal95555Handler.cpp:71
RtosMutex i2c_mutex_
Thread safety for I2C operations.
Definition Pcal95555Handler.h:194
HalI2cPcal95555Comm(BaseI2c &i2c_device) noexcept
Construct the I2C communication adapter.
Definition Pcal95555Handler.cpp:31
BaseI2c & i2c_device_
I2C device interface (not owned).
Definition Pcal95555Handler.h:193
bool EnsureInitialized() noexcept
Ensure the I2C bus is initialized and ready.
Definition Pcal95555Handler.cpp:67
const std::function< void()> & GetInterruptHandler() const noexcept
Get the stored interrupt handler (if any).
Definition Pcal95555Handler.h:190
BaseGpio adapter for a single PCAL95555 GPIO expander pin.
Definition Pcal95555Handler.h:226
hf_gpio_err_t SetPullModeImpl(hf_gpio_pull_mode_t mode) noexcept override
Set pull mode (floating/up/down). PCAL9555A only.
Definition Pcal95555Handler.cpp:970
hf_gpio_err_t SetPolarityInversion(bool invert) noexcept
Set input polarity inversion for this pin.
Definition Pcal95555Handler.cpp:939
~Pcal95555GpioPin() noexcept override=default
Default destructor.
bool Deinitialize() noexcept override
Deinitialize (marks pin as uninitialized; expander state persists).
Definition Pcal95555Handler.cpp:909
hf_gpio_err_t SetOutputModeImpl(hf_gpio_output_mode_t mode) noexcept override
Returns GPIO_ERR_UNSUPPORTED_OPERATION (use Pcal95555Handler::SetOutputMode for per-port control).
Definition Pcal95555Handler.cpp:962
void * interrupt_user_data_
Definition Pcal95555Handler.h:362
hf_gpio_err_t SetPinLevelImpl(hf_gpio_level_t level) noexcept override
Write pin level via the PCAL95555 driver.
Definition Pcal95555Handler.cpp:988
InterruptCallback interrupt_callback_
Definition Pcal95555Handler.h:361
bool IsPinAvailable() const noexcept override
Check if this pin number is valid (0-15).
Definition Pcal95555Handler.cpp:914
bool Initialize() noexcept override
Initialize the pin (configures direction and pull mode on the expander).
Definition Pcal95555Handler.cpp:888
hf_pin_num_t pin_
Pin number (0-15).
Definition Pcal95555Handler.h:355
hf_gpio_err_t SupportsInterrupts() const noexcept override
Check if this pin supports interrupt configuration.
Definition Pcal95555Handler.cpp:918
char description_[32]
Human-readable description.
Definition Pcal95555Handler.h:357
hf_gpio_err_t GetInterruptStatus(bool &status) noexcept
Get the interrupt status for this pin.
Definition Pcal95555Handler.cpp:949
hf_gpio_pull_mode_t GetPullModeImpl() const noexcept override
Get current pull mode (tracked internally).
Definition Pcal95555Handler.cpp:976
bool interrupt_enabled_
Definition Pcal95555Handler.h:365
Pcal95555GpioPin(hf_pin_num_t pin, Pcal95555Handler *parent_handler, hf_gpio_direction_t direction=hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT, hf_gpio_active_state_t active_state=hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH, hf_gpio_output_mode_t output_mode=hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL, hf_gpio_pull_mode_t pull_mode=hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING) noexcept
Construct a PCAL95555 GPIO pin wrapper.
Definition Pcal95555Handler.cpp:874
hf_gpio_err_t SetInterruptMask(bool mask) noexcept
Set the interrupt mask for this pin.
Definition Pcal95555Handler.cpp:944
hf_gpio_err_t GetDirectionImpl(hf_gpio_direction_t &direction) const noexcept override
Get pin direction from the PCAL95555 driver.
Definition Pcal95555Handler.cpp:1007
Pcal95555Handler * parent_handler_
Owning handler (not owned).
Definition Pcal95555Handler.h:356
hf_gpio_err_t SetDirectionImpl(hf_gpio_direction_t direction) noexcept override
Set pin direction via the PCAL95555 driver.
Definition Pcal95555Handler.cpp:956
hf_gpio_err_t ConfigureInterrupt(hf_gpio_interrupt_trigger_t trigger, InterruptCallback callback=nullptr, void *user_data=nullptr) noexcept override
Configure interrupt for this pin.
Definition Pcal95555Handler.cpp:929
const char * GetDescription() const noexcept override
Returns a description string like "PCAL95555_PIN_5".
Definition Pcal95555Handler.cpp:925
hf_gpio_err_t GetOutputModeImpl(hf_gpio_output_mode_t &mode) const noexcept override
Get output mode (tracked internally).
Definition Pcal95555Handler.cpp:1014
hf_gpio_interrupt_trigger_t interrupt_trigger_
Definition Pcal95555Handler.h:363
hf_gpio_err_t GetPinLevelImpl(hf_gpio_level_t &level) noexcept override
Read pin level via the PCAL95555 driver.
Definition Pcal95555Handler.cpp:995
hf_u8_t GetMaxPins() const noexcept override
Returns 16 (total pins on the expander).
Definition Pcal95555Handler.h:260
Unified, non-templated handler for a single PCA9555 / PCAL9555A device.
Definition Pcal95555Handler.h:410
uint8_t GetI2cAddress() const noexcept
Get the I2C address of the underlying device.
Definition Pcal95555Handler.cpp:608
hf_gpio_err_t GetInterruptStatus(hf_pin_num_t pin, bool &status) noexcept
Get interrupt status for a single pin.
Definition Pcal95555Handler.cpp:702
hf_gpio_err_t GetPullMode(uint8_t pin, hf_gpio_pull_mode_t &pull_mode) noexcept
Get the pull resistor mode for a single pin (tracked internally).
Definition Pcal95555Handler.cpp:300
std::unique_ptr< Pcal95555Driver > pcal95555_driver_
Typed driver (created in Initialize).
Definition Pcal95555Handler.h:900
uint16_t GetCreatedPinMask() const noexcept
Get a bitmask of all created pin numbers.
Definition Pcal95555Handler.cpp:655
pcal95555::ChipVariant GetChipVariant() const noexcept
Get the detected chip variant.
Definition Pcal95555Handler.cpp:674
hf_gpio_err_t Deinitialize() noexcept
Internal deinitialization.
Definition Pcal95555Handler.cpp:184
bool EnsureInitialized() noexcept
Ensure the handler is initialized (lazy initialization).
Definition Pcal95555Handler.cpp:93
std::unique_ptr< HalI2cPcal95555Comm > i2c_adapter_
I2C adapter (created in Initialize).
Definition Pcal95555Handler.h:899
bool IsPinCreated(hf_pin_num_t pin) const noexcept
Check if a pin wrapper has been created.
Definition Pcal95555Handler.cpp:649
bool DrainPendingInterrupts() noexcept
Drain any pending interrupt that was deferred from ISR context.
Definition Pcal95555Handler.cpp:526
bool EnsureDeinitialized() noexcept
Deinitialize the handler and release resources.
Definition Pcal95555Handler.cpp:101
hf_gpio_err_t SetPolarityInversion(hf_pin_num_t pin, bool invert) noexcept
Set input polarity inversion for a pin.
Definition Pcal95555Handler.cpp:680
hf_gpio_err_t ReadInput(uint8_t pin, bool &active) noexcept
Read the logical level of an input pin.
Definition Pcal95555Handler.cpp:236
hf_gpio_err_t SetDriveStrength(hf_pin_num_t pin, DriveStrength level) noexcept
Set output drive strength for a pin.
Definition Pcal95555Handler.cpp:712
BaseI2c & i2c_device_
I2C device reference (not owned).
Definition Pcal95555Handler.h:898
bool interrupt_configured_
Whether hardware interrupt is active.
Definition Pcal95555Handler.h:913
uint16_t prev_input_state_
Last-read pin input levels (bitmask).
Definition Pcal95555Handler.h:926
hf_gpio_err_t SetOutput(uint8_t pin, bool active) noexcept
Write a logical level to an output pin.
Definition Pcal95555Handler.cpp:226
hf_gpio_err_t EnableInputLatch(hf_pin_num_t pin, bool enable) noexcept
Enable or disable input latch for a pin.
Definition Pcal95555Handler.cpp:723
hf_gpio_err_t UnregisterPinInterrupt(hf_pin_num_t pin) noexcept
Unregister a per-pin interrupt callback.
Definition Pcal95555Handler.cpp:473
uint16_t GetErrorFlags() const noexcept
Get the driver's error flags.
Definition Pcal95555Handler.cpp:756
bool HasInterruptSupport() const noexcept
Check if hardware interrupt support is available.
Definition Pcal95555Handler.h:594
hf_gpio_err_t RegisterPinInterrupt(hf_pin_num_t pin, hf_gpio_interrupt_trigger_t trigger, InterruptCallback callback, void *user_data) noexcept
Register a per-pin interrupt callback (called by Pcal95555GpioPin).
Definition Pcal95555Handler.cpp:416
hf_gpio_err_t Initialize() noexcept
Internal initialization (called by EnsureInitialized under mutex).
Definition Pcal95555Handler.cpp:115
hf_gpio_err_t SetDirection(uint8_t pin, hf_gpio_direction_t direction) noexcept
Set the direction of a single pin.
Definition Pcal95555Handler.cpp:212
std::shared_ptr< BaseGpio > CreateGpioPin(hf_pin_num_t pin, hf_gpio_direction_t direction=hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT, hf_gpio_active_state_t active_state=hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH, hf_gpio_output_mode_t output_mode=hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL, hf_gpio_pull_mode_t pull_mode=hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING, bool allow_existing=true) noexcept
Create or retrieve an existing BaseGpio pin wrapper.
Definition Pcal95555Handler.cpp:612
BaseGpio * interrupt_pin_
Hardware INT pin (optional, not owned).
Definition Pcal95555Handler.h:912
hf_gpio_err_t GetAllInterruptMasks(uint16_t &mask) noexcept
Get the interrupt mask for all 16 pins.
Definition Pcal95555Handler.cpp:386
hf_gpio_err_t SetDirections(uint16_t pin_mask, hf_gpio_direction_t direction) noexcept
Set direction for multiple pins at once.
Definition Pcal95555Handler.cpp:314
Pcal95555Driver * GetDriver() noexcept
Get the underlying PCAL95555 driver for advanced register-level operations.
Definition Pcal95555Handler.cpp:593
bool IsInitialized() const noexcept
Check if the handler has been initialized.
Definition Pcal95555Handler.h:485
hf_gpio_err_t SetInterruptMask(hf_pin_num_t pin, bool mask) noexcept
Set the per-pin interrupt mask.
Definition Pcal95555Handler.cpp:691
hf_gpio_err_t SetOutputs(uint16_t pin_mask, bool active) noexcept
Write output level for multiple pins at once.
Definition Pcal95555Handler.cpp:327
hf_gpio_err_t SetPullModes(uint16_t pin_mask, hf_gpio_pull_mode_t pull_mode) noexcept
Set pull mode for multiple pins at once.
Definition Pcal95555Handler.cpp:341
std::array< std::shared_ptr< Pcal95555GpioPin >, 16 > pin_registry_
Created pin wrappers.
Definition Pcal95555Handler.h:907
pcal95555::PCAL95555< HalI2cPcal95555Comm > Pcal95555Driver
Typed PCAL95555 driver using our I2C adapter.
Definition Pcal95555Handler.h:418
void ProcessInterrupts() noexcept
Process pending interrupts by reading status and dispatching callbacks.
Definition Pcal95555Handler.cpp:536
bool EnsureInitializedLocked() noexcept
Check init state under already-held handler_mutex_.
Definition Pcal95555Handler.cpp:110
void DumpDiagnostics() const noexcept
Dump comprehensive diagnostics to the system log.
Definition Pcal95555Handler.cpp:770
bool ValidatePin(uint8_t pin) const noexcept
Validate pin number is in range 0-15.
Definition Pcal95555Handler.h:835
static constexpr uint8_t PinCount() noexcept
Total number of GPIO pins on the expander.
Definition Pcal95555Handler.h:641
static void HardwareInterruptCallback(BaseGpio *gpio, hf_gpio_interrupt_trigger_t trigger, void *user_data) noexcept
Static ISR callback for the hardware interrupt pin.
Definition Pcal95555Handler.cpp:515
bool HasAgileIO() const noexcept
Check if the detected chip supports Agile I/O features.
Definition Pcal95555Handler.cpp:670
std::atomic< bool > interrupt_pending_
Deferred interrupt flag (set in ISR, cleared in task context).
Definition Pcal95555Handler.h:914
std::array< hf_gpio_pull_mode_t, 16 > pull_mode_cache_
Definition Pcal95555Handler.h:920
hf_gpio_err_t SetPullMode(uint8_t pin, hf_gpio_pull_mode_t pull_mode) noexcept
Set the pull resistor mode for a single pin.
Definition Pcal95555Handler.cpp:261
hf_gpio_err_t SetOutputMode(bool port0_open_drain, bool port1_open_drain) noexcept
Configure per-port output mode (push-pull or open-drain).
Definition Pcal95555Handler.cpp:733
hf_gpio_err_t ResetToDefault() noexcept
Reset all registers to power-on default state.
Definition Pcal95555Handler.cpp:743
RtosMutex handler_mutex_
Thread safety for handler operations.
Definition Pcal95555Handler.h:902
hf_gpio_err_t Toggle(uint8_t pin) noexcept
Toggle the output state of a pin.
Definition Pcal95555Handler.cpp:251
bool IsInterruptConfigured() const noexcept
Check if hardware interrupt is configured and enabled.
Definition Pcal95555Handler.h:600
hf_gpio_err_t ConfigureHardwareInterrupt() noexcept
Configure the hardware interrupt pin (falling edge, active-low).
Definition Pcal95555Handler.cpp:500
std::shared_ptr< BaseGpio > GetGpioPin(hf_pin_num_t pin) noexcept
Get an existing GPIO pin wrapper by number.
Definition Pcal95555Handler.cpp:643
hf_gpio_err_t GetAllInterruptStatus(uint16_t &status) noexcept
Get the interrupt status for all 16 pins.
Definition Pcal95555Handler.cpp:403
Pcal95555Handler(BaseI2c &i2c_device, BaseGpio *interrupt_pin=nullptr) noexcept
Construct a handler for a PCAL95555 device.
Definition Pcal95555Handler.cpp:81
bool initialized_
Initialization state.
Definition Pcal95555Handler.h:901
void ClearErrorFlags(uint16_t mask=0xFFFF) noexcept
Clear driver error flags.
Definition Pcal95555Handler.cpp:760