Skip to content

Commit

Permalink
Add setters for multiplexed signals
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelbuesing committed Apr 12, 2021
1 parent 1abdffc commit 4c924ad
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 79 deletions.
96 changes: 83 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,16 @@ fn render_message(mut w: impl Write, msg: &Message, dbc: &DBC) -> Result<()> {
let args: Vec<String> = msg
.signals()
.iter()
.map(|signal| {
format!(
"{}: {}",
field_name(signal.name()),
signal_to_rust_type(&signal)
)
.filter_map(|signal| {
if *signal.multiplexer_indicator() == MultiplexIndicator::Plain {
Some(format!(
"{}: {}",
field_name(signal.name()),
signal_to_rust_type(&signal)
))
} else {
None
}
})
.collect();
writeln!(
Expand All @@ -228,10 +232,6 @@ fn render_message(mut w: impl Write, msg: &Message, dbc: &DBC) -> Result<()> {
if *signal.multiplexer_indicator() == MultiplexIndicator::Plain {
writeln!(&mut w, "res.set_{0}_raw({0})?;", field_name(signal.name()))?;
}

if *signal.multiplexer_indicator() == MultiplexIndicator::Multiplexor {
writeln!(&mut w, "res.set_{0}_raw({0})?;", field_name(signal.name()))?;
}
}
writeln!(&mut w, "Ok(res)")?;
}
Expand Down Expand Up @@ -406,7 +406,7 @@ fn render_set_signal_raw(mut w: impl Write, signal: &Signal, msg: &Message) -> R
writeln!(w, "#[inline(always)]")?;
writeln!(
w,
"pub fn set_{}_raw(&mut self, value: {}) -> Result<(), CanError> {{",
"pub fn set_{}_raw(&mut self, value: {}) -> Result<&mut Self, CanError> {{",
field_name(signal.name()),
signal_to_rust_type(&signal)
)?;
Expand Down Expand Up @@ -442,6 +442,54 @@ fn render_set_signal_raw(mut w: impl Write, signal: &Signal, msg: &Message) -> R
Ok(())
}

fn render_set_signal_multiplexer(
mut w: impl Write,
multiplexor: &Signal,
msg: &Message,
switch_index: u64,
) -> Result<()> {
writeln!(&mut w, "/// Set value of {}", multiplexor.name())?;
writeln!(w, "#[inline(always)]")?;
writeln!(
w,
"pub fn set_{enum_variant_wrapper}(&mut self) -> Result<{enum_variant}, CanError> {{",
enum_variant_wrapper = multiplexed_enum_variant_wrapper_name(switch_index),
enum_variant = multiplexed_enum_variant_name(msg, multiplexor, switch_index)?,
)?;

{
// let mut w = PadAdapter::wrap(&mut w);

// writeln!(&mut w, "match value {{")?;
// {
// let mut w = PadAdapter::wrap(&mut w);
// for (switch_index, multiplexed_signal) in multiplexed_signals {
// writeln!(
// w,
// "{enum_name}::{multiplexed_wrapper_name}(m) => {{self.set_{}(m.MULTIPLEXED_SWITCH_INDEX)?;",
// field_name(multiplexor.name()),
// enum_name = multiplex_enum_name(msg, multiplexor)?,
// multiplexed_wrapper_name = multiplexed_enum_variant_wrapper_name(*switch_index)
// )?;
// }
// }
// writeln!(&mut w, "}}")?;

let mut w = PadAdapter::wrap(&mut w);
writeln!(&mut w, "self.set_multiplexor_raw({})?;", switch_index)?;
writeln!(
&mut w,
"Ok({} {{ raw: &mut self.raw }})",
enum_variant = multiplexed_enum_variant_name(msg, multiplexor, switch_index)?
)?;
}

writeln!(&mut w, "}}")?;
writeln!(w)?;

Ok(())
}

fn render_multiplexor_signal(mut w: impl Write, signal: &Signal, msg: &Message) -> Result<()> {
writeln!(w, "/// Get raw value of {}", signal.name())?;
writeln!(w, "///")?;
Expand Down Expand Up @@ -510,6 +558,22 @@ fn render_multiplexor_signal(mut w: impl Write, signal: &Signal, msg: &Message)

render_set_signal_raw(&mut w, signal, msg)?;

// TODO turn this mapping of signals to multiplex_indicator into an extension method for can_dbc::Message or actually add it to can_dbc
let mut multiplexed_signals = BTreeMap::new();
for signal in msg.signals() {
if let MultiplexIndicator::MultiplexedSignal(switch_index) = signal.multiplexer_indicator()
{
multiplexed_signals
.entry(switch_index)
.and_modify(|v: &mut Vec<&Signal>| v.push(&signal))
.or_insert(vec![&signal]);
}
}

for (switch_index, multiplexed_signal) in multiplexed_signals {
render_set_signal_multiplexer(&mut w, signal, msg, *switch_index)?;
}

Ok(())
}

Expand Down Expand Up @@ -660,7 +724,7 @@ fn signal_to_payload(mut w: impl Write, signal: &Signal, msg: &Message) -> Resul
}
};

writeln!(&mut w, "Ok(())")?;
writeln!(&mut w, "Ok(self)")?;
Ok(())
}

Expand Down Expand Up @@ -1023,7 +1087,13 @@ fn render_arbitrary(mut w: impl Write, msg: &Message) -> Result<()> {
let args: Vec<String> = msg
.signals()
.iter()
.map(|signal| field_name(signal.name()))
.filter_map(|signal| {
if *signal.multiplexer_indicator() == MultiplexIndicator::Plain {
Some(field_name(signal.name()))
} else {
None
}
})
.collect();

writeln!(
Expand Down
Loading

0 comments on commit 4c924ad

Please sign in to comment.