|
SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
|
Abstract base class for GPIO expander devices. More...
#include <IoExpanderBase.hpp>
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) | |
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.
|
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.
| pinCount | Number of GPIO pins provided by the expander. |
Definition at line 54 of file IoExpanderBase.hpp.
References _outputStates, _pinCount, _pinModes, and pinCount().
|
virtualdefault |
Virtual destructor.
Ensures proper cleanup of derived classes. The std::unique_ptr members automatically release their memory.
|
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.
| pinMask | Bitmask 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. |
| mode | Desired mode: INPUT or OUTPUT (as defined in the Arduino framework). |
Definition at line 99 of file IoExpanderBase.hpp.
References _pinModes, configPinsImpl(), and SENSORLIB_LOG_E.
Referenced by setup().
|
protectedpure virtual |
Hardware‑specific implementation of batch pin mode configuration.
| pinMask | Bitmask of pins to configure. |
| mode | Desired mode (INPUT or OUTPUT). |
Implemented in IoExpanderPCA9570, and IoExpanderXL9555.
Referenced by configPins().
|
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.
|
inline |
Read the digital value from an input pin.
Reads the current state from hardware via digitalReadImpl().
| pin | Pin number (0 .. pinCount()-1). Must be configured as INPUT. |
Definition at line 182 of file IoExpanderBase.hpp.
References _pinCount, _pinModes, digitalReadImpl(), and SENSORLIB_LOG_E.
Referenced by digitalReadPort(), loop(), TouchDrvDigitalRead(), and IoExpanderSPI::transferDataBits().
|
protectedpure virtual |
Hardware‑specific implementation of digitalRead().
Called by digitalRead() after validating arguments.
| pin | Pin number. |
Implemented in IoExpanderPCA9570, and IoExpanderXL9555.
Referenced by digitalRead().
|
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.
Reimplemented in IoExpanderPCA9570, and IoExpanderXL9555.
Definition at line 248 of file IoExpanderBase.hpp.
References _outputStates, _pinCount, _pinModes, digitalRead(), i, and SENSORLIB_LOG_E.
|
inline |
Toggle the state of an output pin.
Inverts the current cached output state and writes it to the pin.
| pin | Pin 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().
|
inline |
Write a digital value to an output pin.
The value is cached in _outputStates and the hardware is updated via digitalWriteImpl().
| pin | Pin number (0 .. pinCount()-1). Must be configured as OUTPUT. |
| value | true 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().
|
protectedpure virtual |
Hardware‑specific implementation of digitalWrite().
Called by digitalWrite() after validating arguments and updating the cache.
| pin | Pin number. |
| value | true for HIGH, false for LOW. |
Implemented in IoExpanderPCA9570, and IoExpanderXL9555.
Referenced by digitalWrite().
|
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.
| mask | Bitmask where a 1 indicates the pin should be updated. |
| values | Bitmask 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.
|
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).
| param | Opaque parameter passed from begin() (typically I2C address). |
Implemented in IoExpanderPCA9570, and IoExpanderXL9555.
|
inline |
Get the total number of pins provided by this expander.
Definition at line 268 of file IoExpanderBase.hpp.
References _pinCount.
Referenced by IoExpanderBase().
|
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().
| pin | Pin number (0 .. pinCount()-1). |
| mode | Pin 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().
|
protectedpure virtual |
Hardware‑specific implementation of pinMode().
Called by pinMode() after validating arguments and updating the cache.
| pin | Pin number. |
| mode | Desired mode (INPUT or OUTPUT). |
Implemented in IoExpanderPCA9570, and IoExpanderXL9555.
Referenced by pinMode().
|
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().
| pin | Pin number (0 to pinCount()-1). |
Definition at line 285 of file IoExpanderBase.hpp.
References _pinCount, and SENSORLIB_LOG_E.
|
protected |
Cached output states (true = HIGH)
Definition at line 349 of file IoExpanderBase.hpp.
Referenced by digitalReadPort(), digitalToggle(), digitalWrite(), IoExpanderPCA9570::digitalWritePort(), IoExpanderXL9555::digitalWritePort(), and IoExpanderBase().
|
protected |
Number of GPIO pins.
Definition at line 347 of file IoExpanderBase.hpp.
Referenced by digitalRead(), digitalReadPort(), digitalWrite(), digitalWritePort(), IoExpanderBase(), pinCount(), pinMode(), and pinToMask().
|
protected |
Cached pin modes (INPUT/OUTPUT)
Definition at line 348 of file IoExpanderBase.hpp.
Referenced by configPins(), IoExpanderPCA9570::configPinsImpl(), IoExpanderXL9555::configPinsImpl(), digitalRead(), digitalReadPort(), digitalToggle(), digitalWrite(), IoExpanderBase(), and pinMode().