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

Concrete driver for the XL9555 16‑bit I2C GPIO expander. More...

#include <IoExpanderXL9555.hpp>

Inheritance diagram for IoExpanderXL9555:
[legend]

Public Types

enum  Port { PORT0 = 0x00FF , PORT1 = 0xFF00 , PORT_ALL = 0xFFFF }
 
enum  IoExpanderGPIO {
  __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) ,
  __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) ,
  __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) ,
  __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead."))) , __attribute__ =((deprecated("Use integer pin numbers (0-15) directly instead.")))
}
 

Public Member Functions

 IoExpanderXL9555 ()
 Construct a new IoExpanderXL9555 object.
 
 ~IoExpanderXL9555 ()=default
 Destroy the IoExpanderXL9555 object.
 
void digitalWritePort (uint16_t mask, uint16_t values) override
 Write to multiple pins simultaneously using a mask.
 
uint16_t digitalReadPort () override
 Read the values of all 16 input pins as a single 16-bit word.
 
- 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
 Set the direction of a single pin.
 
void configPinsImpl (uint16_t pinMask, uint8_t mode) override
 Hardware‑specific implementation for configuring multiple pins simultaneously.
 
void digitalWriteImpl (uint8_t pin, bool value) override
 Write a digital value to an output pin.
 
bool digitalReadImpl (uint8_t pin) override
 Read the digital value from an input pin.
 
bool initImpl (uint8_t param) override
 Perform chip‑specific initialization.
 
- Protected Member Functions inherited from DeviceBeginCommon
 DeviceBeginCommon ()=default
 
virtual bool ensureValid () const override
 
virtual SensorCommBasegetComm () 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< SensorCommBasecomm
 
std::unique_ptr< SensorHalhal
 
std::unique_ptr< SensorCommStaticstaticComm
 
uint8_t _addr = 0
 
uint8_t _iface = COMM_CUSTOM
 

Detailed Description

Concrete driver for the XL9555 16‑bit I2C GPIO expander.

The XL9555 provides 16 bidirectional I/O pins, organized as two 8‑bit ports. This class implements the hardware‑specific operations required by the IoExpanderBase interface using I2C communication.

Definition at line 97 of file expander/IoExpanderXL9555.hpp.

Member Enumeration Documentation

◆ IoExpanderGPIO

Enumerator
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 
__attribute__ 

Definition at line 106 of file expander/IoExpanderXL9555.hpp.

◆ Port

Enumerator
PORT0 
PORT1 
PORT_ALL 

Definition at line 100 of file expander/IoExpanderXL9555.hpp.

Constructor & Destructor Documentation

◆ IoExpanderXL9555()

IoExpanderXL9555::IoExpanderXL9555 ( )
inline

Construct a new IoExpanderXL9555 object.

Initializes the base class with the fixed pin count of 16.

Definition at line 130 of file expander/IoExpanderXL9555.hpp.

◆ ~IoExpanderXL9555()

IoExpanderXL9555::~IoExpanderXL9555 ( )
default

Destroy the IoExpanderXL9555 object.

Member Function Documentation

◆ configPinsImpl()

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

Hardware‑specific implementation for configuring multiple pins simultaneously.

This function modifies the configuration registers (CFG0/CFG1) of the XL9555. It reads the current register values, updates only the bits specified in pinMask, and writes the new values back. This ensures that the direction of pins not listed in the mask remains unchanged.

After updating the hardware, the internal pin mode cache (_pinModes) is also updated for consistency, so that subsequent individual pin operations see the correct cached state.

Parameters
pinMaskBitmask of pins to configure. Bits 0‑7 correspond to Port 0 (pins 0‑7), bits 8‑15 correspond to Port 1 (pins 8‑15).
modeDesired mode: INPUT or OUTPUT.
Note
This function assumes that comm and _pinModes are valid (they are checked in the base class configPins() before calling here).

Implements IoExpanderBase.

Definition at line 232 of file expander/IoExpanderXL9555.hpp.

References IoExpanderBase::_pinModes, i, SensorCommWrapper::readReg(), and SensorCommWrapper::writeReg().

◆ digitalReadImpl()

bool IoExpanderXL9555::digitalReadImpl ( uint8_t  pin)
inlineoverrideprotectedvirtual

Read the digital value from an input pin.

Reads the input register (INP0 for pins 0‑7, INP1 for pins 8‑15).

Parameters
pinPin number (0‑15).
Returns
true Pin is HIGH.
false Pin is LOW.

Implements IoExpanderBase.

Definition at line 310 of file expander/IoExpanderXL9555.hpp.

References DeviceBeginCommon::ensureValid(), SensorCommWrapper::readReg(), and SENSORLIB_LOG_E.

◆ digitalReadPort()

uint16_t IoExpanderXL9555::digitalReadPort ( )
inlineoverridevirtual

Read the values of all 16 input pins as a single 16-bit word.

Reads both input registers (INP0 and INP1) and combines them into a 16-bit value where the lower 8 bits correspond to Port 0 (pins 0‑7) and the upper 8 bits correspond to Port 1 (pins 8‑15).

Returns
uint16_t Bitmask of all input pin states. Bit 0 is pin 0, bit 15 is pin 15. Returns 0 and logs an error if the expander is not initialized.

Reimplemented from IoExpanderBase.

Definition at line 177 of file expander/IoExpanderXL9555.hpp.

References SensorCommWrapper::readReg().

Referenced by loop().

◆ digitalWriteImpl()

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

Write a digital value to an output pin.

Updates the output register (OUTP0 for pins 0‑7, OUTP1 for pins 8‑15) accordingly.

Parameters
pinPin number (0‑15).
valuetrue for HIGH, false for LOW.

Implements IoExpanderBase.

Definition at line 286 of file expander/IoExpanderXL9555.hpp.

References SensorCommWrapper::clrRegBit(), SENSORLIB_LOG_E, and SensorCommWrapper::setRegBit().

◆ digitalWritePort()

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

Write to multiple pins simultaneously using a mask.

This override writes whole output registers when the mask covers an entire port, and updates the internal output state cache.

Parameters
maskBitmask where a 1 indicates the pin should be updated.
valuesBitmask of the new values for the selected pins.

Reimplemented from IoExpanderBase.

Definition at line 146 of file expander/IoExpanderXL9555.hpp.

References IoExpanderBase::_outputStates, i, SENSORLIB_LOG_E, and SensorCommWrapper::writeReg().

Referenced by loop().

◆ initImpl()

bool IoExpanderXL9555::initImpl ( uint8_t  param)
inlineoverrideprotectedvirtual

Perform chip‑specific initialization.

If the provided address is XL9555_UNKNOWN_ADDRESS, this function attempts to auto‑discover the device by scanning the standard XL9555 I2C addresses (0x20‑0x27). Otherwise, it verifies communication by reading the input port 0 register.

Parameters
paramI2C device address, or XL9555_UNKNOWN_ADDRESS for auto‑discovery.
Returns
true Initialization successful.
false Initialization failed (device not found or communication error).

Implements IoExpanderBase.

Definition at line 337 of file expander/IoExpanderXL9555.hpp.

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

◆ pinModeImpl()

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

Set the direction of a single pin.

This function configures the corresponding bit in the configuration register (CFG0 for pins 0‑7, CFG1 for pins 8‑15). A 0 in the configuration register means output, a 1 means input.

Parameters
pinPin number (0‑15).
modeDesired mode: OUTPUT (0) or INPUT (1).

Implements IoExpanderBase.

Definition at line 197 of file expander/IoExpanderXL9555.hpp.

References SensorCommWrapper::clrRegBit(), SENSORLIB_LOG_E, and SensorCommWrapper::setRegBit().


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