6 releases (stable)

2.0.0 Jan 21, 2024
1.2.0 Dec 31, 2022
1.1.0 Jun 14, 2022
1.0.0 Mar 20, 2022
0.0.1 Dec 25, 2021

#132 in Embedded development

Download history 1/week @ 2024-01-23 1/week @ 2024-01-30 2/week @ 2024-02-06 75/week @ 2024-02-13 178/week @ 2024-02-20 243/week @ 2024-02-27 328/week @ 2024-03-05 218/week @ 2024-03-12 71/week @ 2024-03-19 167/week @ 2024-03-26 226/week @ 2024-04-02 247/week @ 2024-04-09 176/week @ 2024-04-16

829 downloads per month

MIT license

69KB
807 lines

PCA9535

crates.io documentation license

PCA9535 IO-Expander driver using embedded-hal.

Features

Two expander modes:

  • Immediate
  • Cached

Immediate mode issues an i2c bus transaction on each function call, behaving like a normal i2c device library does.

Cached mode takes advantage of the interrupt pin of the device, which indicates a change in the register value. The driver holds an internal representation of the device's registers; thus, it only issues a read if any data changed as indicated by the interrupt pin. This mode reduces read access on the bus significantly compared to immediate mode.

Two ways of interacting:

  • Standard Interface
  • HAL Pin Interface

The standard interface offers all the needed functions to interact with the device's GPIO pins.

The HAL Pin Interface offers a way to use the Expander GPIO as embedded-hal GPIO, which makes it possible to use them in any other libraries using embedded-hal. The pins are usable across threads using an ExpanderMutex.

Usage Example

This is a basic usage example; for more information, visit the docs.

Immediate expander using standard interface:

use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};

let i2c = I2c::new().unwrap();

let mut expander = Pca9535Immediate::new(i2c, 32);

expander.pin_into_input(GPIOBank::Bank0, 4).unwrap();
expander.pin_into_output(GPIOBank::Bank1, 6).unwrap();

if expander.pin_is_high(GPIOBank::Bank0, 4).unwrap() {
    expander.pin_set_high(GPIOBank::Bank1, 6).unwrap();
}

Cached expander using hal pin interface:

use std::sync::Mutex;
use embedded_hal::digital::blocking::{InputPin, OutputPin};
use pca9535::{ExpanderInputPin, ExpanderOutputPin, GPIOBank, IoExpander, Pca9535Cached, PinState};

let i2c = I2c::new().unwrap();
let interrupt_pin = Gpio::new().unwrap().get(1).unwrap().into_input();

let expander = Pca9535Cached::new(i2c, 32, interrupt_pin, true).unwrap();
let io_expander: IoExpander<_, _, Mutex<_>> = IoExpander::new(expander);

let mut input_pin = ExpanderInputPin::new(&io_expander, GPIOBank::Bank0, 4).unwrap();
let mut output_pin = ExpanderOutputPin::new(&io_expander, GPIOBank::Bank1, 6, PinState::Low).unwrap();

if input_pin.is_high().unwrap() {
    output_pin.set_high().unwrap();
}

Changelog

See CHANGELOG.md or release page for details.

Dependencies