Giter Site home page Giter Site logo

eldruin / pcf857x-rs Goto Github PK

View Code? Open in Web Editor NEW
8.0 4.0 3.0 86 KB

Platform agnostic driver for the PCF8574, PCF8574A and PCF8575 I/O expanders written in Rust using embedded-hal

License: Apache License 2.0

Rust 100.00%
rust embedded embedded-hal embedded-hal-driver no-std io-expander rust-library driver expander i2c

pcf857x-rs's Introduction

Rust PCF857x I/O Expanders Driver

crates.io Docs Build Status Coverage Status

This is a platform agnostic Rust driver for the PCF8574, PCF8574A and PCF8575 I2C I/O expanders, based on the embedded-hal traits.

This driver allows you to:

  • Set all the outputs to 0 or 1 at once. See set().
  • Read selected inputs. See get().
  • Set all the outputs repeatedly looping through an array. See write_array().
  • Read selected inputs repeatedly filling up an array. See read_array().
  • Split the device into individual input/output pins. See split().

The devices

The devices consist of 8 or 16 quasi-bidirectional ports, I²C-bus interface, three hardware address inputs and interrupt output. The quasi-bidirectional port can be independently assigned as an input to monitor interrupt status or keypads, or as an output to activate indicator devices such as LEDs.

The active LOW open-drain interrupt output (INT) can be connected to the interrupt logic of the microcontroller and is activated when any input state differs from its corresponding input port register state.

Datasheets:

Usage

Please find additional examples using hardware in this repository: driver-examples

use linux_embedded_hal::I2cdev;
use pcf857x::{Pcf8574, PinFlag, SlaveAddr};

fn main() {
    let dev = I2cdev::new("/dev/i2c-1").unwrap();
    let address = SlaveAddr::default();
    let mut expander = Pcf8574::new(dev, address);
    let output_pin_status = 0b1010_1010;
    expander.set(output_pin_status).unwrap();

    let pins_to_be_read = PinFlag::P0 | PinFlag::P7;
    let status = expander.get(pins_to_be_read).unwrap();

    println!("Input pin status: {}", status);
}

Support

For questions, issues, feature requests, and other changes, please file an issue in the github project.

License

Licensed under either of

at your option.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

pcf857x-rs's People

Contributors

eldruin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pcf857x-rs's Issues

Pins don't implement Peripheral

Looks like there is a lack of impl DerefMut for pins.
Example:

fn main() -> eyre::Result<()> {
    // It is necessary to call this function once. Otherwise some patches to the runtime
    // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
    esp_idf_svc::sys::link_patches();

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

    let i2c = peripherals.i2c0;
    let sda = peripherals.pins.gpio5;
    let scl = peripherals.pins.gpio6;

    let config = I2cConfig::new().baudrate(100.kHz().into());
    let i2c = I2cDriver::new(i2c, sda, scl, &config)?;
    let mut expander: Pcf8574<I2cDriver> = pcf857x::Pcf8574::new(i2c, SlaveAddr::Alternative(true, true, true));
    let parts = expander.split();

    fn test<'d>(p: impl Peripheral<P = impl OutputPin> + 'd) {
        todo!()
    }
    
    test(parts.p0); // doesn't work
}

Error:

error[E0277]: the trait bound `P0<'_, Pcf8574<I2cDriver<'_>>, I2cError>: DerefMut` is not satisfied
  --> examples/uln2003_via_pcf8574.rs:42:10
   |
42 |     test(parts.p0);
   |     ---- ^^^^^^^^ the trait `DerefMut` is not implemented for `P0<'_, Pcf8574<I2cDriver<'_>>, I2cError>`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following other types implement trait `Peripheral`:
             AnyIOPin
             AnyInputPin
             AnyOutputPin
             Modem
             ADC1
             ADC2
             CAN
             Gpio0
           and 46 others
   = note: required for `P0<'_, Pcf8574<I2cDriver<'_>>, I2cError>` to implement `Peripheral`
note: required by a bound in `test`
  --> examples/uln2003_via_pcf8574.rs:38:25
   |
38 |     fn test<'d>(p: impl Peripheral<P = impl OutputPin> + 'd) {
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `test`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.