Crate pcf857x_simple

source ·
Expand description

A Rust driver for the PCF857x I/O expanders, based on the embedded-hal traits.

This driver is very simple. You can read/write all pins or individual pins. Driver use a cache to avoid read each time you want know pins status or write.

§Devices

Devices consist of 8 or 16 bidirectional ports, I²C-bus interface, with three hardware address inputs and interrupt output.

Datasheets:

§How use-it (ESP32 example)?

§With the default address

Import this crate and an embedded_hal implementation, then instantiate the device:

use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::i2c::I2cDriver;
use esp_idf_hal::i2c::config::Config
use pcf857x_simple::pcf8574::Pcf8574;
use pcf857x_simple::PCF857X_DEFAULT_ADDRESS;

esp_idf_sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();

let peripherals = Peripherals::take().unwrap();

let scl = peripherals.pins.gpio22;
let sda = peripherals.pins.gpio21;

let i2c_config = Config::new()
  .baudrate(KiloHertz(100).into())
  .scl_enable_pullup(true)
  .sda_enable_pullup(true);

let i2c_driver = I2cDriver::new(peripherals.i2c0, sda, scl, &i2c_config).unwrap();

let mut expander = Pcf8574::new(i2c_driver, PCF857X_DEFAULT_ADDRESS);

§With helper address

use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::i2c::I2cDriver;
use esp_idf_hal::i2c::config::Config
use pcf857x_simple::pcf8574::Pcf8574;
use pcf857x_simple::pcf857x_address;

esp_idf_sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();

let peripherals = Peripherals::take().unwrap();

let scl = peripherals.pins.gpio22;
let sda = peripherals.pins.gpio21;

let i2c_config = Config::new()
  .baudrate(KiloHertz(100).into())
  .scl_enable_pullup(true)
  .sda_enable_pullup(true);

let i2c_driver = I2cDriver::new(peripherals.i2c0, sda, scl, &i2c_config).unwrap();

let mut expander = Pcf8574::new(i2c_driver, pcf857x_address(false, false, false));

§With direct address

use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::i2c::I2cDriver;
use esp_idf_hal::i2c::config::Config
use pcf857x_simple::pcf8574::Pcf8574;
use pcf857x_simple::pcf857x_address;

esp_idf_sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();

let peripherals = Peripherals::take().unwrap();

let scl = peripherals.pins.gpio22;
let sda = peripherals.pins.gpio21;

let i2c_config = Config::new()
  .baudrate(KiloHertz(100).into())
  .scl_enable_pullup(true)
  .sda_enable_pullup(true);

let i2c_driver = I2cDriver::new(peripherals.i2c0, sda, scl, &i2c_config).unwrap();

let mut expander = Pcf8574::new(i2c_driver, 0x24);

§Set P0 and P7 high

use pcf857x_simple::Pin;
...
let _ = expander.clear();
let _ = expander.up_pins(&[Pin::P0, Pin::P7]);

§Read P0 and P7 high direct from device

use pcf857x_simple::Pin;
...
let _ = expander.clear();
let _ = expander.read_pin(Pin::P0);
let _ = expander.read_pin(Pin::P7);

§Read P0 and P7 high direct from cache

use pcf857x_simple::Pin;
...
let _ = expander.clear();
let _ = expander.get_pin_from_cache(Pin::P0);
let _ = expander.get_pin_from_cache(Pin::P7);

Modules§

Enums§

Constants§

Functions§

  • Helper to set address depending of address’ pin’s device