Skip to main content

Rules

Struct Rules 

Source
pub struct Rules {
    pub write: fn(registers: &mut [u8; 256], register: u8, value: u8),
    pub read: fn(registers: &[u8; 256], register: u8) -> u8,
    pub next: fn(registers: &[u8; 256], register: u8) -> u8,
}
Expand description

The rules a part with byte-wide registers keeps beyond plain memory: which writes it takes, what a read returns, and where its register pointer moves.

Rules::MEMORY stores every byte written, reads back what it stored, and moves on one register a byte, which is most parts. A part whose datasheet says otherwise, such as one that ignores a register while it runs or loads many registers from one write, gives its own functions. They are plain functions over the register map, so a part that keeps rules still needs no allocator.

§Examples

use pamoja_hal::i2c::I2c;
use pamoja_hal::sim::{I2cPart, Rules, REGISTERS};

// A PCA9685 takes a new PWM frequency only while it sleeps: its prescale register
// ignores a write unless the SLEEP bit of MODE1 is set.
const PCA9685: u8 = 0x40;
const MODE1: u8 = 0x00;
const SLEEP: u8 = 1 << 4;
const PRE_SCALE: u8 = 0xFE;
fn write(registers: &mut [u8; REGISTERS], register: u8, value: u8) {
    if register != PRE_SCALE || registers[usize::from(MODE1)] & SLEEP != 0 {
        registers[usize::from(register)] = value;
    }
}
let rules = Rules { write, ..Rules::MEMORY };

// Awake, at its power-on prescale of 30, about 200 Hz from its 25 MHz clock.
let mut part = I2cPart::new(PCA9685).following(rules).holding(PRE_SCALE, &[30]);

// 121 is the prescale for the 50 Hz a servo wants.
part.write(PCA9685, &[PRE_SCALE, 121]).unwrap();
assert_eq!(part.register(PRE_SCALE), 30, "ignored while it runs");
part.write(PCA9685, &[MODE1, SLEEP]).unwrap();
part.write(PCA9685, &[PRE_SCALE, 121]).unwrap();
assert_eq!(part.register(PRE_SCALE), 121, "taken while it sleeps");

Fields§

§write: fn(registers: &mut [u8; 256], register: u8, value: u8)

Takes one byte a driver wrote to a register: stores it, drops it, or loads it elsewhere.

§read: fn(registers: &[u8; 256], register: u8) -> u8

What a read of a register returns.

§next: fn(registers: &[u8; 256], register: u8) -> u8

Where the register pointer goes after a byte at a register is written or read.

Implementations§

Source§

impl Rules

Source

pub const MEMORY: Rules

Plain memory: every write stored, every read what was stored, one register a byte.

Trait Implementations§

Source§

impl Clone for Rules

Source§

fn clone(&self) -> Rules

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 Copy for Rules

Source§

impl Debug for Rules

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Rules

§

impl RefUnwindSafe for Rules

§

impl Send for Rules

§

impl Sync for Rules

§

impl Unpin for Rules

§

impl UnsafeUnpin for Rules

§

impl UnwindSafe for Rules

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.