From 83b9cea3fd2cb58554211916147c480355833cac Mon Sep 17 00:00:00 2001 From: LandryNorris Date: Wed, 11 Sep 2024 10:07:06 -0500 Subject: [PATCH 1/5] Allow multiple messages to be scheduled with the same ID if the interval is set to 0. --- .../include/rev/Drivers/DriverDeviceThread.h | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/native/include/rev/Drivers/DriverDeviceThread.h b/src/main/native/include/rev/Drivers/DriverDeviceThread.h index b01d7ff..de2e072 100644 --- a/src/main/native/include/rev/Drivers/DriverDeviceThread.h +++ b/src/main/native/include/rev/Drivers/DriverDeviceThread.h @@ -84,6 +84,15 @@ class DriverDeviceThread { return nullptr; // If no matching element found } + detail::CANThreadSendQueueElement* findFirstMatchingIdWithNonZeroInterval(int targetId) { + for (auto& element : m_sendQueue) { + if (element.m_msg.GetMessageId() == targetId && element.m_intervalMs > 0) { + return &element; + } + } + return nullptr; // If no matching element found + } + void removeElementsWithId(int targetId) { m_sendQueue.erase(std::remove_if(m_sendQueue.begin(), m_sendQueue.end(), [targetId](detail::CANThreadSendQueueElement element) { return element.m_msg.GetMessageId() == targetId; }), m_sendQueue.end()); } @@ -91,13 +100,19 @@ class DriverDeviceThread { bool EnqueueMessage(const CANMessage& msg, int32_t timeIntervalMs) { std::lock_guard lock(m_writeMutex); - detail::CANThreadSendQueueElement* existing = findFirstMatchingId(msg.GetMessageId()); - - if(existing) { - existing->m_intervalMs = timeIntervalMs; - existing->m_msg = msg; - } else { + if(timeIntervalMs == 0) { + // If the time interval is 0, we want to allow duplicates. m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs)); + } else { + // We don't want to replace elements with zero as the interval. Those should be guaranteed to be sent + detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId()); + + if(existing) { + existing->m_intervalMs = timeIntervalMs; + existing->m_msg = msg; + } else { + m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs)); + } } // TODO: Limit the max queue size From 31795ab49d7b3f232b6d94341460375226cc985a Mon Sep 17 00:00:00 2001 From: LandryNorris Date: Wed, 11 Sep 2024 10:45:25 -0500 Subject: [PATCH 2/5] Cancel any repeating frames when calling sendCANMessage with periodMs == 0 --- src/main/native/include/rev/Drivers/DriverDeviceThread.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/native/include/rev/Drivers/DriverDeviceThread.h b/src/main/native/include/rev/Drivers/DriverDeviceThread.h index de2e072..884e491 100644 --- a/src/main/native/include/rev/Drivers/DriverDeviceThread.h +++ b/src/main/native/include/rev/Drivers/DriverDeviceThread.h @@ -103,6 +103,12 @@ class DriverDeviceThread { if(timeIntervalMs == 0) { // If the time interval is 0, we want to allow duplicates. m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs)); + + // Cancel existing repeating frame with same id + detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId()); + if(existing) { + existing->m_intervalMs = -1; + } } else { // We don't want to replace elements with zero as the interval. Those should be guaranteed to be sent detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId()); From e6aa6a01ad153ee9406e8c32e2823600538b7c8d Mon Sep 17 00:00:00 2001 From: LandryNorris Date: Wed, 11 Sep 2024 11:19:31 -0500 Subject: [PATCH 3/5] Remove now-unused method --- src/main/native/include/rev/Drivers/DriverDeviceThread.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/native/include/rev/Drivers/DriverDeviceThread.h b/src/main/native/include/rev/Drivers/DriverDeviceThread.h index 884e491..c56d553 100644 --- a/src/main/native/include/rev/Drivers/DriverDeviceThread.h +++ b/src/main/native/include/rev/Drivers/DriverDeviceThread.h @@ -75,15 +75,6 @@ class DriverDeviceThread { } } - detail::CANThreadSendQueueElement* findFirstMatchingId(int targetId) { - for (auto& element : m_sendQueue) { - if (element.m_msg.GetMessageId() == targetId) { - return &element; - } - } - return nullptr; // If no matching element found - } - detail::CANThreadSendQueueElement* findFirstMatchingIdWithNonZeroInterval(int targetId) { for (auto& element : m_sendQueue) { if (element.m_msg.GetMessageId() == targetId && element.m_intervalMs > 0) { From e03703ca6f2ced2b7958c25044ae8e458189da8f Mon Sep 17 00:00:00 2001 From: LandryNorris Date: Wed, 11 Sep 2024 11:35:31 -0500 Subject: [PATCH 4/5] Update README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c08da1..a577492 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ new call with different data is sent before the previous interval is up. Sending a frame with an interval of -1 will cancel the repeat, and not send the frame. Sending with an interval of 0 will schedule the new frame once, then stop -repeating. +repeating. Unlike with a positive interval, and interval of 0 +guarantees that each new message will be sent. ## Build Requirements From cf937c8d03d9c369c9e9f0b85445a4d7083faeb6 Mon Sep 17 00:00:00 2001 From: LandryNorris Date: Wed, 11 Sep 2024 11:36:47 -0500 Subject: [PATCH 5/5] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a577492..d89cb62 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ new call with different data is sent before the previous interval is up. Sending a frame with an interval of -1 will cancel the repeat, and not send the frame. Sending with an interval of 0 will schedule the new frame once, then stop -repeating. Unlike with a positive interval, and interval of 0 +repeating. Unlike with a positive interval, an interval of 0 guarantees that each new message will be sent. ## Build Requirements