pub struct I2cInterface<I2C> {
pub i2c: I2C,
pub address: u8,
}
Expand description
I2C interface
§Example
use embedded_hal::blocking::i2c;
use i2c_reg::*;
use i2c_reg_derive::*;
#[derive(Register, I2cReadRegister, I2cWriteRegister)]
#[address = 0b1110]
#[size = 4]
struct ExampleRegister;
type Raw = <ExampleRegister as Register>::Raw;
#[derive(Debug, PartialEq)]
struct Value(u32);
impl Into<Raw> for Value {
fn into(self) -> Raw {
self.0.to_be_bytes()
}
}
impl From<Raw> for Value {
fn from(raw: Raw) -> Self {
Value(u32::from_be_bytes(raw))
}
}
let mut interface = I2cInterface { i2c, address: 0b0110 };
interface.write_register(ExampleRegister, Value(0x89abcdef)).unwrap();
let value: Value = interface.read_register(ExampleRegister).unwrap();
assert_eq!(Value(0x89abcdef), value);
Fields§
§i2c: I2C
Slave device I2C
address: u8
Slave device address
Implementations§
Source§impl<I2C> I2cInterface<I2C>
impl<I2C> I2cInterface<I2C>
Sourcepub fn read_register<Raw, Value, Err>(
&mut self,
register: impl I2cReadRegister<Raw>,
) -> Result<Value, Err>
pub fn read_register<Raw, Value, Err>( &mut self, register: impl I2cReadRegister<Raw>, ) -> Result<Value, Err>
Read bytes from register and map output to Value
Sourcepub fn write_register<Raw, Err>(
&mut self,
register: impl I2cWriteRegister<Raw>,
value: impl Into<Raw>,
) -> Result<(), Err>where
I2C: Write<Error = Err>,
pub fn write_register<Raw, Err>(
&mut self,
register: impl I2cWriteRegister<Raw>,
value: impl Into<Raw>,
) -> Result<(), Err>where
I2C: Write<Error = Err>,
Map value
to bytes and write to register
Trait Implementations§
Auto Trait Implementations§
impl<I2C> Freeze for I2cInterface<I2C>where
I2C: Freeze,
impl<I2C> RefUnwindSafe for I2cInterface<I2C>where
I2C: RefUnwindSafe,
impl<I2C> Send for I2cInterface<I2C>where
I2C: Send,
impl<I2C> Sync for I2cInterface<I2C>where
I2C: Sync,
impl<I2C> Unpin for I2cInterface<I2C>where
I2C: Unpin,
impl<I2C> UnwindSafe for I2cInterface<I2C>where
I2C: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more