Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operational round bool, simulation enum support #69

Merged
merged 7 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
- name: Test
run: cargo test --verbose
- name: Clippy
run: cargo clippy --verbose -- -D warnings
run: cargo clippy --verbose --all -- -D warnings
- name: Audit
run: cargo audit
30 changes: 13 additions & 17 deletions libs/calypso-cangen/src/can_gen_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use quote::{format_ident, quote};

/**
* Trait to generate individual decode function for a CANMsg
* For NetField and CANPoint, generates parts of the function
* For NetField and CANPoint, generates parts of the function
*/
pub trait CANGenDecode {
fn gen_decoder_fn(&mut self) -> ProcMacro2TokenStream;
Expand Down Expand Up @@ -62,11 +62,11 @@ impl CANGenDecode for NetField {
let mut point_skips = ProcMacro2TokenStream::new();
for point in &self.points {
let size_literal = Literal::usize_unsuffixed(point.size);
let skip_line = quote! {
reader.skip(#size_literal).unwrap();
let skip_line = quote! {
reader.skip(#size_literal).unwrap();
};
point_skips.extend(skip_line);
}
}
quote! {
#point_skips
}
Expand Down Expand Up @@ -148,20 +148,16 @@ impl CANGenDecode for CANPoint {
_ => 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 }
}
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 },
},
_ => {
match self.size {
0..=8 => quote! { u8 },
9..=16 => quote! { u16 },
_ => quote! { u32 }
}
}
};

// prefix to call potential format function
Expand Down
32 changes: 14 additions & 18 deletions libs/calypso-cangen/src/can_gen_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ impl CANGenEncode for CANMsg {
}
}
}
},
}
None => {
quote! { }
quote! {}
}
}
}
Expand Down Expand Up @@ -83,27 +83,23 @@ impl CANGenEncode for CANPoint {
fn gen_encoder_fn(&mut self) -> ProcMacro2TokenStream {
let size_literal = Literal::usize_unsuffixed(self.size);
let write_type = match self.signed {
Some(true) => {
match self.size {
0..=8 => quote! { i8 },
9..=16 => quote! { i16 },
_ => quote! { i32 }
}
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 },
},
_ => {
match self.size {
0..=8 => quote! { u8 },
9..=16 => quote! { u16 },
_ => quote! { u32 }
}
}
};
let format_prefix = match &self.format {
Some(format) => {
let id = format_ident!("{}_e", format);
quote! { FormatData::#id }
}
_ => quote! { },
_ => quote! {},
};
let default_value: f32 = match self.default_value {
Some(default_value) => default_value,
Expand All @@ -119,7 +115,7 @@ impl CANGenEncode for CANPoint {
quote! {
writer.write_as_from::<LittleEndian, #write_type>(#float_final as #write_type).unwrap();
}
},
}
// Big endian (default)
_ => {
match self.signed {
Expand All @@ -128,7 +124,7 @@ impl CANGenEncode for CANPoint {
quote! {
writer.write_signed_out::<#size_literal, #write_type>(#float_final as #write_type).unwrap();
}
},
}
// Unsigned (default)
_ => {
quote! {
Expand Down
20 changes: 16 additions & 4 deletions libs/calypso-cangen/src/can_types.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, I'm assuming you've tested that it deserializes well

Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub struct NetField {
pub points: Vec<CANPoint>,
pub send: Option<bool>,
pub topic_append: Option<bool>,
pub sim_min: Option<f32>,
pub sim_max: Option<f32>,
pub sim_inc_min: Option<f32>,
pub sim_inc_max: Option<f32>,
pub sim: Option<Sim>,
}

/**
Expand All @@ -42,3 +39,18 @@ pub struct CANPoint {
pub format: Option<String>,
pub default_value: Option<f32>,
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum Sim {
SimSweep {
min: f32,
max: f32,
inc_min: f32,
inc_max: f32,
round: Option<bool>,
},
SimEnum {
options: Vec<[f32; 2]>,
},
}
2 changes: 1 addition & 1 deletion libs/calypso-cangen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod can_types;
pub mod can_gen_decode;
pub mod can_gen_encode;
pub mod can_types;
Loading
Loading