Skip to content

Commit b6d72fd

Browse files
committed
Improves virtual device data generation
Refactors the virtual device to simulate data generation, allowing it to function independently of physical serial port connections. This change also fixes an issue where the deviceConnected status was not being correctly updated when using a virtual device.
1 parent 27738f3 commit b6d72fd

4 files changed

Lines changed: 145 additions & 63 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "../node_modules/@nordicsemiconductor/pc-nrfconnect-shared"
5+
},
6+
{
7+
"path": ".."
8+
}
9+
],
10+
"settings": {}
11+
}

src/components/SidePanel/SidePanel.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
clearConfirmBeforeClose,
1313
Group,
1414
selectedDevice,
15+
selectedVirtualDevice,
1516
SidePanel,
1617
Spinner,
1718
} from '@nordicsemiconductor/pc-nrfconnect-shared';
@@ -61,7 +62,8 @@ const useConfirmBeforeClose = () => {
6162

6263
export default () => {
6364
const dispatch = useDispatch();
64-
const deviceConnected = useSelector(selectedDevice);
65+
const deviceConnected =
66+
useSelector(selectedDevice) || useSelector(selectedVirtualDevice);
6567
const deviceOpen = useSelector(deviceOpenSelector);
6668
const fileLoaded = useSelector(isFileLoaded);
6769
const sessionActive = useSelector(isSessionActive);

src/device/serialDevice.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ class SerialDevice extends Device {
8787
samples: 3,
8888
};
8989
this.path = device.serialPorts?.at(0)?.comName;
90-
this.child = fork(
91-
path.resolve(getAppDir(), 'worker', 'virtualDevice.js')
92-
// path.resolve(getAppDir(), 'worker', 'serialDevice.js')
93-
);
90+
this.child = !device.serialPorts
91+
? fork(path.resolve(getAppDir(), 'worker', 'virtualDevice.js'))
92+
: fork(path.resolve(getAppDir(), 'worker', 'serialDevice.js'));
93+
9494
this.parser = null;
9595
this.resetDataLossCounter();
9696

worker/virtualDevice.js

Lines changed: 127 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,135 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
55
*/
66

7+
/* eslint-disable no-bitwise */
8+
79
// 用于替代 ppk2 硬件向上层注入采样数据的虚拟设备
810

9-
const dataMock = () => Buffer.alloc(9999, 'a');
11+
const CBITS = (val, mask, pos) => (val & mask) << pos;
12+
13+
const generateData = (a, r, c, l) =>
14+
CBITS(a, 0x3fff, 0) |
15+
CBITS(r, 0x7, 14) |
16+
CBITS(c, 0x3f, 18) |
17+
CBITS(l, 0xff, 24);
18+
19+
let tmpIndex = 0;
20+
const sampRate = 100_000; // Hz
21+
const sendIntv = 30; // ms
22+
23+
let timeStamp = 0;
24+
const dataType = 0; // 0: raw, 1: processed
25+
26+
const dataMockRaw = () => {
27+
const now = Date.now();
28+
// How many Samples to send
29+
const trunks = Math.floor(sampRate * ((now - timeStamp) / 1000));
30+
const dataSize = trunks * 4; // bytes
31+
32+
const data = Buffer.alloc(dataSize);
33+
34+
for (let i = 0; i < trunks; i += 1) {
35+
const adc =
36+
0x200 +
37+
Math.round(0x1ff * Math.sin((2 * Math.PI * tmpIndex) / sampRate));
38+
const range = 3;
39+
const counter = tmpIndex & 0x3f;
40+
const logic = tmpIndex % 2;
41+
42+
tmpIndex += 1;
43+
44+
data.writeUInt32LE(generateData(adc, range, counter, logic), i * 4);
45+
}
46+
47+
timeStamp = now;
48+
49+
return data;
50+
};
51+
52+
const dataMockProcessed = () => {
53+
const now = Date.now();
54+
const trunks = Math.floor(sampRate * ((now - timeStamp) / 1000));
55+
const dataSize = trunks * 10; // 8bytes double + 2bytes uint16
56+
57+
const data = Buffer.alloc(dataSize);
58+
59+
for (let i = 0; i < trunks; i += 1) {
60+
const adc = Math.round(
61+
(1 + Math.sin((2 * Math.PI * tmpIndex) / sampRate)) * 1e6
62+
);
63+
const logic = 0x1;
64+
65+
tmpIndex += 1;
66+
67+
data.writeDoubleLE(adc, i * 10);
68+
data.writeUInt16LE(logic, i * 10 + 8);
69+
}
70+
71+
timeStamp = now;
72+
73+
return data;
74+
};
1075

1176
const meta = [
12-
32, 49, 48, 48, 54, 46, 54, 52, 56, 57, 56, 54, 56, 49, 54, 52, 48, 54, 50,
13-
48, 48, 48, 48, 48, 48, 48, 10, 82, 49, 58, 32, 49, 48, 49, 46, 57, 54, 51,
14-
54, 50, 51, 48, 52, 54, 56, 55, 53, 48, 48, 48, 48, 48, 48, 48, 48, 10, 82,
15-
50, 58, 32, 49, 48, 46, 51, 53, 56, 56, 49, 55, 49, 48, 48, 53, 50, 52, 57,
16-
48, 50, 48, 48, 48, 48, 48, 10, 82, 51, 58, 32, 48, 46, 57, 55, 51, 50, 56,
17-
51, 48, 53, 50, 52, 52, 52, 52, 53, 56, 48, 48, 48, 48, 48, 10, 82, 52, 58,
18-
32, 48, 46, 48, 53, 55, 48, 55, 52, 50, 52, 56, 55, 57, 48, 55, 52, 48, 57,
19-
55, 48, 48, 48, 10, 71, 83, 48, 58, 32, 48, 46, 48, 48, 48, 48, 48, 48, 48,
20-
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, 48, 10, 71, 83, 49, 58, 32,
21-
49, 50, 57, 46, 54, 55, 51, 57, 49, 57, 54, 55, 55, 55, 51, 52, 51, 56, 48,
22-
48, 48, 48, 48, 48, 10, 71, 83, 50, 58, 32, 49, 57, 46, 54, 57, 57, 49, 51,
23-
50, 57, 49, 57, 51, 49, 49, 53, 50, 51, 48, 48, 48, 48, 48, 10, 71, 83, 51,
24-
58, 32, 50, 46, 55, 52, 49, 56, 55, 51, 55, 52, 49, 49, 52, 57, 57, 48, 50,
25-
51, 48, 48, 48, 48, 10, 71, 83, 52, 58, 32, 48, 46, 48, 55, 56, 56, 49, 50,
26-
54, 50, 49, 53, 51, 51, 56, 55, 48, 55, 48, 48, 48, 48, 10, 71, 73, 48, 58,
77+
32, 82, 48, 58, 32, 49, 48, 48, 54, 46, 54, 52, 56, 57, 56, 54, 56, 49, 54,
78+
52, 48, 54, 50, 48, 48, 48, 48, 48, 48, 48, 10, 82, 49, 58, 32, 49, 48, 49,
79+
46, 57, 54, 51, 54, 50, 51, 48, 52, 54, 56, 55, 53, 48, 48, 48, 48, 48, 48,
80+
48, 48, 10, 82, 50, 58, 32, 49, 48, 46, 51, 53, 56, 56, 49, 55, 49, 48, 48,
81+
53, 50, 52, 57, 48, 50, 48, 48, 48, 48, 48, 10, 82, 51, 58, 32, 48, 46, 57,
82+
55, 51, 50, 56, 51, 48, 53, 50, 52, 52, 52, 52, 53, 56, 48, 48, 48, 48, 48,
83+
10, 82, 52, 58, 32, 48, 46, 48, 53, 55, 48, 55, 52, 50, 52, 56, 55, 57, 48,
84+
55, 52, 48, 57, 55, 48, 48, 48, 10, 71, 83, 48, 58, 32, 48, 46, 48, 48, 48,
85+
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, 48, 10, 71,
86+
83, 49, 58, 32, 49, 50, 57, 46, 54, 55, 51, 57, 49, 57, 54, 55, 55, 55, 51,
87+
52, 51, 56, 48, 48, 48, 48, 48, 48, 10, 71, 83, 50, 58, 32, 49, 57, 46, 54,
88+
57, 57, 49, 51, 50, 57, 49, 57, 51, 49, 49, 53, 50, 51, 48, 48, 48, 48, 48,
89+
10, 71, 83, 51, 58, 32, 50, 46, 55, 52, 49, 56, 55, 51, 55, 52, 49, 49, 52,
90+
57, 57, 48, 50, 51, 48, 48, 48, 48, 10, 71, 83, 52, 58, 32, 48, 46, 48, 55,
91+
56, 56, 49, 50, 54, 50, 49, 53, 51, 51, 56, 55, 48, 55, 48, 48, 48, 48, 10,
92+
71, 73, 48, 58, 32, 49, 46, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
93+
48, 48, 48, 48, 48, 48, 48, 48, 10, 71, 73, 49, 58, 32, 48, 46, 57, 54, 52,
94+
49, 51, 48, 54, 57, 57, 54, 51, 52, 53, 53, 50, 48, 48, 48, 48, 48, 10, 71,
95+
73, 50, 58, 32, 48, 46, 57, 53, 57, 52, 57, 49, 52, 51, 49, 55, 49, 51, 49,
96+
48, 52, 50, 48, 48, 48, 48, 10, 71, 73, 51, 58, 32, 48, 46, 57, 52, 55, 52,
97+
57, 55, 56, 52, 52, 54, 57, 54, 48, 52, 52, 57, 48, 48, 48, 48, 10, 71, 73,
98+
52, 58, 32, 48, 46, 57, 53, 54, 53, 53, 54, 52, 51, 57, 51, 57, 57, 55, 49,
99+
57, 50, 48, 48, 48, 48, 10, 79, 48, 58, 32, 49, 50, 54, 46, 53, 54, 54, 48,
100+
49, 55, 49, 53, 48, 56, 55, 56, 57, 48, 48, 48, 48, 48, 48, 48, 10, 79, 49,
101+
58, 32, 56, 54, 46, 54, 50, 48, 54, 48, 53, 52, 54, 56, 55, 53, 48, 48, 48,
102+
48, 48, 48, 48, 48, 48, 10, 79, 50, 58, 32, 55, 51, 46, 57, 57, 55, 48, 52,
103+
55, 52, 50, 52, 51, 49, 54, 52, 48, 48, 48, 48, 48, 48, 48, 10, 79, 51, 58,
104+
32, 53, 55, 46, 55, 53, 57, 55, 55, 55, 48, 54, 57, 48, 57, 49, 56, 48, 48,
105+
48, 48, 48, 48, 48, 10, 79, 52, 58, 32, 49, 48, 48, 46, 51, 50, 54, 52, 53,
106+
52, 49, 54, 50, 53, 57, 55, 54, 54, 48, 48, 48, 48, 48, 48, 10, 86, 68, 68,
107+
58, 32, 51, 51, 48, 48, 10, 72, 87, 58, 32, 51, 53, 56, 55, 10, 109, 111,
108+
100, 101, 58, 32, 50, 10, 83, 48, 58, 32, 45, 48, 46, 48, 48, 48, 48, 48,
109+
48, 48, 49, 55, 54, 49, 50, 57, 53, 49, 48, 57, 55, 53, 57, 10, 83, 49, 58,
110+
32, 45, 48, 46, 48, 48, 48, 48, 48, 48, 48, 56, 50, 57, 54, 54, 56, 53, 54,
111+
52, 57, 54, 49, 49, 10, 83, 50, 58, 32, 45, 48, 46, 48, 48, 48, 48, 48, 48,
112+
51, 50, 57, 48, 51, 53, 55, 49, 52, 50, 51, 48, 52, 57, 10, 83, 51, 58, 32,
113+
45, 48, 46, 48, 48, 48, 48, 48, 51, 52, 53, 48, 53, 54, 54, 57, 54, 52, 54,
114+
48, 50, 51, 51, 10, 83, 52, 58, 32, 48, 46, 48, 48, 50, 51, 56, 55, 48, 49,
115+
53, 49, 52, 56, 57, 57, 55, 51, 48, 55, 48, 48, 10, 73, 48, 58, 32, 48, 46,
116+
48, 48, 48, 48, 48, 48, 48, 54, 56, 53, 50, 56, 49, 56, 48, 52, 54, 51, 53,
117+
53, 10, 73, 49, 58, 32, 48, 46, 48, 48, 48, 48, 48, 48, 49, 51, 50, 53, 50,
118+
49, 53, 55, 55, 55, 49, 49, 52, 57, 10, 73, 50, 58, 32, 48, 46, 48, 48, 48,
119+
48, 53, 55, 54, 51, 57, 54, 55, 48, 53, 53, 56, 50, 55, 51, 55, 57, 10, 73,
120+
51, 58, 32, 45, 48, 46, 48, 48, 48, 50, 51, 51, 54, 50, 49, 52, 56, 50, 48,
121+
51, 56, 56, 55, 48, 52, 53, 10, 73, 52, 58, 32, 45, 48, 46, 48, 48, 55, 57,
122+
54, 57, 55, 57, 50, 57, 51, 50, 50, 55, 49, 57, 53, 55, 48, 48, 10, 85, 71,
123+
48, 58, 32, 49, 46, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
124+
48, 48, 48, 48, 48, 48, 10, 85, 71, 49, 58, 32, 49, 46, 48, 48, 48, 48, 48,
125+
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 10, 85, 71, 50,
126+
58, 32, 49, 46, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
127+
48, 48, 48, 48, 48, 10, 85, 71, 51, 58, 32, 49, 46, 48, 48, 48, 48, 48, 48,
128+
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 10, 85, 71, 52, 58,
27129
32, 49, 46, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
28-
48, 48, 48, 48, 10, 71, 73, 49, 58, 32, 48, 46, 57, 54, 52, 49, 51, 48, 54,
29-
57, 57, 54, 51, 52, 53, 53, 50, 48, 48, 48, 48, 48, 10, 71, 73, 50, 58, 32,
30-
48, 46, 57, 53, 57, 52, 57, 49, 52, 51, 49, 55, 49, 51, 49, 48, 52, 50, 48,
31-
48, 48, 48, 10, 71, 73, 51, 58, 32, 48, 46, 57, 52, 55, 52, 57, 55, 56, 52,
32-
52, 54, 57, 54, 48, 52, 52, 57, 48, 48, 48, 48, 10, 71, 73, 52, 58, 32, 48,
33-
46, 57, 53, 54, 53, 53, 54, 52, 51, 57, 51, 57, 57, 55, 49, 57, 50, 48, 48,
34-
48, 48, 10, 79, 48, 58, 32, 49, 50, 54, 46, 53, 54, 54, 48, 49, 55, 49, 53,
35-
48, 56, 55, 56, 57, 48, 48, 48, 48, 48, 48, 48, 10, 79, 49, 58, 32, 56, 54,
36-
46, 54, 50, 48, 54, 48, 53, 52, 54, 56, 55, 53, 48, 48, 48, 48, 48, 48, 48,
37-
48, 48, 10, 79, 50, 58, 32, 55, 51, 46, 57, 57, 55, 48, 52, 55, 52, 50, 52,
38-
51, 49, 54, 52, 48, 48, 48, 48, 48, 48, 48, 10, 79, 51, 58, 32, 53, 55, 46,
39-
55, 53, 57, 55, 55, 55, 48, 54, 57, 48, 57, 49, 56, 48, 48, 48, 48, 48, 48,
40-
48, 10, 79, 52, 58, 32, 49, 48, 48, 46, 51, 50, 54, 52, 53, 52, 49, 54, 50,
41-
53, 57, 55, 54, 54, 48, 48, 48, 48, 48, 48, 10, 86, 68, 68, 58, 32, 51, 51,
42-
48, 48, 10, 72, 87, 58, 32, 51, 53, 56, 55, 10, 109, 111, 100, 101, 58, 32,
43-
50, 10, 83, 48, 58, 32, 45, 48, 46, 48, 48, 48, 48, 48, 48, 48, 49, 55, 54,
44-
49, 50, 57, 53, 49, 48, 57, 55, 53, 57, 10, 83, 49, 58, 32, 45, 48, 46, 48,
45-
48, 48, 48, 48, 48, 48, 56, 50, 57, 54, 54, 56, 53, 54, 52, 57, 54, 49, 49,
46-
10, 83, 50, 58, 32, 45, 48, 46, 48, 48, 48, 48, 48, 48, 51, 50, 57, 48, 51,
47-
53, 55, 49, 52, 50, 51, 48, 52, 57, 10, 83, 51, 58, 32, 45, 48, 46, 48, 48,
48-
48, 48, 48, 51, 52, 53, 48, 53, 54, 54, 57, 54, 52, 54, 48, 50, 51, 51, 10,
49-
83, 52, 58, 32, 48, 46, 48, 48, 50, 51, 56, 55, 48, 49, 53, 49, 52, 56, 57,
50-
57, 55, 51, 48, 55, 48, 48, 10, 73, 48, 58, 32, 48, 46, 48, 48, 48, 48, 48,
51-
48, 48, 54, 56, 53, 50, 56, 49, 56, 48, 52, 54, 51, 53, 53, 10, 73, 49, 58,
52-
32, 48, 46, 48, 48, 48, 48, 48, 48, 49, 51, 50, 53, 50, 49, 53, 55, 55, 55,
53-
49, 49, 52, 57, 10, 73, 50, 58, 32, 48, 46, 48, 48, 48, 48, 53, 55, 54, 51,
54-
57, 54, 55, 48, 53, 53, 56, 50, 55, 51, 55, 57, 10, 73, 51, 58, 32, 45, 48,
55-
46, 48, 48, 48, 50, 51, 51, 54, 50, 49, 52, 56, 50, 48, 51, 56, 56, 55, 48,
56-
52, 53, 10, 73, 52, 58, 32, 45, 48, 46, 48, 48, 55, 57, 54, 57, 55, 57, 50,
57-
57, 51, 50, 50, 55, 49, 57, 53, 55, 48, 48, 10, 85, 71, 48, 58, 32, 49, 46,
58-
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
59-
48, 10, 85, 71, 49, 58, 32, 49, 46, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
60-
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 10, 85, 71, 50, 58, 32, 49, 46, 48,
61-
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
62-
10, 85, 71, 51, 58, 32, 49, 46, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
63-
48, 48, 48, 48, 48, 48, 48, 48, 48, 10, 85, 71, 52, 58, 32, 49, 46, 48, 48,
64-
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 10,
65-
73, 65, 58, 32, 53, 56, 10, 69, 78, 68, 10,
130+
48, 48, 48, 48, 10, 73, 65, 58, 32, 53, 56, 10, 69, 78, 68, 10,
66131
];
67132

68-
process.on('message', msg => {
69-
let handle = null;
133+
let handle = null;
70134

135+
process.on('message', msg => {
71136
if (msg.open) {
72137
console.log('\x1b[2J'); // ansi clear screen
73138
process.send({ opening: msg.open });
@@ -85,15 +150,19 @@ process.on('message', msg => {
85150
clearInterval(handle);
86151
}
87152
handle = setInterval(() => {
88-
const data = dataMock();
153+
const data =
154+
dataType === 0 ? dataMockRaw() : dataMockProcessed();
89155
process.send(data.slice(), err => {
90156
if (err) console.log(err);
91157
});
92-
}, 30);
158+
}, sendIntv);
159+
timeStamp = Date.now();
160+
tmpIndex = Math.random() * sampRate; // random offset
93161
break;
94162
case 7: // stop
95163
clearInterval(handle);
96164
handle = null;
165+
tmpIndex = 0;
97166
break;
98167
case 12: // power on
99168
case 13: // set power mode

0 commit comments

Comments
 (0)