Struct Twi

Source
pub struct Twi<'a> { /* private fields */ }
Expand description

Provides access to the TWI (aka I2C).

See: Twi::attach_slave() and Twi::detach_slave().

Implementations§

Source§

impl<'a> Twi<'a>

Source

pub fn attach_slave(&mut self, slave: impl TwiSlave + 'static) -> TwiSlaveId

Attaches given slave into this TWI, executing it for each packet received on this interface - slave can then decide whether to respond or ignore the packet.

When multiple slaves are attached, they all get executed in the order of the attachment until any slave responds (if any).

See the twi.rs example for usage.

See also: Self::attach_slave_fn().

Source

pub fn attach_slave_fn( &mut self, slave: impl FnMut(TwiPacket) -> Option<TwiPacket> + 'static, ) -> TwiSlaveId

Shortcut for Self::attach_slave() that allows to create a device with just a function:

let mut avr = AvrTester::test();

avr.twi0().attach_slave_fn(|packet| {
    if packet.addr != 0x33 {
        return None;
    }

    if packet.is_start() || packet.is_stop() {
        return Some(packet.respond_ack());
    }

    if packet.is_write() {
        todo!();
    }

    if packet.is_read() {
        todo!();
    }

    None
});

See Self::attach_slave() for details.

Source

pub fn detach_slave(&mut self, id: TwiSlaveId)

Detaches given slave from this TWI, preventing it from being executed again.

See: Self::attach_slave().

Auto Trait Implementations§

§

impl<'a> Freeze for Twi<'a>

§

impl<'a> !RefUnwindSafe for Twi<'a>

§

impl<'a> !Send for Twi<'a>

§

impl<'a> !Sync for Twi<'a>

§

impl<'a> Unpin for Twi<'a>

§

impl<'a> !UnwindSafe for Twi<'a>

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.