diff --git a/integration_tests/tests/pdo_tests.rs b/integration_tests/tests/pdo_tests.rs index 87d9279..ed45761 100644 --- a/integration_tests/tests/pdo_tests.rs +++ b/integration_tests/tests/pdo_tests.rs @@ -140,7 +140,7 @@ async fn test_tpdo_assignment() { rx.flush(); - let sync_msg = SyncObject::new(1).into(); + let sync_msg = SyncObject::new(Some(1)).into(); sender.send(sync_msg).await.unwrap(); // We expect to receive the sync message just sent first diff --git a/zencan-common/src/messages.rs b/zencan-common/src/messages.rs index e8532c8..7b5e0e7 100644 --- a/zencan-common/src/messages.rs +++ b/zencan-common/src/messages.rs @@ -337,37 +337,39 @@ impl From for CanMessage { /// Represents a SYNC object/message /// /// A single CAN node can serve as the SYNC provider, sending a periodic sync object to all other -/// nodes. The one byte count value starts at 1, and increments. On overflow, it should be reset to -/// 1. -#[derive(Clone, Copy, Debug)] +/// nodes. When used, the one byte count value starts at 1, and increments. On overflow, it should be reset to +/// 1. It is optional, and when not provided the sync message DLC will be zero. +#[derive(Clone, Copy, Debug, Default)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct SyncObject { - count: u8, + count: Option, } impl SyncObject { /// Create a new SyncObjectd - pub fn new(count: u8) -> Self { + pub fn new(count: Option) -> Self { Self { count } } } -impl Default for SyncObject { - fn default() -> Self { - Self { count: 1 } - } -} - impl From for CanMessage { fn from(value: SyncObject) -> Self { - CanMessage::new(SYNC_ID, &[value.count]) + if let Some(count) = value.count { + CanMessage::new(SYNC_ID, &[count]) + } else { + CanMessage::new(SYNC_ID, &[]) + } } } impl From for SyncObject { fn from(msg: CanMessage) -> Self { if msg.id() == SYNC_ID { - let count = msg.data()[0]; + let count = if msg.data().is_empty() { + None + } else { + Some(msg.data()[0]) + }; Self { count } } else { panic!("Invalid message ID for SyncObject");