Crates.io | ruspiro-i2c |
lib.rs | ruspiro-i2c |
version | 0.4.0 |
source | src |
created_at | 2019-08-09 12:22:42.476792 |
updated_at | 2021-09-07 12:26:05.068662 |
description | Raspberry Pi - I²C bus interface implementation |
homepage | |
repository | https://github.com/RusPiRo/ruspiro-i2c/tree/v0.4.0 |
max_upload_size | |
id | 155351 |
size | 45,833 |
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.
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.
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);
}
});
}
Licensed under Apache License, Version 2.0, (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)