@springfield/ham-radio-utils 4.2.0 → 4.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.
@@ -1,4 +1,4 @@
1
- import type { RadioMemoryMap, RadioMemoryMapFieldUi, RadioMemoryMapValueKind } from '@springfield/ham-radio-api';
1
+ import type { RadioMemoryMap, RadioMemoryMapFieldUi, RadioMemoryMapValueKind, RadioSettingValue } from '@springfield/ham-radio-api';
2
2
  /**
3
3
  * Flattened field descriptor for schema-driven settings UI.
4
4
  */
@@ -13,8 +13,18 @@ export interface RadioMemoryMapUiField {
13
13
  }
14
14
  /**
15
15
  * Collect writable UI fields from a memory map, in declaration order.
16
+ * Channel-bound structs are skipped (use {@link collectChannelMemoryMapUiFields}).
16
17
  */
17
18
  export declare function collectMemoryMapUiFields(memoryMap: RadioMemoryMap): RadioMemoryMapUiField[];
19
+ /**
20
+ * Collect per-channel UI fields from the `channelBindings.records` struct.
21
+ * Skips reserved fields, fields without `ui`, and fields bound onto RadioChannel.
22
+ */
23
+ export declare function collectChannelMemoryMapUiFields(memoryMap: RadioMemoryMap): RadioMemoryMapUiField[];
24
+ /**
25
+ * Format a decoded memory-map field value for read-only display (e.g. channel table).
26
+ */
27
+ export declare function formatMemoryMapFieldValue(value: RadioSettingValue | undefined, field: Pick<RadioMemoryMapUiField, 'value' | 'fieldId'>): string;
18
28
  /**
19
29
  * Group UI fields by their ui.group key, preserving first-seen group order.
20
30
  */
@@ -1 +1 @@
1
- {"version":3,"file":"memory-map-ui.d.ts","sourceRoot":"","sources":["../../src/memory/memory-map-ui.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAEjH;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,4EAA4E;IAC5E,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAA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CAAC,CAc5G"}
@@ -1,9 +1,39 @@
1
+ function channelBoundFieldIds(memoryMap) {
2
+ const bindings = memoryMap.channelBindings;
3
+ const ids = new Set();
4
+ if (!bindings) {
5
+ return ids;
6
+ }
7
+ for (const key of [
8
+ bindings.receiveFrequency,
9
+ bindings.transmitFrequency,
10
+ bindings.receiveTone,
11
+ bindings.transmitTone,
12
+ bindings.nameField,
13
+ ]) {
14
+ if (key) {
15
+ ids.add(key);
16
+ }
17
+ }
18
+ return ids;
19
+ }
1
20
  /**
2
21
  * Collect writable UI fields from a memory map, in declaration order.
22
+ * Channel-bound structs are skipped (use {@link collectChannelMemoryMapUiFields}).
3
23
  */
4
24
  export function collectMemoryMapUiFields(memoryMap) {
5
25
  const fields = [];
26
+ const channelStructIds = new Set();
27
+ if (memoryMap.channelBindings) {
28
+ channelStructIds.add(memoryMap.channelBindings.records);
29
+ if (memoryMap.channelBindings.names) {
30
+ channelStructIds.add(memoryMap.channelBindings.names);
31
+ }
32
+ }
6
33
  for (const struct of memoryMap.structs) {
34
+ if (channelStructIds.has(struct.id)) {
35
+ continue;
36
+ }
7
37
  const count = struct.count ?? 1;
8
38
  for (let index = 0; index < count; index += 1) {
9
39
  for (const field of struct.fields) {
@@ -27,6 +57,65 @@ export function collectMemoryMapUiFields(memoryMap) {
27
57
  }
28
58
  return fields;
29
59
  }
60
+ /**
61
+ * Collect per-channel UI fields from the `channelBindings.records` struct.
62
+ * Skips reserved fields, fields without `ui`, and fields bound onto RadioChannel.
63
+ */
64
+ export function collectChannelMemoryMapUiFields(memoryMap) {
65
+ const bindings = memoryMap.channelBindings;
66
+ if (!bindings) {
67
+ return [];
68
+ }
69
+ const struct = memoryMap.structs.find((entry) => entry.id === bindings.records);
70
+ if (!struct) {
71
+ return [];
72
+ }
73
+ const boundIds = channelBoundFieldIds(memoryMap);
74
+ const fields = [];
75
+ for (const field of struct.fields) {
76
+ if (field.reserved || !field.ui || boundIds.has(field.id)) {
77
+ continue;
78
+ }
79
+ fields.push({
80
+ path: field.id,
81
+ structId: struct.id,
82
+ fieldId: field.id,
83
+ ui: field.ui,
84
+ value: field.value,
85
+ });
86
+ }
87
+ return fields;
88
+ }
89
+ /**
90
+ * Format a decoded memory-map field value for read-only display (e.g. channel table).
91
+ */
92
+ export function formatMemoryMapFieldValue(value, field) {
93
+ if (value === undefined || value === null) {
94
+ return '';
95
+ }
96
+ if (field.value?.kind === 'enum' && typeof value === 'string') {
97
+ return value;
98
+ }
99
+ if (field.value?.kind === 'boolean' || typeof value === 'boolean') {
100
+ if (field.fieldId === 'wide') {
101
+ return value ? 'Wide' : 'Narrow';
102
+ }
103
+ if (field.fieldId === 'scan') {
104
+ return value ? 'On' : 'Off';
105
+ }
106
+ return value ? 'On' : 'Off';
107
+ }
108
+ if (field.fieldId === 'lowpower' && typeof value === 'number') {
109
+ return value === 0 ? 'High' : 'Low';
110
+ }
111
+ if (field.fieldId === 'scode' && typeof value === 'number') {
112
+ return String(value + 1);
113
+ }
114
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
115
+ return String(value);
116
+ }
117
+ return '';
118
+ }
30
119
  /**
31
120
  * Group UI fields by their ui.group key, preserving first-seen group order.
32
121
  */
@@ -1 +1 @@
1
- {"version":3,"file":"memory-map-ui.js","sourceRoot":"","sources":["../../src/memory/memory-map-ui.ts"],"names":[],"mappings":"AAeA;;GAEG;AACH,MAAM,UAAU,wBAAwB,CAAC,SAAyB;IAChE,MAAM,MAAM,GAA4B,EAAE,CAAC;IAE3C,KAAK,MAAM,MAAM,IAAI,SAAS,CAAC,OAAO,EAAE,CAAC;QACvC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,CAAC,CAAC;QAEhC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;YAC9C,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;gBAClC,IAAI,KAAK,CAAC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CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { RadioMemoryMap, RadioMemoryMapFieldUi, RadioMemoryMapValueKind } from '@springfield/ham-radio-api';\n\n/**\n * Flattened field descriptor for schema-driven settings UI.\n */\nexport interface RadioMemoryMapUiField {\n /** Dot path into decoded settings, e.g. settings.squelch or pttid.0.code */\n path: string;\n structId: string;\n fieldId: string;\n arrayIndex?: number;\n ui: RadioMemoryMapFieldUi;\n value?: RadioMemoryMapValueKind;\n}\n\n/**\n * Collect writable UI fields from a memory map, in declaration order.\n */\nexport function collectMemoryMapUiFields(memoryMap: RadioMemoryMap): RadioMemoryMapUiField[] {\n const fields: RadioMemoryMapUiField[] = [];\n\n for (const struct of memoryMap.structs) {\n const count = struct.count ?? 1;\n\n for (let index = 0; index < count; index += 1) {\n for (const field of struct.fields) {\n if (field.reserved || !field.ui) {\n continue;\n }\n\n const path =\n count > 1 ? `${struct.id}.${index}.${field.id}` : `${struct.id}.${field.id}`;\n\n const label =\n count > 1 && field.ui.label.indexOf('%') === -1\n ? `${field.ui.label} ${index + 1}`\n : field.ui.label;\n\n fields.push({\n path,\n structId: struct.id,\n fieldId: field.id,\n arrayIndex: count > 1 ? index : undefined,\n ui: { ...field.ui, label },\n value: field.value,\n });\n }\n }\n }\n\n return fields;\n}\n\n/**\n * Group UI fields by their ui.group key, preserving first-seen group order.\n */\nexport function groupMemoryMapUiFields(fields: RadioMemoryMapUiField[]): Map<string, RadioMemoryMapUiField[]> {\n const groups = new Map<string, RadioMemoryMapUiField[]>();\n\n for (const field of fields) {\n const existing = groups.get(field.ui.group);\n\n if (existing) {\n existing.push(field);\n } else {\n groups.set(field.ui.group, [field]);\n }\n }\n\n return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I,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n RadioMemoryMap,\n RadioMemoryMapFieldUi,\n RadioMemoryMapValueKind,\n RadioSettingValue,\n} from '@springfield/ham-radio-api';\n\n/**\n * Flattened field descriptor for schema-driven settings UI.\n */\nexport interface RadioMemoryMapUiField {\n /** Dot path into decoded settings, e.g. settings.squelch or pttid.0.code */\n path: string;\n structId: string;\n fieldId: string;\n arrayIndex?: number;\n ui: RadioMemoryMapFieldUi;\n value?: RadioMemoryMapValueKind;\n}\n\nfunction channelBoundFieldIds(memoryMap: RadioMemoryMap): Set<string> {\n const bindings = memoryMap.channelBindings;\n const ids = new Set<string>();\n\n if (!bindings) {\n return ids;\n }\n\n for (const key of [\n bindings.receiveFrequency,\n bindings.transmitFrequency,\n bindings.receiveTone,\n bindings.transmitTone,\n bindings.nameField,\n ]) {\n if (key) {\n ids.add(key);\n }\n }\n\n return ids;\n}\n\n/**\n * Collect writable UI fields from a memory map, in declaration order.\n * Channel-bound structs are skipped (use {@link collectChannelMemoryMapUiFields}).\n */\nexport function collectMemoryMapUiFields(memoryMap: RadioMemoryMap): RadioMemoryMapUiField[] {\n const fields: RadioMemoryMapUiField[] = [];\n const channelStructIds = new Set<string>();\n\n if (memoryMap.channelBindings) {\n channelStructIds.add(memoryMap.channelBindings.records);\n\n if (memoryMap.channelBindings.names) {\n channelStructIds.add(memoryMap.channelBindings.names);\n }\n }\n\n for (const struct of memoryMap.structs) {\n if (channelStructIds.has(struct.id)) {\n continue;\n }\n\n const count = struct.count ?? 1;\n\n for (let index = 0; index < count; index += 1) {\n for (const field of struct.fields) {\n if (field.reserved || !field.ui) {\n continue;\n }\n\n const path =\n count > 1 ? `${struct.id}.${index}.${field.id}` : `${struct.id}.${field.id}`;\n\n const label =\n count > 1 && field.ui.label.indexOf('%') === -1\n ? `${field.ui.label} ${index + 1}`\n : field.ui.label;\n\n fields.push({\n path,\n structId: struct.id,\n fieldId: field.id,\n arrayIndex: count > 1 ? index : undefined,\n ui: { ...field.ui, label },\n value: field.value,\n });\n }\n }\n }\n\n return fields;\n}\n\n/**\n * Collect per-channel UI fields from the `channelBindings.records` struct.\n * Skips reserved fields, fields without `ui`, and fields bound onto RadioChannel.\n */\nexport function collectChannelMemoryMapUiFields(memoryMap: RadioMemoryMap): RadioMemoryMapUiField[] {\n const bindings = memoryMap.channelBindings;\n\n if (!bindings) {\n return [];\n }\n\n const struct = memoryMap.structs.find((entry) => entry.id === bindings.records);\n\n if (!struct) {\n return [];\n }\n\n const boundIds = channelBoundFieldIds(memoryMap);\n const fields: RadioMemoryMapUiField[] = [];\n\n for (const field of struct.fields) {\n if (field.reserved || !field.ui || boundIds.has(field.id)) {\n continue;\n }\n\n fields.push({\n path: field.id,\n structId: struct.id,\n fieldId: field.id,\n ui: field.ui,\n value: field.value,\n });\n }\n\n return fields;\n}\n\n/**\n * Format a decoded memory-map field value for read-only display (e.g. channel table).\n */\nexport function formatMemoryMapFieldValue(\n value: RadioSettingValue | undefined,\n field: Pick<RadioMemoryMapUiField, 'value' | 'fieldId'>,\n): string {\n if (value === undefined || value === null) {\n return '';\n }\n\n if (field.value?.kind === 'enum' && typeof value === 'string') {\n return value;\n }\n\n if (field.value?.kind === 'boolean' || typeof value === 'boolean') {\n if (field.fieldId === 'wide') {\n return value ? 'Wide' : 'Narrow';\n }\n\n if (field.fieldId === 'scan') {\n return value ? 'On' : 'Off';\n }\n\n return value ? 'On' : 'Off';\n }\n\n if (field.fieldId === 'lowpower' && typeof value === 'number') {\n return value === 0 ? 'High' : 'Low';\n }\n\n if (field.fieldId === 'scode' && typeof value === 'number') {\n return String(value + 1);\n }\n\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {\n return String(value);\n }\n\n return '';\n}\n\n/**\n * Group UI fields by their ui.group key, preserving first-seen group order.\n */\nexport function groupMemoryMapUiFields(fields: RadioMemoryMapUiField[]): Map<string, RadioMemoryMapUiField[]> {\n const groups = new Map<string, RadioMemoryMapUiField[]>();\n\n for (const field of fields) {\n const existing = groups.get(field.ui.group);\n\n if (existing) {\n existing.push(field);\n } else {\n groups.set(field.ui.group, [field]);\n }\n }\n\n return groups;\n}\n"]}
@@ -18,6 +18,9 @@
18
18
  "items": {
19
19
  "$ref": "#/definitions/struct"
20
20
  }
21
+ },
22
+ "channelBindings": {
23
+ "$ref": "#/definitions/channelBindings"
21
24
  }
22
25
  },
23
26
  "definitions": {
@@ -27,6 +30,28 @@
27
30
  { "type": "string", "pattern": "^(0[xX][0-9a-fA-F]+|[0-9]+)$" }
28
31
  ]
29
32
  },
33
+ "channelBindings": {
34
+ "type": "object",
35
+ "required": ["records", "receiveFrequency", "transmitFrequency", "receiveTone", "transmitTone"],
36
+ "properties": {
37
+ "records": { "type": "string", "minLength": 1 },
38
+ "names": { "type": "string", "minLength": 1 },
39
+ "nameField": { "type": "string", "minLength": 1 },
40
+ "receiveFrequency": { "type": "string", "minLength": 1 },
41
+ "transmitFrequency": { "type": "string", "minLength": 1 },
42
+ "receiveTone": { "type": "string", "minLength": 1 },
43
+ "transmitTone": { "type": "string", "minLength": 1 }
44
+ },
45
+ "additionalProperties": false
46
+ },
47
+ "emptyWhen": {
48
+ "type": "object",
49
+ "required": ["equals"],
50
+ "properties": {
51
+ "equals": { "type": "integer", "minimum": 0, "maximum": 255 }
52
+ },
53
+ "additionalProperties": false
54
+ },
30
55
  "ui": {
31
56
  "type": "object",
32
57
  "required": ["group", "label", "widget"],
@@ -112,6 +137,31 @@
112
137
  "length": { "type": "integer", "minimum": 1 }
113
138
  },
114
139
  "additionalProperties": false
140
+ },
141
+ {
142
+ "type": "object",
143
+ "required": ["kind", "length"],
144
+ "properties": {
145
+ "kind": { "const": "lbcd" },
146
+ "length": { "type": "integer", "minimum": 1 },
147
+ "scale": { "type": "number" }
148
+ },
149
+ "additionalProperties": false
150
+ },
151
+ {
152
+ "type": "object",
153
+ "required": ["kind", "values"],
154
+ "properties": {
155
+ "kind": { "const": "tone" },
156
+ "values": {
157
+ "type": "array",
158
+ "items": { "type": "integer" },
159
+ "minItems": 1
160
+ },
161
+ "ctcssMin": { "type": "integer" },
162
+ "reverseOffset": { "type": "integer" }
163
+ },
164
+ "additionalProperties": false
115
165
  }
116
166
  ]
117
167
  },
@@ -143,7 +193,9 @@
143
193
  "items": { "$ref": "#/definitions/field" }
144
194
  },
145
195
  "count": { "type": "integer", "minimum": 1 },
146
- "stride": { "type": "integer", "minimum": 1 }
196
+ "stride": { "type": "integer", "minimum": 1 },
197
+ "emptyWhen": { "$ref": "#/definitions/emptyWhen" },
198
+ "clearEmpty": { "type": "boolean" }
147
199
  },
148
200
  "additionalProperties": false
149
201
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@springfield/ham-radio-utils",
3
- "version": "4.2.0",
3
+ "version": "4.4.0",
4
4
  "author": "Bryan Hunt",
5
5
  "license": "MIT",
6
6
  "readme": "README.md",
@@ -30,7 +30,7 @@
30
30
  "test:unit": "tsx --test test/unit/**/*.test.ts"
31
31
  },
32
32
  "dependencies": {
33
- "@springfield/ham-radio-api": "^17.1.0",
33
+ "@springfield/ham-radio-api": "^17.2.0",
34
34
  "ajv": "^8.20.0",
35
35
  "ajv-formats": "^3.0.1",
36
36
  "fishery": "^2.4.0",
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './memory/segmented-memory.js';
2
2
  export * from './memory/memory-map-codec.js';
3
+ export * from './memory/memory-map-channels.js';
3
4
  export * from './memory/memory-map-ui.js';
4
5
 
5
6
  export * from './test-utils/radio-channel-factory.js';
@@ -0,0 +1,264 @@
1
+ import {
2
+ Frequency,
3
+ type RadioChannel,
4
+ type RadioMemoryConfig,
5
+ type RadioMemoryMap,
6
+ type RadioMemoryMapToneValue,
7
+ type RadioProgram,
8
+ type RadioProgrammedChannel,
9
+ type RadioSettings,
10
+ type RadioSettingValue,
11
+ type RadioTone,
12
+ RadioToneType,
13
+ } from '@springfield/ham-radio-api';
14
+ import { decodeMemoryMap, encodeMemoryMap } from './memory-map-codec.js';
15
+
16
+ function isToneValue(value: RadioSettingValue | undefined): value is RadioMemoryMapToneValue {
17
+ return Boolean(value && typeof value === 'object' && !Array.isArray(value) && 'mode' in value);
18
+ }
19
+
20
+ function toneToRadioTone(value: RadioSettingValue | undefined): RadioTone {
21
+ if (!isToneValue(value) || value.mode === 'none') {
22
+ return { tone: 0, type: RadioToneType.CTCSS };
23
+ }
24
+
25
+ if (value.mode === 'ctcss') {
26
+ return { tone: value.value, type: RadioToneType.CTCSS };
27
+ }
28
+
29
+ return { tone: value.code, type: RadioToneType.DCS };
30
+ }
31
+
32
+ function radioToneToMapTone(tone: RadioTone | undefined): RadioMemoryMapToneValue {
33
+ if (!tone || !tone.tone) {
34
+ return { mode: 'none' };
35
+ }
36
+
37
+ if (tone.type === RadioToneType.DCS) {
38
+ return { mode: 'dcs', code: Number(tone.tone), polarity: 'N' };
39
+ }
40
+
41
+ return { mode: 'ctcss', value: Number(tone.tone) };
42
+ }
43
+
44
+ function asRecord(value: RadioSettingValue | undefined): Record<string, RadioSettingValue> | undefined {
45
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
46
+ return undefined;
47
+ }
48
+
49
+ return value as Record<string, RadioSettingValue>;
50
+ }
51
+
52
+ /**
53
+ * Project decoded memory-map channel structs into RadioProgrammedChannel[].
54
+ */
55
+ export function bindingsToChannels(
56
+ memoryMap: RadioMemoryMap,
57
+ settings: RadioSettings,
58
+ ): RadioProgrammedChannel[] {
59
+ const bindings = memoryMap.channelBindings;
60
+
61
+ if (!bindings) {
62
+ return [];
63
+ }
64
+
65
+ const records = settings[bindings.records];
66
+ const names = bindings.names ? settings[bindings.names] : undefined;
67
+ const recordItems = Array.isArray(records) ? records : [];
68
+ const nameItems = Array.isArray(names) ? names : [];
69
+ const nameField = bindings.nameField ?? 'name';
70
+ const boundFieldIds = new Set([
71
+ bindings.receiveFrequency,
72
+ bindings.transmitFrequency,
73
+ bindings.receiveTone,
74
+ bindings.transmitTone,
75
+ ]);
76
+
77
+ const channels: RadioProgrammedChannel[] = [];
78
+
79
+ for (let index = 0; index < recordItems.length; index += 1) {
80
+ const record = asRecord(recordItems[index]);
81
+
82
+ if (!record) {
83
+ continue;
84
+ }
85
+
86
+ const nameRecord = asRecord(nameItems[index]);
87
+ const nameValue = nameRecord?.[nameField];
88
+ const name = typeof nameValue === 'string' ? nameValue : '';
89
+
90
+ const receiveFrequency = Number(record[bindings.receiveFrequency] ?? 0);
91
+ const transmitFrequency = Number(record[bindings.transmitFrequency] ?? 0);
92
+
93
+ const channelSettings: RadioSettings = {};
94
+
95
+ for (const [fieldId, fieldValue] of Object.entries(record)) {
96
+ if (boundFieldIds.has(fieldId) || fieldId.startsWith('_')) {
97
+ continue;
98
+ }
99
+
100
+ channelSettings[fieldId] = fieldValue;
101
+ }
102
+
103
+ // Map Chirp lowpower index onto legacy transmitPower watts for UV-5R UI compatibility.
104
+ if (typeof channelSettings.lowpower === 'number') {
105
+ channelSettings.transmitPower = channelSettings.lowpower === 0 ? 5 : 1;
106
+ }
107
+
108
+ if (typeof channelSettings.wide === 'boolean') {
109
+ channelSettings.mode = channelSettings.wide ? 'FM' : 'NFM';
110
+ }
111
+
112
+ if (typeof channelSettings.scan === 'boolean') {
113
+ channelSettings.skip = channelSettings.scan ? '' : 'S';
114
+ }
115
+
116
+ const radioChannel: RadioChannel = {
117
+ name,
118
+ receiveFrequency: Frequency(receiveFrequency),
119
+ transmitFrequency: Frequency(transmitFrequency),
120
+ receiveTone: toneToRadioTone(record[bindings.receiveTone]),
121
+ transmitTone: toneToRadioTone(record[bindings.transmitTone]),
122
+ };
123
+
124
+ channels.push({
125
+ channelNumber: index,
126
+ radioChannel,
127
+ settings: Object.keys(channelSettings).length > 0 ? channelSettings : undefined,
128
+ });
129
+ }
130
+
131
+ return channels;
132
+ }
133
+
134
+ /**
135
+ * Strip channel-bound structs from decoded settings so RadioProgram.settings
136
+ * only contains radio-wide settings.
137
+ */
138
+ export function settingsWithoutChannels(memoryMap: RadioMemoryMap, settings: RadioSettings): RadioSettings {
139
+ const bindings = memoryMap.channelBindings;
140
+
141
+ if (!bindings) {
142
+ return settings;
143
+ }
144
+
145
+ const next: RadioSettings = { ...settings };
146
+ delete next[bindings.records];
147
+
148
+ if (bindings.names) {
149
+ delete next[bindings.names];
150
+ }
151
+
152
+ return next;
153
+ }
154
+
155
+ /**
156
+ * Merge RadioProgram channels back into a memory-map settings bag for encode.
157
+ */
158
+ export function programToChannelSettings(memoryMap: RadioMemoryMap, program: RadioProgram): RadioSettings {
159
+ const bindings = memoryMap.channelBindings;
160
+
161
+ if (!bindings) {
162
+ return program.settings;
163
+ }
164
+
165
+ const recordsStruct = memoryMap.structs.find((struct) => struct.id === bindings.records);
166
+ const namesStruct = bindings.names ? memoryMap.structs.find((struct) => struct.id === bindings.names) : undefined;
167
+ const count = recordsStruct?.count ?? 0;
168
+ const nameField = bindings.nameField ?? 'name';
169
+
170
+ const records: RadioSettingValue[] = Array.from({ length: count }, () => null);
171
+ const names: RadioSettingValue[] = Array.from({ length: count }, () => null);
172
+
173
+ for (const programmed of program.channels) {
174
+ const index = programmed.channelNumber;
175
+
176
+ if (index < 0 || index >= count) {
177
+ continue;
178
+ }
179
+
180
+ if (typeof programmed.radioChannel === 'string') {
181
+ continue;
182
+ }
183
+
184
+ const channel = programmed.radioChannel;
185
+ const channelSettings = programmed.settings ?? {};
186
+ const record: Record<string, RadioSettingValue> = {
187
+ [bindings.receiveFrequency]: channel.receiveFrequency,
188
+ [bindings.transmitFrequency]: channel.transmitFrequency,
189
+ [bindings.receiveTone]: radioToneToMapTone(channel.receiveTone),
190
+ [bindings.transmitTone]: radioToneToMapTone(channel.transmitTone),
191
+ };
192
+
193
+ for (const [key, value] of Object.entries(channelSettings)) {
194
+ if (key === 'transmitPower') {
195
+ record.lowpower = value === 5 || value === 4 || value === 0 ? 0 : 1;
196
+ continue;
197
+ }
198
+
199
+ if (key === 'mode') {
200
+ record.wide = value === 'FM';
201
+ continue;
202
+ }
203
+
204
+ if (key === 'skip') {
205
+ record.scan = value !== 'S';
206
+ continue;
207
+ }
208
+
209
+ record[key] = value;
210
+ }
211
+
212
+ if (record.lowpower === undefined) {
213
+ record.lowpower = 0;
214
+ }
215
+
216
+ if (record.wide === undefined) {
217
+ record.wide = true;
218
+ }
219
+
220
+ if (record.scan === undefined) {
221
+ record.scan = true;
222
+ }
223
+
224
+ records[index] = record;
225
+ names[index] = { [nameField]: channel.name ?? '' };
226
+ }
227
+
228
+ const bag: RadioSettings = { ...program.settings, [bindings.records]: records };
229
+
230
+ if (bindings.names && namesStruct) {
231
+ bag[bindings.names] = names;
232
+ }
233
+
234
+ return bag;
235
+ }
236
+
237
+ /**
238
+ * Decode a full RadioProgram (channels + radio-wide settings) from memory.
239
+ */
240
+ export function decodeRadioProgram(
241
+ memoryMap: RadioMemoryMap,
242
+ contents: Uint8Array,
243
+ memoryConfig: RadioMemoryConfig,
244
+ ): RadioProgram {
245
+ const decoded = decodeMemoryMap(memoryMap, contents, memoryConfig);
246
+
247
+ return {
248
+ channels: bindingsToChannels(memoryMap, decoded),
249
+ settings: settingsWithoutChannels(memoryMap, decoded),
250
+ };
251
+ }
252
+
253
+ /**
254
+ * Encode a RadioProgram into an existing memory image (patch in place).
255
+ */
256
+ export function encodeRadioProgram(
257
+ memoryMap: RadioMemoryMap,
258
+ program: RadioProgram,
259
+ contents: Uint8Array,
260
+ memoryConfig: RadioMemoryConfig,
261
+ ): Uint8Array {
262
+ const bag = programToChannelSettings(memoryMap, program);
263
+ return encodeMemoryMap(memoryMap, bag, contents, memoryConfig);
264
+ }