use crate::conversion::CompuMethod;
use a2l_parse::{CharType, DataType};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CharKind {
Value,
Curve,
Map,
ValBlk,
Ascii,
}
impl CharKind {
#[must_use]
pub const fn from_char_type(kind: CharType) -> Self {
match kind {
CharType::Value => Self::Value,
CharType::Curve => Self::Curve,
CharType::Map => Self::Map,
CharType::ValBlk => Self::ValBlk,
CharType::Ascii => Self::Ascii,
}
}
#[must_use]
pub const fn keyword(self) -> &'static str {
match self {
Self::Value => "VALUE",
Self::Curve => "CURVE",
Self::Map => "MAP",
Self::ValBlk => "VAL_BLK",
Self::Ascii => "ASCII",
}
}
#[must_use]
pub const fn is_multi_element(self) -> bool {
matches!(self, Self::Curve | Self::Map | Self::ValBlk)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Characteristic {
pub name: String,
pub kind: CharKind,
pub address: u32,
pub deposit: String,
pub deposit_position: u8,
pub datatype: DataType,
pub elements: usize,
pub conversion: CompuMethod,
pub lower_limit: f64,
pub upper_limit: f64,
pub max_diff: f64,
}
impl Characteristic {
#[must_use]
pub fn element_bits(&self) -> u32 {
u32::try_from(self.datatype.size_bytes() * 8).unwrap_or(32)
}
#[must_use]
pub const fn is_signed(&self) -> bool {
matches!(
self.datatype,
DataType::Sbyte | DataType::Sword | DataType::Slong | DataType::Int64
)
}
#[must_use]
pub fn element_mask(&self) -> u64 {
let bits = self.element_bits();
if bits >= 64 {
u64::MAX
} else {
(1_u64 << bits) - 1
}
}
#[must_use]
pub fn size_bytes(&self) -> usize {
self.elements * self.datatype.size_bytes()
}
#[must_use]
pub fn raw_value(&self, raw: u64) -> f64 {
let masked = raw & self.element_mask();
if self.is_signed() {
#[allow(clippy::cast_precision_loss)]
{
sign_extend(masked, self.element_bits()) as f64
}
} else {
#[allow(clippy::cast_precision_loss)]
{
masked as f64
}
}
}
#[must_use]
pub fn raw_bits(&self, value: f64) -> u64 {
if self.is_signed() {
#[allow(clippy::cast_possible_wrap)]
let bits = (value as i64 as u64) & self.element_mask();
bits
} else {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let bits = (value as u64) & self.element_mask();
bits
}
}
#[must_use]
pub fn value_bounds(&self) -> (f64, f64) {
let bits = self.element_bits();
let exponent = i32::try_from(bits).unwrap_or(32);
if self.is_signed() {
let half = 2_f64.powi(exponent - 1);
(-half, half - 1.0)
} else if bits >= 64 {
#[allow(clippy::cast_precision_loss)]
{
(0.0, u64::MAX as f64)
}
} else {
(0.0, 2_f64.powi(exponent) - 1.0)
}
}
#[must_use]
pub fn has_limits(&self) -> bool {
self.lower_limit.is_finite()
&& self.upper_limit.is_finite()
&& self.lower_limit < self.upper_limit
}
#[must_use]
pub fn within_limits(&self, physical: f64) -> bool {
if !physical.is_finite() {
return false;
}
if !self.has_limits() {
return true;
}
physical >= self.lower_limit && physical <= self.upper_limit
}
#[must_use]
pub fn with_elements(mut self, elements: usize) -> Self {
self.elements = elements;
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Measurement {
pub name: String,
pub address: u32,
pub conversion: CompuMethod,
pub datatype: DataType,
}
impl Measurement {
#[must_use]
pub fn size_bytes(&self) -> usize {
self.datatype.size_bytes()
}
#[must_use]
pub const fn is_signed(&self) -> bool {
matches!(
self.datatype,
DataType::Sbyte | DataType::Sword | DataType::Slong | DataType::Int64
)
}
#[must_use]
pub fn raw_value(&self, raw: u64) -> f64 {
let bits = u32::try_from(self.datatype.size_bytes() * 8).unwrap_or(32);
let mask = if bits >= 64 {
u64::MAX
} else {
(1_u64 << bits) - 1
};
let masked = raw & mask;
#[allow(clippy::cast_precision_loss)]
let value = if self.is_signed() {
sign_extend(masked, bits) as f64
} else {
masked as f64
};
value
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Module {
pub name: String,
pub characteristics: Vec<Characteristic>,
pub measurements: Vec<Measurement>,
}
impl Module {
#[must_use]
pub fn characteristic(&self, name: &str) -> Option<&Characteristic> {
self.characteristics.iter().find(|c| c.name == name)
}
#[must_use]
pub fn measurement(&self, name: &str) -> Option<&Measurement> {
self.measurements.iter().find(|m| m.name == name)
}
pub fn characteristic_names(&self) -> impl Iterator<Item = &str> {
self.characteristics.iter().map(|c| c.name.as_str())
}
pub fn measurement_names(&self) -> impl Iterator<Item = &str> {
self.measurements.iter().map(|m| m.name.as_str())
}
}
fn sign_extend(raw: u64, bits: u32) -> i64 {
if bits == 0 || bits >= 64 {
#[allow(clippy::cast_possible_wrap)]
{
return raw as i64;
}
}
let sign_bit = 1_u64 << (bits - 1);
#[allow(clippy::cast_possible_wrap)]
if raw & sign_bit != 0 {
(raw | (u64::MAX << bits)) as i64
} else {
#[allow(clippy::cast_possible_wrap)]
{
raw as i64
}
}
}