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

Abstract base class for GPIO expander devices. More...

#include <IoExpanderBase.hpp>

Inheritance diagram for IoExpanderBase:
[legend]

Public Member Functions

 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.
 
virtual void digitalWritePort (uint16_t mask, uint16_t values)
 Write to multiple pins simultaneously using a mask.
 
virtual uint16_t digitalReadPort ()
 Read the values of all input pins.
 
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.
 

Protected Member Functions

virtual bool initImpl (uint8_t param)=0
 Chip‑specific initialization.
 
virtual void configPinsImpl (uint16_t pinMask, uint8_t mode)=0
 Hardware‑specific implementation of batch pin mode configuration.
 
virtual void pinModeImpl (uint8_t pin, uint8_t mode)=0
 Hardware‑specific implementation of pinMode().
 
virtual void digitalWriteImpl (uint8_t pin, bool value)=0
 Hardware‑specific implementation of digitalWrite().
 
virtual bool digitalReadImpl (uint8_t pin)=0
 Hardware‑specific implementation of digitalRead().
 

Protected Attributes

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)
 

Detailed Description

Abstract base class for GPIO expander devices.

This class provides a common interface for controlling GPIO expanders with any number of pins. It manages pin mode caching, output state caching, and delegates hardware‑specific operations to pure virtual functions implemented by derived classes.

Definition at line 42 of file IoExpanderBase.hpp.

Constructor & Destructor Documentation

◆ IoExpanderBase()

IoExpanderBase::IoExpanderBase ( uint8_t  pinCount)
inline

Construct a new IoExpanderBase object.

Allocates internal arrays for pin modes and output states. If allocation fails, _pinCount is set to 0 and subsequent operations will be safely rejected.

Parameters
pinCountNumber of GPIO pins provided by the expander.

Definition at line 54 of file IoExpanderBase.hpp.

References _outputStates, _pinCount, _pinModes, and pinCount().

◆ ~IoExpanderBase()

virtual IoExpanderBase::~IoExpanderBase ( )
virtualdefault

Virtual destructor.

Ensures proper cleanup of derived classes. The std::unique_ptr members automatically release their memory.

Member Function Documentation

◆ configPins()

void IoExpanderBase::configPins ( uint16_t  pinMask,
uint8_t  mode 
)
inline

Configure multiple pins as input or output simultaneously.

This function sets the direction of all pins specified in the mask to the given mode. It validates the arguments and updates the internal pin mode cache before delegating the hardware configuration to the pure virtual function configPinsImpl(), which must be implemented by derived classes.

Parameters
pinMaskBitmask where a 1 indicates the pin should be configured. Bit 0 corresponds to pin 0, bit 15 to pin 15 (if applicable). Only the lowest _pinCount bits are considered.
modeDesired mode: INPUT or OUTPUT (as defined in the Arduino framework).
Note
This function logs errors and returns early if:
  • The expander is not initialized (comm is null).
  • The internal pin mode cache is uninitialized.
  • An invalid mode is provided.

Definition at line 99 of file IoExpanderBase.hpp.

References _pinModes, configPinsImpl(), and SENSORLIB_LOG_E.

Referenced by setup().

◆ configPinsImpl()

virtual void IoExpanderBase::configPinsImpl ( uint16_t  pinMask,
uint8_t  mode 
)
protectedpure virtual

Hardware‑specific implementation of batch pin mode configuration.

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

Implemented in IoExpanderPCA9570, and IoExpanderXL9555.

Referenced by configPins().

◆ deinit()

virtual void IoExpanderBase::deinit ( )
inlinevirtual

Deinitialize the expander.

Can be overridden by derived classes to perform chip‑specific cleanup (e.g., power‑down, reset). The base implementation does nothing.

Definition at line 77 of file IoExpanderBase.hpp.

◆ digitalRead()

bool IoExpanderBase::digitalRead ( uint8_t  pin)
inline

Read the digital value from an input pin.

Reads the current state from hardware via digitalReadImpl().

Parameters
pinPin number (0 .. pinCount()-1). Must be configured as INPUT.
Returns
true Pin is HIGH.
false Pin is LOW (or error, logged).

Definition at line 182 of file IoExpanderBase.hpp.

References _pinCount, _pinModes, digitalReadImpl(), and SENSORLIB_LOG_E.

Referenced by digitalReadPort(), loop(), TouchDrvDigitalRead(), and IoExpanderSPI::transferDataBits().

◆ digitalReadImpl()

virtual bool IoExpanderBase::digitalReadImpl ( uint8_t  pin)
protectedpure virtual

Hardware‑specific implementation of digitalRead().

Called by digitalRead() after validating arguments.

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

Implemented in IoExpanderPCA9570, and IoExpanderXL9555.

Referenced by digitalRead().

◆ digitalReadPort()

virtual uint16_t IoExpanderBase::digitalReadPort ( )
inlinevirtual

Read the values of all input pins.

The default implementation reads each input pin individually. Derived classes may override this to perform a faster register read.

Returns
uint16_t Bitmask where bit i is 1 if input pin i is HIGH.

Reimplemented in IoExpanderPCA9570, and IoExpanderXL9555.

Definition at line 248 of file IoExpanderBase.hpp.

References _outputStates, _pinCount, _pinModes, digitalRead(), i, and SENSORLIB_LOG_E.

◆ digitalToggle()

void IoExpanderBase::digitalToggle ( uint8_t  pin)
inline

Toggle the state of an output pin.

Inverts the current cached output state and writes it to the pin.

Parameters
pinPin number (0 .. pinCount()-1). Must be configured as OUTPUT.

Definition at line 207 of file IoExpanderBase.hpp.

References _outputStates, _pinModes, digitalWrite(), and SENSORLIB_LOG_E.

Referenced by loop().

◆ digitalWrite()

void IoExpanderBase::digitalWrite ( uint8_t  pin,
bool  value 
)
inline

Write a digital value to an output pin.

The value is cached in _outputStates and the hardware is updated via digitalWriteImpl().

Parameters
pinPin number (0 .. pinCount()-1). Must be configured as OUTPUT.
valuetrue for HIGH, false for LOW.

Definition at line 150 of file IoExpanderBase.hpp.

References _outputStates, _pinCount, _pinModes, digitalWriteImpl(), and SENSORLIB_LOG_E.

Referenced by AW9364LedDriver::_digitalWrite(), IoExpanderSPI::beginSPI(), digitalToggle(), digitalWritePort(), loop(), testInit(), TouchDrvDigitalWrite(), and IoExpanderSPI::transferDataBits().

◆ digitalWriteImpl()

virtual void IoExpanderBase::digitalWriteImpl ( uint8_t  pin,
bool  value 
)
protectedpure virtual

Hardware‑specific implementation of digitalWrite().

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

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

Implemented in IoExpanderPCA9570, and IoExpanderXL9555.

Referenced by digitalWrite().

◆ digitalWritePort()

virtual void IoExpanderBase::digitalWritePort ( uint16_t  mask,
uint16_t  values 
)
inlinevirtual

Write to multiple pins simultaneously using a mask.

The default implementation writes each selected pin individually. Derived classes may override this with a more efficient register‑based write when the hardware supports it.

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

Reimplemented in IoExpanderPCA9570, and IoExpanderXL9555.

Definition at line 231 of file IoExpanderBase.hpp.

References _pinCount, digitalWrite(), and i.

◆ initImpl()

virtual bool IoExpanderBase::initImpl ( uint8_t  param)
protectedpure virtual

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.

Implemented in IoExpanderPCA9570, and IoExpanderXL9555.

◆ pinCount()

uint8_t IoExpanderBase::pinCount ( ) const
inline

Get the total number of pins provided by this expander.

Returns
uint8_t Pin count.

Definition at line 268 of file IoExpanderBase.hpp.

References _pinCount.

Referenced by IoExpanderBase().

◆ pinMode()

void IoExpanderBase::pinMode ( uint8_t  pin,
uint8_t  mode 
)
inline

Set the direction of a GPIO pin.

The mode is cached in _pinModes and the hardware is updated via the pure virtual function pinModeImpl().

Parameters
pinPin number (0 .. pinCount()-1).
modePin mode: INPUT or OUTPUT (as defined in Arduino framework).

Definition at line 122 of file IoExpanderBase.hpp.

References _pinCount, _pinModes, pinModeImpl(), and SENSORLIB_LOG_E.

Referenced by AW9364LedDriver::_pinMode(), IoExpanderSPI::beginSPI(), testInit(), and TouchDrvPinMode().

◆ pinModeImpl()

virtual void IoExpanderBase::pinModeImpl ( uint8_t  pin,
uint8_t  mode 
)
protectedpure virtual

Hardware‑specific implementation of pinMode().

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

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

Implemented in IoExpanderPCA9570, and IoExpanderXL9555.

Referenced by pinMode().

◆ pinToMask()

uint16_t IoExpanderBase::pinToMask ( uint8_t  pin) const
inline

Convert a pin number to its corresponding bitmask.

This utility function returns a 16-bit mask with a single bit set at the position corresponding to the given pin number. The mask can be used with batch functions such as digitalWritePort() or configPins().

Parameters
pinPin number (0 to pinCount()-1).
Returns
uint16_t Bitmask with bit (pin) set to 1. Returns 0 and logs an error if the pin number is out of range.

Definition at line 285 of file IoExpanderBase.hpp.

References _pinCount, and SENSORLIB_LOG_E.

Member Data Documentation

◆ _outputStates

std::unique_ptr<bool[]> IoExpanderBase::_outputStates
protected

◆ _pinCount

uint8_t IoExpanderBase::_pinCount
protected

Number of GPIO pins.

Definition at line 347 of file IoExpanderBase.hpp.

Referenced by digitalRead(), digitalReadPort(), digitalWrite(), digitalWritePort(), IoExpanderBase(), pinCount(), pinMode(), and pinToMask().

◆ _pinModes

std::unique_ptr<uint8_t[]> IoExpanderBase::_pinModes
protected

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