Skip to content

Commit d753999

Browse files
author
Ritika Mishra
committed
Merge branch 'reconnection' of https://github.com/Amanmahe/BioSignal-Recorder-Web into plotter-update
2 parents 03fcada + b76e95d commit d753999

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

src/app/serial-plotter/page.tsx

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ const SerialPlotter = () => {
3232
const [boardName, setBoardName] = useState<string | null>(null);
3333
const [viewMode, setViewMode] = useState<"monitor" | "plotter" | "both">("both");
3434
const baudRateref = useRef<number>(115200);
35-
35+
const bitsref = useRef<number>(10);
36+
const channelsref = useRef<number>(1);
37+
const sweepPositions = useRef<number[]>(new Array(channelsref.current).fill(0)); // Array for sweep positions
3638

3739
useEffect(() => {
3840
if (rawDataRef.current) {
@@ -89,7 +91,6 @@ const SerialPlotter = () => {
8991

9092
// Re-plot existing data
9193
updateWebGLPlot(data); // Ensure existing data is plotted
92-
9394
wglp.update();
9495
} else {
9596
wglpRef.current = null; // Reset the WebGL plot reference when hiding
@@ -121,6 +122,14 @@ const SerialPlotter = () => {
121122
linesRef.current = [];
122123
selectedChannelsRef.current = [];
123124
readSerialData(selectedPort);
125+
126+
127+
setTimeout(() => {
128+
sweepPositions.current = new Array(6).fill(0);
129+
130+
}, 6000);
131+
132+
124133
} catch (err) {
125134
console.error("Error connecting to serial:", err);
126135
}
@@ -171,12 +180,13 @@ const SerialPlotter = () => {
171180
const values = line.trim().split(/\s+/).map(parseFloat).filter((v) => !isNaN(v));
172181
if (values.length > 0) {
173182
newData.push({ time: Date.now(), values });
174-
183+
channelsref.current = values.length;
175184
// ✅ Ensure `selectedChannels` updates before plotting
176185
setSelectedChannels((prevChannels) => {
177186
if (prevChannels.length !== values.length) {
178187
return Array.from({ length: values.length }, (_, i) => i);
179188
}
189+
180190
return prevChannels;
181191
});
182192
}
@@ -233,25 +243,50 @@ const SerialPlotter = () => {
233243

234244
const updateWebGLPlot = (newData: DataPoint[]) => {
235245
if (!wglpRef.current || linesRef.current.length === 0 || newData.length === 0) return;
236-
237246
// Calculate Y-axis min and max values
238247
const yMin = Math.min(...newData.flatMap(dp => dp.values));
239248
const yMax = Math.max(...newData.flatMap(dp => dp.values));
240249
const yRange = yMax - yMin || 1; // Avoid division by zero
241250

242251
// Iterate over new data points and update plots
243252
newData.forEach((dataPoint) => {
244-
selectedChannels.forEach((index) => {
245-
if (index >= dataPoint.values.length) return; // Prevent out-of-bounds errors
253+
linesRef.current.forEach((line, i) => {
246254

247-
// Clamp Y-value to be within -1 and 1
248-
const yValue = Math.max(-1, Math.min(1, ((dataPoint.values[index] - yMin) / yRange) * 2 - 1));
255+
if (i >= dataPoint.values.length) return; // Prevent out-of-bounds errors
256+
257+
// Clamp Y-value to be within -1 and 1
258+
const yValue = Math.max(-1, Math.min(1, ((dataPoint.values[i] - yMin) / yRange) * 2 - 1));
249259

250260
// Update combined plot
251-
const combinedLine = linesRef.current[index];
252-
if (combinedLine) {
253-
combinedLine.shiftAdd(new Float32Array([yValue]));
261+
// Ensure sweepPositions.current[i] is initialized
262+
if (sweepPositions.current[i] === undefined) {
263+
sweepPositions.current[i] = 0;
264+
}
265+
266+
const currentPos = sweepPositions.current[i] % line.numPoints;
267+
if (Number.isNaN(currentPos)) {
268+
console.error(`Invalid currentPos at i ${i}. sweepPositions.current[i]:`, sweepPositions.current[i]);
269+
return;
270+
}
271+
272+
if (line) {
273+
try {
274+
line.setY(currentPos, yValue);
275+
} catch (error) {
276+
console.error(`Error plotting data for line ${i} at position ${currentPos}:`, error);
277+
}
278+
254279
}
280+
// Clear the next point for visual effect
281+
const clearPosition = Math.ceil((currentPos + maxPoints / 100) % line.numPoints);
282+
try {
283+
line.setY(clearPosition, NaN);
284+
} catch (error) {
285+
console.error(`Error clearing data at position ${clearPosition} for line ${i}:`, error);
286+
}
287+
288+
// Increment the sweep position
289+
sweepPositions.current[i] = (currentPos + 1) % line.numPoints;
255290
});
256291
});
257292

0 commit comments

Comments
 (0)