@stapfel/stabench-core 0.0.0-stage → 0.1.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/LICENSE +17 -0
- package/README.md +73 -2
- package/dist/index.d.ts +166 -0
- package/dist/index.js +492 -0
- package/package.json +34 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# StaBench core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Platform-independent frame and time-axis types plus small helpers shared by the
|
|
4
|
+
StaBench packages. This package contains no Electron, instrument, or graph code.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import {
|
|
8
|
+
fromWaveforms,
|
|
9
|
+
timeAt,
|
|
10
|
+
toWaveforms,
|
|
11
|
+
validateFrame,
|
|
12
|
+
} from "@stapfel/stabench-core";
|
|
13
|
+
|
|
14
|
+
const frame = fromWaveforms(waveforms, { node: "scope", port: "out" }, "session-1");
|
|
15
|
+
validateFrame(frame);
|
|
16
|
+
|
|
17
|
+
const t = timeAt(frame.time, 1000);
|
|
18
|
+
const window = toWaveforms(frame, 0);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Frames
|
|
22
|
+
|
|
23
|
+
A `Frame` is a chunk on a stream. Analog and digital frames carry one `TimeAxis`
|
|
24
|
+
with piecewise-uniform segments; a segment is a contiguous stretch that shares a
|
|
25
|
+
single `dt`. `t0` is the time of the segment's first sample on the stream axis:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
t(i) = segment.t0 + (i - segment.start) * dt
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`gapBefore` describes what happened between the previous segment and this one:
|
|
32
|
+
`{ kind: "none" }` (lossless, continuous), `{ kind: "unknown" }` (lost, unknown
|
|
33
|
+
length), or `{ kind: "estimated", seconds, uncertainty }`. Typed arrays
|
|
34
|
+
(`Float32Array` or `Float64Array`) hold the samples.
|
|
35
|
+
|
|
36
|
+
Invariants checked by `validateFrame`:
|
|
37
|
+
|
|
38
|
+
- one positive finite `dt` for the whole axis;
|
|
39
|
+
- segments are ordered and contiguous (`start + length` of one is `start` of
|
|
40
|
+
the next) and cover the sample arrays exactly;
|
|
41
|
+
- a segment without a gap starts exactly where the previous one ends;
|
|
42
|
+
- channel names are unique, sample counts match, samples are finite.
|
|
43
|
+
|
|
44
|
+
`fromWaveforms` builds a one-segment frame from the legacy oscilloscope
|
|
45
|
+
`Waveform` shape (`Float32Array`, `unit: "V"`). `toWaveforms` extracts one
|
|
46
|
+
segment back into the legacy shape used by the app's plot and CSV writer.
|
|
47
|
+
`sliceSegment` copies one segment into a standalone frame.
|
|
48
|
+
|
|
49
|
+
## Synthetic I2C data
|
|
50
|
+
|
|
51
|
+
`synthI2c` generates two sampled traces for a scripted list of I2C
|
|
52
|
+
transactions, including NACKs, repeated STARTs, and optional noise. It is
|
|
53
|
+
intended for tests and demos that run without hardware. `synthI2cFrame` wraps
|
|
54
|
+
the same data in an analog frame with `SCL` and `SDA` channels.
|
|
55
|
+
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
- `npm test`
|
|
59
|
+
- `npm run build`
|
|
60
|
+
- `npx tsc --noEmit` for a quick type check
|
|
61
|
+
|
|
62
|
+
## Publishing
|
|
63
|
+
|
|
64
|
+
Run the meta repository's release script from the workspace root. It builds and
|
|
65
|
+
tests the package, stages it on npm, then commits, tags, and pushes:
|
|
66
|
+
|
|
67
|
+
```powershell
|
|
68
|
+
.\.tools\release.ps1 -Project stabench-core -VersionType current
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The script asks for the npm stage-only token with masked input. Use `-DryRun`
|
|
72
|
+
to check the workflow without entering a token or staging a package. Staging
|
|
73
|
+
requires Node.js 22.9 or newer and does not publish the package publicly; the
|
|
74
|
+
approval with 2FA is a separate manual step.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
type Samples = Float32Array | Float64Array;
|
|
2
|
+
interface HostTime {
|
|
3
|
+
epochMs: number;
|
|
4
|
+
uncertaintyMs: number;
|
|
5
|
+
}
|
|
6
|
+
type Gap = {
|
|
7
|
+
kind: "none";
|
|
8
|
+
} | {
|
|
9
|
+
kind: "unknown";
|
|
10
|
+
} | {
|
|
11
|
+
kind: "estimated";
|
|
12
|
+
seconds: number;
|
|
13
|
+
uncertainty: number;
|
|
14
|
+
};
|
|
15
|
+
interface Segment {
|
|
16
|
+
start: number;
|
|
17
|
+
length: number;
|
|
18
|
+
t0: number;
|
|
19
|
+
gapBefore: Gap;
|
|
20
|
+
hostTime?: HostTime;
|
|
21
|
+
}
|
|
22
|
+
interface TimeAxis {
|
|
23
|
+
dt: number;
|
|
24
|
+
segments: Segment[];
|
|
25
|
+
}
|
|
26
|
+
interface FrameOrigin {
|
|
27
|
+
node: string;
|
|
28
|
+
port: string;
|
|
29
|
+
}
|
|
30
|
+
interface FrameBase {
|
|
31
|
+
streamId: string;
|
|
32
|
+
seq: number;
|
|
33
|
+
origin: FrameOrigin;
|
|
34
|
+
labels?: Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
interface AnalogChannel {
|
|
37
|
+
name: string;
|
|
38
|
+
unit: string;
|
|
39
|
+
samples: Samples;
|
|
40
|
+
}
|
|
41
|
+
interface AnalogFrame extends FrameBase {
|
|
42
|
+
kind: "analog";
|
|
43
|
+
time: TimeAxis;
|
|
44
|
+
channels: AnalogChannel[];
|
|
45
|
+
}
|
|
46
|
+
interface DigitalChannel {
|
|
47
|
+
name: string;
|
|
48
|
+
bits: Uint8Array;
|
|
49
|
+
}
|
|
50
|
+
interface DigitalFrame extends FrameBase {
|
|
51
|
+
kind: "digital";
|
|
52
|
+
time: TimeAxis;
|
|
53
|
+
channels: DigitalChannel[];
|
|
54
|
+
}
|
|
55
|
+
interface Packet {
|
|
56
|
+
t: number;
|
|
57
|
+
tEnd?: number;
|
|
58
|
+
segment?: number;
|
|
59
|
+
flags?: string[];
|
|
60
|
+
data: unknown;
|
|
61
|
+
}
|
|
62
|
+
interface PacketFrame<T extends Packet = Packet> extends FrameBase {
|
|
63
|
+
kind: "packets";
|
|
64
|
+
schema: string;
|
|
65
|
+
packets: T[];
|
|
66
|
+
}
|
|
67
|
+
interface ScalarFrame extends FrameBase {
|
|
68
|
+
kind: "scalar";
|
|
69
|
+
name: string;
|
|
70
|
+
unit: string;
|
|
71
|
+
points: {
|
|
72
|
+
t: number;
|
|
73
|
+
value: number;
|
|
74
|
+
}[];
|
|
75
|
+
}
|
|
76
|
+
interface ControlFrame extends FrameBase {
|
|
77
|
+
kind: "control";
|
|
78
|
+
command: "reset" | "flush" | "eos" | "trigger";
|
|
79
|
+
}
|
|
80
|
+
type Frame = AnalogFrame | DigitalFrame | PacketFrame | ScalarFrame | ControlFrame;
|
|
81
|
+
|
|
82
|
+
declare function frameLength(axis: TimeAxis): number;
|
|
83
|
+
declare function segmentIndexAt(axis: TimeAxis, index: number): number;
|
|
84
|
+
declare function segmentOf(axis: TimeAxis, index: number): Segment;
|
|
85
|
+
declare function timeAt(axis: TimeAxis, index: number): number;
|
|
86
|
+
|
|
87
|
+
declare function validateFrame(frame: Frame): void;
|
|
88
|
+
declare function validateTimeAxis(axis: TimeAxis): number;
|
|
89
|
+
|
|
90
|
+
/** Structural subset of the oscilloscope driver `Waveform` type. */
|
|
91
|
+
interface WaveformLike {
|
|
92
|
+
channel: string;
|
|
93
|
+
xIncrement: number;
|
|
94
|
+
xZero: number;
|
|
95
|
+
pointOffset: number;
|
|
96
|
+
yIncrement: number;
|
|
97
|
+
yZero: number;
|
|
98
|
+
yOffset: number;
|
|
99
|
+
samples: ArrayLike<number>;
|
|
100
|
+
capturedAt?: string;
|
|
101
|
+
displaySettings?: unknown;
|
|
102
|
+
}
|
|
103
|
+
interface WaveformTemplate {
|
|
104
|
+
capturedAt?: string;
|
|
105
|
+
displaySettings?: unknown;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Converts legacy oscilloscope waveforms into a single-segment analog frame.
|
|
109
|
+
* All waveforms must share one time axis; the samples become Float32Arrays.
|
|
110
|
+
*/
|
|
111
|
+
declare function fromWaveforms(waveforms: readonly WaveformLike[], origin: FrameOrigin, streamId: string, options?: {
|
|
112
|
+
seq?: number;
|
|
113
|
+
}): AnalogFrame;
|
|
114
|
+
/**
|
|
115
|
+
* Extracts one segment of a frame as legacy oscilloscope waveforms so the
|
|
116
|
+
* existing plot and CSV code keeps working. Samples are plain numbers and the
|
|
117
|
+
* segment's time axis becomes xZero/xIncrement with pointOffset 0.
|
|
118
|
+
*/
|
|
119
|
+
declare function toWaveforms(frame: AnalogFrame, segIndex?: number, template?: WaveformTemplate): WaveformLike[];
|
|
120
|
+
/** Copy of one segment as a standalone single-segment frame. */
|
|
121
|
+
declare function sliceSegment(frame: AnalogFrame, segIndex: number): AnalogFrame;
|
|
122
|
+
|
|
123
|
+
interface SynthI2cTransaction {
|
|
124
|
+
/** 7-bit target address. */
|
|
125
|
+
addr: number;
|
|
126
|
+
dir: "write" | "read";
|
|
127
|
+
/** Data bytes (written, or read by the controller). */
|
|
128
|
+
bytes: number[];
|
|
129
|
+
/** False makes the address byte NACKed. Defaults to true. */
|
|
130
|
+
addressAcked?: boolean;
|
|
131
|
+
/** Index into `bytes` whose acknowledge bit is a NACK. */
|
|
132
|
+
nackIndex?: number;
|
|
133
|
+
/**
|
|
134
|
+
* Begin this transaction with a repeated START. The previous transaction
|
|
135
|
+
* must set `stop: false` so the bus stays active.
|
|
136
|
+
*/
|
|
137
|
+
repeatedStart?: boolean;
|
|
138
|
+
/** Emit a STOP after this transaction. Defaults to true. */
|
|
139
|
+
stop?: boolean;
|
|
140
|
+
}
|
|
141
|
+
interface SynthI2cOptions {
|
|
142
|
+
sampleRateHz?: number;
|
|
143
|
+
sclHz?: number;
|
|
144
|
+
vHigh?: number;
|
|
145
|
+
vLow?: number;
|
|
146
|
+
noiseV?: number;
|
|
147
|
+
seed?: number;
|
|
148
|
+
/** Idle bit periods before the first transaction and after every STOP. */
|
|
149
|
+
idleBits?: number;
|
|
150
|
+
transactions: SynthI2cTransaction[];
|
|
151
|
+
}
|
|
152
|
+
interface SynthI2cResult {
|
|
153
|
+
scl: Float32Array;
|
|
154
|
+
sda: Float32Array;
|
|
155
|
+
dt: number;
|
|
156
|
+
sampleRateHz: number;
|
|
157
|
+
}
|
|
158
|
+
declare function synthI2c(options: SynthI2cOptions): SynthI2cResult;
|
|
159
|
+
/** Convenience wrapper that returns a one-segment SCL/SDA analog frame. */
|
|
160
|
+
declare function synthI2cFrame(options: SynthI2cOptions, meta?: {
|
|
161
|
+
streamId?: string;
|
|
162
|
+
capturedAt?: string;
|
|
163
|
+
seq?: number;
|
|
164
|
+
}): AnalogFrame;
|
|
165
|
+
|
|
166
|
+
export { type AnalogChannel, type AnalogFrame, type ControlFrame, type DigitalChannel, type DigitalFrame, type Frame, type FrameBase, type FrameOrigin, type Gap, type HostTime, type Packet, type PacketFrame, type Samples, type ScalarFrame, type Segment, type SynthI2cOptions, type SynthI2cResult, type SynthI2cTransaction, type TimeAxis, type WaveformLike, type WaveformTemplate, frameLength, fromWaveforms, segmentIndexAt, segmentOf, sliceSegment, synthI2c, synthI2cFrame, timeAt, toWaveforms, validateFrame, validateTimeAxis };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
// src/time.ts
|
|
2
|
+
function frameLength(axis) {
|
|
3
|
+
const last = axis.segments[axis.segments.length - 1];
|
|
4
|
+
return last ? last.start + last.length : 0;
|
|
5
|
+
}
|
|
6
|
+
function segmentIndexAt(axis, index) {
|
|
7
|
+
if (!Number.isInteger(index) || index < 0) {
|
|
8
|
+
throw new Error(`Sample index must be a non-negative integer, received ${index}`);
|
|
9
|
+
}
|
|
10
|
+
for (let segmentIndex = 0; segmentIndex < axis.segments.length; segmentIndex += 1) {
|
|
11
|
+
const segment = axis.segments[segmentIndex];
|
|
12
|
+
if (index >= segment.start && index < segment.start + segment.length) {
|
|
13
|
+
return segmentIndex;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
throw new Error(`Sample index ${index} is outside the time axis (${frameLength(axis)} samples)`);
|
|
17
|
+
}
|
|
18
|
+
function segmentOf(axis, index) {
|
|
19
|
+
return axis.segments[segmentIndexAt(axis, index)];
|
|
20
|
+
}
|
|
21
|
+
function timeAt(axis, index) {
|
|
22
|
+
const segment = segmentOf(axis, index);
|
|
23
|
+
return segment.t0 + (index - segment.start) * axis.dt;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/validate.ts
|
|
27
|
+
var TIME_TOLERANCE = 1e-6;
|
|
28
|
+
var CONTROL_COMMANDS = /* @__PURE__ */ new Set(["reset", "flush", "eos", "trigger"]);
|
|
29
|
+
function validateFrame(frame) {
|
|
30
|
+
validateBase(frame);
|
|
31
|
+
switch (frame.kind) {
|
|
32
|
+
case "analog": {
|
|
33
|
+
const sampleCount = validateTimeAxis(frame.time);
|
|
34
|
+
if (frame.channels.length === 0) {
|
|
35
|
+
throw new Error("An analog frame must contain at least one channel");
|
|
36
|
+
}
|
|
37
|
+
const names = /* @__PURE__ */ new Set();
|
|
38
|
+
for (const channel of frame.channels) {
|
|
39
|
+
if (typeof channel.name !== "string" || !channel.name) {
|
|
40
|
+
throw new Error("Analog channel names must be non-empty strings");
|
|
41
|
+
}
|
|
42
|
+
if (names.has(channel.name)) {
|
|
43
|
+
throw new Error(`Duplicate analog channel name: ${channel.name}`);
|
|
44
|
+
}
|
|
45
|
+
names.add(channel.name);
|
|
46
|
+
if (typeof channel.unit !== "string") {
|
|
47
|
+
throw new Error(`Analog channel ${channel.name} must declare a unit string`);
|
|
48
|
+
}
|
|
49
|
+
if (!isTypedSamples(channel.samples)) {
|
|
50
|
+
throw new Error(`Analog channel ${channel.name} must use a Float32Array or Float64Array`);
|
|
51
|
+
}
|
|
52
|
+
if (channel.samples.length !== sampleCount) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Analog channel ${channel.name} has ${channel.samples.length} samples, expected ${sampleCount}`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
case "digital": {
|
|
61
|
+
const sampleCount = validateTimeAxis(frame.time);
|
|
62
|
+
if (frame.channels.length === 0) {
|
|
63
|
+
throw new Error("A digital frame must contain at least one channel");
|
|
64
|
+
}
|
|
65
|
+
for (const channel of frame.channels) {
|
|
66
|
+
if (typeof channel.name !== "string" || !channel.name) {
|
|
67
|
+
throw new Error("Digital channel names must be non-empty strings");
|
|
68
|
+
}
|
|
69
|
+
if (!(channel.bits instanceof Uint8Array)) {
|
|
70
|
+
throw new Error(`Digital channel ${channel.name} must use a Uint8Array`);
|
|
71
|
+
}
|
|
72
|
+
if (channel.bits.length !== sampleCount) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`Digital channel ${channel.name} has ${channel.bits.length} bits, expected ${sampleCount}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
for (let index = 0; index < channel.bits.length; index += 1) {
|
|
78
|
+
if (channel.bits[index] !== 0 && channel.bits[index] !== 1) {
|
|
79
|
+
throw new Error(`Digital channel ${channel.name} must contain only 0 or 1 at ${index}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
case "packets": {
|
|
86
|
+
if (typeof frame.schema !== "string" || !frame.schema) {
|
|
87
|
+
throw new Error("A packet frame must declare a schema");
|
|
88
|
+
}
|
|
89
|
+
for (const packet of frame.packets) {
|
|
90
|
+
if (!Number.isFinite(packet.t)) {
|
|
91
|
+
throw new Error("Packet timestamps must be finite");
|
|
92
|
+
}
|
|
93
|
+
if (packet.tEnd !== void 0 && !Number.isFinite(packet.tEnd)) {
|
|
94
|
+
throw new Error("Packet end timestamps must be finite");
|
|
95
|
+
}
|
|
96
|
+
if (packet.segment !== void 0 && (!Number.isInteger(packet.segment) || packet.segment < 0)) {
|
|
97
|
+
throw new Error("Packet segment indices must be non-negative integers");
|
|
98
|
+
}
|
|
99
|
+
if (packet.flags !== void 0 && !packet.flags.every((flag) => typeof flag === "string")) {
|
|
100
|
+
throw new Error("Packet flags must be strings");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
case "scalar": {
|
|
106
|
+
if (typeof frame.name !== "string" || !frame.name) {
|
|
107
|
+
throw new Error("A scalar frame must have a name");
|
|
108
|
+
}
|
|
109
|
+
if (typeof frame.unit !== "string") {
|
|
110
|
+
throw new Error("A scalar frame must declare a unit string");
|
|
111
|
+
}
|
|
112
|
+
for (const point of frame.points) {
|
|
113
|
+
if (!Number.isFinite(point.t) || !Number.isFinite(point.value)) {
|
|
114
|
+
throw new Error("Scalar points must have finite time and value");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
case "control": {
|
|
120
|
+
if (!CONTROL_COMMANDS.has(frame.command)) {
|
|
121
|
+
throw new Error(`Unsupported control command: ${String(frame.command)}`);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
default: {
|
|
126
|
+
const exhaustive = frame;
|
|
127
|
+
throw new Error(`Unsupported frame kind: ${JSON.stringify(exhaustive)}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function validateTimeAxis(axis) {
|
|
132
|
+
if (!Number.isFinite(axis.dt) || axis.dt <= 0) {
|
|
133
|
+
throw new Error("The sample interval must be a positive finite number");
|
|
134
|
+
}
|
|
135
|
+
if (axis.segments.length === 0) {
|
|
136
|
+
throw new Error("A time axis must contain at least one segment");
|
|
137
|
+
}
|
|
138
|
+
let expectedStart = 0;
|
|
139
|
+
let previous;
|
|
140
|
+
for (const [index, segment] of axis.segments.entries()) {
|
|
141
|
+
if (!Number.isInteger(segment.start) || segment.start < 0) {
|
|
142
|
+
throw new Error("Segment start indices must be non-negative integers");
|
|
143
|
+
}
|
|
144
|
+
if (!Number.isInteger(segment.length) || segment.length <= 0) {
|
|
145
|
+
throw new Error("Segment lengths must be positive integers");
|
|
146
|
+
}
|
|
147
|
+
if (segment.start !== expectedStart) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Time axis segments must be contiguous: segment ${index} starts at ${segment.start}, expected ${expectedStart}`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
if (!Number.isFinite(segment.t0)) {
|
|
153
|
+
throw new Error("Segment start times must be finite");
|
|
154
|
+
}
|
|
155
|
+
validateGap(segment.gapBefore);
|
|
156
|
+
if (segment.hostTime) validateHostTime(segment.hostTime);
|
|
157
|
+
if (previous) {
|
|
158
|
+
const previousEnd = previous.t0 + previous.length * axis.dt;
|
|
159
|
+
if (segment.gapBefore.kind === "none") {
|
|
160
|
+
const scale = Math.max(Math.abs(previousEnd), Math.abs(segment.t0), axis.dt);
|
|
161
|
+
if (Math.abs(segment.t0 - previousEnd) > TIME_TOLERANCE * scale) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`Segment ${index} continues without a gap but starts at ${segment.t0}s instead of ${previousEnd}s`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
const scale = Math.max(Math.abs(previousEnd), Math.abs(segment.t0), axis.dt);
|
|
168
|
+
if (segment.t0 < previousEnd - TIME_TOLERANCE * scale) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
`Segment ${index} starts before the end of the previous segment (${segment.t0}s < ${previousEnd}s)`
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
expectedStart = segment.start + segment.length;
|
|
176
|
+
previous = segment;
|
|
177
|
+
}
|
|
178
|
+
return expectedStart;
|
|
179
|
+
}
|
|
180
|
+
function validateGap(gap) {
|
|
181
|
+
switch (gap.kind) {
|
|
182
|
+
case "none":
|
|
183
|
+
case "unknown":
|
|
184
|
+
return;
|
|
185
|
+
case "estimated":
|
|
186
|
+
if (!Number.isFinite(gap.seconds) || gap.seconds < 0) {
|
|
187
|
+
throw new Error("Estimated gap durations must be non-negative finite numbers");
|
|
188
|
+
}
|
|
189
|
+
if (!Number.isFinite(gap.uncertainty) || gap.uncertainty < 0) {
|
|
190
|
+
throw new Error("Estimated gap uncertainties must be non-negative finite numbers");
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
default: {
|
|
194
|
+
const exhaustive = gap;
|
|
195
|
+
throw new Error(`Unsupported gap kind: ${JSON.stringify(exhaustive)}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function validateHostTime(hostTime) {
|
|
200
|
+
if (!Number.isFinite(hostTime.epochMs)) {
|
|
201
|
+
throw new Error("Host time epoch must be finite");
|
|
202
|
+
}
|
|
203
|
+
if (!Number.isFinite(hostTime.uncertaintyMs) || hostTime.uncertaintyMs < 0) {
|
|
204
|
+
throw new Error("Host time uncertainty must be a non-negative finite number");
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function validateBase(frame) {
|
|
208
|
+
if (typeof frame.streamId !== "string" || !frame.streamId) {
|
|
209
|
+
throw new Error("A frame must have a non-empty streamId");
|
|
210
|
+
}
|
|
211
|
+
if (!Number.isInteger(frame.seq) || frame.seq < 0) {
|
|
212
|
+
throw new Error("A frame sequence number must be a non-negative integer");
|
|
213
|
+
}
|
|
214
|
+
if (!frame.origin || typeof frame.origin.node !== "string" || !frame.origin.node || typeof frame.origin.port !== "string" || !frame.origin.port) {
|
|
215
|
+
throw new Error("A frame must declare its origin node and port");
|
|
216
|
+
}
|
|
217
|
+
if (frame.labels) {
|
|
218
|
+
for (const [key, value] of Object.entries(frame.labels)) {
|
|
219
|
+
if (typeof value !== "string") {
|
|
220
|
+
throw new Error(`Frame label ${key} must be a string`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function isTypedSamples(samples) {
|
|
226
|
+
return samples instanceof Float32Array || samples instanceof Float64Array;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/adapters.ts
|
|
230
|
+
var TIME_TOLERANCE2 = 1e-6;
|
|
231
|
+
function fromWaveforms(waveforms, origin, streamId, options = {}) {
|
|
232
|
+
if (waveforms.length === 0) throw new Error("Cannot build a frame from zero waveforms");
|
|
233
|
+
const first = waveforms[0];
|
|
234
|
+
const count = first.samples.length;
|
|
235
|
+
if (count === 0) throw new Error("Cannot build a frame from empty waveforms");
|
|
236
|
+
if (!Number.isFinite(first.xIncrement) || first.xIncrement <= 0) {
|
|
237
|
+
throw new Error("The waveform sample interval must be a positive finite number");
|
|
238
|
+
}
|
|
239
|
+
const names = /* @__PURE__ */ new Set();
|
|
240
|
+
const channels = waveforms.map((waveform) => {
|
|
241
|
+
if (names.has(waveform.channel)) {
|
|
242
|
+
throw new Error(`Duplicate waveform channel: ${waveform.channel}`);
|
|
243
|
+
}
|
|
244
|
+
names.add(waveform.channel);
|
|
245
|
+
if (!Number.isFinite(waveform.xIncrement) || waveform.xIncrement <= 0) {
|
|
246
|
+
throw new Error(`Waveform ${waveform.channel} has an invalid sample interval`);
|
|
247
|
+
}
|
|
248
|
+
if (waveform.samples.length !== count) {
|
|
249
|
+
throw new Error("Waveform channels must have the same sample count");
|
|
250
|
+
}
|
|
251
|
+
const scale = Math.max(Math.abs(first.xIncrement), Math.abs(waveform.xIncrement));
|
|
252
|
+
if (Math.abs(waveform.xIncrement - first.xIncrement) > TIME_TOLERANCE2 * scale) {
|
|
253
|
+
throw new Error("Waveform channels must have the same sample interval");
|
|
254
|
+
}
|
|
255
|
+
const firstStart = first.xZero - first.pointOffset * first.xIncrement;
|
|
256
|
+
const channelStart = waveform.xZero - waveform.pointOffset * waveform.xIncrement;
|
|
257
|
+
const startScale = Math.max(Math.abs(firstStart), Math.abs(channelStart), first.xIncrement);
|
|
258
|
+
if (Math.abs(firstStart - channelStart) > TIME_TOLERANCE2 * startScale) {
|
|
259
|
+
throw new Error("Waveform channels must share one time axis");
|
|
260
|
+
}
|
|
261
|
+
const samples = new Float32Array(count);
|
|
262
|
+
for (let index = 0; index < count; index += 1) {
|
|
263
|
+
const value = waveform.samples[index];
|
|
264
|
+
if (!Number.isFinite(value)) {
|
|
265
|
+
throw new Error(`Waveform ${waveform.channel} contains a non-finite sample`);
|
|
266
|
+
}
|
|
267
|
+
samples[index] = value;
|
|
268
|
+
}
|
|
269
|
+
return { name: waveform.channel, unit: "V", samples };
|
|
270
|
+
});
|
|
271
|
+
const t0 = first.xZero - first.pointOffset * first.xIncrement;
|
|
272
|
+
const labels = {};
|
|
273
|
+
if (first.capturedAt) labels.capturedAt = first.capturedAt;
|
|
274
|
+
const frame = {
|
|
275
|
+
kind: "analog",
|
|
276
|
+
streamId,
|
|
277
|
+
seq: options.seq ?? 0,
|
|
278
|
+
origin,
|
|
279
|
+
labels,
|
|
280
|
+
time: {
|
|
281
|
+
dt: first.xIncrement,
|
|
282
|
+
segments: [
|
|
283
|
+
{
|
|
284
|
+
start: 0,
|
|
285
|
+
length: count,
|
|
286
|
+
t0,
|
|
287
|
+
gapBefore: { kind: "none" }
|
|
288
|
+
}
|
|
289
|
+
]
|
|
290
|
+
},
|
|
291
|
+
channels
|
|
292
|
+
};
|
|
293
|
+
return frame;
|
|
294
|
+
}
|
|
295
|
+
function toWaveforms(frame, segIndex = 0, template = {}) {
|
|
296
|
+
const segment = frame.time.segments[segIndex];
|
|
297
|
+
if (!segment) {
|
|
298
|
+
throw new Error(`Frame has no segment ${segIndex}`);
|
|
299
|
+
}
|
|
300
|
+
const capturedAt = template.capturedAt ?? frame.labels?.capturedAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
301
|
+
return frame.channels.map((channel) => {
|
|
302
|
+
const samples = new Array(segment.length);
|
|
303
|
+
const start = segment.start;
|
|
304
|
+
for (let index = 0; index < segment.length; index += 1) {
|
|
305
|
+
samples[index] = channel.samples[start + index];
|
|
306
|
+
}
|
|
307
|
+
const waveform = {
|
|
308
|
+
channel: channel.name,
|
|
309
|
+
capturedAt,
|
|
310
|
+
xIncrement: frame.time.dt,
|
|
311
|
+
xZero: segment.t0,
|
|
312
|
+
pointOffset: 0,
|
|
313
|
+
yIncrement: 1,
|
|
314
|
+
yZero: 0,
|
|
315
|
+
yOffset: 0,
|
|
316
|
+
samples
|
|
317
|
+
};
|
|
318
|
+
if (template.displaySettings !== void 0) {
|
|
319
|
+
waveform.displaySettings = template.displaySettings;
|
|
320
|
+
}
|
|
321
|
+
return waveform;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
function sliceSegment(frame, segIndex) {
|
|
325
|
+
const segment = frame.time.segments[segIndex];
|
|
326
|
+
if (!segment) {
|
|
327
|
+
throw new Error(`Frame has no segment ${segIndex}`);
|
|
328
|
+
}
|
|
329
|
+
const slice = {
|
|
330
|
+
start: 0,
|
|
331
|
+
length: segment.length,
|
|
332
|
+
t0: segment.t0,
|
|
333
|
+
gapBefore: cloneGap(segment.gapBefore)
|
|
334
|
+
};
|
|
335
|
+
if (segment.hostTime) slice.hostTime = { ...segment.hostTime };
|
|
336
|
+
return {
|
|
337
|
+
...frame,
|
|
338
|
+
time: { dt: frame.time.dt, segments: [slice] },
|
|
339
|
+
channels: frame.channels.map((channel) => ({
|
|
340
|
+
...channel,
|
|
341
|
+
samples: channel.samples.slice(
|
|
342
|
+
segment.start,
|
|
343
|
+
segment.start + segment.length
|
|
344
|
+
)
|
|
345
|
+
}))
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function cloneGap(gap) {
|
|
349
|
+
return { ...gap };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/testing.ts
|
|
353
|
+
function synthI2c(options) {
|
|
354
|
+
const sampleRateHz = options.sampleRateHz ?? 1e6;
|
|
355
|
+
const sclHz = options.sclHz ?? 1e5;
|
|
356
|
+
const vHigh = options.vHigh ?? 3.3;
|
|
357
|
+
const vLow = options.vLow ?? 0;
|
|
358
|
+
const noiseV = options.noiseV ?? 0;
|
|
359
|
+
if (!Number.isFinite(sampleRateHz) || sampleRateHz <= 0) {
|
|
360
|
+
throw new Error("The synthetic sample rate must be a positive number");
|
|
361
|
+
}
|
|
362
|
+
if (!Number.isFinite(sclHz) || sclHz <= 0) {
|
|
363
|
+
throw new Error("The synthetic SCL frequency must be a positive number");
|
|
364
|
+
}
|
|
365
|
+
if (noiseV < 0 || !Number.isFinite(noiseV)) {
|
|
366
|
+
throw new Error("The synthetic noise level must be a non-negative number");
|
|
367
|
+
}
|
|
368
|
+
if (vHigh <= vLow) throw new Error("The synthetic logic high must exceed the logic low");
|
|
369
|
+
const half = Math.max(2, Math.round(sampleRateHz / sclHz / 2));
|
|
370
|
+
const idleBits = Math.max(1, Math.round(options.idleBits ?? 8));
|
|
371
|
+
const levels = { scl: [], sda: [] };
|
|
372
|
+
const push = (count, scl2, sda2) => {
|
|
373
|
+
for (let index = 0; index < count; index += 1) {
|
|
374
|
+
levels.scl.push(scl2);
|
|
375
|
+
levels.sda.push(sda2);
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
const idle = () => push(idleBits * 2 * half, true, true);
|
|
379
|
+
const startCondition = () => {
|
|
380
|
+
push(half, false, true);
|
|
381
|
+
push(half, true, true);
|
|
382
|
+
push(half, true, false);
|
|
383
|
+
};
|
|
384
|
+
const stopCondition = () => {
|
|
385
|
+
push(half, false, false);
|
|
386
|
+
push(half, true, false);
|
|
387
|
+
push(half, true, true);
|
|
388
|
+
idle();
|
|
389
|
+
};
|
|
390
|
+
const pushBit = (bit) => {
|
|
391
|
+
push(half, false, bit);
|
|
392
|
+
push(half, true, bit);
|
|
393
|
+
};
|
|
394
|
+
const pushByte = (value, acknowledged) => {
|
|
395
|
+
for (let bit = 7; bit >= 0; bit -= 1) {
|
|
396
|
+
pushBit((value >> bit & 1) === 1);
|
|
397
|
+
}
|
|
398
|
+
const ackLevel = !acknowledged;
|
|
399
|
+
push(half, false, ackLevel);
|
|
400
|
+
push(half, true, ackLevel);
|
|
401
|
+
};
|
|
402
|
+
idle();
|
|
403
|
+
options.transactions.forEach((transaction, transactionIndex) => {
|
|
404
|
+
if (!Number.isInteger(transaction.addr) || transaction.addr < 0 || transaction.addr > 127) {
|
|
405
|
+
throw new Error(`Transaction ${transactionIndex}: address must be a 7-bit integer`);
|
|
406
|
+
}
|
|
407
|
+
for (const byte of transaction.bytes) {
|
|
408
|
+
if (!Number.isInteger(byte) || byte < 0 || byte > 255) {
|
|
409
|
+
throw new Error(`Transaction ${transactionIndex}: data bytes must be integers 0..255`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (transaction.repeatedStart && transactionIndex === 0) {
|
|
413
|
+
throw new Error("The first transaction cannot begin with a repeated START");
|
|
414
|
+
}
|
|
415
|
+
startCondition();
|
|
416
|
+
const addressByte = transaction.addr << 1 | (transaction.dir === "read" ? 1 : 0);
|
|
417
|
+
pushByte(addressByte, transaction.addressAcked ?? true);
|
|
418
|
+
transaction.bytes.forEach((byte, byteIndex) => {
|
|
419
|
+
const isLast = byteIndex === transaction.bytes.length - 1;
|
|
420
|
+
const defaultAck = transaction.dir === "read" ? !isLast : true;
|
|
421
|
+
const acknowledged = transaction.nackIndex === byteIndex ? false : defaultAck;
|
|
422
|
+
pushByte(byte, acknowledged);
|
|
423
|
+
});
|
|
424
|
+
if (transaction.stop !== false) stopCondition();
|
|
425
|
+
});
|
|
426
|
+
const dt = 1 / sampleRateHz;
|
|
427
|
+
const random = mulberry32(options.seed ?? 24301);
|
|
428
|
+
const scl = new Float32Array(levels.scl.length);
|
|
429
|
+
const sda = new Float32Array(levels.sda.length);
|
|
430
|
+
for (let index = 0; index < levels.scl.length; index += 1) {
|
|
431
|
+
const noise = () => (random() * 2 - 1) * noiseV;
|
|
432
|
+
scl[index] = (levels.scl[index] ? vHigh : vLow) + noise();
|
|
433
|
+
sda[index] = (levels.sda[index] ? vHigh : vLow) + noise();
|
|
434
|
+
}
|
|
435
|
+
return { scl, sda, dt, sampleRateHz };
|
|
436
|
+
}
|
|
437
|
+
function synthI2cFrame(options, meta = {}) {
|
|
438
|
+
const result = synthI2c(options);
|
|
439
|
+
const capturedAt = meta.capturedAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
440
|
+
return fromWaveforms(
|
|
441
|
+
[
|
|
442
|
+
{
|
|
443
|
+
channel: "SCL",
|
|
444
|
+
capturedAt,
|
|
445
|
+
xIncrement: result.dt,
|
|
446
|
+
xZero: 0,
|
|
447
|
+
pointOffset: 0,
|
|
448
|
+
yIncrement: 1,
|
|
449
|
+
yZero: 0,
|
|
450
|
+
yOffset: 0,
|
|
451
|
+
samples: result.scl
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
channel: "SDA",
|
|
455
|
+
capturedAt,
|
|
456
|
+
xIncrement: result.dt,
|
|
457
|
+
xZero: 0,
|
|
458
|
+
pointOffset: 0,
|
|
459
|
+
yIncrement: 1,
|
|
460
|
+
yZero: 0,
|
|
461
|
+
yOffset: 0,
|
|
462
|
+
samples: result.sda
|
|
463
|
+
}
|
|
464
|
+
],
|
|
465
|
+
{ node: "testing.synth", port: "out" },
|
|
466
|
+
meta.streamId ?? "synth-i2c",
|
|
467
|
+
{ seq: meta.seq ?? 0 }
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
function mulberry32(seed) {
|
|
471
|
+
let state = seed >>> 0;
|
|
472
|
+
return () => {
|
|
473
|
+
state = state + 1831565813 >>> 0;
|
|
474
|
+
let value = state;
|
|
475
|
+
value = Math.imul(value ^ value >>> 15, value | 1);
|
|
476
|
+
value ^= value + Math.imul(value ^ value >>> 7, value | 61);
|
|
477
|
+
return ((value ^ value >>> 14) >>> 0) / 4294967296;
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
export {
|
|
481
|
+
frameLength,
|
|
482
|
+
fromWaveforms,
|
|
483
|
+
segmentIndexAt,
|
|
484
|
+
segmentOf,
|
|
485
|
+
sliceSegment,
|
|
486
|
+
synthI2c,
|
|
487
|
+
synthI2cFrame,
|
|
488
|
+
timeAt,
|
|
489
|
+
toWaveforms,
|
|
490
|
+
validateFrame,
|
|
491
|
+
validateTimeAxis
|
|
492
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@stapfel/stabench-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@stapfel/stabench-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Frame and time-axis types plus waveform adapters shared by StaBench packages",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
23
|
+
"test": "tsx --test src/core.test.ts"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^22.0.0",
|
|
30
|
+
"tsup": "^8.5.0",
|
|
31
|
+
"tsx": "^4.19.0",
|
|
32
|
+
"typescript": "^5.7.0"
|
|
33
|
+
}
|
|
34
|
+
}
|