14#include "pca9685_version.h"
34template <
typename I2cType>
91 PCA9685(I2cType* bus, uint8_t address);
127 bool Reset() noexcept;
152 bool SetPwm(uint8_t channel, uint16_t on_time, uint16_t off_time) noexcept;
166 bool SetDuty(uint8_t channel,
float duty) noexcept;
177 bool SetAllPwm(uint16_t on_time, uint16_t off_time) noexcept;
238 [[nodiscard]]
bool GetPrescale(uint8_t& prescale)
noexcept;
279 bool Sleep() noexcept;
292 bool Wake() noexcept;
375 return HF_PCA9685_VERSION_STRING;
383 return HF_PCA9685_VERSION_MAJOR;
391 return HF_PCA9685_VERSION_MINOR;
399 return HF_PCA9685_VERSION_PATCH;
425 bool writeReg(uint8_t reg, uint8_t value)
noexcept;
428 bool readReg(uint8_t reg, uint8_t& value)
noexcept;
431 bool writeRegBlock(uint8_t reg,
const uint8_t* data,
size_t len)
noexcept;
434 bool readRegBlock(uint8_t reg, uint8_t* data,
size_t len)
noexcept;
437 [[nodiscard]] uint8_t
calcPrescale(
float freq_hz)
const noexcept;
466 [[nodiscard]] static ::std::array<uint8_t, 4>
pwmFrame(uint16_t on_time,
467 uint16_t off_time) noexcept;
476 bool modifyReg(uint8_t reg, uint8_t mask, uint8_t value) noexcept;
480#define PCA9685_HEADER_INCLUDED
483#include "../src/pca9685.ipp"
484#undef PCA9685_HEADER_INCLUDED
488 return HF_PCA9685_VERSION_STRING;
Driver for the PCA9685 16-channel 12-bit PWM controller.
Definition pca9685.hpp:35
void(*)() RetryDelayFn
Type of optional callback invoked between I2C retry attempts.
Definition pca9685.hpp:254
void SetRetries(int retries) noexcept
Set the I2C retry count for register read/write operations.
Definition pca9685.hpp:244
bool IsInitialized() const noexcept
Check if the driver has been initialized.
Definition pca9685.hpp:112
bool SetPwmFreq(float freq_hz) noexcept
Set the PWM frequency for all channels.
Definition pca9685.ipp:57
static constexpr uint8_t MODE1_RESTART_
MODE1 bit 7: RESTART (Table 5)
Definition pca9685.hpp:403
bool SetPwm(uint8_t channel, uint16_t on_time, uint16_t off_time) noexcept
Set the PWM on/off time for a channel.
Definition pca9685.ipp:91
static constexpr uint8_t GetDriverVersionPatch() noexcept
Get the compiled driver patch version number.
Definition pca9685.hpp:398
bool SetAllPwm(uint16_t on_time, uint16_t off_time) noexcept
Set all channels to the same PWM value.
Definition pca9685.ipp:127
bool initialized_
Definition pca9685.hpp:415
static constexpr uint32_t OSC_STARTUP_US_
Definition pca9685.hpp:83
uint8_t calcPrescale(float freq_hz) const noexcept
Compute prescale value for given frequency.
Definition pca9685.ipp:410
uint8_t addr_
Definition pca9685.hpp:410
static constexpr uint8_t GetDriverVersionMajor() noexcept
Get the compiled driver major version number.
Definition pca9685.hpp:382
void setError(Error e) noexcept
Set last error and add to error flags.
Definition pca9685.hpp:418
bool Wake() noexcept
Wake the PCA9685 from sleep mode.
Definition pca9685.ipp:177
void ClearErrorFlags(uint16_t mask=0xFFFF) noexcept
Clear specific error flags by raw bitmask.
Definition pca9685.hpp:221
Error
Error codes for PCA9685 operations (bitmask flags).
Definition pca9685.hpp:44
@ OutOfRange
Value out of hardware range.
@ I2cRead
An I2C read operation failed.
@ NotInitialized
Driver not initialized.
@ DeviceNotFound
Device did not respond.
@ InvalidParam
Invalid parameter (channel, value, etc.)
@ I2cWrite
An I2C write operation failed.
static constexpr uint32_t OSC_STARTUP_FALLBACK_READS_
MODE1 reads that cover OSC_STARTUP_US_ at 1 MHz SCL when the port has no DelayUs().
Definition pca9685.hpp:407
bool SetOutputInvert(bool invert) noexcept
Set output polarity inversion.
Definition pca9685.ipp:202
static ::std::array< uint8_t, 4 > pwmFrame(uint16_t on_time, uint16_t off_time) noexcept
Build the four LEDn / ALL_LED bytes for ON and OFF ticks; equal ticks give full OFF.
Definition pca9685.ipp:384
bool EnsureInitialized() noexcept
Ensure the driver and I2C bus are initialized (lazy initialization).
Definition pca9685.ipp:28
static constexpr uint16_t MAX_PWM_
Maximum tick value (12-bit)
Definition pca9685.hpp:79
Error last_error_
Definition pca9685.hpp:411
bool Reset() noexcept
Reset the device to its power-on default state.
Definition pca9685.ipp:36
void ClearError(Error e) noexcept
Clear a single error flag.
Definition pca9685.hpp:213
int retries_
Definition pca9685.hpp:413
uint16_t error_flags_
Definition pca9685.hpp:412
RetryDelayFn retry_delay_
Definition pca9685.hpp:414
static constexpr uint8_t GetDriverVersionMinor() noexcept
Get the compiled driver minor version number.
Definition pca9685.hpp:390
void waitOscillatorStartup() noexcept
Wait OSC_STARTUP_US_ through I2cType::DelayUs(), or with MODE1 reads if the port has none.
Definition pca9685.ipp:368
bool readReg(uint8_t reg, uint8_t &value) noexcept
Read one byte from a register.
Definition pca9685.ipp:294
bool SetDuty(uint8_t channel, float duty) noexcept
Set the duty cycle for a channel (0.0-1.0).
Definition pca9685.ipp:113
I2cType * i2c_
Definition pca9685.hpp:409
bool SetOutputEnable(bool enable) noexcept
Enable or blank all 16 outputs with the OE pin.
Definition pca9685.ipp:222
static constexpr uint32_t OSC_FREQ_
Definition pca9685.hpp:80
void SetRetryDelay(RetryDelayFn fn) noexcept
Set optional callback invoked between I2C retry attempts (after a failure, before next try).
Definition pca9685.hpp:265
bool readRegBlock(uint8_t reg, uint8_t *data, size_t len) noexcept
Read a block of bytes from a register.
Definition pca9685.ipp:329
bool SetChannelFullOn(uint8_t channel) noexcept
Set a channel to fully ON (100% duty, no PWM).
Definition pca9685.ipp:235
bool modifyReg(uint8_t reg, uint8_t mask, uint8_t value) noexcept
Read-modify-write a single register.
Definition pca9685.ipp:396
bool Sleep() noexcept
Put the PCA9685 into low-power sleep mode.
Definition pca9685.ipp:160
static constexpr uint8_t MODE1_SLEEP_
Definition pca9685.hpp:405
Register
PCA9685 register map (I2C register addresses).
Definition pca9685.hpp:57
@ LED0_OFF_H
LED0 output and brightness control byte 3 (off time, high bits + full-off)
@ ALL_LED_ON_L
All LED on time, low byte.
@ SUBADR1
I2C subaddress 1.
@ PRE_SCALE
Prescaler for PWM frequency (oscillator / (4096 * freq))
@ LED0_ON_L
LED0 output and brightness control byte 0 (on time, low byte)
@ LED0_ON_H
LED0 output and brightness control byte 1 (on time, high bits + full-on)
@ LED0_OFF_L
LED0 output and brightness control byte 2 (off time, low byte)
@ MODE2
Mode register 2 (output drive, invert, output change behaviour)
@ TESTMODE
Test mode register.
@ ALL_LED_OFF_L
All LED off time, low byte.
@ ALL_LED_OFF_H
All LED off time, high bits.
@ ALLCALLADR
All-call I2C address.
@ MODE1
Mode register 1 (reset, sleep, auto-increment, etc.)
@ SUBADR3
I2C subaddress 3.
@ ALL_LED_ON_H
All LED on time, high bits.
@ SUBADR2
I2C subaddress 2.
bool SetChannelFullOff(uint8_t channel) noexcept
Set a channel to fully OFF (0% duty, no PWM).
Definition pca9685.ipp:255
bool writeMode1(uint8_t value) noexcept
Write MODE1 with AI forced to 1 and RESTART forced to 0.
Definition pca9685.ipp:346
bool restartAfterSleep(uint8_t mode1, bool restart) noexcept
Clear SLEEP, wait for the oscillator, then optionally write RESTART (datasheet 7.3....
Definition pca9685.ipp:354
Error GetLastError() const noexcept
Get the last error code (single-error convenience accessor).
Definition pca9685.hpp:229
bool HasAnyError() const noexcept
Check if any error flag is set.
Definition pca9685.hpp:205
uint16_t GetErrorFlags() const noexcept
Get the accumulated error flags (bitmask).
Definition pca9685.hpp:183
static constexpr const char * GetDriverVersion() noexcept
Get the compiled driver version string.
Definition pca9685.hpp:374
static constexpr uint8_t MODE1_AI_
MODE1 bit 5: register auto-increment.
Definition pca9685.hpp:404
bool GetPrescale(uint8_t &prescale) noexcept
Get the current prescale value (frequency divider).
Definition pca9685.ipp:145
static constexpr uint8_t MAX_CHANNELS_
Number of PWM channels (0-15)
Definition pca9685.hpp:78
bool HasError(Error e) const noexcept
Check if a specific error flag is set.
Definition pca9685.hpp:197
bool writeReg(uint8_t reg, uint8_t value) noexcept
Write one byte to a register.
Definition pca9685.ipp:277
bool writeRegBlock(uint8_t reg, const uint8_t *data, size_t len) noexcept
Write a block of bytes to a register.
Definition pca9685.ipp:311
bool SetOutputDriverMode(bool totem_pole) noexcept
Set the output driver mode.
Definition pca9685.ipp:212
Definition pca9685.hpp:16
CRTP-based I2C interface for PCA9685 driver.