Skip to content
Merged
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
2 changes: 1 addition & 1 deletion integration_tests/tests/pdo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 15 additions & 13 deletions zencan-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,37 +337,39 @@ impl From<Heartbeat> 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<u8>,
}

impl SyncObject {
/// Create a new SyncObjectd
pub fn new(count: u8) -> Self {
pub fn new(count: Option<u8>) -> Self {
Self { count }
}
}

impl Default for SyncObject {
fn default() -> Self {
Self { count: 1 }
}
}

impl From<SyncObject> 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<CanMessage> 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");
Expand Down