From 06bfbdb60b637c0d141519e69b8fa100b9e99f76 Mon Sep 17 00:00:00 2001 From: harry Date: Thu, 5 Dec 2024 21:41:54 -0500 Subject: [PATCH 1/7] Added ieee754_f32 field to CANPoint --- libs/calypso-cangen/src/can_types.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libs/calypso-cangen/src/can_types.rs b/libs/calypso-cangen/src/can_types.rs index 8b367f2..3eb5658 100644 --- a/libs/calypso-cangen/src/can_types.rs +++ b/libs/calypso-cangen/src/can_types.rs @@ -2,6 +2,12 @@ use serde::Deserialize; // TODO: Implement MsgType +/** + * Classes to represent levels of the CAN hierarchy + * For more specific descriptions, refer to the README + * in Embedded-Base/cangen + */ + /** * Class representing a CAN message */ @@ -38,6 +44,7 @@ pub struct CANPoint { pub endianness: Option, pub format: Option, pub default_value: Option, + pub ieee754_f32: Option, } #[derive(Deserialize, Debug)] From 66354159c28216c43f7c1fa385a934ef63f3235f Mon Sep 17 00:00:00 2001 From: harry Date: Thu, 5 Dec 2024 22:14:11 -0500 Subject: [PATCH 2/7] Added support for decoding CANPoints as IEEE754 32-bit floats Also rearranged CANPoint decoding code --- libs/calypso-cangen/src/can_gen_decode.rs | 59 ++++++++++++----------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/libs/calypso-cangen/src/can_gen_decode.rs b/libs/calypso-cangen/src/can_gen_decode.rs index 12eb33a..88ad739 100644 --- a/libs/calypso-cangen/src/can_gen_decode.rs +++ b/libs/calypso-cangen/src/can_gen_decode.rs @@ -141,46 +141,51 @@ impl CANGenDecode for NetField { */ impl CANGenDecode for CANPoint { fn gen_decoder_fn(&mut self) -> ProcMacro2TokenStream { - // read_func and read_type to map signedness (read_func for big endian, read_type for little endian) let size_literal = Literal::usize_unsuffixed(self.size); - let read_func = match self.signed { - Some(true) => quote! { reader.read_signed_in::<#size_literal, i32>().unwrap() }, - _ => quote! { reader.read_in::<#size_literal, u32>().unwrap() }, - }; - let read_type = match self.signed { - Some(true) => match self.size { - 0..=8 => quote! { i8 }, - 9..=16 => quote! { i16 }, - _ => quote! { i32 }, - }, - _ => match self.size { - 0..=8 => quote! { u8 }, - 9..=16 => quote! { u16 }, - _ => quote! { u32 }, - }, - }; - // prefix to call potential format function + // If this point is an IEEE754 f32, always read it as a u32, and transmute to f32 later + let read_type = match self.ieee754_f32 { + Some(true) => quote! { u32 }, + _ => match self.signed { + Some(true) => match self.size { + 0..=8 => quote! { i8 }, + 9..=16 => quote! { i16 }, + _ => quote! { i32 }, + } + _ => match self.size { + 0..=8 => quote! { u8 }, + 9..=16 => quote! { u16 }, + _ => quote! { u32 }, + } + } + }; + + // Prefix to call potential format function let format_prefix = match &self.format { Some(format) => { let id = format_ident!("{}_d", format); quote! { FormatData::#id } } - _ => quote! {}, + _ => quote! {} }; - // Endianness affects which read to use - match self.endianness { + // Endianness and signedness affect which read to use + let read_func = match self.endianness { Some(ref s) if s == "little" => { quote! { - #format_prefix (reader.read_as_to::().unwrap() as f32) - } - } - _ => { - quote! { - #format_prefix (#read_func as f32) + reader.read_as_to::().unwrap() } } + _ => match self.signed { + Some(true) if self.ieee754_f32 == None => quote! { reader.read_signed_in::<#size_literal, i32>().unwrap() }, + _ => quote! { reader.read_in::<#size_literal, u32>().unwrap() }, + } + }; + + // Transmute if point is IEEE754 f32, else convert + match self.ieee754_f32 { + Some(true) => quote! { #format_prefix (f32::from_bits(#read_func)) }, + _ => quote! { #format_prefix (#read_func as f32) }, } } } From df0e00e7c3bfa7cad232ee83b0b9650a71b408ac Mon Sep 17 00:00:00 2001 From: harry Date: Thu, 5 Dec 2024 22:28:20 -0500 Subject: [PATCH 3/7] Updated submodule to use experimental Embedded-Base branch --- .gitmodules | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitmodules b/.gitmodules index f18e99b..1b5854c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ [submodule "Embedded-Base"] path = Embedded-Base url = ../Embedded-Base + branch = feature/can-spec-ieee754-f32 From e325f41498f678dec023f3f3f603cb386830f643 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Sun, 8 Dec 2024 18:09:51 -0500 Subject: [PATCH 4/7] bump --- Embedded-Base | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Embedded-Base b/Embedded-Base index 40f07ee..5d4b634 160000 --- a/Embedded-Base +++ b/Embedded-Base @@ -1 +1 @@ -Subproject commit 40f07ee0d15ad984d5a9e5c3d31cbae41fec5fc1 +Subproject commit 5d4b634132fa9dc44e4f9496c265c6e214715075 From 452b5957da3bf1d528d18ff5d5f4b07a6e4558ea Mon Sep 17 00:00:00 2001 From: harry Date: Tue, 10 Dec 2024 15:46:14 -0500 Subject: [PATCH 5/7] Submodule now pointing to main branch --- .gitmodules | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 1b5854c..f18e99b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,3 @@ [submodule "Embedded-Base"] path = Embedded-Base url = ../Embedded-Base - branch = feature/can-spec-ieee754-f32 From a760579de7a72ce975e8971f0eadf8b95d7598da Mon Sep 17 00:00:00 2001 From: harry Date: Tue, 10 Dec 2024 16:05:17 -0500 Subject: [PATCH 6/7] Formatting pass --- libs/calypso-cangen/src/can_gen_decode.rs | 18 ++++++++++-------- libs/calypso-cangen/src/can_types.rs | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/libs/calypso-cangen/src/can_gen_decode.rs b/libs/calypso-cangen/src/can_gen_decode.rs index 88ad739..21b5505 100644 --- a/libs/calypso-cangen/src/can_gen_decode.rs +++ b/libs/calypso-cangen/src/can_gen_decode.rs @@ -151,14 +151,14 @@ impl CANGenDecode for CANPoint { 0..=8 => quote! { i8 }, 9..=16 => quote! { i16 }, _ => quote! { i32 }, - } + }, _ => match self.size { 0..=8 => quote! { u8 }, 9..=16 => quote! { u16 }, _ => quote! { u32 }, - } - } - }; + }, + }, + }; // Prefix to call potential format function let format_prefix = match &self.format { @@ -166,7 +166,7 @@ impl CANGenDecode for CANPoint { let id = format_ident!("{}_d", format); quote! { FormatData::#id } } - _ => quote! {} + _ => quote! {}, }; // Endianness and signedness affect which read to use @@ -177,15 +177,17 @@ impl CANGenDecode for CANPoint { } } _ => match self.signed { - Some(true) if self.ieee754_f32 == None => quote! { reader.read_signed_in::<#size_literal, i32>().unwrap() }, + Some(true) if self.ieee754_f32 == None => { + quote! { reader.read_signed_in::<#size_literal, i32>().unwrap() } + } _ => quote! { reader.read_in::<#size_literal, u32>().unwrap() }, - } + }, }; // Transmute if point is IEEE754 f32, else convert match self.ieee754_f32 { Some(true) => quote! { #format_prefix (f32::from_bits(#read_func)) }, - _ => quote! { #format_prefix (#read_func as f32) }, + _ => quote! { #format_prefix (#read_func as f32) }, } } } diff --git a/libs/calypso-cangen/src/can_types.rs b/libs/calypso-cangen/src/can_types.rs index 3eb5658..6e82a10 100644 --- a/libs/calypso-cangen/src/can_types.rs +++ b/libs/calypso-cangen/src/can_types.rs @@ -44,7 +44,7 @@ pub struct CANPoint { pub endianness: Option, pub format: Option, pub default_value: Option, - pub ieee754_f32: Option, + pub ieee754_f32: Option, } #[derive(Deserialize, Debug)] From ab74f8155fe17ca1820a0e584db5f7ec52569378 Mon Sep 17 00:00:00 2001 From: harry Date: Tue, 10 Dec 2024 16:18:05 -0500 Subject: [PATCH 7/7] Clippy appeasement --- libs/calypso-cangen/src/can_gen_decode.rs | 2 +- libs/calypso-cangen/src/can_types.rs | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libs/calypso-cangen/src/can_gen_decode.rs b/libs/calypso-cangen/src/can_gen_decode.rs index 21b5505..b369d8f 100644 --- a/libs/calypso-cangen/src/can_gen_decode.rs +++ b/libs/calypso-cangen/src/can_gen_decode.rs @@ -177,7 +177,7 @@ impl CANGenDecode for CANPoint { } } _ => match self.signed { - Some(true) if self.ieee754_f32 == None => { + Some(true) if self.ieee754_f32.is_none() => { quote! { reader.read_signed_in::<#size_literal, i32>().unwrap() } } _ => quote! { reader.read_in::<#size_literal, u32>().unwrap() }, diff --git a/libs/calypso-cangen/src/can_types.rs b/libs/calypso-cangen/src/can_types.rs index 6e82a10..242e3aa 100644 --- a/libs/calypso-cangen/src/can_types.rs +++ b/libs/calypso-cangen/src/can_types.rs @@ -2,11 +2,9 @@ use serde::Deserialize; // TODO: Implement MsgType -/** - * Classes to represent levels of the CAN hierarchy - * For more specific descriptions, refer to the README - * in Embedded-Base/cangen - */ +// Classes to represent levels of the CAN hierarchy +// For more specific descriptions, refer to the README +// in Embedded-Base/cangen /** * Class representing a CAN message