Skip to main content

CommandPart

Struct CommandPart 

Source
pub struct CommandPart { /* private fields */ }
Expand description

A part on an I2C bus that takes commands rather than register addresses, answering each with the reply it was given for that command.

This is how Sensirion lays out parts such as the SHT3x and the SCD4x. A write sends a command, and any arguments after it; a read then takes the reply that command left, once. A command given no reply leaves none, and a read then is not acknowledged, which is what the real parts do when asked for data they do not have. A read longer than the reply gets 0xFF for the rest, the level an idle bus reads.

§Examples

use pamoja_hal::i2c::I2c;
use pamoja_hal::sim::CommandPart;

// An SHT3x, from its datasheet: the read-status command answers with the status word,
// 0x8010 after a reset, then that word's CRC.
const SHT3X: u8 = 0x44;
const READ_STATUS: [u8; 2] = 0xF32Du16.to_be_bytes();
const STATUS_AFTER_RESET: [u8; 3] = [0x80, 0x10, 0xE1];
let mut part = CommandPart::new(SHT3X, 2).answering(&READ_STATUS, &STATUS_AFTER_RESET);

part.write(SHT3X, &READ_STATUS).unwrap();
let mut status = [0u8; 3];
part.read(SHT3X, &mut status).unwrap();
assert_eq!(status, STATUS_AFTER_RESET);

// The reply was taken, so a second read finds nothing waiting.
assert!(part.read(SHT3X, &mut status).is_err());
assert_eq!(part.received(), [READ_STATUS.to_vec()]);

Implementations§

Source§

impl CommandPart

Source

pub fn new(address: u8, width: usize) -> CommandPart

A part answering at one address that has been given no replies yet.

§Arguments
  • address - the address it answers to.
  • width - how many bytes a command takes: two for Sensirion’s 16-bit commands.
§Returns

The part.

Source

pub fn answering(self, command: &[u8], reply: &[u8]) -> CommandPart

The same part, answering one command with a reply.

§Arguments
  • command - the command’s bytes.
  • reply - what a read after it returns, in place of any reply given before.
§Returns

The part.

Source

pub fn answer(&mut self, command: &[u8], reply: &[u8])

Answers one command with a reply from now on, in place of any reply given before.

§Arguments
  • command - the command’s bytes.
  • reply - what a read after it returns.
Source

pub fn received(&self) -> &[Vec<u8>]

Every write the part has received, oldest first.

§Returns

The writes, each a command and whatever arguments followed it.

Source

pub const fn address(&self) -> u8

The address this part answers to.

§Returns

The address.

Source

pub const fn transfers(&self) -> usize

How many transfers the part has served.

§Returns

The count, one for each transfer a driver made, however many operations it held.

Trait Implementations§

Source§

impl Clone for CommandPart

Source§

fn clone(&self) -> CommandPart

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CommandPart

Source§

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

Formats the value using the given formatter. Read more
Source§

impl ErrorType for CommandPart

Available on crate feature alloc only.
Source§

type Error = PartError

Error type
Source§

impl From<CommandPart> for Part

Available on crate feature alloc only.
Source§

fn from(part: CommandPart) -> Part

Converts to this type from the input type.
Source§

impl FromPart for CommandPart

Available on crate feature alloc only.
Source§

fn from_part(part: Part) -> Option<CommandPart>

The part, when it is this kind. Read more
Source§

impl I2c for CommandPart

Available on crate feature alloc only.
Source§

fn transaction( &mut self, address: SevenBitAddress, operations: &mut [Operation<'_>], ) -> Result<(), Self::Error>

Execute the provided operations on the I2C bus. Read more
§

fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error>

Reads enough bytes from slave with address to fill read. Read more
§

fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error>

Writes bytes to slave with address address. Read more
§

fn write_read( &mut self, address: A, write: &[u8], read: &mut [u8], ) -> Result<(), Self::Error>

Writes bytes to slave with address address and then reads enough bytes to fill read in a single transaction. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.