zencan_common/pdo.rs
1//! Definitions and data types related to PDOs
2
3/// Represents a PDO mapping
4///
5/// Each mapping specifies one sub-object to be included in the PDO data bytes.
6#[derive(Clone, Copy, Debug, PartialEq)]
7#[cfg_attr(
8 feature = "std",
9 derive(serde::Deserialize),
10 serde(deny_unknown_fields)
11)]
12pub struct PdoMapping {
13 /// The object index
14 pub index: u16,
15 /// The object sub index
16 pub sub: u8,
17 /// The size of the object to map, in **bits**
18 pub size: u8,
19}
20
21impl PdoMapping {
22 /// Convert a PdoMapping object to the u32 representation stored in the PdoMapping object
23 pub fn to_object_value(&self) -> u32 {
24 ((self.index as u32) << 16) | ((self.sub as u32) << 8) | (self.size as u32)
25 }
26
27 /// Create a PdoMapping object from the raw u32 representation stored in the PdoMapping object
28 pub fn from_object_value(value: u32) -> Self {
29 let index = (value >> 16) as u16;
30 let sub = ((value >> 8) & 0xff) as u8;
31 let size = (value & 0xff) as u8;
32 Self { index, sub, size }
33 }
34}