process_image_owned

Macro process_image_owned 

Source
macro_rules! process_image_owned {
    (
        $( #[$meta:meta] )*
        $vis:vis struct $ProcessImage:ident, mut $ProcessImageMut:ident: $SIZE:literal {
            $(
                $( #[$field_meta:meta] )*
                $field_vis:vis $field_name:ident: ($($tag:tt)+)
            ),*
            $(,)?
        }
    ) => { ... };
}
Expand description

Build tag table for symbolic access into an owned process image buffer.

  • This macro generates a struct that owns the process image buffer. For a referenced variant, see process_image!{}.
  • The method immediately available on the struct provide immutable access. For mutable access, call .as_mut() to get a mutable accessor struct. The methods on that one then provide mutable access.
  • The tag addresses are in the format described in the tag!() macro.
  • You can construct a process_image_owned from zeros (new_zeroed()) or from a pre-initialized buffer by using From<[u8; SIZE] or TryFrom<&[u8]>.

ยงExample

process_image::process_image_owned! {
    //                                      +-- Size of the process image in bytes
    //                                      V
    pub struct PiExampleOwned, mut PiExampleMut: 16 {
        //  +-- Tag Name  +-- Absolute Address
        //  V             V
        pub sensor_left:  (X, 0, 0),    // %MX0.0
        pub sensor_right: (X, 0, 1),    // %MX0.1
        pub temperature:  (D, 4),       // %MD4
        pub setpoint:     (W, 2),       // %MW2
    }
}

let pi = PiExampleOwned::new_zeroed();

dbg!(pi.sensor_left());
dbg!(pi.sensor_left());

// You need to use try_from() when using a slice.  The unwrap() will panic when the size of the
// slice does not match the size of the process image.
let pi_buf = [0u8; 16];
let pi_slice = &pi_buf[..];
let mut pi = PiExampleOwned::try_from(pi_slice).unwrap();

// Mutable access:
*pi.as_mut().temperature() = 1234;
*pi.as_mut().setpoint() = 72;
*pi.as_mut().sensor_left() = false;