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

Software SPI master class for driving GPIO expanders via bit-banging. More...

#include <IoExpanderSPI.hpp>

Public Member Functions

 IoExpanderSPI ()
 Construct a new IoExpanderSPI object.
 
 ~IoExpanderSPI ()=default
 Destroy the IoExpanderSPI object.
 
bool beginSPI (IoExpanderBase &base, int mosi, int miso, int sck, int cs)
 Initialize the software SPI interface.
 
void setBitOrder (uint8_t order)
 Set the bit order for SPI transfers.
 
uint8_t transfer8 (uint8_t val)
 Transfer 8 bits over SPI.
 
uint16_t transfer9 (uint16_t val)
 Transfer 9 bits over SPI.
 
uint32_t transferDataBits (uint32_t val, uint32_t bits)
 Transfer an arbitrary number of bits over SPI.
 

Detailed Description

Software SPI master class for driving GPIO expanders via bit-banging.

This class implements a software SPI master using the pin control functions provided by any IoExpanderBase-derived class. It allows expanders without dedicated hardware SPI to be controlled via general-purpose I/O pins.

Definition at line 44 of file IoExpanderSPI.hpp.

Constructor & Destructor Documentation

◆ IoExpanderSPI()

IoExpanderSPI::IoExpanderSPI ( )
inline

Construct a new IoExpanderSPI object.

Initializes all pin numbers to -1 (unused) and bit order to 0 (MSB first).

Definition at line 52 of file IoExpanderSPI.hpp.

◆ ~IoExpanderSPI()

IoExpanderSPI::~IoExpanderSPI ( )
default

Destroy the IoExpanderSPI object.

Member Function Documentation

◆ beginSPI()

bool IoExpanderSPI::beginSPI ( IoExpanderBase base,
int  mosi,
int  miso,
int  sck,
int  cs 
)
inline

Initialize the software SPI interface.

Configures the specified pins for SPI operation using the provided expander instance. MOSI and SCK are mandatory and set as outputs; CS is optional (if not -1) and set as output high; MISO is optional and set as input if provided.

Parameters
baseReference to the IoExpanderBase instance used for pin control.
mosiMOSI pin number (must be specified).
misoMISO pin number (set to -1 if not used).
sckSCK pin number (must be specified).
csChip select pin number (set to -1 if not used).
Returns
true Initialization successful.
false Initialization failed (e.g., missing MOSI or SCK).

Definition at line 75 of file IoExpanderSPI.hpp.

References IoExpanderBase::digitalWrite(), IoExpanderBase::pinMode(), and SENSORLIB_LOG_E.

◆ setBitOrder()

void IoExpanderSPI::setBitOrder ( uint8_t  order)
inline

Set the bit order for SPI transfers.

Parameters
order0 for MSB first, 1 for LSB first. Only the least significant bit is used; any other bits are ignored.

Definition at line 110 of file IoExpanderSPI.hpp.

◆ transfer8()

uint8_t IoExpanderSPI::transfer8 ( uint8_t  val)
inline

Transfer 8 bits over SPI.

Parameters
val8-bit data to send.
Returns
uint8_t 8-bit data received.

Definition at line 121 of file IoExpanderSPI.hpp.

References transferDataBits().

◆ transfer9()

uint16_t IoExpanderSPI::transfer9 ( uint16_t  val)
inline

Transfer 9 bits over SPI.

Useful for devices requiring 9-bit transfers (e.g., certain sensors).

Parameters
val9-bit data to send (only lower 9 bits are used).
Returns
uint16_t 9-bit data received (only lower 9 bits are valid).

Definition at line 134 of file IoExpanderSPI.hpp.

References transferDataBits().

◆ transferDataBits()

uint32_t IoExpanderSPI::transferDataBits ( uint32_t  val,
uint32_t  bits 
)
inline

Transfer an arbitrary number of bits over SPI.

Performs a full-duplex software SPI transfer of the specified number of bits. The data is shifted out MSB-first or LSB-first according to the current bit order.

Parameters
valData to send (only the lower 'bits' bits are used).
bitsNumber of bits to transfer (1-32).
Returns
uint32_t Data received (only the lower 'bits' bits are valid).

Definition at line 149 of file IoExpanderSPI.hpp.

References IoExpanderBase::digitalRead(), IoExpanderBase::digitalWrite(), i, and SENSORLIB_LOG_E.

Referenced by transfer8(), and transfer9().


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