bme680-rust/src/i2c.rs

79 lines
1.9 KiB
Rust
Raw Normal View History

use crate::Bme680Error;
use crate::Bme680Error::{I2CRead, I2CWrite};
2024-02-27 20:09:16 +00:00
use embedded_hal::i2c::I2c;
///
/// Represents the I2C address of the BME680 Sensor.
///
2024-02-27 20:12:24 +00:00
#[derive(Debug, Clone, Copy, Default)]
pub enum Address {
/// Primary Address 0x76
2024-02-27 20:12:24 +00:00
#[default]
Primary,
/// Secondary Address 0x77
Secondary,
/// Alternative address
Other(u8),
}
impl Address {
pub fn addr(&self) -> u8 {
match &self {
Address::Primary => 0x76u8,
Address::Secondary => 0x77u8,
Address::Other(addr) => *addr,
}
}
}
/// I2CUtility is a simple wrapper over the I2c trait to make reading and writing data easier.
pub(crate) struct I2CUtility {}
impl I2CUtility {
/// Reads a byte from the I2C bus.
pub fn read_byte<I2C: I2c>(
i2c_handle: &mut I2C,
device_address: u8,
register_address: u8,
2024-02-27 20:09:16 +00:00
) -> Result<u8, Bme680Error> {
let mut buf = [0; 1];
2024-02-27 20:09:16 +00:00
i2c_handle
.write(device_address, &[register_address])
.map_err(|_e| I2CWrite)?;
match i2c_handle.read(device_address, &mut buf) {
Ok(()) => Ok(buf[0]),
Err(_e) => Err(I2CRead),
}
}
/// Reads bytes from the I2C bus.
pub fn read_bytes<I2C: I2c>(
i2c_handle: &mut I2C,
device_address: u8,
register_address: u8,
buffer: &mut [u8],
2024-02-27 20:09:16 +00:00
) -> Result<(), Bme680Error> {
i2c_handle
.write(device_address, &[register_address])
.map_err(|_e| I2CWrite)?;
match i2c_handle.read(device_address, buffer) {
Ok(()) => Ok(()),
Err(_e) => Err(I2CRead),
}
}
/// Writes bytes to the I2C bus.
2024-02-27 20:09:16 +00:00
pub fn write_bytes<I2C: I2c>(
i2c_handle: &mut I2C,
device_address: u8,
buffer: &[u8],
) -> Result<(), Bme680Error> {
i2c_handle
2024-02-27 20:12:24 +00:00
.write(device_address, buffer)
2024-02-27 20:09:16 +00:00
.map_err(|_e| I2CWrite)
}
}