@tsplc/typegen 0.2.17 → 0.2.19
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 +2 -2
- package/dist/cli.js +798 -544
- package/dist/cli.js.map +15 -9
- package/dist/codegen.js +252 -177
- package/dist/codegen.js.map +5 -5
- package/dist/hardware.js +447 -338
- package/dist/hardware.js.map +10 -5
- package/dist/index.js +848 -602
- package/dist/index.js.map +16 -9
- package/package.json +55 -62
- package/src/cli.ts +68 -51
- package/src/codegen/constants.ts +177 -86
- package/src/codegen/generate.ts +436 -388
- package/src/codegen/parser.ts +358 -287
- package/src/device-tree.ts +152 -140
- package/src/hardware/channels.ts +157 -0
- package/src/hardware/eds.ts +408 -0
- package/src/hardware/mapping-types.ts +88 -0
- package/src/hardware/modbus.ts +105 -0
- package/src/hardware/process-image.ts +102 -0
- package/src/hardware/selection.ts +136 -0
- package/src/hardware.ts +195 -844
- package/src/index.ts +25 -1
- package/src/project.ts +246 -151
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { HardwareDevice } from "@tsplc/config";
|
|
2
|
+
import type { EdsAssembly, EdsConnection, HardwareDescriptor } from "./eds.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An explicit connection must carry the selected assemblies. Otherwise the one
|
|
6
|
+
* matching connection, or the one exclusive owner that carries both directions, is
|
|
7
|
+
* implied; any other choice is listed for the config to name.
|
|
8
|
+
*/
|
|
9
|
+
export function selectConnection(
|
|
10
|
+
id: string,
|
|
11
|
+
settings: HardwareDevice,
|
|
12
|
+
descriptor: HardwareDescriptor,
|
|
13
|
+
): EdsConnection | undefined {
|
|
14
|
+
const compatible = descriptor.connections.filter(
|
|
15
|
+
(connection) =>
|
|
16
|
+
(settings.inputs?.assembly === undefined ||
|
|
17
|
+
connection.inputAssembly === settings.inputs.assembly) &&
|
|
18
|
+
(settings.outputs?.assembly === undefined ||
|
|
19
|
+
connection.outputAssembly === settings.outputs.assembly),
|
|
20
|
+
);
|
|
21
|
+
if (settings.connection) {
|
|
22
|
+
const connection = compatible.find(
|
|
23
|
+
(item) => item.id === settings.connection,
|
|
24
|
+
);
|
|
25
|
+
if (!connection) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`${id}: connection ${settings.connection} does not match the selected assemblies.`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
return connection;
|
|
31
|
+
}
|
|
32
|
+
if (compatible.length <= 1) {
|
|
33
|
+
return compatible[0];
|
|
34
|
+
}
|
|
35
|
+
const owners = compatible.filter(
|
|
36
|
+
(item) =>
|
|
37
|
+
item.owner &&
|
|
38
|
+
item.inputAssembly !== undefined &&
|
|
39
|
+
item.outputAssembly !== undefined,
|
|
40
|
+
);
|
|
41
|
+
if (owners.length === 1) {
|
|
42
|
+
return owners[0];
|
|
43
|
+
}
|
|
44
|
+
const choices = compatible
|
|
45
|
+
.map(
|
|
46
|
+
(item) =>
|
|
47
|
+
`${item.id} "${item.name}" (in ${item.inputAssembly ?? "-"}, out ${item.outputAssembly ?? "-"})`,
|
|
48
|
+
)
|
|
49
|
+
.join(", ");
|
|
50
|
+
throw new Error(
|
|
51
|
+
`${id}: select connection; ${settings.file} offers ${choices}.`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** An assembly a device exchanges, with the payload size its connection transfers. */
|
|
56
|
+
export interface AssemblySelection {
|
|
57
|
+
id: string;
|
|
58
|
+
direction: "input" | "output";
|
|
59
|
+
region: "I" | "Q";
|
|
60
|
+
instance: number;
|
|
61
|
+
assembly: EdsAssembly;
|
|
62
|
+
size: number;
|
|
63
|
+
/** The configured base; without one the device tree places the assembly. */
|
|
64
|
+
address: string | undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The input and output assemblies a device selects, or its connection carries, each
|
|
69
|
+
* with a payload size inside the connection's bounds.
|
|
70
|
+
*/
|
|
71
|
+
export function selectAssemblies(
|
|
72
|
+
id: string,
|
|
73
|
+
settings: HardwareDevice,
|
|
74
|
+
descriptor: HardwareDescriptor,
|
|
75
|
+
connection: EdsConnection | undefined,
|
|
76
|
+
): AssemblySelection[] {
|
|
77
|
+
return (["input", "output"] as const).flatMap((direction) => {
|
|
78
|
+
const selection =
|
|
79
|
+
direction === "input" ? settings.inputs : settings.outputs;
|
|
80
|
+
const instance =
|
|
81
|
+
selection?.assembly ??
|
|
82
|
+
(direction === "input"
|
|
83
|
+
? connection?.inputAssembly
|
|
84
|
+
: connection?.outputAssembly);
|
|
85
|
+
if (instance === undefined) {
|
|
86
|
+
if (selection) {
|
|
87
|
+
throw new Error(`${id}: select its ${direction}s assembly.`);
|
|
88
|
+
}
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const assembly = descriptor.assemblies.find((a) => a.instance === instance);
|
|
92
|
+
if (!assembly) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
`${id}: assembly ${instance} is not present in ${settings.file}.`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
const region = direction === "input" ? "I" : "Q";
|
|
98
|
+
if (selection?.address && !selection.address.startsWith(`%${region}B`)) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`${id}: ${direction} assembly needs a %${region}B address.`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const declaredSize =
|
|
104
|
+
direction === "input" ? connection?.inputSize : connection?.outputSize;
|
|
105
|
+
const payloadSize = selection?.size ?? declaredSize?.value ?? assembly.size;
|
|
106
|
+
// A parameter-backed size may be overridden within its bounds; a literal size is fixed.
|
|
107
|
+
if (
|
|
108
|
+
!Number.isSafeInteger(payloadSize) ||
|
|
109
|
+
payloadSize <= 0 ||
|
|
110
|
+
payloadSize > assembly.size ||
|
|
111
|
+
(declaredSize?.minimum !== undefined &&
|
|
112
|
+
payloadSize < declaredSize.minimum) ||
|
|
113
|
+
(declaredSize?.maximum !== undefined &&
|
|
114
|
+
payloadSize > declaredSize.maximum) ||
|
|
115
|
+
(selection?.size !== undefined &&
|
|
116
|
+
declaredSize &&
|
|
117
|
+
!declaredSize.parameter &&
|
|
118
|
+
payloadSize !== declaredSize.value)
|
|
119
|
+
) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
`${id}: ${direction} payload size ${payloadSize} is outside the selected EDS connection's bounds.`,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return [
|
|
125
|
+
{
|
|
126
|
+
id,
|
|
127
|
+
direction,
|
|
128
|
+
region,
|
|
129
|
+
instance,
|
|
130
|
+
assembly,
|
|
131
|
+
size: payloadSize,
|
|
132
|
+
address: selection?.address,
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
});
|
|
136
|
+
}
|