diff --git a/src/lib.rs b/src/lib.rs index cb2c37f..97a0748 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,6 +72,10 @@ pub struct Config<'a> { #[builder(default)] pub impl_serde: FeatureConfig<'a>, + /// Optional: `impl defmt::Format` for generated types.. Default: `Never`. + #[builder(default)] + pub impl_defmt: FeatureConfig<'a>, + /// Optional: `impl Error` for generated error type. Default: `Never`. /// /// Note: this feature depends on `std`. @@ -89,6 +93,10 @@ pub struct Config<'a> { /// Optional: Allow dead code in the generated module. Default: `false`. #[builder(default)] pub allow_dead_code: bool, + + /// Optional: Padding value used; set all unused bits to 1 if true else 0. Default: `false`. + #[builder(default)] + pub padding_value: bool, } /// Configuration for including features in the codegenerator. @@ -157,6 +165,10 @@ pub fn codegen(config: Config<'_>, out: impl Write) -> Result<()> { writeln!(w, "use arbitrary::{{Arbitrary, Unstructured}};") })?; + config.impl_defmt.fmt_cfg(&mut w, |w| { + writeln!(w, "use defmt::Format;") + })?; + config.impl_serde.fmt_cfg(&mut w, |w| { writeln!(w, "use serde::{{Serialize, Deserialize}};") })?; @@ -265,6 +277,7 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D writeln!(w, "#[derive(Clone, Copy)]")?; config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?; config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?; + config.impl_serde.fmt_attr(&mut w, "derive(defmt::Format)")?; writeln!(w, "pub struct {} {{", type_name(msg.message_name()))?; { let mut w = PadAdapter::wrap(&mut w); @@ -352,8 +365,9 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D let mut w = PadAdapter::wrap(&mut w); writeln!( &mut w, - "let {}res = Self {{ raw: [0u8; {}] }};", + "let {}res = Self {{ raw: [{}; {}] }};", if msg.signals().is_empty() { "" } else { "mut " }, + if config.padding_value { "0xFF" } else { "0x00" }, msg.message_size() )?; for signal in msg.signals().iter() { @@ -998,6 +1012,7 @@ fn write_enum( .fmt_attr(&mut w, "derive(defmt::Format)")?; config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?; config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?; + config.impl_defmt.fmt_attr(&mut w, "derive(defmt::Format)")?; writeln!(w, "pub enum {} {{", type_name)?; { let mut w = PadAdapter::wrap(&mut w); @@ -1475,6 +1490,7 @@ fn render_multiplexor_enums( .fmt_attr(&mut w, "derive(defmt::Format)")?; config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?; config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?; + config.impl_defmt.fmt_attr(&mut w, "derive(defmt::Format)")?; writeln!( w, "pub enum {} {{", @@ -1505,6 +1521,7 @@ fn render_multiplexor_enums( .fmt_attr(&mut w, "derive(defmt::Format)")?; config.impl_serde.fmt_attr(&mut w, "derive(Serialize)")?; config.impl_serde.fmt_attr(&mut w, "derive(Deserialize)")?; + config.impl_defmt.fmt_attr(&mut w, "derive(defmt::Format)")?; writeln!(w, r##"#[derive(Default)]"##)?; writeln!( w, diff --git a/testing/can-messages/src/messages.rs b/testing/can-messages/src/messages.rs index a78d2bd..ab788fd 100644 --- a/testing/can-messages/src/messages.rs +++ b/testing/can-messages/src/messages.rs @@ -18,6 +18,7 @@ use arbitrary::{Arbitrary, Unstructured}; use bitvec::prelude::*; use core::ops::BitOr; +use defmt::Format; use embedded_can::{ExtendedId, Id, StandardId}; /// All messages @@ -108,7 +109,7 @@ impl Foo { /// Construct new Foo from values pub fn new(voltage: f32, current: f32) -> Result { - let mut res = Self { raw: [0u8; 4] }; + let mut res = Self { raw: [0x00; 4] }; res.set_voltage(voltage)?; res.set_current(current)?; Ok(res) @@ -317,7 +318,7 @@ impl Bar { /// Construct new Bar from values pub fn new(one: u8, two: f32, three: u8, four: u8, xtype: bool) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_one(one)?; res.set_two(two)?; res.set_three(three)?; @@ -739,7 +740,7 @@ impl X4wd { /// Construct new _4WD from values pub fn new(x4drive: u8) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_x4drive(x4drive)?; Ok(res) } @@ -925,7 +926,7 @@ impl Amet { /// Construct new Amet from values pub fn new(one: u8, two: f32, three: u8, four: u8, five: bool) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_one(one)?; res.set_two(two)?; res.set_three(three)?; @@ -1264,7 +1265,7 @@ impl Dolor { /// Construct new Dolor from values pub fn new(one_float: f32) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_one_float(one_float)?; Ok(res) } @@ -1447,7 +1448,7 @@ impl MultiplexTest { /// Construct new MultiplexTest from values pub fn new(multiplexor: u8, unmultiplexed_signal: u8) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_multiplexor(multiplexor)?; res.set_unmultiplexed_signal(unmultiplexed_signal)?; Ok(res) @@ -1889,7 +1890,7 @@ impl IntegerFactorOffset { byte_with_negative_offset: i16, byte_with_negative_min: i16, ) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_byte_with_offset(byte_with_offset)?; res.set_byte_with_factor(byte_with_factor)?; res.set_byte_with_both(byte_with_both)?; @@ -2256,7 +2257,7 @@ impl NegativeFactorTest { unsigned_negative_factor_signal: i32, width_more_than_min_max: i16, ) -> Result { - let mut res = Self { raw: [0u8; 4] }; + let mut res = Self { raw: [0x00; 4] }; res.set_unsigned_negative_factor_signal(unsigned_negative_factor_signal)?; res.set_width_more_than_min_max(width_more_than_min_max)?; Ok(res) @@ -2470,7 +2471,7 @@ impl LargerIntsWithOffsets { /// Construct new LargerIntsWithOffsets from values pub fn new(twelve: i16, sixteen: i32) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_twelve(twelve)?; res.set_sixteen(sixteen)?; Ok(res) @@ -2682,7 +2683,7 @@ impl MsgWithoutSignals { /// Construct new MsgWithoutSignals from values pub fn new() -> Result { - let res = Self { raw: [0u8; 8] }; + let res = Self { raw: [0x00; 8] }; Ok(res) } @@ -2784,7 +2785,7 @@ impl TruncatedBeSignal { /// Construct new TruncatedBeSignal from values pub fn new(foo: i16) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_foo(foo)?; Ok(res) } @@ -2937,7 +2938,7 @@ impl TruncatedLeSignal { /// Construct new TruncatedLeSignal from values pub fn new(foo: i16) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_foo(foo)?; Ok(res) } @@ -3090,7 +3091,7 @@ impl MsgExtendedId { /// Construct new MsgExtendedId from values pub fn new(dummy: u8) -> Result { - let mut res = Self { raw: [0u8; 8] }; + let mut res = Self { raw: [0x00; 8] }; res.set_dummy(dummy)?; Ok(res) }