joulescope_driver 2.3.5 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joulescope_driver",
3
- "version": "2.3.5",
3
+ "version": "2.4.0",
4
4
  "description": "Interface with Joulescopes using node.js and Electron",
5
5
  "keywords": [
6
6
  "Joulescope",
@@ -211,7 +211,10 @@ static Napi::Value stream_to_js(Napi::Env env, const struct jsdrv_union_s * valu
211
211
  static Napi::Value stats_to_js(Napi::Env env, const struct jsdrv_union_s * value) {
212
212
  const struct jsdrv_statistics_s * s =(const struct jsdrv_statistics_s *) value->value.bin;
213
213
  uint32_t sample_freq = s->sample_freq;
214
- uint64_t samples_full_rate = s->block_sample_count * (uint64_t) s->decimate_factor;
214
+ // u8 decimate_factor saturates at 255; decimate_factor32 has the exact
215
+ // value (0 from older producers - fall back)
216
+ uint32_t decimate_factor = s->decimate_factor32 ? s->decimate_factor32 : s->decimate_factor;
217
+ uint64_t samples_full_rate = s->block_sample_count * (uint64_t) decimate_factor;
215
218
  uint64_t sample_id_start = s->block_sample_id;
216
219
  uint64_t sample_id_end = s->block_sample_id + samples_full_rate;
217
220
  uint64_t t_start = sample_id_start / sample_freq;
@@ -228,7 +231,7 @@ static Napi::Value stats_to_js(Napi::Env env, const struct jsdrv_union_s * value
228
231
  "ms"));
229
232
  time.Set("range", obj_value2(env, (double) t_start, (double) (t_start + t_delta), "s"));
230
233
  time.Set("delta", obj_value1(env, (double) t_delta, "s"));
231
- time.Set("decimate_factor", obj_value1(env, (double) s->decimate_factor, "samples"));
234
+ time.Set("decimate_factor", obj_value1(env, (double) decimate_factor, "samples"));
232
235
  time.Set("decimate_sample_count", obj_value1(env, (double) s->block_sample_count, "samples"));
233
236
  time.Set("accum_samples", obj_value2(env, (double) s->accum_sample_id, (double) sample_id_end, "samples"));
234
237
  obj.Set("time_map", obj_time_map(env, &s->time_map));
@@ -271,12 +274,80 @@ static Napi::Value stats_to_js(Napi::Env env, const struct jsdrv_union_s * value
271
274
  return obj;
272
275
  }
273
276
 
277
+ // Note: int64/uint64 values convert to JavaScript Number (double),
278
+ // matching the existing stream/statistics converters.
279
+ static Napi::Value buffer_info_inner_to_js(Napi::Env env, const struct jsdrv_buffer_info_s * info) {
280
+ Napi::Object obj = Napi::Object::New(env);
281
+ obj.Set("version", info->version);
282
+ obj.Set("field_id", info->field_id);
283
+ obj.Set("index", info->index);
284
+ obj.Set("element_type", info->element_type);
285
+ obj.Set("element_size_bits", info->element_size_bits);
286
+ obj.Set("topic", info->topic);
287
+ obj.Set("decimate_factor", info->decimate_factor);
288
+ obj.Set("size_in_utc", (double) info->size_in_utc);
289
+ obj.Set("size_in_samples", (double) info->size_in_samples);
290
+ Napi::Object utc = Napi::Object::New(env);
291
+ utc.Set("start", (double) info->time_range_utc.start);
292
+ utc.Set("end", (double) info->time_range_utc.end);
293
+ utc.Set("length", (double) info->time_range_utc.length);
294
+ obj.Set("time_range_utc", utc);
295
+ Napi::Object samples = Napi::Object::New(env);
296
+ samples.Set("start", (double) info->time_range_samples.start);
297
+ samples.Set("end", (double) info->time_range_samples.end);
298
+ samples.Set("length", (double) info->time_range_samples.length);
299
+ obj.Set("time_range_samples", samples);
300
+ obj.Set("time_map", obj_time_map(env, &info->time_map));
301
+ return obj;
302
+ }
303
+
274
304
  static Napi::Value buffer_info_to_js(Napi::Env env, const struct jsdrv_union_s * value) {
275
- return env.Undefined(); // todo
305
+ const struct jsdrv_buffer_info_s * info = (const struct jsdrv_buffer_info_s *) value->value.bin;
306
+ return buffer_info_inner_to_js(env, info);
276
307
  }
277
308
 
278
309
  static Napi::Value buffer_rsp_to_js(Napi::Env env, const struct jsdrv_union_s * value) {
279
- return env.Undefined(); // todo
310
+ const struct jsdrv_buffer_response_s * r = (const struct jsdrv_buffer_response_s *) value->value.bin;
311
+ Napi::Object obj = Napi::Object::New(env);
312
+ obj.Set("version", r->version);
313
+ obj.Set("rsp_id", (double) r->rsp_id);
314
+ obj.Set("info", buffer_info_inner_to_js(env, &r->info));
315
+ size_t length = (size_t) r->info.time_range_samples.length;
316
+ const uint8_t * src_u8 = (const uint8_t *) r->data;
317
+ if (JSDRV_BUFFER_RESPONSE_SAMPLES == r->response_type) {
318
+ obj.Set("response_type", "samples");
319
+ if ((JSDRV_DATA_TYPE_FLOAT == r->info.element_type) && (32 == r->info.element_size_bits)) {
320
+ Napi::Float32Array data = Napi::Float32Array::New(env, length);
321
+ const float * src = (const float *) r->data;
322
+ for (size_t idx = 0; idx < length; ++idx) {
323
+ data[idx] = src[idx];
324
+ }
325
+ obj.Set("data", data);
326
+ } else if ((JSDRV_DATA_TYPE_UINT == r->info.element_type) && (1 == r->info.element_size_bits)) {
327
+ // unpack to one sample per entry, matching stream_to_js
328
+ Napi::Uint8Array data = Napi::Uint8Array::New(env, length);
329
+ for (size_t idx = 0; idx < length; ++idx) {
330
+ data[idx] = (src_u8[idx >> 3] >> (idx & 7)) & 1;
331
+ }
332
+ obj.Set("data", data);
333
+ } else if ((JSDRV_DATA_TYPE_UINT == r->info.element_type) && (4 == r->info.element_size_bits)) {
334
+ Napi::Uint8Array data = Napi::Uint8Array::New(env, length);
335
+ for (size_t idx = 0; idx < length; ++idx) {
336
+ data[idx] = (src_u8[idx >> 1] >> (4 * (idx & 1))) & 0x0f;
337
+ }
338
+ obj.Set("data", data);
339
+ }
340
+ } else if (JSDRV_BUFFER_RESPONSE_SUMMARY == r->response_type) {
341
+ // flat [length][4] float32 entries: avg, std, min, max
342
+ obj.Set("response_type", "summary");
343
+ Napi::Float32Array data = Napi::Float32Array::New(env, length * 4);
344
+ const float * src = (const float *) r->data;
345
+ for (size_t idx = 0; idx < (length * 4); ++idx) {
346
+ data[idx] = src[idx];
347
+ }
348
+ obj.Set("data", data);
349
+ }
350
+ return obj;
280
351
  }
281
352
 
282
353
  static Napi::Value bin_to_js(Napi::Env env, const struct jsdrv_union_s * value) {
@@ -285,8 +356,14 @@ static Napi::Value bin_to_js(Napi::Env env, const struct jsdrv_union_s * value)
285
356
  case JSDRV_PAYLOAD_TYPE_STATISTICS: return stats_to_js(env, value);
286
357
  case JSDRV_PAYLOAD_TYPE_BUFFER_INFO: return buffer_info_to_js(env, value);
287
358
  case JSDRV_PAYLOAD_TYPE_BUFFER_RSP: return buffer_rsp_to_js(env, value);
288
- default:
289
- return env.Undefined(); // todo
359
+ default: {
360
+ // raw binary payload: copy to a Uint8Array
361
+ Napi::Uint8Array data = Napi::Uint8Array::New(env, value->size);
362
+ for (size_t idx = 0; idx < value->size; ++idx) {
363
+ data[idx] = value->value.bin[idx];
364
+ }
365
+ return data;
366
+ }
290
367
  }
291
368
  }
292
369
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2024 Jetperch LLC
2
+ * Copyright 2024-2026 Jetperch LLC
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -24,8 +24,14 @@ async function testBasic(){
24
24
  var drv = new JoulescopeDriver();
25
25
  var device_paths = drv.device_paths();
26
26
  console.log('device_paths: ' + device_paths);
27
+ assert.ok(Array.isArray(device_paths), 'device_paths must be an array');
27
28
  drv.finalize();
28
- console.log('testBasic passed')
29
+ console.log('testBasic passed');
29
30
  }
30
31
 
31
- assert.doesNotThrow(async() => {await testBasic()}, undefined, "testBasic threw an exception");
32
+ // assert.doesNotThrow cannot observe an async rejection; await instead so
33
+ // any failure rejects the promise and fails the process with exit code 1.
34
+ testBasic().catch((err) => {
35
+ console.error('testBasic FAILED:', err);
36
+ process.exit(1);
37
+ });