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
impl CommandPart
Sourcepub fn new(address: u8, width: usize) -> CommandPart
pub fn new(address: u8, width: usize) -> CommandPart
Sourcepub fn answering(self, command: &[u8], reply: &[u8]) -> CommandPart
pub fn answering(self, command: &[u8], reply: &[u8]) -> CommandPart
Sourcepub fn answer(&mut self, command: &[u8], reply: &[u8])
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.
Trait Implementations§
Source§impl Clone for CommandPart
impl Clone for CommandPart
Source§fn clone(&self) -> CommandPart
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CommandPart
impl Debug for CommandPart
Source§impl ErrorType for CommandPart
Available on crate feature alloc only.
impl ErrorType for CommandPart
Available on crate feature
alloc only.Source§impl From<CommandPart> for Part
Available on crate feature alloc only.
impl From<CommandPart> for Part
Available on crate feature
alloc only.Source§fn from(part: CommandPart) -> Part
fn from(part: CommandPart) -> Part
Converts to this type from the input type.
Source§impl FromPart for CommandPart
Available on crate feature alloc only.
impl FromPart for CommandPart
Available on crate feature
alloc only.Source§impl I2c for CommandPart
Available on crate feature alloc only.
impl I2c for CommandPart
Available on crate feature
alloc only.Source§fn transaction(
&mut self,
address: SevenBitAddress,
operations: &mut [Operation<'_>],
) -> Result<(), Self::Error>
fn transaction( &mut self, address: SevenBitAddress, operations: &mut [Operation<'_>], ) -> Result<(), Self::Error>
Execute the provided operations on the I2C bus. Read more
Auto Trait Implementations§
impl Freeze for CommandPart
impl RefUnwindSafe for CommandPart
impl Send for CommandPart
impl Sync for CommandPart
impl Unpin for CommandPart
impl UnsafeUnpin for CommandPart
impl UnwindSafe for CommandPart
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