ruspiro-i2c

Crates.ioruspiro-i2c
lib.rsruspiro-i2c
version0.4.0
sourcesrc
created_at2019-08-09 12:22:42.476792
updated_at2021-09-07 12:26:05.068662
descriptionRaspberry Pi - I²C bus interface implementation
homepage
repositoryhttps://github.com/RusPiRo/ruspiro-i2c/tree/v0.4.0
max_upload_size
id155351
size45,833
(2ndTaleStudio)

documentation

https://docs.rs/ruspiro-i2c/0.4.0

README

RusPiRo I²C Bus interface crate

Simple and safe access to the I²C bus available on the Raspberry Pi. This implementation will need the GPIO pins 2 and 3 for exclusive use for the I²C bus.

Travis-CI Status Latest Version Documentation License

Dependency

The I²C implementation requires an allocator to be build into the final binary. It's recommended to use the ruspiro-allocator with this crate.

Usage

To use the crate just add the following dependency to your Cargo.toml file:

[dependencies]
ruspiro-i2c = "0.3"

Once done the access to the I²C bus interface is available in your rust files like so:

use ruspiro_i2c::I2C;

fn demo() {
    I2C.take_for(|i2c| {
        if i2c.initialize(250_000_000, true).is_ok() {
            // now scan I2C devices connected to RPi, this will print their
            // addresses to the console
            let devices = i2c.scan().unwrap();
            for d in devices {
                info!("device detected at 0x{:2X}", d);
            }
        }
    });
}

To configure and use a device connected to the I²C bus you can simply use the provided functions provided when taking the I2C. First off you may check whether the device is connected to the bus before continuing with the config:

use ruspiro_i2c::I2C;

fn demo() {
    let device_addr = 0x68;
    I2C.take_for(|i2c| {
        if i2c.check_device(device_addr).is_ok() {
            // configure the device...
            // as arbitary example pass value 0x1 to the 8bit register 0x10
            i2c.write_register_u8(device_addr, 0x10, 0x1);
        }
    });
}

License

Licensed under Apache License, Version 2.0, (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)

Commit count: 0

cargo fmt