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

Concrete driver for the PCA9570 4‑bit output‑only I2C GPIO expander. More...

#include <IoExpanderPCA9570.hpp>

Inheritance diagram for IoExpanderPCA9570:
[legend]

Public Types

enum  Port { PORT_ALL = 0x0F }
 

Public Member Functions

 IoExpanderPCA9570 ()
 Construct a new IoExpanderPCA9570 object.
 
 ~IoExpanderPCA9570 ()=default
 Destroy the IoExpanderPCA9570 object.
 
void digitalWritePort (uint16_t mask, uint16_t values) override
 Write to multiple pins simultaneously using a mask.
 
uint16_t digitalReadPort () override
 Read the current output register value.
 
- 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 current output value of a 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 PCA9570 4‑bit output‑only I2C GPIO expander.

The PCA9570 provides 4 output‑only pins. All pins are configured as outputs by default; pinMode() will accept only OUTPUT. Reading a pin returns the last written value (output register).

Definition at line 49 of file IoExpanderPCA9570.hpp.

Member Enumeration Documentation

◆ Port

Enumerator
PORT_ALL 

Definition at line 53 of file IoExpanderPCA9570.hpp.

Constructor & Destructor Documentation

◆ IoExpanderPCA9570()

IoExpanderPCA9570::IoExpanderPCA9570 ( )
inline

Construct a new IoExpanderPCA9570 object.

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

Definition at line 62 of file IoExpanderPCA9570.hpp.

◆ ~IoExpanderPCA9570()

IoExpanderPCA9570::~IoExpanderPCA9570 ( )
default

Destroy the IoExpanderPCA9570 object.

Member Function Documentation

◆ configPinsImpl()

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

Hardware‑specific implementation for configuring multiple pins simultaneously.

The PCA9570 has fixed output‑only pins; therefore, this function does not perform any hardware configuration. It validates that the requested mode is OUTPUT (as INPUT is not supported) and updates the internal pin mode cache accordingly.

Parameters
pinMaskBitmask of pins to configure (ignored, as all pins behave identically).
modeDesired mode. Only OUTPUT is accepted; INPUT will log an error.
Note
This function assumes that comm and _pinModes are valid (checked in the base class configPins() before calling here).

Implements IoExpanderBase.

Definition at line 164 of file IoExpanderPCA9570.hpp.

References IoExpanderBase::_pinModes, i, and SENSORLIB_LOG_E.

◆ digitalReadImpl()

bool IoExpanderPCA9570::digitalReadImpl ( uint8_t  pin)
inlineoverrideprotectedvirtual

Read the current output value of a pin.

Since PCA9570 has no input capability, this returns the last written value (output register state).

Parameters
pinPin number (0‑3).
Returns
true Pin is currently set HIGH.
false Pin is currently set LOW.

Implements IoExpanderBase.

Definition at line 210 of file IoExpanderPCA9570.hpp.

References digitalReadPort().

◆ digitalReadPort()

uint16_t IoExpanderPCA9570::digitalReadPort ( )
inlineoverridevirtual

Read the current output register value.

Returns
uint16_t Low 4 bits contain the output state of pins 0‑3.

Reimplemented from IoExpanderBase.

Definition at line 119 of file IoExpanderPCA9570.hpp.

References SensorCommWrapper::readBuff(), and SENSORLIB_LOG_E.

Referenced by digitalReadImpl(), and digitalWriteImpl().

◆ digitalWriteImpl()

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

Write a digital value to an output pin.

Reads the current output register, updates the specified bit, and writes the new value back.

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

Implements IoExpanderBase.

Definition at line 189 of file IoExpanderPCA9570.hpp.

References digitalReadPort(), and SensorCommWrapper::writeRegBuff().

◆ digitalWritePort()

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

Write to multiple pins simultaneously using a mask.

Reads the current output register, modifies only the bits specified by the mask, and writes the result back. The internal output state cache is also updated.

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 79 of file IoExpanderPCA9570.hpp.

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

Referenced by loop().

◆ initImpl()

bool IoExpanderPCA9570::initImpl ( uint8_t  param)
inlineoverrideprotectedvirtual

Perform chip‑specific initialization.

Reads the device ID registers to verify communication and logs the manufacturer ID, part ID, and revision.

Parameters
paramNo use
Returns
true Initialization successful (device ID read succeeded).
false Initialization failed.

Implements IoExpanderBase.

Definition at line 226 of file IoExpanderPCA9570.hpp.

References SensorCommWrapper::readRegBuff(), SENSORLIB_LOG_D, and SENSORLIB_LOG_E.

◆ pinModeImpl()

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

Set the direction of a single pin.

The PCA9570 only supports output mode; therefore, any request for INPUT mode is rejected with an error. No hardware configuration is performed because all pins are fixed as outputs.

Parameters
pinPin number (0‑3). (Not used, as all pins behave identically.)
modeDesired mode. Only OUTPUT is accepted.

Implements IoExpanderBase.

Definition at line 141 of file IoExpanderPCA9570.hpp.

References SENSORLIB_LOG_E.


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