This guide provides complete, working examples demonstrating various use cases for the PCAL95555 driver.
Example 1: Basic GPIO Control
This example shows basic input/output operations.
#include "pcal95555.hpp"
class MyI2c : public pcal95555::I2cInterface<MyI2c> {
};
MyI2c i2c;
pcal95555::PCAL95555<MyI2c> gpio(&i2c, 0x20);
void app_main(void) {
if (!gpio.EnsureInitialized()) {
ESP_LOGE("APP", "Failed to initialize PCAL95555");
return;
}
gpio.ResetToDefault();
gpio.SetPinDirection(0, pcal95555::PCAL95555<MyI2c>::GPIODir::Output);
gpio.WritePin(0, true);
gpio.SetPinDirection(1, pcal95555::PCAL95555<MyI2c>::GPIODir::Input);
bool value = gpio.ReadPin(1);
}
Explanation
- Reset: Put device in known state
- Configure: Set pin directions
- Use: Read and write pin states
Example 3: Interrupt Handling
This example shows how to use interrupts with per-pin callbacks.
gpio.SetPinDirection(8, pcal95555::PCAL95555<MyI2c>::GPIODir::Input);
gpio.SetPullEnable(8, true);
gpio.SetPullDirection(8, true);
gpio.EnableInputLatch(8, true);
gpio.ConfigureInterrupt(8, InterruptState::Enabled);
gpio.RegisterPinInterrupt(8, InterruptEdge::Rising, [](uint16_t pin, bool state) {
printf("Pin %d interrupt: went HIGH\n", pin);
});
gpio.RegisterInterruptHandler();
gpio.HandleInterrupt();
Configure Multiple Interrupts
gpio.ConfigureInterrupts({
{0, InterruptState::Enabled},
{5, InterruptState::Enabled},
{10, InterruptState::Enabled},
{3, InterruptState::Disabled}
});
Global Interrupt Callback
gpio.SetInterruptCallback([](uint16_t status) {
printf("Interrupt on pins: 0x%04X\n", status);
});
Example 5: Drive Strength (PCAL9555A only)
This example shows drive strength configuration. Requires PCAL9555A.
if (gpio.HasAgileIO()) {
gpio.SetPinDirection(0, pcal95555::PCAL95555<MyI2c>::GPIODir::Output);
gpio.SetDriveStrength(0, pcal95555::PCAL95555<MyI2c>::DriveStrength::Level1);
}
Example 8: Chip Variant Detection
This example shows how to check which chip is connected and conditionally use features.
gpio.EnsureInitialized();
if (gpio.HasAgileIO()) {
printf("PCAL9555A detected -- full feature set available\n");
gpio.SetPullEnable(0, true);
gpio.SetDriveStrength(0, DriveStrength::Level3);
} else {
printf("PCA9555 detected -- standard GPIO only\n");
}
auto variant = gpio.GetChipVariant();
switch (variant) {
case pcal95555::ChipVariant::PCA9555:
printf("Standard PCA9555\n");
break;
case pcal95555::ChipVariant::PCAL9555A:
printf("Enhanced PCAL9555A with Agile I/O\n");
break;
default:
printf("Unknown variant (detection may have failed)\n");
break;
}
Running the Examples
ESP32
Two example applications are available in examples/esp32:
cd examples/esp32
# Comprehensive test suite (tests all 43 API methods)
./scripts/build_app.sh pcal95555_comprehensive_test Debug
./scripts/flash_app.sh flash_monitor pcal95555_comprehensive_test Debug
# LED animation demo (16-LED patterns)
./scripts/build_app.sh pcal95555_led_animation Debug
./scripts/flash_app.sh flash_monitor pcal95555_led_animation Debug
Other Platforms
Adapt the I2C interface implementation for your platform (see Platform Integration) and compile with your platform's toolchain.
Next Steps