Struct I2cInterface

Source
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>

Source

pub fn read_register<Raw, Value, Err>( &mut self, register: impl I2cReadRegister<Raw>, ) -> Result<Value, Err>
where I2C: WriteRead<Error = Err>, Raw: Into<Value>,

Read bytes from register and map output to Value

Source

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§

Source§

impl<I2C: Debug> Debug for I2cInterface<I2C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.