SensorLib 0.5.1
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
IoExpanderAW9523 Class Reference

Driver for the AW9523/AW9523B 16-channel GPIO and LED controller. More...

#include <IoExpanderAW9523.hpp>

Inheritance diagram for IoExpanderAW9523:
[legend]

Public Types

enum  Port : uint16_t { PORT0 = 0x00FF , PORT1 = 0xFF00 , PORT_ALL = 0xFFFF }
 
enum class  LedCurrentRange : uint8_t { FULL = 0 , THREE_QUARTERS = 1 , HALF = 2 , QUARTER = 3 }
 Global maximum LED current as a fraction of the typical 37 mA IMAX. More...
 

Public Member Functions

 IoExpanderAW9523 ()
 
 ~IoExpanderAW9523 ()=default
 
void digitalWritePort (uint16_t mask, uint16_t values) override
 Write selected GPIO outputs while preserving unselected pins.
 
uint16_t digitalReadPort () override
 Read both GPIO input ports.
 
bool setPort0PushPull (bool pushPull)
 Select the P0 GPIO output driver type.
 
bool setLedCurrentRange (LedCurrentRange range)
 Set the global maximum current range for all LED channels.
 
bool setLedMode (uint8_t pin, bool enable=true)
 Enable or disable constant-current LED mode on one pin.
 
bool setLedModes (uint16_t pinMask, bool enable=true)
 Enable or disable constant-current LED mode on selected pins.
 
bool analogWrite (uint8_t pin, uint8_t value)
 Set one constant-current LED channel's brightness.
 
bool enableInterrupt (uint8_t pin, bool enable=true)
 Enable or disable input-change interrupt generation for one pin.
 
bool enableInterrupts (uint16_t pinMask, bool enable=true)
 Enable or disable input-change interrupts for selected pins.
 
bool softwareReset ()
 Restore all registers to their hardware defaults over I2C.
 
- Public Member Functions inherited from IoExpanderBase
 IoExpanderBase (uint8_t pinCount)
 Construct a new IoExpanderBase object.
 
virtual ~IoExpanderBase ()=default
 Virtual destructor.
 
virtual void deinit ()
 Deinitialize the expander.
 
void configPins (uint16_t pinMask, uint8_t mode)
 Configure multiple pins as input or output simultaneously.
 
void pinMode (uint8_t pin, uint8_t mode)
 Set the direction of a GPIO pin.
 
void digitalWrite (uint8_t pin, bool value)
 Write a digital value to an output pin.
 
bool digitalRead (uint8_t pin)
 Read the digital value from an input pin.
 
void digitalToggle (uint8_t pin)
 Toggle the state of an output pin.
 
uint8_t pinCount () const
 Get the total number of pins provided by this expander.
 
uint16_t pinToMask (uint8_t pin) const
 Convert a pin number to its corresponding bitmask.
 
- Public Member Functions inherited from I2CDeviceNoHal
bool begin (SensorCommCustom::CustomCallback callback, uint8_t addr)
 Initialize the sensor using custom callback interface.
 
- Public Member Functions inherited from DeviceBeginCommon
virtual ~DeviceBeginCommon ()
 
uint8_t getAddress () const
 
uint8_t getInterface () const
 
- Public Member Functions inherited from SensorCommWrapper
 SensorCommWrapper ()=default
 
int readReg (uint8_t reg) const
 
int readRegBuff (uint8_t reg, uint8_t *buf, size_t len) const
 
int readBuff (uint8_t *buf, size_t len) const
 
int writeReg (uint8_t reg, uint8_t val)
 
int writeRegBuff (uint8_t reg, uint8_t *buf, size_t len)
 
int writeBuff (uint8_t *buf, size_t len)
 
int writeThenRead (const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len)
 
bool setRegBit (uint8_t reg, uint8_t bit)
 
bool clrRegBit (uint8_t reg, uint8_t bit)
 
bool getRegBit (uint8_t reg, uint8_t bit)
 
int updateBits (uint8_t reg, uint8_t mask, uint8_t value_shifted)
 
void setAddress (uint8_t address)
 
void setAck (bool enable)
 
- Public Member Functions inherited from SensorCommInterface
virtual ~SensorCommInterface ()=default
 

Protected Member Functions

void pinModeImpl (uint8_t pin, uint8_t mode) override
 Hardware‑specific implementation of pinMode().
 
void configPinsImpl (uint16_t pinMask, uint8_t mode) override
 Hardware‑specific implementation of batch pin mode configuration.
 
void digitalWriteImpl (uint8_t pin, bool value) override
 Hardware‑specific implementation of digitalWrite().
 
bool digitalReadImpl (uint8_t pin) override
 Hardware‑specific implementation of digitalRead().
 
bool initImpl (uint8_t address) override
 Chip‑specific initialization.
 
- Protected Member Functions inherited from DeviceBeginCommon
 DeviceBeginCommon ()=default
 
virtual bool ensureValid () const override
 
virtual SensorCommBase * getComm () const override
 
virtual void beforeBegin ()
 
virtual void afterCommReady ()
 
virtual void afterInitSuccess (uint8_t param)
 
virtual void onBeginFail ()
 
bool fail ()
 
void clearCommState ()
 
template<typename CreateComm >
bool beginLifecycle (uint8_t addr, uint8_t iface, uint8_t initParam, CreateComm createComm)
 

Additional Inherited Members

- Protected Attributes inherited from IoExpanderBase
uint8_t _pinCount
 Number of GPIO pins.
 
std::unique_ptr< uint8_t[]> _pinModes
 Cached pin modes (INPUT/OUTPUT)
 
std::unique_ptr< bool[]> _outputStates
 Cached output states (true = HIGH)
 
- Protected Attributes inherited from DeviceBeginCommon
std::unique_ptr< SensorCommBase > comm
 
std::unique_ptr< SensorHal > hal
 
std::unique_ptr< SensorCommStatic > staticComm
 
uint8_t _addr = 0
 
uint8_t _iface = COMM_CUSTOM
 

Detailed Description

Driver for the AW9523/AW9523B 16-channel GPIO and LED controller.

Pins 0-7 map to P0_0-P0_7 and pins 8-15 map to P1_0-P1_7. In addition to the IoExpanderBase GPIO API, this class supports input-change interrupts, P0 open-drain/push-pull selection, and 256-step constant-current LED output.

Definition at line 58 of file IoExpanderAW9523.hpp.

Member Enumeration Documentation

◆ LedCurrentRange

enum class IoExpanderAW9523::LedCurrentRange : uint8_t
strong

Global maximum LED current as a fraction of the typical 37 mA IMAX.

Enumerator
FULL 
THREE_QUARTERS 
HALF 
QUARTER 

Definition at line 68 of file IoExpanderAW9523.hpp.

◆ Port

enum IoExpanderAW9523::Port : uint16_t
Enumerator
PORT0 
PORT1 
PORT_ALL 

Definition at line 61 of file IoExpanderAW9523.hpp.

Constructor & Destructor Documentation

◆ IoExpanderAW9523()

IoExpanderAW9523::IoExpanderAW9523 ( )
inline

Definition at line 75 of file IoExpanderAW9523.hpp.

◆ ~IoExpanderAW9523()

IoExpanderAW9523::~IoExpanderAW9523 ( )
default

Member Function Documentation

◆ analogWrite()

bool IoExpanderAW9523::analogWrite ( uint8_t  pin,
uint8_t  value 
)
inline

Set one constant-current LED channel's brightness.

Parameters
pinPin number from 0 to 15.
valueLinear brightness/current value from 0 (off) to 255 (maximum).
Returns
true on success.

Definition at line 207 of file IoExpanderAW9523.hpp.

References SensorCommWrapper::writeReg().

Referenced by loop(), and setup().

◆ configPinsImpl()

void IoExpanderAW9523::configPinsImpl ( uint16_t  pinMask,
uint8_t  mode 
)
inlineoverrideprotectedvirtual

Hardware‑specific implementation of batch pin mode configuration.

Parameters
pinMaskBitmask of pins to configure.
modeDesired mode (INPUT or OUTPUT).

Implements IoExpanderBase.

Definition at line 280 of file IoExpanderAW9523.hpp.

References SENSORLIB_LOG_E.

◆ digitalReadImpl()

bool IoExpanderAW9523::digitalReadImpl ( uint8_t  pin)
inlineoverrideprotectedvirtual

Hardware‑specific implementation of digitalRead().

Called by digitalRead() after validating arguments.

Parameters
pinPin number.
Returns
true Pin is HIGH.
false Pin is LOW.

Implements IoExpanderBase.

Definition at line 308 of file IoExpanderAW9523.hpp.

References SensorCommWrapper::readReg(), and SENSORLIB_LOG_E.

◆ digitalReadPort()

uint16_t IoExpanderAW9523::digitalReadPort ( )
inlineoverridevirtual

Read both GPIO input ports.

Note
Reading a port also clears an interrupt generated by that port.
Returns
Current P0 and P1 levels, or 0 if either read fails.

Reimplemented from IoExpanderBase.

Definition at line 115 of file IoExpanderAW9523.hpp.

References SensorCommWrapper::readReg(), and SENSORLIB_LOG_E.

Referenced by scanKeyboard().

◆ digitalWriteImpl()

void IoExpanderAW9523::digitalWriteImpl ( uint8_t  pin,
bool  value 
)
inlineoverrideprotectedvirtual

Hardware‑specific implementation of digitalWrite().

Called by digitalWrite() after validating arguments and updating the cache.

Parameters
pinPin number.
valuetrue for HIGH, false for LOW.

Implements IoExpanderBase.

Definition at line 299 of file IoExpanderAW9523.hpp.

References SENSORLIB_LOG_E.

◆ digitalWritePort()

void IoExpanderAW9523::digitalWritePort ( uint16_t  mask,
uint16_t  values 
)
inlineoverridevirtual

Write selected GPIO outputs while preserving unselected pins.

Parameters
maskBits set to 1 select pins to update.
valuesNew output levels; bit 0 maps to P0_0 and bit 15 to P1_7.

Reimplemented from IoExpanderBase.

Definition at line 83 of file IoExpanderAW9523.hpp.

References IoExpanderBase::_outputStates, and SENSORLIB_LOG_E.

Referenced by scanKeyboard(), and setup().

◆ enableInterrupt()

bool IoExpanderAW9523::enableInterrupt ( uint8_t  pin,
bool  enable = true 
)
inline

Enable or disable input-change interrupt generation for one pin.

Parameters
pinPin number from 0 to 15.
enabletrue to enable interrupt generation.
Returns
true on success.

Definition at line 221 of file IoExpanderAW9523.hpp.

References enableInterrupts().

Referenced by setup().

◆ enableInterrupts()

bool IoExpanderAW9523::enableInterrupts ( uint16_t  pinMask,
bool  enable = true 
)
inline

Enable or disable input-change interrupts for selected pins.

Parameters
pinMaskPins to modify.
enabletrue to enable interrupt generation.
Returns
true on success.

Interrupt mask bits are active high in this API even though the AW9523 interrupt registers use 0 for enabled and 1 for disabled.

Definition at line 238 of file IoExpanderAW9523.hpp.

Referenced by enableInterrupt(), and setup().

◆ initImpl()

bool IoExpanderAW9523::initImpl ( uint8_t  param)
inlineoverrideprotectedvirtual

Chip‑specific initialization.

Called by all begin() overloads after the communication object is set up. Derived classes should perform any necessary hardware configuration (e.g., reset, verify device ID, set default registers).

Parameters
paramOpaque parameter passed from begin() (typically I2C address).
Returns
true Initialization successful.
false Initialization failed.

Implements IoExpanderBase.

Definition at line 319 of file IoExpanderAW9523.hpp.

References DeviceBeginCommon::_addr, SensorCommWrapper::readReg(), SENSORLIB_LOG_D, SENSORLIB_LOG_E, and SensorCommWrapper::setAddress().

◆ pinModeImpl()

void IoExpanderAW9523::pinModeImpl ( uint8_t  pin,
uint8_t  mode 
)
inlineoverrideprotectedvirtual

Hardware‑specific implementation of pinMode().

Called by pinMode() after validating arguments and updating the cache.

Parameters
pinPin number.
modeDesired mode (INPUT or OUTPUT).

Implements IoExpanderBase.

Definition at line 264 of file IoExpanderAW9523.hpp.

References SENSORLIB_LOG_E.

◆ setLedCurrentRange()

bool IoExpanderAW9523::setLedCurrentRange ( LedCurrentRange  range)
inline

Set the global maximum current range for all LED channels.

Parameters
rangeMaximum current range relative to the typical 37 mA IMAX.
Returns
true on success.

Definition at line 147 of file IoExpanderAW9523.hpp.

References QUARTER, SensorCommWrapper::readReg(), SENSORLIB_LOG_E, and SensorCommWrapper::writeReg().

Referenced by setup().

◆ setLedMode()

bool IoExpanderAW9523::setLedMode ( uint8_t  pin,
bool  enable = true 
)
inline

Enable or disable constant-current LED mode on one pin.

Parameters
pinPin number from 0 to 15.
enabletrue for LED mode, false for GPIO mode.
Returns
true on success.

Definition at line 169 of file IoExpanderAW9523.hpp.

References setLedModes().

Referenced by setup().

◆ setLedModes()

bool IoExpanderAW9523::setLedModes ( uint16_t  pinMask,
bool  enable = true 
)
inline

Enable or disable constant-current LED mode on selected pins.

Parameters
pinMaskPins to modify.
enabletrue for LED mode, false for GPIO mode.
Returns
true on success.

Definition at line 183 of file IoExpanderAW9523.hpp.

Referenced by setLedMode().

◆ setPort0PushPull()

bool IoExpanderAW9523::setPort0PushPull ( bool  pushPull)
inline

Select the P0 GPIO output driver type.

Parameters
pushPulltrue for push-pull, false for open-drain.
Returns
true on success.

Definition at line 131 of file IoExpanderAW9523.hpp.

References SensorCommWrapper::readReg(), and SensorCommWrapper::writeReg().

Referenced by setup().

◆ softwareReset()

bool IoExpanderAW9523::softwareReset ( )
inline

Restore all registers to their hardware defaults over I2C.

Returns
true if reset and subsequent state synchronization succeed.

Definition at line 255 of file IoExpanderAW9523.hpp.

References SensorCommWrapper::writeReg().


The documentation for this class was generated from the following file: