Skip to main content

I2cPart

Struct I2cPart 

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

A part on an I2C bus, answering from its registers.

§Examples

use pamoja_hal::i2c::I2c;
use pamoja_hal::sim::I2cPart;

// A BME280, whose chip id register holds the id its datasheet gives.
const BME280: u8 = 0x76;
const CHIP_ID_REGISTER: u8 = 0xD0;
const BME280_CHIP_ID: u8 = 0x60;
let mut part = I2cPart::new(BME280).holding(CHIP_ID_REGISTER, &[BME280_CHIP_ID]);

let mut id = [0u8; 1];
part.write_read(BME280, &[CHIP_ID_REGISTER], &mut id).unwrap();
assert_eq!(id, [BME280_CHIP_ID]);

// What a driver writes stays written, so a test can read its configuration back: here
// one temperature sample, one pressure sample, and forced mode, in ctrl_meas.
const CTRL_MEAS: u8 = 0xF4;
const FORCED_ONCE: u8 = 0b001_001_01;
part.write(BME280, &[CTRL_MEAS, FORCED_ONCE]).unwrap();
assert_eq!(part.register(CTRL_MEAS), FORCED_ONCE);

Implementations§

Source§

impl I2cPart

Source

pub const fn new(address: u8) -> I2cPart

A part answering at one address, with every register reading zero.

§Arguments
  • address - the address it answers to.
§Returns

The part.

Source

pub const fn following(self, rules: Rules) -> I2cPart

The same part, keeping its datasheet’s rules for writes, reads, and its pointer.

§Arguments
  • rules - the rules; see Rules.
§Returns

The part.

Source

pub fn holding(self, first: u8, bytes: &[u8]) -> I2cPart

The same part, holding these bytes from a register on.

§Arguments
  • first - the register the bytes start at.
  • bytes - what to put there. Past the last register it wraps to the first.
§Returns

The part.

§Examples
use pamoja_hal::sim::I2cPart;

// A 16-bit reading, high byte first, lands in two registers from the first one.
let [high, low] = 1000u16.to_be_bytes();
let part = I2cPart::new(0x48).holding(0x00, &[high, low]);
assert_eq!([part.register(0x00), part.register(0x01)], [high, low]);
Source

pub fn load(&mut self, first: u8, bytes: &[u8])

Puts bytes in the part, from a register on.

§Arguments
  • first - the register the bytes start at.
  • bytes - what to put there. Past the last register it wraps to the first.
Source

pub const fn register(&self, register: u8) -> u8

What a register holds now.

§Arguments
  • register - which one.
§Returns

Its value, which is what a driver wrote if it wrote one.

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 I2cPart

Source§

fn clone(&self) -> I2cPart

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 I2cPart

Source§

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

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

impl ErrorType for I2cPart

Source§

type Error = PartError

Error type
Source§

impl From<I2cPart> for Part

Available on crate feature alloc only.
Source§

fn from(part: I2cPart) -> Part

Converts to this type from the input type.
Source§

impl FromPart for I2cPart

Available on crate feature alloc only.
Source§

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

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

impl I2c for I2cPart

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.