hamlib 0.3.1 → 0.3.3

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/README.md CHANGED
@@ -204,11 +204,29 @@ Spectrum API summary:
204
204
  - `getSpectrumCapabilities()` returns conservative backend metadata exposed by the native addon.
205
205
  - `getSpectrumSupportSummary()` returns a product-oriented summary of whether official spectrum streaming is usable on the current rig/backend.
206
206
  - `configureSpectrum()` applies supported `SPECTRUM_*` levels and optional `SPECTRUM_HOLD`.
207
+ - `getSpectrumDisplayState()` returns a normalized display state with `mode/span/fixed edges/edge slot`.
208
+ - `configureSpectrumDisplay()` applies a normalized display config and reads back the resulting state.
209
+ - `getSpectrumEdgeSlot()` / `setSpectrumEdgeSlot()` expose backend edge-slot control when available.
210
+ - `getSpectrumFixedEdges()` / `setSpectrumFixedEdges()` expose direct fixed-range control using `SPECTRUM_EDGE_LOW/HIGH`.
207
211
  - `startSpectrumStream(callback?)` registers the official Hamlib spectrum callback only.
208
212
  - `stopSpectrumStream()` unregisters the official spectrum callback.
209
213
  - `startManagedSpectrum(config?)` runs the validated startup sequence for Icom/Hamlib async spectrum.
210
214
  - `stopManagedSpectrum()` runs the symmetric shutdown sequence and unregisters the callback.
211
215
 
216
+ Fixed-range example:
217
+
218
+ ```javascript
219
+ await rig.configureSpectrumDisplay({
220
+ mode: 'fixed',
221
+ edgeSlot: 1,
222
+ edgeLowHz: 14074000,
223
+ edgeHighHz: 14077000,
224
+ });
225
+
226
+ const displayState = await rig.getSpectrumDisplayState();
227
+ console.log(displayState);
228
+ ```
229
+
212
230
  Emitted events:
213
231
 
214
232
  - `spectrumLine` carries a single `SpectrumLine` object with frequency edges, mode, and raw bin payload.
package/index.d.ts CHANGED
@@ -200,17 +200,40 @@ interface SpectrumSupportSummary extends SpectrumCapabilities {
200
200
  hasSpectrumHoldFunction: boolean;
201
201
  hasTransceiveFunction: boolean;
202
202
  configurableLevels: string[];
203
+ supportsFixedEdges: boolean;
204
+ supportsEdgeSlotSelection: boolean;
205
+ supportedEdgeSlots: number[];
203
206
  }
204
207
 
208
+ type SpectrumDisplayMode = 'center' | 'fixed' | 'scroll-center' | 'scroll-fixed';
209
+
205
210
  interface SpectrumConfig {
206
211
  hold?: boolean;
207
- mode?: number;
212
+ mode?: number | SpectrumDisplayMode;
208
213
  spanHz?: number;
214
+ edgeSlot?: number;
215
+ edgeLowHz?: number;
216
+ edgeHighHz?: number;
209
217
  speed?: number;
210
218
  referenceLevel?: number;
211
219
  averageMode?: number;
212
220
  }
213
221
 
222
+ interface SpectrumDisplayState {
223
+ mode: SpectrumDisplayMode | null;
224
+ modeId: number | null;
225
+ modeName: string | null;
226
+ spanHz: number | null;
227
+ edgeSlot: number | null;
228
+ edgeLowHz: number | null;
229
+ edgeHighHz: number | null;
230
+ supportedModes: SpectrumModeInfo[];
231
+ supportedSpans: number[];
232
+ supportedEdgeSlots: number[];
233
+ supportsFixedEdges: boolean;
234
+ supportsEdgeSlotSelection: boolean;
235
+ }
236
+
214
237
  /**
215
238
  * Split mode info interface
216
239
  */
@@ -599,7 +622,7 @@ declare class HamLib extends EventEmitter {
599
622
  * @param levelType Level type ('AF', 'RF', 'SQL', 'RFPOWER', etc.)
600
623
  * @param value Level value (0.0-1.0 typically)
601
624
  */
602
- setLevel(levelType: LevelType, value: number): Promise<number>;
625
+ setLevel(levelType: LevelType, value: number, vfo?: string): Promise<number>;
603
626
 
604
627
  /**
605
628
  * Get radio level
@@ -1287,6 +1310,41 @@ declare class HamLib extends EventEmitter {
1287
1310
  */
1288
1311
  configureSpectrum(config?: SpectrumConfig): Promise<SpectrumSupportSummary>;
1289
1312
 
1313
+ /**
1314
+ * Get the current spectrum edge slot if exposed by the backend.
1315
+ */
1316
+ getSpectrumEdgeSlot(): Promise<number>;
1317
+
1318
+ /**
1319
+ * Set the current spectrum edge slot if exposed by the backend.
1320
+ */
1321
+ setSpectrumEdgeSlot(slot: number): Promise<number>;
1322
+
1323
+ /**
1324
+ * Get supported spectrum edge slots.
1325
+ */
1326
+ getSpectrumSupportedEdgeSlots(): Promise<number[]>;
1327
+
1328
+ /**
1329
+ * Read current fixed spectrum edges.
1330
+ */
1331
+ getSpectrumFixedEdges(): Promise<{lowHz: number, highHz: number}>;
1332
+
1333
+ /**
1334
+ * Set current fixed spectrum edges.
1335
+ */
1336
+ setSpectrumFixedEdges(range: {lowHz: number, highHz: number}): Promise<{lowHz: number, highHz: number}>;
1337
+
1338
+ /**
1339
+ * Get a normalized spectrum display state for application use.
1340
+ */
1341
+ getSpectrumDisplayState(): Promise<SpectrumDisplayState>;
1342
+
1343
+ /**
1344
+ * Configure spectrum display state using a normalized application-facing shape.
1345
+ */
1346
+ configureSpectrumDisplay(config?: SpectrumConfig): Promise<SpectrumDisplayState>;
1347
+
1290
1348
  /**
1291
1349
  * Start receiving official Hamlib spectrum line events.
1292
1350
  */
package/lib/index.js CHANGED
@@ -4,6 +4,17 @@ const nodeGypBuild = require('node-gyp-build');
4
4
  // Ensure loader resolves from package root (contains prebuilds/ and build/)
5
5
  const nativeModule = nodeGypBuild(path.join(__dirname, '..'));
6
6
 
7
+ const DEFAULT_SPECTRUM_EDGE_SLOTS = [1, 2, 3, 4];
8
+
9
+ function normalizeSpectrumModeName(name) {
10
+ const normalized = String(name || '').trim().toLowerCase();
11
+ if (normalized === 'center') return 'center';
12
+ if (normalized === 'fixed') return 'fixed';
13
+ if (normalized === 'center scroll' || normalized === 'center-scroll' || normalized === 'scroll-center') return 'scroll-center';
14
+ if (normalized === 'fixed scroll' || normalized === 'fixed-scroll' || normalized === 'scroll-fixed') return 'scroll-fixed';
15
+ return null;
16
+ }
17
+
7
18
  /**
8
19
  * HamLib class for controlling amateur radio devices
9
20
  *
@@ -22,6 +33,29 @@ class HamLib extends EventEmitter {
22
33
  super();
23
34
  this._nativeInstance = new nativeModule.HamLib(model, port);
24
35
  this._managedSpectrumRunning = false;
36
+ this._lastSpectrumLine = null;
37
+ }
38
+
39
+ _recordSpectrumLine(line) {
40
+ if (!line || typeof line !== 'object') {
41
+ return line;
42
+ }
43
+ this._lastSpectrumLine = line;
44
+ return line;
45
+ }
46
+
47
+ _getLastSpectrumDisplayStateFromLine() {
48
+ const line = this._lastSpectrumLine;
49
+ if (!line || typeof line !== 'object') {
50
+ return null;
51
+ }
52
+
53
+ return {
54
+ modeId: Number.isFinite(line.mode) ? line.mode : null,
55
+ spanHz: Number.isFinite(line.spanHz) ? line.spanHz : null,
56
+ edgeLowHz: Number.isFinite(line.lowEdgeFreq) ? line.lowEdgeFreq : null,
57
+ edgeHighHz: Number.isFinite(line.highEdgeFreq) ? line.highEdgeFreq : null,
58
+ };
25
59
  }
26
60
 
27
61
  /**
@@ -311,7 +345,10 @@ class HamLib extends EventEmitter {
311
345
  * @param {string} levelType - Level type ('AF', 'RF', 'SQL', 'RFPOWER', etc.)
312
346
  * @param {number} value - Level value (0.0-1.0 typically)
313
347
  */
314
- async setLevel(levelType, value) {
348
+ async setLevel(levelType, value, vfo) {
349
+ if (vfo) {
350
+ return this._nativeInstance.setLevel(levelType, value, vfo);
351
+ }
315
352
  return this._nativeInstance.setLevel(levelType, value);
316
353
  }
317
354
 
@@ -1180,8 +1217,16 @@ class HamLib extends EventEmitter {
1180
1217
  const hasSpectrumHoldFunction = supportedFunctions.includes('SPECTRUM_HOLD');
1181
1218
  const hasTransceiveFunction = supportedFunctions.includes('TRANSCEIVE');
1182
1219
  const asyncDataSupported = capabilities.asyncDataSupported ?? hasSpectrumFunction;
1183
- const configurableLevels = ['SPECTRUM_MODE', 'SPECTRUM_SPAN', 'SPECTRUM_SPEED', 'SPECTRUM_REF', 'SPECTRUM_AVG']
1220
+ const configurableLevels = ['SPECTRUM_MODE', 'SPECTRUM_SPAN', 'SPECTRUM_EDGE_LOW', 'SPECTRUM_EDGE_HIGH', 'SPECTRUM_SPEED', 'SPECTRUM_REF', 'SPECTRUM_AVG']
1184
1221
  .filter((name) => supportedLevels.includes(name));
1222
+ let supportsEdgeSlotSelection = false;
1223
+
1224
+ try {
1225
+ const edgeSlot = await this.getConf('SPECTRUM_EDGE');
1226
+ supportsEdgeSlotSelection = Number.isFinite(Number.parseInt(String(edgeSlot), 10));
1227
+ } catch (_) {
1228
+ supportsEdgeSlotSelection = false;
1229
+ }
1185
1230
 
1186
1231
  return {
1187
1232
  supported: Boolean(hasSpectrumFunction),
@@ -1190,6 +1235,9 @@ class HamLib extends EventEmitter {
1190
1235
  hasSpectrumHoldFunction,
1191
1236
  hasTransceiveFunction,
1192
1237
  configurableLevels,
1238
+ supportsFixedEdges: configurableLevels.includes('SPECTRUM_EDGE_LOW') && configurableLevels.includes('SPECTRUM_EDGE_HIGH'),
1239
+ supportsEdgeSlotSelection,
1240
+ supportedEdgeSlots: supportsEdgeSlotSelection ? [...DEFAULT_SPECTRUM_EDGE_SLOTS] : [],
1193
1241
  scopes: capabilities.scopes ?? [],
1194
1242
  modes: capabilities.modes ?? [],
1195
1243
  spans: capabilities.spans ?? [],
@@ -1208,13 +1256,36 @@ class HamLib extends EventEmitter {
1208
1256
  if (value === undefined || !summary.configurableLevels.includes(name)) return;
1209
1257
  await this.setLevel(name, value);
1210
1258
  };
1259
+ const resolveModeId = async (mode) => {
1260
+ if (mode === undefined || mode === null) return undefined;
1261
+ if (Number.isFinite(mode)) return mode;
1262
+ const requested = normalizeSpectrumModeName(mode);
1263
+ if (!requested) {
1264
+ throw new Error(`Unsupported spectrum mode: ${mode}`);
1265
+ }
1266
+ const matched = (summary.modes ?? []).find((entry) => normalizeSpectrumModeName(entry?.name) === requested);
1267
+ if (!matched) {
1268
+ throw new Error(`Spectrum mode not supported by this backend: ${mode}`);
1269
+ }
1270
+ return matched.id;
1271
+ };
1211
1272
 
1212
1273
  if (summary.hasSpectrumHoldFunction && config.hold !== undefined) {
1213
1274
  await this.setFunction('SPECTRUM_HOLD', Boolean(config.hold));
1214
1275
  }
1215
1276
 
1216
- await applyLevel('SPECTRUM_MODE', config.mode);
1277
+ if (config.edgeSlot !== undefined && summary.supportsEdgeSlotSelection) {
1278
+ await this.setSpectrumEdgeSlot(config.edgeSlot);
1279
+ }
1280
+
1281
+ await applyLevel('SPECTRUM_MODE', await resolveModeId(config.mode));
1217
1282
  await applyLevel('SPECTRUM_SPAN', config.spanHz);
1283
+ if (config.edgeLowHz !== undefined || config.edgeHighHz !== undefined) {
1284
+ await this.setSpectrumFixedEdges({
1285
+ lowHz: config.edgeLowHz,
1286
+ highHz: config.edgeHighHz,
1287
+ });
1288
+ }
1218
1289
  await applyLevel('SPECTRUM_SPEED', config.speed);
1219
1290
  await applyLevel('SPECTRUM_REF', config.referenceLevel);
1220
1291
  await applyLevel('SPECTRUM_AVG', config.averageMode);
@@ -1222,15 +1293,114 @@ class HamLib extends EventEmitter {
1222
1293
  return summary;
1223
1294
  }
1224
1295
 
1296
+ async getSpectrumEdgeSlot() {
1297
+ const raw = await this.getConf('SPECTRUM_EDGE');
1298
+ const parsed = Number.parseInt(String(raw), 10);
1299
+ if (!Number.isFinite(parsed)) {
1300
+ throw new Error('Spectrum edge slot is not available');
1301
+ }
1302
+ return parsed;
1303
+ }
1304
+
1305
+ async setSpectrumEdgeSlot(slot) {
1306
+ const parsed = Number.parseInt(String(slot), 10);
1307
+ if (!Number.isFinite(parsed) || parsed < 1) {
1308
+ throw new Error(`Invalid spectrum edge slot: ${slot}`);
1309
+ }
1310
+ await this.setConf('SPECTRUM_EDGE', String(parsed));
1311
+ return parsed;
1312
+ }
1313
+
1314
+ async getSpectrumSupportedEdgeSlots() {
1315
+ const summary = await this.getSpectrumSupportSummary();
1316
+ return summary.supportedEdgeSlots ?? [];
1317
+ }
1318
+
1319
+ async getSpectrumFixedEdges() {
1320
+ const lineState = this._getLastSpectrumDisplayStateFromLine();
1321
+ if (lineState?.edgeLowHz !== null && lineState?.edgeHighHz !== null) {
1322
+ return { lowHz: lineState.edgeLowHz, highHz: lineState.edgeHighHz };
1323
+ }
1324
+
1325
+ const targetVfo = 'VFO-A';
1326
+ const [lowHz, highHz] = await Promise.all([
1327
+ this.getLevel('SPECTRUM_EDGE_LOW', targetVfo),
1328
+ this.getLevel('SPECTRUM_EDGE_HIGH', targetVfo),
1329
+ ]);
1330
+ return { lowHz, highHz };
1331
+ }
1332
+
1333
+ async setSpectrumFixedEdges({ lowHz, highHz }) {
1334
+ if (!Number.isFinite(lowHz) || !Number.isFinite(highHz) || lowHz >= highHz) {
1335
+ throw new Error('Spectrum fixed edge range must satisfy lowHz < highHz');
1336
+ }
1337
+ const targetVfo = 'VFO-A';
1338
+ await this.setLevel('SPECTRUM_EDGE_LOW', lowHz, targetVfo);
1339
+ await this.setLevel('SPECTRUM_EDGE_HIGH', highHz, targetVfo);
1340
+ return { lowHz, highHz };
1341
+ }
1342
+
1343
+ async getSpectrumDisplayState() {
1344
+ const summary = await this.getSpectrumSupportSummary();
1345
+ const lineState = this._getLastSpectrumDisplayStateFromLine();
1346
+ const [queriedModeId, queriedSpanHz, fixedEdges, edgeSlot] = await Promise.all([
1347
+ summary.configurableLevels.includes('SPECTRUM_MODE') ? this.getLevel('SPECTRUM_MODE') : Promise.resolve(null),
1348
+ summary.configurableLevels.includes('SPECTRUM_SPAN') ? this.getLevel('SPECTRUM_SPAN') : Promise.resolve(null),
1349
+ summary.supportsFixedEdges ? this.getSpectrumFixedEdges().catch(() => null) : Promise.resolve(null),
1350
+ summary.supportsEdgeSlotSelection ? this.getSpectrumEdgeSlot().catch(() => null) : Promise.resolve(null),
1351
+ ]);
1352
+
1353
+ const modeId = queriedModeId ?? lineState?.modeId ?? null;
1354
+ const spanHz = queriedSpanHz ?? lineState?.spanHz ?? null;
1355
+ const modeInfo = (summary.modes ?? []).find((entry) => entry.id === modeId) ?? null;
1356
+ const mode = normalizeSpectrumModeName(modeInfo?.name);
1357
+ const edgeLowHz = fixedEdges?.lowHz ?? lineState?.edgeLowHz ?? null;
1358
+ const edgeHighHz = fixedEdges?.highHz ?? lineState?.edgeHighHz ?? null;
1359
+ const derivedSpanHz = (edgeLowHz !== null && edgeHighHz !== null) ? (edgeHighHz - edgeLowHz) : null;
1360
+
1361
+ return {
1362
+ mode,
1363
+ modeId,
1364
+ modeName: modeInfo?.name ?? null,
1365
+ spanHz: spanHz ?? derivedSpanHz,
1366
+ edgeSlot,
1367
+ edgeLowHz,
1368
+ edgeHighHz,
1369
+ supportedModes: summary.modes ?? [],
1370
+ supportedSpans: summary.spans ?? [],
1371
+ supportedEdgeSlots: summary.supportedEdgeSlots ?? [],
1372
+ supportsFixedEdges: Boolean(summary.supportsFixedEdges),
1373
+ supportsEdgeSlotSelection: Boolean(summary.supportsEdgeSlotSelection),
1374
+ };
1375
+ }
1376
+
1377
+ async configureSpectrumDisplay(config = {}) {
1378
+ const normalizedConfig = { ...config };
1379
+ if ((normalizedConfig.mode === 'fixed' || normalizedConfig.mode === 'scroll-fixed')
1380
+ && normalizedConfig.edgeLowHz !== undefined
1381
+ && normalizedConfig.edgeHighHz !== undefined
1382
+ && normalizedConfig.edgeLowHz >= normalizedConfig.edgeHighHz) {
1383
+ throw new Error('Spectrum fixed edge range must satisfy edgeLowHz < edgeHighHz');
1384
+ }
1385
+
1386
+ await this.configureSpectrum(normalizedConfig);
1387
+ return this.getSpectrumDisplayState();
1388
+ }
1389
+
1225
1390
  /**
1226
1391
  * Start the official Hamlib spectrum callback stream.
1227
1392
  * @param {(line: Object) => void} [callback]
1228
1393
  * @returns {Promise<boolean>}
1229
1394
  */
1230
1395
  async startSpectrumStream(callback) {
1231
- const listener = typeof callback === 'function'
1232
- ? callback
1233
- : (line) => this.emit('spectrumLine', line);
1396
+ const listener = (line) => {
1397
+ const recorded = this._recordSpectrumLine(line);
1398
+ if (typeof callback === 'function') {
1399
+ callback(recorded);
1400
+ return;
1401
+ }
1402
+ this.emit('spectrumLine', recorded);
1403
+ };
1234
1404
  return this._nativeInstance.startSpectrumStream(listener);
1235
1405
  }
1236
1406
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hamlib",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Node.js bindings for Hamlib rig control with official spectrum streaming support",
5
5
  "main": "index.js",
6
6
  "module": "lib/index.mjs",
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/src/hamlib.cpp CHANGED
@@ -2938,12 +2938,21 @@ Napi::Value NodeHamLib::SetLevel(const Napi::CallbackInfo & info) {
2938
2938
  }
2939
2939
 
2940
2940
  if (info.Length() < 2 || !info[0].IsString() || !info[1].IsNumber()) {
2941
- Napi::TypeError::New(env, "Expected (levelType: string, value: number)").ThrowAsJavaScriptException();
2941
+ Napi::TypeError::New(env, "Expected (levelType: string, value: number, vfo?: string)").ThrowAsJavaScriptException();
2942
2942
  return env.Null();
2943
2943
  }
2944
2944
 
2945
2945
  std::string levelTypeStr = info[0].As<Napi::String>().Utf8Value();
2946
2946
  double levelValue = info[1].As<Napi::Number>().DoubleValue();
2947
+ int vfo = SHIM_RIG_VFO_CURR;
2948
+ if (info.Length() >= 3 && info[2].IsString()) {
2949
+ std::string vfoStr = info[2].As<Napi::String>().Utf8Value();
2950
+ if (vfoStr == "VFO-A") {
2951
+ vfo = SHIM_RIG_VFO_A;
2952
+ } else if (vfoStr == "VFO-B") {
2953
+ vfo = SHIM_RIG_VFO_B;
2954
+ }
2955
+ }
2947
2956
 
2948
2957
  // Map level type strings to hamlib constants
2949
2958
  uint64_t levelType;
@@ -3021,8 +3030,8 @@ Napi::Value NodeHamLib::SetLevel(const Napi::CallbackInfo & info) {
3021
3030
  if (isSpectrumLevel) {
3022
3031
  Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
3023
3032
  int result = shim_rig_level_is_float(levelType)
3024
- ? shim_rig_set_level_f(my_rig, SHIM_RIG_VFO_CURR, levelType, static_cast<float>(levelValue))
3025
- : shim_rig_set_level_i(my_rig, SHIM_RIG_VFO_CURR, levelType, static_cast<int>(levelValue));
3033
+ ? shim_rig_set_level_f(my_rig, vfo, levelType, static_cast<float>(levelValue))
3034
+ : shim_rig_set_level_i(my_rig, vfo, levelType, static_cast<int>(levelValue));
3026
3035
 
3027
3036
  if (result != SHIM_RIG_OK) {
3028
3037
  deferred.Reject(Napi::Error::New(env, shim_rigerror(result)).Value());