Skip to content

Commit

Permalink
Add getTimestampsForAllReceivedMessages() function
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahAndrews committed Nov 7, 2024
1 parent a6feef6 commit d134630
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
27 changes: 5 additions & 22 deletions lib/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export class CanBridge {
startRevCommonHeartbeat: (descriptor: string) => void;
stopHeartbeats: (descriptor: string, sendDisabledHeartbeatsFirst: boolean) => void;
ackHeartbeats: () => void;
/**
* @return Object that maps arbitration IDs to the timestamp a message with that ID was last received at
*/
getTimestampsForAllReceivedMessages: (descriptor: string) => Record<number, number>;

constructor() {
try {
Expand Down Expand Up @@ -103,30 +107,9 @@ export class CanBridge {
this.startRevCommonHeartbeat = addon.startRevCommonHeartbeat;
this.ackHeartbeats = addon.ackHeartbeats;
this.stopHeartbeats = addon.stopHeartbeats;
this.getTimestampsForAllReceivedMessages = addon.getTimestampsForAllReceivedMessages;
} catch (e: any) {
throw new CanBridgeInitializationError(e);
}
}
}






















2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rev-robotics/can-bridge",
"version": "3.4.2",
"version": "3.5.0",
"author": "REV Robotics",
"description": "Access CAN Data from a USB device in Node.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion scripts/download-CanBridge.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "path";
import axios from 'axios';
import AdmZip from 'adm-zip';

const canBridgeTag = "v2.5.1";
const canBridgeTag = "v2.6.0";
const canBridgeReleaseAssetUrlPrefix = `https://github.com/REVrobotics/CANBridge/releases/download/${canBridgeTag}`;

const externalCompileTimeDepsPath = 'externalCompileTimeDeps';
Expand Down
2 changes: 2 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function::New(env, stopHeartbeats));
exports.Set(Napi::String::New(env, "ackHeartbeats"),
Napi::Function::New(env, ackHeartbeats));
exports.Set(Napi::String::New(env, "getTimestampsForAllReceivedMessages"),
Napi::Function::New(env, getTimestampsForAllReceivedMessages));
return exports;
}

Expand Down
35 changes: 35 additions & 0 deletions src/canWrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,41 @@ Napi::Array getImageElements(const Napi::CallbackInfo& info) {
return elements;
}

Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string descriptor = info[0].As<Napi::String>().Utf8Value();

std::shared_ptr<rev::usb::CANDevice> device;

{ // This block exists to define how long we hold canDevicesMtx
std::scoped_lock lock{canDevicesMtx};
auto deviceIterator = canDeviceMap.find(descriptor);
if (deviceIterator == canDeviceMap.end()) {
if (devicesRegisteredToHal.find(descriptor) != devicesRegisteredToHal.end()) return receiveHalMessage(info);
Napi::Error::New(env, DEVICE_NOT_FOUND_ERROR).ThrowAsJavaScriptException();
return Napi::Object::New(env);
}
device = deviceIterator->second;
}

std::map<uint32_t, std::shared_ptr<rev::usb::CANMessage>> messages;
bool success = device->CopyReceivedMessagesMap(messages);
if (!success) {
Napi::Error::New(env, "Failed to copy the map of received messages").ThrowAsJavaScriptException();
return Napi::Object::New(env);
}

Napi::Object result = Napi::Object::New(env);
for (auto& m: messages) {
uint32_t arbId = m.first;
// GetTimestampUs() actually returns timestamps in milliseconds
uint32_t timestampMs = m.second->GetTimestampUs();
result.Set(arbId, timestampMs);
}

return result;
}

void cleanupHeartbeatsRunning() {
// Erase removed CAN buses from heartbeatsRunning
std::scoped_lock lock{watchdogMtx, canDevicesMtx};
Expand Down
1 change: 1 addition & 0 deletions src/canWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ void setSparkMaxHeartbeatData(const Napi::CallbackInfo& info);
void startRevCommonHeartbeat(const Napi::CallbackInfo& info);
void stopHeartbeats(const Napi::CallbackInfo& info);
void ackHeartbeats(const Napi::CallbackInfo& info);
Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info);
#endif

0 comments on commit d134630

Please sign in to comment.