Skip to content

Commit 06b6b44

Browse files
author
NoahAndrews
committed
Add stopHeartbeats() function
1 parent 9115366 commit 06b6b44

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

lib/binding.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class CanBridge {
6363
setThreadPriority: (descriptor: string, priority: ThreadPriority) => void;
6464
setSparkMaxHeartbeatData: (descriptor: string, heartbeatData: number[]) => void;
6565
startRevCommonHeartbeat: (descriptor: string) => void;
66+
stopHeartbeats: (descriptor: string) => void;
6667
ackHeartbeats: () => void;
6768

6869
constructor() {
@@ -90,6 +91,7 @@ export class CanBridge {
9091
this.setSparkMaxHeartbeatData = addon.setSparkMaxHeartbeatData;
9192
this.startRevCommonHeartbeat = addon.startRevCommonHeartbeat;
9293
this.ackHeartbeats = addon.ackHeartbeats;
94+
this.stopHeartbeats = addon.stopHeartbeats;
9395
} catch (e: any) {
9496
throw new CanBridgeInitializationError(e);
9597
}

src/addon.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
4242
Napi::Function::New(env, setSparkMaxHeartbeatData));
4343
exports.Set(Napi::String::New(env, "startRevCommonHeartbeat"),
4444
Napi::Function::New(env, startRevCommonHeartbeat));
45+
exports.Set(Napi::String::New(env, "stopHeartbeats"),
46+
Napi::Function::New(env, stopHeartbeats));
4547
exports.Set(Napi::String::New(env, "ackHeartbeats"),
4648
Napi::Function::New(env, ackHeartbeats));
4749
return exports;

src/canWrapper.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#define SPARK_HEARTBEAT_LENGTH 8
3131
#define REV_COMMON_HEARTBEAT_LENGTH 1
32+
uint8_t disabledSparkHeartbeat[] = {0, 0, 0, 0, 0, 0, 0, 0};
33+
uint8_t disabledRevCommonHeartbeat[] = {0};
3234

3335
rev::usb::CandleWinUSBDriver* driver = new rev::usb::CandleWinUSBDriver();
3436

@@ -682,8 +684,6 @@ void heartbeatsWatchdog() {
682684
if (elapsed_seconds.count() >= 1 && !heartbeatTimeoutExpired) {
683685
// The heartbeat timeout just expired
684686
heartbeatTimeoutExpired = true;
685-
uint8_t disabledSparkHeartbeat[] = {0, 0, 0, 0, 0, 0, 0, 0};
686-
uint8_t disabledRevCommonHeartbeat[] = {0};
687687
for(int i = 0; i < heartbeatsRunning.size(); i++) {
688688
if (sparkHeartbeatMap.contains(heartbeatsRunning[i])) {
689689
// Clear the scheduled heartbeat that has outdated data so that the updated one gets sent out immediately
@@ -804,3 +804,11 @@ void setSparkMaxHeartbeatData(const Napi::CallbackInfo& info) {
804804
heartbeatsRunning.push_back(descriptor);
805805
}
806806
}
807+
808+
void stopHeartbeats(const Napi::CallbackInfo& info) {
809+
std::string descriptor = info[0].As<Napi::String>().Utf8Value();
810+
std::scoped_lock lock{watchdogMtx};
811+
// Send disabled SPARK and REV common heartbeats and un-schedule them for the future
812+
_sendCANMessage(descriptor, SPARK_HEARTBEAT_ID, disabledSparkHeartbeat, SPARK_HEARTBEAT_LENGTH, 0);
813+
_sendCANMessage(descriptor, REV_COMMON_HEARTBEAT_ID, disabledRevCommonHeartbeat, REV_COMMON_HEARTBEAT_LENGTH, 0);
814+
}

src/canWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ void closeHALStreamSession(const Napi::CallbackInfo& info);
2424
void setThreadPriority(const Napi::CallbackInfo& info);
2525
void setSparkMaxHeartbeatData(const Napi::CallbackInfo& info);
2626
void startRevCommonHeartbeat(const Napi::CallbackInfo& info);
27+
void stopHeartbeats(const Napi::CallbackInfo& info);
2728
void ackHeartbeats(const Napi::CallbackInfo& info);
2829
#endif

0 commit comments

Comments
 (0)