I2cDiscreteIoExpander  v4.0.1
Arduino library for TI PCF8575C 16-bit I2C I/O expander.
I2cDiscreteIoExpander Class Reference

Public Member Functions

 I2cDiscreteIoExpander (uint8_t)
 Constructor. More...
 
uint8_t digitalRead ()
 Retrieve discrete values from device. More...
 
uint8_t digitalWrite (uint16_t)
 Write discrete values to device. More...
 
uint8_t getAddress ()
 Retrieve device address. More...
 
uint16_t getPorts ()
 Retrieve ports 1 (P17..P10), 0 (P07..P00). More...
 
void enableBitwiseInversion ()
 Enable bitwise inversion. More...
 
void disableBitwiseInversion ()
 Disable bitwise inversion. More...
 
bool isInverted ()
 Indicate whether bitwise inversion is enabled. More...
 

Private Attributes

uint8_t address_
 Device address as defined by pins A2, A1, A0.
 
uint16_t ports_
 Storage object for I2cDiscreteIoExpander ports 1 (P17..P10), 0 (P07..P00).
 
bool shouldInvert_
 Flag indicating whether bits are to be inverted before read/write (false=don't invert, true=invert).
 

Static Private Attributes

static const uint8_t BASE_ADDRESS_ = 0x20
 Factory pre-set slave address.
 

Related Functions

(Note that these are not member functions.)

static const uint8_t TWI_SUCCESS = 0
 I2C/TWI success (transaction was successful).
 
static const uint8_t TWI_DEVICE_NACK = 2
 I2C/TWI device not present (address sent, NACK received).
 
static const uint8_t TWI_DATA_NACK = 3
 I2C/TWI data not received (data sent, NACK received).
 
static const uint8_t TWI_ERROR = 4
 I2C/TWI other error.
 

Detailed Description

Constructor & Destructor Documentation

§ I2cDiscreteIoExpander()

I2cDiscreteIoExpander::I2cDiscreteIoExpander ( uint8_t  address)

Constructor.

Assigns device address, resets storage object, enables bitwise inversion.

Required Function:
Call this to construct I2cDiscreteIoExpander object.
Usage:
...
I2cDiscreteIoExpander exampleA(1); // device with address 1
I2cDiscreteIoExpander exampleB[2] = { 2, 3 }; // devices with addresses 2, 3
I2cDiscreteIoExpander exampleC[2] = { I2cDiscreteIoExpander(4), I2cDiscreteIoExpander(5) }; // alternate constructor syntax; devices with addresses 4, 5
I2cDiscreteIoExpander exampleD[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; // addresses 0..7
...

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Assigns device address, resets storage object, enables bitwise inversion.

Usage:
...
I2cDiscreteIoExpander device; // implies device address 0
...
44 {
45  address_ = address & 0b111;
46  ports_ = 0;
47  shouldInvert_ = true;
48 }
uint8_t address_
Device address as defined by pins A2, A1, A0.
Definition: I2cDiscreteIoExpander.h:152
bool shouldInvert_
Flag indicating whether bits are to be inverted before read/write (false=don't invert, true=invert).
Definition: I2cDiscreteIoExpander.h:158
uint16_t ports_
Storage object for I2cDiscreteIoExpander ports 1 (P17..P10), 0 (P07..P00).
Definition: I2cDiscreteIoExpander.h:155
Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

§ digitalRead()

uint8_t I2cDiscreteIoExpander::digitalRead ( )

Retrieve discrete values from device.

Required Function:
Call this from within loop() in order to read from device.
Return values
0success
1length too long for buffer
2address send, NACK received (device not on bus)
3data send, NACK received
4other twi error (lost bus arbitration, bus error, ...)
Usage:
...
I2cDiscreteIoExpander device;
...
uint8_t status = device.digitalRead();
if (TWI_SUCCESS == status)
{
// do something with device.getPorts()
}
...
Examples:
examples/BareMinimum/BareMinimum.ino, and examples/MultipleDevices/MultipleDevices.ino.
88 {
89  uint8_t hi, lo, status;
90 
91  Wire.beginTransmission(BASE_ADDRESS_ | address_);
92  status = Wire.endTransmission();
93 
94  if (TWI_SUCCESS == status)
95  {
96  if (Wire.requestFrom(BASE_ADDRESS_ | address_, 2) == 2)
97  {
98  lo = Wire.read();
99  hi = Wire.read();
100  ports_ = shouldInvert_ ? word(~hi, ~lo) : word(hi, lo);
101  }
102  else
103  {
104  return TWI_ERROR;
105  }
106  }
107 
108  return status;
109 }
uint8_t address_
Device address as defined by pins A2, A1, A0.
Definition: I2cDiscreteIoExpander.h:152
static const uint8_t BASE_ADDRESS_
Factory pre-set slave address.
Definition: I2cDiscreteIoExpander.h:162
static const uint8_t TWI_SUCCESS
I2C/TWI success (transaction was successful).
Definition: I2cDiscreteIoExpander.h:106
static const uint8_t TWI_ERROR
I2C/TWI other error.
Definition: I2cDiscreteIoExpander.h:121
bool shouldInvert_
Flag indicating whether bits are to be inverted before read/write (false=don't invert, true=invert).
Definition: I2cDiscreteIoExpander.h:158
uint16_t ports_
Storage object for I2cDiscreteIoExpander ports 1 (P17..P10), 0 (P07..P00).
Definition: I2cDiscreteIoExpander.h:155

§ digitalWrite()

uint8_t I2cDiscreteIoExpander::digitalWrite ( uint16_t  ports)

Write discrete values to device.

Required Function:
Call this from within loop() in order to write to device.
Parameters
portsword to be written to device (0x0000..0xFFFF)
Return values
0success
1length too long for buffer
2address send, NACK received (device not on bus)
3data send, NACK received
4other twi error (lost bus arbitration, bus error, ...)
Usage:
...
I2cDiscreteIoExpander device;
...
uint8_t status = device.digitalWrite(0xFFFF);
if (TWI_SUCCESS == status)
{
// do something
}
...
Examples:
examples/BareMinimum/BareMinimum.ino, and examples/MultipleDevices/MultipleDevices.ino.
133 {
134  ports_ = shouldInvert_ ? ~ports : ports;
135  Wire.beginTransmission(BASE_ADDRESS_ | address_);
136  Wire.write(lowByte(ports_));
137  Wire.write(highByte(ports_));
138  // Wire.write(lowByte(shouldInvert_ ? ~ports_ : ports_));
139  // Wire.write(highByte(shouldInvert_ ? ~ports_ : ports_));
140 
141  return Wire.endTransmission();
142 }
uint8_t address_
Device address as defined by pins A2, A1, A0.
Definition: I2cDiscreteIoExpander.h:152
static const uint8_t BASE_ADDRESS_
Factory pre-set slave address.
Definition: I2cDiscreteIoExpander.h:162
bool shouldInvert_
Flag indicating whether bits are to be inverted before read/write (false=don't invert, true=invert).
Definition: I2cDiscreteIoExpander.h:158
uint16_t ports_
Storage object for I2cDiscreteIoExpander ports 1 (P17..P10), 0 (P07..P00).
Definition: I2cDiscreteIoExpander.h:155

§ getAddress()

uint8_t I2cDiscreteIoExpander::getAddress ( )

Retrieve device address.

Optional Function (Troubleshooting):
This function is for testing and troubleshooting.
Returns
address of device (0..7)
Usage:
...
I2cDiscreteIoExpander device;
...
address = device.getAddress();
...
Examples:
examples/BareMinimum/BareMinimum.ino.
157 {
158  return address_;
159 }
uint8_t address_
Device address as defined by pins A2, A1, A0.
Definition: I2cDiscreteIoExpander.h:152

§ getPorts()

uint16_t I2cDiscreteIoExpander::getPorts ( )

Retrieve ports 1 (P17..P10), 0 (P07..P00).

Required Function:
Call this from within loop() to retrieve ports.
Returns
ports of device (0x0000..0xFFFF)
Usage:
...
I2cDiscreteIoExpander device;
...
ports = device.getPorts();
...
Examples:
examples/BareMinimum/BareMinimum.ino.
174 {
175  return ports_;
176 }
uint16_t ports_
Storage object for I2cDiscreteIoExpander ports 1 (P17..P10), 0 (P07..P00).
Definition: I2cDiscreteIoExpander.h:155

§ enableBitwiseInversion()

void I2cDiscreteIoExpander::enableBitwiseInversion ( )

Enable bitwise inversion.

All bits will be inverted prior to future read/write operations.

Usage:
...
I2cDiscreteIoExpander device;
...
device.enableBitwiseInversion(); // bits will now inverted
...
See also
I2cDiscreteIoExpander::disableBitwiseInversion()
I2cDiscreteIoExpander::isInverted()
192 {
194  shouldInvert_ = true;
195 }
bool shouldInvert_
Flag indicating whether bits are to be inverted before read/write (false=don't invert, true=invert).
Definition: I2cDiscreteIoExpander.h:158
uint16_t ports_
Storage object for I2cDiscreteIoExpander ports 1 (P17..P10), 0 (P07..P00).
Definition: I2cDiscreteIoExpander.h:155

§ disableBitwiseInversion()

void I2cDiscreteIoExpander::disableBitwiseInversion ( )

Disable bitwise inversion.

Bits will not be inverted prior to future read/write operations.

Usage:
...
I2cDiscreteIoExpander device;
...
device.disableBitwiseInversion(); // bits will no longer be inverted
...
See also
I2cDiscreteIoExpander::enableBitwiseInversion()
I2cDiscreteIoExpander::isInverted()
211 {
213  shouldInvert_ = false;
214 }
bool shouldInvert_
Flag indicating whether bits are to be inverted before read/write (false=don't invert, true=invert).
Definition: I2cDiscreteIoExpander.h:158
uint16_t ports_
Storage object for I2cDiscreteIoExpander ports 1 (P17..P10), 0 (P07..P00).
Definition: I2cDiscreteIoExpander.h:155

§ isInverted()

bool I2cDiscreteIoExpander::isInverted ( )

Indicate whether bitwise inversion is enabled.

Returns
status of bitwise inversion (false=not inverted, true=inverted)
Usage:
...
I2cDiscreteIoExpander device;
...
if (device.isInverted())
{
// do something
}
...
See also
I2cDiscreteIoExpander::enableBitwiseInversion()
I2cDiscreteIoExpander::disableBitwiseInversion()
233 {
234  return shouldInvert_;
235 }
bool shouldInvert_
Flag indicating whether bits are to be inverted before read/write (false=don't invert, true=invert).
Definition: I2cDiscreteIoExpander.h:158

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