This is a shameless copy of a shameless copy of Zac Berkowitz' tca9535-rs library.
WARNING: Due to time constraints, this is not properly tested, although it does work for what I am using it for. This is an invitation to help me test it out and give me a PR when you find problems. :)
The PCA9554 is an 8-bit I/O expander for the two-line bidirectional bus (I2C) is designed for 1.65-V to 5.5-V VCC operation. It provides general-purpose remote I/O expansion for most micro-controller families through the I2C interface (serial clock, SCL, and serial data, SDA, pins).
Include the library as a dependency in your Cargo.toml
[dependencies.pca9554] version = "0.1"
Use embedded-hal
implementation to get the i2c bus for the chip then
create the handle.
use Pca9554::{Pca9554, Address, Port};
// Obtain an i2c bus using embedded-hal
// let mut i2c = ...;
// Note that the handle does *not* capture the i2c bus
// object and that the i2c bus object must be supplied for each method call.
// This slight inconvenience allows sharing the i2c bus between peripherals.
let gpio = Pca9554::new(&i2c, Address::ADDR_0x20);
// Set all outputs low
gpio.clear_outputs(&mut i2c)?;
// Set port P01 and P05 as inputs and the rest as outputs.
gpio.write_config(&mut i2c, Port::P01 | Port::P05)?;
// Read pin input logic levels
let inputs = gpio.read_inputs(&mut i2c)?;
// Do something if port P01 is high:
if inputs & Port::P01 {
// ...
}