use crate::{ByteOrder, Signal};
#[derive(Clone, Copy)]
pub(super) struct SignalDecode {
pub byte_start: u8,
pub bit_offset: u8,
pub length: u8,
pub flags: u8,
pub factor: f64,
pub offset: f64,
}
impl SignalDecode {
pub const FLAG_UNSIGNED: u8 = 0b0001;
pub const FLAG_LITTLE_ENDIAN: u8 = 0b0010;
pub const FLAG_IDENTITY: u8 = 0b0100;
#[inline(always)]
pub fn is_unsigned(self) -> bool {
(self.flags & Self::FLAG_UNSIGNED) != 0
}
#[inline(always)]
pub fn is_little_endian(self) -> bool {
(self.flags & Self::FLAG_LITTLE_ENDIAN) != 0
}
#[inline(always)]
pub fn is_identity(self) -> bool {
(self.flags & Self::FLAG_IDENTITY) != 0
}
pub fn from_signal(signal: &Signal) -> Self {
let start_bit = signal.start_bit() as usize;
let length = signal.length() as usize;
let mut flags = 0u8;
if signal.is_unsigned() {
flags |= Self::FLAG_UNSIGNED;
}
if signal.byte_order() == ByteOrder::LittleEndian {
flags |= Self::FLAG_LITTLE_ENDIAN;
}
if signal.factor() == 1.0 && signal.offset() == 0.0 {
flags |= Self::FLAG_IDENTITY;
}
Self {
byte_start: (start_bit / 8) as u8,
bit_offset: (start_bit % 8) as u8,
length: length as u8,
flags,
factor: signal.factor(),
offset: signal.offset(),
}
}
}
pub(super) struct DecodePlan {
pub message_index: usize,
pub min_bytes: u8,
pub signals: Vec<SignalDecode>,
}