Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ impl GroupedAlerts {
}

#[derive(Template)]
#[template(path = "app_canAlerts.c.j2")]
#[template(path = "app_canAlerts.cpp.j2")]
struct AppCanAlertsModuleSource<'a> {
node_tx_alerts: Vec<CanAlert>,
all_alerts: Vec<CanAlert>,
node_name_and_alerts: &'a Vec<(String, &'a GroupedAlerts)>,
node_name: &'a String,
}

#[derive(Template)]
#[template(path = "app_canAlerts.h.j2")]
#[template(path = "app_canAlerts.hpp.j2")]
struct AppCanAlertsModuleHeader<'a> {
node_tx_alerts: &'a GroupedAlerts,
node_name: &'a String,
Expand Down Expand Up @@ -63,15 +63,18 @@ impl AppCanAlertsModule<'_> {
}

impl CPPGenerator for AppCanAlertsModule<'_> {
fn file_stem(&self) -> String {
"app_canAlerts".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
let node_tx_alerts = self
.node_name_and_alerts
.iter()
.find(|(a, _b)| a == self.node_name)
.unwrap()
.1;
AppCanAlertsModuleSource {
node_tx_alerts: self
.node_name_and_alerts
.iter()
.find(|(a, _b)| a == self.node_name)
.unwrap()
.1
.flatten(),
all_alerts: node_tx_alerts.flatten(),
node_name_and_alerts: &self.node_name_and_alerts,
node_name: self.node_name,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::{
};

#[derive(Template)]
#[template(path = "app_canDataCapture.c.j2")]
#[template(path = "app_canDataCapture.cpp.j2")]
struct AppCanDataCaptureModuleSource<'a> {
messages: &'a Vec<CanMessage>,
}

#[derive(Template)]
#[template(path = "app_canDataCapture.h.j2")]
#[template(path = "app_canDataCapture.hpp.j2")]
struct AppCanDataCaptureModuleHeader {}

pub struct AppCanDataCaptureModule {
Expand All @@ -30,6 +30,9 @@ impl AppCanDataCaptureModule {
}

impl CPPGenerator for AppCanDataCaptureModule {
fn file_stem(&self) -> String {
"app_canDataCapture".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
AppCanDataCaptureModuleHeader {}.render()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use crate::{
use askama::Template;

#[derive(Template)]
#[template(path = "app_canRx.c.j2")]
#[template(path = "app_canRx.cpp.j2")]
struct AppCanRxModuleSource<'a> {
node: &'a String,
boards_messages: &'a HashMap<String, Vec<CanMessage>>, // maps messages to the board they came from
}

#[derive(Template)]
#[template(path = "app_canRx.h.j2")]
#[template(path = "app_canRx.hpp.j2")]
struct AppCanRxModuleHeader<'a> {
boards_messages: &'a HashMap<String, Vec<CanMessage>>, // maps messages to the board they came from
}
Expand Down Expand Up @@ -48,6 +48,9 @@ impl AppCanRxModule<'_> {
}

impl CPPGenerator for AppCanRxModule<'_> {
fn file_stem(&self) -> String {
"app_canRx".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
AppCanRxModuleSource {
node: self.node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::{
};

#[derive(Template)]
#[template(path = "app_canTx.c.j2")]
#[template(path = "app_canTx.cpp.j2")]
struct AppCanTxModuleSource<'a> {
tx_messages: &'a Vec<CanMessage>,
}

#[derive(Template)]
#[template(path = "app_canTx.h.j2")]
#[template(path = "app_canTx.hpp.j2")]
struct AppCanTxModuleHeader<'a> {
tx_messages: &'a Vec<CanMessage>,
}
Expand All @@ -30,6 +30,9 @@ impl AppCanTxModule {
}

impl CPPGenerator for AppCanTxModule {
fn file_stem(&self) -> String {
"app_canTx".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
AppCanTxModuleHeader {
tx_messages: &self.tx_messages,
Expand Down
33 changes: 17 additions & 16 deletions scripts/code_generation/jsoncan-rust/src/codegen/cpp/app_canutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{
codegen::cpp::CPPGenerator,
};

mod filters {}

struct Iteration {
starting_byte: u16,
shift: u16,
Expand All @@ -14,7 +16,7 @@ struct Iteration {
}

#[derive(Template)]
#[template(path = "app_canUtils.c.j2")]
#[template(path = "app_canUtils.cpp.j2")]
struct AppCanUtilsModuleSource<'a> {
messages: &'a Vec<CanMessage>,
}
Expand Down Expand Up @@ -57,27 +59,25 @@ impl AppCanUtilsModuleSource<'_> {

while packed_bits < signal.bits {
let bit_start = signal.start_bit + packed_bits;
let starting_byte = bit_start / BYTE_SIZE;
let bit_in_byte = bit_start % BYTE_SIZE;
let bits_to_pack = std::cmp::min(BYTE_SIZE - bit_in_byte, signal.bits - packed_bits);

let shift = if signal.big_endian {
signal.bits - packed_bits - bits_to_pack - bit_in_byte
} else {
packed_bits - bit_in_byte
};

let mask = (1 << bits_to_pack) - 1 << bit_in_byte;
let mask_text = format!("0x{:X}", mask);
let mut comment_data = vec!["_"; BYTE_SIZE as usize];
for i in (bit_start % BYTE_SIZE)..((bit_start % BYTE_SIZE) + bits_to_pack) {
comment_data[i as usize] = "#";
}

iterations.push(Iteration {
starting_byte,
shift,
mask_text,
starting_byte: bit_start / BYTE_SIZE,
shift: if signal.big_endian {
signal.bits - packed_bits - bits_to_pack - bit_in_byte
} else {
if packed_bits < bit_in_byte {
panic!("{}", signal.name);
}
packed_bits - bit_in_byte
},
mask_text: format!("0x{:X}", (1 << bits_to_pack) - (1 << bit_in_byte)),
comment_data: comment_data.into_iter().rev().collect::<String>(),
bits_to_pack,
});
Expand All @@ -93,10 +93,8 @@ impl AppCanUtilsModuleSource<'_> {
}
}

mod filters {}

#[derive(Template)]
#[template(path = "app_canUtils.h.j2")]
#[template(path = "app_canUtils.hpp.j2")]
struct AppCanUtilsModuleHeader<'a> {
messages: &'a Vec<CanMessage>,
node_names: &'a Vec<String>,
Expand Down Expand Up @@ -125,6 +123,9 @@ impl AppCanUtilsModule {
}

impl CPPGenerator for AppCanUtilsModule {
fn file_stem(&self) -> String {
"app_canUtils".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
AppCanUtilsModuleHeader {
messages: &self.messages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};

#[derive(Template)]
#[template(path = "io_canReroute.c.j2")]
#[template(path = "io_canReroute.cpp.j2")]
struct IoCanRerouteModuleSource<'a> {
can_db: &'a CanDatabase,
node_busses: &'a Vec<&'a CanBus>,
Expand All @@ -21,7 +21,7 @@ impl IoCanRerouteModuleSource<'_> {
}

#[derive(Template)]
#[template(path = "io_canReroute.h.j2")]
#[template(path = "io_canReroute.hpp.j2")]
struct IoCanRerouteModuleHeader<'a> {
node_busses: &'a Vec<&'a CanBus>,
}
Expand Down Expand Up @@ -52,6 +52,9 @@ impl IoCanRerouteModule<'_> {
}

impl CPPGenerator for IoCanRerouteModule<'_> {
fn file_stem(&self) -> String {
"io_canReroute".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
IoCanRerouteModuleHeader {
node_busses: &self.node_busses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::can_database::CanMessage;
use crate::{can_database::CanDatabase, codegen::cpp::CPPGenerator, reroute::CanRxConfig};

#[derive(Template)]
#[template(path = "io_canRx.c.j2")]
#[template(path = "io_canRx.cpp.j2")]
struct IoCanRxModuleSource<'a> {
messages_on_busses: &'a Vec<(String, Vec<CanMessage>)>,
}

#[derive(Template)]
#[template(path = "io_canRx.h.j2")]
#[template(path = "io_canRx.hpp.j2")]
struct IoCanRxModuleHeader<'a> {
node: &'a String,
messages_on_busses: &'a Vec<(String, Vec<CanMessage>)>,
Expand Down Expand Up @@ -44,6 +44,9 @@ impl IoCanRxModule<'_> {
}

impl CPPGenerator for IoCanRxModule<'_> {
fn file_stem(&self) -> String {
"io_canRx".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
IoCanRxModuleHeader {
node: self.board,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use crate::{
};

#[derive(Template)]
#[template(path = "io_canTx.c.j2")]
#[template(path = "io_canTx.cpp.j2")]
struct IoCanTxModuleSource<'a> {
node_buses: &'a Vec<&'a CanBus>,
messages: &'a Vec<(CanMessage, Vec<String>)>,
}

#[derive(Template)]
#[template(path = "io_canTx.h.j2")]
#[template(path = "io_canTx.hpp.j2")]
struct IoCanTxModuleHeader<'a> {
node_buses: &'a Vec<&'a CanBus>,
messages: &'a Vec<(CanMessage, Vec<String>)>,
Expand Down Expand Up @@ -50,6 +50,9 @@ impl IoCanTxModule<'_> {
}

impl CPPGenerator for IoCanTxModule<'_> {
fn file_stem(&self) -> String {
"io_canTx".to_string()
}
fn header_template(&self) -> Result<String, askama::Error> {
IoCanTxModuleHeader {
messages: &self.messages,
Expand Down
42 changes: 40 additions & 2 deletions scripts/code_generation/jsoncan-rust/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use convert_case::{Case, Casing};
pub trait CPPGenerator {
fn header_template(&self) -> Result<String, askama::Error>;
fn source_template(&self) -> Result<String, askama::Error>;
fn file_stem(&self) -> String;
}

pub enum CPPModule<'a> {
Expand All @@ -37,6 +38,18 @@ pub enum CPPModule<'a> {
}

impl CPPGenerator for CPPModule<'_> {
fn file_stem(&self) -> String {
match self {
CPPModule::AppCanUtilsModule(module) => module.file_stem(),
CPPModule::AppCanTxModule(module) => module.file_stem(),
CPPModule::AppCanAlertsModule(module) => module.file_stem(),
CPPModule::AppCanDataCaptureModule(module) => module.file_stem(),
CPPModule::AppCanRxModule(module) => module.file_stem(),
CPPModule::IoCanTxModule(module) => module.file_stem(),
CPPModule::IoCanRxModule(module) => module.file_stem(),
CPPModule::IoCanRerouteModule(module) => module.file_stem(),
}
}
fn header_template(&self) -> Result<String, askama::Error> {
match self {
CPPModule::AppCanUtilsModule(module) => module.header_template(),
Expand Down Expand Up @@ -93,11 +106,36 @@ impl CanSignal {
}

pub fn datatype(self: &Self) -> String {
todo!()
if self.scale != 1.0 || self.offset != 0.0 {
return "float".to_string();
}
if self.signed {
match self.bits {
8 => "int8_t".to_string(),
16 => "int16_t".to_string(),
32 => "int32_t".to_string(),
64 => "int64_t".to_string(),
_ => "int".to_string(), // Fallback for unusual bit lengths
}
} else {
match self.bits {
8 => "uint8_t".to_string(),
16 => "uint16_t".to_string(),
32 => "uint32_t".to_string(),
64 => "uint64_t".to_string(),
_ => "unsigned int".to_string(), // Fallback for unusual bit lengths
}
}
}

pub fn representation(self: &Self) -> String {
todo!()
if self.scale != 1.0 || self.offset != 0.0 {
return "float".to_string();
}
if self.signed {
return "int".to_string();
}
"uint".to_string()
}
}

Expand Down
Loading
Loading