Skip to content

Commit 2d63e0a

Browse files
authored
Emit MESSAGE_CYCLE_TIME as integer (#186)
Emits the `MESSAGE_CYCLE_TIME` constant as an raw integer instead of a `core::time::Duration` and renames it to `MESSAGE_CYCLE_TIME_MS`. This allows downstream users to ingest the value cleanly into any timing library (`core`, `fugit`, `embassy_time`, etc.).
1 parent a23d8ce commit 2d63e0a

12 files changed

Lines changed: 33 additions & 50 deletions

src/lib.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ impl Config<'_> {
209209
writeln!(w, "use bitvec::prelude::*;")?;
210210
writeln!(w, "#[allow(unused_imports)]")?;
211211
writeln!(w, "use embedded_can::{{Id, StandardId, ExtendedId}};")?;
212-
if get_relevant_messages(&dbc).any(|m| message_cycle_time_ms(&dbc, m.id).is_some()) {
213-
writeln!(w, "use core::time::Duration;")?;
214-
}
215212

216213
self.impl_arbitrary.fmt_cfg(&mut w, |w| {
217214
writeln!(w, "use arbitrary::{{Arbitrary, Unstructured}};")
@@ -677,10 +674,7 @@ impl Config<'_> {
677674
let Some(ms) = message_cycle_time_ms(dbc, msg.id) else {
678675
return Ok(());
679676
};
680-
writeln!(
681-
w,
682-
"pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis({ms});"
683-
)?;
677+
writeln!(w, "pub const MESSAGE_CYCLE_TIME_MS: u32 = {ms};")?;
684678
Ok(())
685679
}
686680

@@ -1635,12 +1629,12 @@ fn attr_value_literal(value: &AttributeValue) -> String {
16351629
}
16361630
}
16371631

1638-
/// Cycle time in milliseconds from an assigned and valid
1639-
// `GenMsgCycleTime` (`BA_`) attribute.
1640-
fn message_cycle_time_ms(dbc: &Dbc, id: MessageId) -> Option<u64> {
1632+
/// Cycle time in milliseconds from an assigned `GenMsgCycleTime` (`BA_`)
1633+
/// attribute, if it is present and a non-negative integer that fits within a `u32`.
1634+
fn message_cycle_time_ms(dbc: &Dbc, id: MessageId) -> Option<u32> {
16411635
match dbc.message_attribute(id, "GenMsgCycleTime")? {
1642-
AttributeValue::Uint(v) => Some(*v),
1643-
AttributeValue::Int(v) => u64::try_from(*v).ok(),
1636+
AttributeValue::Uint(v) => u32::try_from(*v).ok(),
1637+
AttributeValue::Int(v) => u32::try_from(*v).ok(),
16441638
AttributeValue::Double(_) | AttributeValue::String(_) => None,
16451639
}
16461640
}

tests-snapshots/dbc-cantools/attribute_Event.snap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -71,7 +70,7 @@ impl Inv2EventMsg1 {
7170
StandardId::new_unchecked(0x4d2)
7271
});
7372
pub const MESSAGE_SIZE: usize = 8;
74-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(0);
73+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 0;
7574
pub const THE_SIGNAL_MIN: i8 = 0_i8;
7675
pub const THE_SIGNAL_MAX: i8 = 0_i8;
7776
/// Construct new INV2EventMsg1 from values

tests-snapshots/dbc-cantools/attributes.snap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -75,7 +74,7 @@ impl TheMessage {
7574
ExtendedId::new_unchecked(0x39)
7675
});
7776
pub const MESSAGE_SIZE: usize = 8;
78-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(1000);
77+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 1000;
7978
pub const THE_SIGNAL_MIN: i8 = 0_i8;
8079
pub const THE_SIGNAL_MAX: i8 = 0_i8;
8180
/// Construct new TheMessage from values

tests-snapshots/dbc-cantools/attributes_relation.snap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -72,7 +71,7 @@ impl Message2 {
7271
StandardId::new_unchecked(0x53)
7372
});
7473
pub const MESSAGE_SIZE: usize = 8;
75-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(50);
74+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 50;
7675
pub const SIGNAL_2_MIN: i8 = 0_i8;
7776
pub const SIGNAL_2_MAX: i8 = 0_i8;
7877
/// Construct new Message_2 from values

tests-snapshots/dbc-cantools/big_numbers.snap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -75,7 +74,7 @@ impl TheMessage {
7574
ExtendedId::new_unchecked(0x39)
7675
});
7776
pub const MESSAGE_SIZE: usize = 8;
78-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(1000);
77+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 1000;
7978
pub const THE_SIGNAL_MIN: i8 = 0_i8;
8079
pub const THE_SIGNAL_MAX: i8 = 0_i8;
8180
/// Construct new TheMessage from values

tests-snapshots/dbc-cantools/foobar.snap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -242,7 +241,7 @@ impl Fum {
242241
ExtendedId::new_unchecked(0x12331)
243242
});
244243
pub const MESSAGE_SIZE: usize = 5;
245-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(1);
244+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 1;
246245
pub const FUM_MIN: i16 = 0_i16;
247246
pub const FUM_MAX: i16 = 10_i16;
248247
pub const FAM_MIN: i16 = 0_i16;

tests-snapshots/dbc-cantools/issue_168.snap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -242,7 +241,7 @@ impl Fum {
242241
ExtendedId::new_unchecked(0x12331)
243242
});
244243
pub const MESSAGE_SIZE: usize = 5;
245-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(1);
244+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 1;
246245
pub const FUM_MIN: i16 = 0_i16;
247246
pub const FUM_MAX: i16 = 10_i16;
248247
pub const FAM_MIN: i16 = 0_i16;

tests-snapshots/dbc-cantools/socialledge-written-by-cantools-with-default-sort-signals.snap.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -89,7 +88,7 @@ impl DriverHeartbeat {
8988
StandardId::new_unchecked(0x64)
9089
});
9190
pub const MESSAGE_SIZE: usize = 1;
92-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(1000);
91+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 1000;
9392
pub const DRIVER_HEARTBEAT_CMD_MIN: u8 = 0_u8;
9493
pub const DRIVER_HEARTBEAT_CMD_MAX: u8 = 0_u8;
9594
/// Construct new DRIVER_HEARTBEAT from values
@@ -243,7 +242,7 @@ impl IoDebug {
243242
StandardId::new_unchecked(0x1f4)
244243
});
245244
pub const MESSAGE_SIZE: usize = 4;
246-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
245+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
247246
pub const IO_DEBUG_TEST_FLOAT_MIN: f32 = 0_f32;
248247
pub const IO_DEBUG_TEST_FLOAT_MAX: f32 = 0_f32;
249248
pub const IO_DEBUG_TEST_SIGNED_MIN: i8 = 0_i8;
@@ -529,7 +528,7 @@ impl MotorCmd {
529528
StandardId::new_unchecked(0x65)
530529
});
531530
pub const MESSAGE_SIZE: usize = 1;
532-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
531+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
533532
pub const MOTOR_CMD_DRIVE_MIN: u8 = 0_u8;
534533
pub const MOTOR_CMD_DRIVE_MAX: u8 = 9_u8;
535534
pub const MOTOR_CMD_STEER_MIN: i8 = -5_i8;
@@ -693,7 +692,7 @@ impl MotorStatus {
693692
StandardId::new_unchecked(0x190)
694693
});
695694
pub const MESSAGE_SIZE: usize = 3;
696-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
695+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
697696
pub const MOTOR_STATUS_SPEED_KPH_MIN: f32 = 0_f32;
698697
pub const MOTOR_STATUS_SPEED_KPH_MAX: f32 = 0_f32;
699698
/// Construct new MOTOR_STATUS from values
@@ -841,7 +840,7 @@ impl SensorSonars {
841840
StandardId::new_unchecked(0xc8)
842841
});
843842
pub const MESSAGE_SIZE: usize = 8;
844-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
843+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
845844
pub const SENSOR_SONARS_NO_FILT_REAR_MIN: f32 = 0_f32;
846845
pub const SENSOR_SONARS_NO_FILT_REAR_MAX: f32 = 0_f32;
847846
pub const SENSOR_SONARS_REAR_MIN: f32 = 0_f32;

tests-snapshots/dbc-cantools/socialledge-written-by-cantools-with-sort-signals-by-name.snap.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -89,7 +88,7 @@ impl DriverHeartbeat {
8988
StandardId::new_unchecked(0x64)
9089
});
9190
pub const MESSAGE_SIZE: usize = 1;
92-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(1000);
91+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 1000;
9392
pub const DRIVER_HEARTBEAT_CMD_MIN: u8 = 0_u8;
9493
pub const DRIVER_HEARTBEAT_CMD_MAX: u8 = 0_u8;
9594
/// Construct new DRIVER_HEARTBEAT from values
@@ -243,7 +242,7 @@ impl IoDebug {
243242
StandardId::new_unchecked(0x1f4)
244243
});
245244
pub const MESSAGE_SIZE: usize = 4;
246-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
245+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
247246
pub const IO_DEBUG_TEST_ENUM_MIN: u8 = 0_u8;
248247
pub const IO_DEBUG_TEST_ENUM_MAX: u8 = 0_u8;
249248
pub const IO_DEBUG_TEST_FLOAT_MIN: f32 = 0_f32;
@@ -529,7 +528,7 @@ impl MotorCmd {
529528
StandardId::new_unchecked(0x65)
530529
});
531530
pub const MESSAGE_SIZE: usize = 1;
532-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
531+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
533532
pub const MOTOR_CMD_DRIVE_MIN: u8 = 0_u8;
534533
pub const MOTOR_CMD_DRIVE_MAX: u8 = 9_u8;
535534
pub const MOTOR_CMD_STEER_MIN: i8 = -5_i8;
@@ -693,7 +692,7 @@ impl MotorStatus {
693692
StandardId::new_unchecked(0x190)
694693
});
695694
pub const MESSAGE_SIZE: usize = 3;
696-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
695+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
697696
pub const MOTOR_STATUS_SPEED_KPH_MIN: f32 = 0_f32;
698697
pub const MOTOR_STATUS_SPEED_KPH_MAX: f32 = 0_f32;
699698
/// Construct new MOTOR_STATUS from values
@@ -841,7 +840,7 @@ impl SensorSonars {
841840
StandardId::new_unchecked(0xc8)
842841
});
843842
pub const MESSAGE_SIZE: usize = 8;
844-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
843+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
845844
pub const SENSOR_SONARS_ERR_COUNT_MIN: u16 = 0_u16;
846845
pub const SENSOR_SONARS_ERR_COUNT_MAX: u16 = 0_u16;
847846
pub const SENSOR_SONARS_LEFT_MIN: f32 = 0_f32;

tests-snapshots/dbc-cantools/socialledge-written-by-cantools.snap.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::BitOr;
1010
use bitvec::prelude::*;
1111
#[allow(unused_imports)]
1212
use embedded_can::{Id, StandardId, ExtendedId};
13-
use core::time::Duration;
1413
/// All messages
1514
#[allow(
1615
clippy::absurd_extreme_comparisons,
@@ -89,7 +88,7 @@ impl DriverHeartbeat {
8988
StandardId::new_unchecked(0x64)
9089
});
9190
pub const MESSAGE_SIZE: usize = 1;
92-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(1000);
91+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 1000;
9392
pub const DRIVER_HEARTBEAT_CMD_MIN: u8 = 0_u8;
9493
pub const DRIVER_HEARTBEAT_CMD_MAX: u8 = 0_u8;
9594
/// Construct new DRIVER_HEARTBEAT from values
@@ -243,7 +242,7 @@ impl IoDebug {
243242
StandardId::new_unchecked(0x1f4)
244243
});
245244
pub const MESSAGE_SIZE: usize = 4;
246-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
245+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
247246
pub const IO_DEBUG_TEST_UNSIGNED_MIN: u8 = 0_u8;
248247
pub const IO_DEBUG_TEST_UNSIGNED_MAX: u8 = 0_u8;
249248
pub const IO_DEBUG_TEST_ENUM_MIN: u8 = 0_u8;
@@ -529,7 +528,7 @@ impl MotorCmd {
529528
StandardId::new_unchecked(0x65)
530529
});
531530
pub const MESSAGE_SIZE: usize = 1;
532-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
531+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
533532
pub const MOTOR_CMD_STEER_MIN: i8 = -5_i8;
534533
pub const MOTOR_CMD_STEER_MAX: i8 = 5_i8;
535534
pub const MOTOR_CMD_DRIVE_MIN: u8 = 0_u8;
@@ -693,7 +692,7 @@ impl MotorStatus {
693692
StandardId::new_unchecked(0x190)
694693
});
695694
pub const MESSAGE_SIZE: usize = 3;
696-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
695+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
697696
pub const MOTOR_STATUS_SPEED_KPH_MIN: f32 = 0_f32;
698697
pub const MOTOR_STATUS_SPEED_KPH_MAX: f32 = 0_f32;
699698
/// Construct new MOTOR_STATUS from values
@@ -841,7 +840,7 @@ impl SensorSonars {
841840
StandardId::new_unchecked(0xc8)
842841
});
843842
pub const MESSAGE_SIZE: usize = 8;
844-
pub const MESSAGE_CYCLE_TIME: Duration = Duration::from_millis(100);
843+
pub const MESSAGE_CYCLE_TIME_MS: u32 = 100;
845844
pub const SENSOR_SONARS_MUX_MIN: u8 = 0_u8;
846845
pub const SENSOR_SONARS_MUX_MAX: u8 = 0_u8;
847846
pub const SENSOR_SONARS_ERR_COUNT_MIN: u16 = 0_u16;

0 commit comments

Comments
 (0)