|
| enum class | Error : uint16_t {
None = 0
, I2cWrite = 1 << 0
, I2cRead = 1 << 1
, InvalidParam = 1 << 2
,
DeviceNotFound = 1 << 3
, NotInitialized = 1 << 4
, OutOfRange = 1 << 5
} |
| | Error codes for PCA9685 operations (bitmask flags). More...
|
| |
| enum class | Register : uint8_t {
MODE1 = 0x00
, MODE2 = 0x01
, SUBADR1 = 0x02
, SUBADR2 = 0x03
,
SUBADR3 = 0x04
, ALLCALLADR = 0x05
, LED0_ON_L = 0x06
, LED0_ON_H = 0x07
,
LED0_OFF_L = 0x08
, LED0_OFF_H
, ALL_LED_ON_L = 0xFA
, ALL_LED_ON_H = 0xFB
,
ALL_LED_OFF_L = 0xFC
, ALL_LED_OFF_H = 0xFD
, PRE_SCALE = 0xFE
, TESTMODE = 0xFF
} |
| | PCA9685 register map (I2C register addresses). More...
|
| |
| using | RetryDelayFn = void(*)() |
| | Type of optional callback invoked between I2C retry attempts.
|
| |
|
| | PCA9685 (I2cType *bus, uint8_t address) |
| | Construct a new PCA9685 driver instance.
|
| |
| bool | EnsureInitialized () noexcept |
| | Ensure the driver and I2C bus are initialized (lazy initialization).
|
| |
| bool | IsInitialized () const noexcept |
| | Check if the driver has been initialized.
|
| |
| bool | Reset () noexcept |
| | Reset the device to its power-on default state.
|
| |
| bool | SetPwmFreq (float freq_hz) noexcept |
| | Set the PWM frequency for all channels.
|
| |
| bool | SetPwm (uint8_t channel, uint16_t on_time, uint16_t off_time) noexcept |
| | Set the PWM on/off time for a channel.
|
| |
| bool | SetDuty (uint8_t channel, float duty) noexcept |
| | Set the duty cycle for a channel (0.0-1.0).
|
| |
| bool | SetAllPwm (uint16_t on_time, uint16_t off_time) noexcept |
| | Set all channels to the same PWM value.
|
| |
| uint16_t | GetErrorFlags () const noexcept |
| | Get the accumulated error flags (bitmask).
|
| |
| bool | HasError (Error e) const noexcept |
| | Check if a specific error flag is set.
|
| |
| bool | HasAnyError () const noexcept |
| | Check if any error flag is set.
|
| |
| void | ClearError (Error e) noexcept |
| | Clear a single error flag.
|
| |
| void | ClearErrorFlags (uint16_t mask=0xFFFF) noexcept |
| | Clear specific error flags by raw bitmask.
|
| |
| Error | GetLastError () const noexcept |
| | Get the last error code (single-error convenience accessor).
|
| |
| bool | GetPrescale (uint8_t &prescale) noexcept |
| | Get the current prescale value (frequency divider).
|
| |
| void | SetRetries (int retries) noexcept |
| | Set the I2C retry count for register read/write operations.
|
| |
| void | SetRetryDelay (RetryDelayFn fn) noexcept |
| | Set optional callback invoked between I2C retry attempts (after a failure, before next try).
|
| |
| bool | Sleep () noexcept |
| | Put the PCA9685 into low-power sleep mode.
|
| |
| bool | Wake () noexcept |
| | Wake the PCA9685 from sleep mode.
|
| |
| bool | SetOutputInvert (bool invert) noexcept |
| | Set output polarity inversion.
|
| |
| bool | SetOutputDriverMode (bool totem_pole) noexcept |
| | Set the output driver mode.
|
| |
| bool | SetOutputEnable (bool enable) noexcept |
| | Enable or blank all 16 outputs with the OE pin.
|
| |
| bool | SetChannelFullOn (uint8_t channel) noexcept |
| | Set a channel to fully ON (100% duty, no PWM).
|
| |
| bool | SetChannelFullOff (uint8_t channel) noexcept |
| | Set a channel to fully OFF (0% duty, no PWM).
|
| |
|
| void | setError (Error e) noexcept |
| | Set last error and add to error flags.
|
| |
| bool | writeReg (uint8_t reg, uint8_t value) noexcept |
| | Write one byte to a register.
|
| |
| bool | readReg (uint8_t reg, uint8_t &value) noexcept |
| | Read one byte from a register.
|
| |
| bool | writeRegBlock (uint8_t reg, const uint8_t *data, size_t len) noexcept |
| | Write a block of bytes to a register.
|
| |
| bool | readRegBlock (uint8_t reg, uint8_t *data, size_t len) noexcept |
| | Read a block of bytes from a register.
|
| |
| uint8_t | calcPrescale (float freq_hz) const noexcept |
| | Compute prescale value for given frequency.
|
| |
| bool | writeMode1 (uint8_t value) noexcept |
| | Write MODE1 with AI forced to 1 and RESTART forced to 0.
|
| |
| bool | restartAfterSleep (uint8_t mode1, bool restart) noexcept |
| | Clear SLEEP, wait for the oscillator, then optionally write RESTART (datasheet 7.3.1.1).
|
| |
| void | waitOscillatorStartup () noexcept |
| | Wait OSC_STARTUP_US_ through I2cType::DelayUs(), or with MODE1 reads if the port has none.
|
| |
| bool | modifyReg (uint8_t reg, uint8_t mask, uint8_t value) noexcept |
| | Read-modify-write a single register.
|
| |
template<typename I2cType>
class pca9685::PCA9685< I2cType >
Driver for the PCA9685 16-channel 12-bit PWM controller.
This class provides all register-level operations required to configure and operate the PCA9685, including frequency setting, per-channel PWM, and device reset.
All I2C operations are routed through the user-supplied I2cInterface.
- Template Parameters
-
| I2cType | The I2C interface implementation type that inherits from pca9685::I2cInterface<I2cType> |
- Note
- The driver uses CRTP-based I2C interface for zero virtual call overhead.
template<typename I2cType >
Ensure the driver and I2C bus are initialized (lazy initialization).
On first call, ensures the I2C bus is initialized via I2cInterface::EnsureInitialized(), then performs a device reset to confirm communication. On subsequent calls, returns true immediately if already initialized.
- Returns
- true if already initialized or initialization succeeded; false if I2C bus init or device communication failed.
- Note
- This mirrors the PCAL95555 driver's lazy initialization pattern.
template<typename I2cType >
Reset the device to its power-on default state.
Ensures the I2C bus is initialized, then writes MODE1 = 0x20: oscillator on (SLEEP = 0) and register auto-increment on (AI = 1), which the driver's four-byte channel writes need (datasheet 7.3.1, Table 5). Sub-address and All Call responses are turned off. The channel registers, MODE2 and PRE_SCALE are not touched. Returns after the 500 us oscillator start-up time (OSC_STARTUP_US_, datasheet Table 5 note 2).
- Returns
- true on success; false on I2C failure.
template<typename I2cType >
Set the duty cycle for a channel (0.0-1.0).
Writes ON = 0, OFF = lround(duty * 4095). The two ends use the full-OFF and full-ON bits (datasheet 7.3.3, Table 7, p. 24): a duty that rounds to OFF = 0 sets LEDn_OFF_H[4] (constantly low), and 1.0 sets LEDn_ON_H[4] (constantly high), so ON and OFF never hold the same count (7.3.4).
- Parameters
-
| channel | Channel number (0-15). |
| duty | Duty cycle, clamped to 0.0 (always off) .. 1.0 (always on). |
- Returns
- true on success; false on I2C failure or invalid parameter.
template<typename I2cType >
Enable or blank all 16 outputs with the OE pin.
OE is active LOW (datasheet 7.4, Table 11): with OE low the outputs follow their LEDn registers, with OE high they take the MODE2.OUTNE state (low by default). This calls the port's GpioSet(CtrlPin::OE, ACTIVE) to enable and GpioSet(CtrlPin::OE, INACTIVE) to blank; the port maps ACTIVE to a low level. No I2C traffic, and the chip need not be initialized. A port that does not declare GpioSet() inherits the no-op default, for OE tied low.
The driver never drives OE on its own, so a port can hold the outputs blanked from power-up until the channels are programmed.
- Parameters
-
| enable | true to enable the outputs (OE low), false to blank them (OE high). |
- Returns
- true if the request was passed to the port; false if no bus was given.
template<typename I2cType >
| bool pca9685::PCA9685< I2cType >::SetPwm |
( |
uint8_t |
channel, |
|
|
uint16_t |
on_time, |
|
|
uint16_t |
off_time |
|
) |
| |
|
noexcept |
Set the PWM on/off time for a channel.
ON and OFF must never hold the same count (datasheet 7.3.4, p. 25): when on_time equals off_time (zero duty) the driver writes the full-OFF bit, LEDn_OFF_H[4], instead.
- Parameters
-
| channel | Channel number (0-15). |
| on_time | Tick count when signal turns ON (0-4095). |
| off_time | Tick count when signal turns OFF (0-4095). |
- Returns
- true on success; false on I2C failure or invalid parameter.
template<typename I2cType >
Set the PWM frequency for all channels.
PRE_SCALE is writable only while MODE1.SLEEP = 1, so the driver puts the chip to sleep, writes PRE_SCALE and, if the chip was running, clears SLEEP, waits OSC_STARTUP_US_ and writes RESTART so that the running channels resume (datasheet 7.3.1.1, 7.3.5). A sleeping chip stays asleep.
- Parameters
-
| freq_hz | Desired frequency in Hz (24-1526 typical). |
- Returns
- true on success; false on I2C failure or invalid parameter.
template<typename I2cType >
Wake the PCA9685 from sleep mode.
Follows the restart procedure of datasheet section 7.3.1.1: reads MODE1, clears SLEEP, waits OSC_STARTUP_US_ (500 us) for the oscillator, then writes 1 to RESTART if it read 1, so the channels that were running resume with their previous values. The wait goes through the port's DelayUs() (see I2cInterface); a port without one is timed with bus reads instead. Does nothing when the chip is already awake and RESTART is clear.
- Returns
- true on success; false on I2C failure.