template<typename Derived>
class pca9685::I2cInterface< Derived >
CRTP-based template interface for I2C bus operations.
This template class provides a hardware-agnostic interface for I2C communication using the CRTP pattern. Platform-specific implementations should inherit from this template with themselves as the template parameter.
Benefits of CRTP:
- Compile-time polymorphism (no virtual function overhead)
- Static dispatch instead of dynamic dispatch
- Better optimization opportunities for the compiler
Example usage:
public:
bool Write(uint8_t addr, uint8_t reg, const uint8_t* data, size_t len) noexcept { ... }
bool Read(uint8_t addr, uint8_t reg, uint8_t* data, size_t len) noexcept { ... }
bool EnsureInitialized() noexcept { ... }
void DelayUs(uint32_t us) noexcept { ... }
};
CRTP-based template interface for I2C bus operations.
Definition pca9685_i2c_interface.hpp:77
Optional hooks, detected at compile time (a port that does not declare them still compiles):
void GpioSet(CtrlPin pin, GpioSignal signal) noexcept: drive the OE pin (see GpioSet() below). Called by PCA9685::SetOutputEnable(); the default does nothing.
void DelayUs(uint32_t us) noexcept: busy-wait or sleep for at least us microseconds. The driver calls it with 500 after clearing MODE1.SLEEP (Reset(), Wake(), SetPwmFreq()), for the oscillator start-up time of datasheet Table 5 note 2 and section 7.3.1.1. Without it the driver times the wait with 13 one-byte MODE1 reads, which is at least 500 us on any bus clock up to the chip's 1 MHz maximum, but only if each Read() really runs a bus transaction.
- Template Parameters
-
| Derived | The derived class type (CRTP pattern) |
template<typename Derived >
Ensure the I2C bus is initialized and ready for communication.
This method performs lazy initialization of the I2C bus. It should initialize the I2C hardware, configure pins, set up the bus, and verify it's ready for communication. On subsequent calls, it should return true immediately if already initialized.
- Returns
- true if initialization succeeded or was already initialized; false if initialization failed.
template<typename Derived >
Set a control pin to the specified signal state.
For CtrlPin::OE, ACTIVE means outputs enabled, which is a LOW level on the pin; INACTIVE means outputs blanked to the MODE2.OUTNE state, a HIGH level (datasheet 7.4, Table 11). The port does the polarity mapping.
- Parameters
-
| [in] | pin | Which control pin to drive (OE). |
| [in] | signal | ACTIVE to assert the pin function, INACTIVE to deassert. |
- Note
- The default implementation is a no-op, for boards where OE is tied low. Declare the same function in the derived class if the OE pin is wired to a GPIO: the driver's SetOutputEnable() and the helpers below call the derived version.