Skip to content

Commit bb4c308

Browse files
author
Ritika Mishra
committed
ensure canvases use correct data point count when timeBase changes
1 parent 31c3c52 commit bb4c308

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/app/muscle-strength/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { useTheme } from "next-themes";
3636

3737
const MuscleStrength = () => {
3838
const [isDisplay, setIsDisplay] = useState<boolean>(true); // Display state
39-
const sampingrateref = useRef<number>(250);
39+
const samplingrateref = useRef<number>(250);
4040
const [isFilterPopoverOpen, setIsFilterPopoverOpen] = useState(false);
4141
const connectedDeviceRef = useRef<any | null>(null); // UseRef for device tracking
4242
const canvasContainerRef = useRef<HTMLDivElement>(null);
@@ -644,7 +644,7 @@ const MuscleStrength = () => {
644644
forceUpdate(); // Trigger re-render
645645
};
646646
useEffect(() => {
647-
dataPointCountRef.current = (sampingrateref.current * timeBase);
647+
dataPointCountRef.current = (samplingrateref.current * timeBase);
648648
}, [timeBase]);
649649
const zoomRef = useRef(Zoom);
650650

@@ -675,10 +675,10 @@ const MuscleStrength = () => {
675675
);
676676

677677
notchFilters.forEach((filter) => {
678-
filter.setbits(sampingrateref.current);
678+
filter.setbits(samplingrateref.current);
679679
});
680680
EXGFilters.forEach((filter) => {
681-
filter.setbits("12", sampingrateref.current);
681+
filter.setbits("12", samplingrateref.current);
682682
});
683683
function processSample(dataView: DataView): void {
684684
if (dataView.byteLength !== SINGLE_SAMPLE_LEN) {

src/app/npg-lite/page.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const NPG_Ble = () => {
5656
const [recordingElapsedTime, setRecordingElapsedTime] = useState<number>(0); // State to store the recording duration
5757
const [customTimeInput, setCustomTimeInput] = useState<string>(""); // State to store the custom stop time input
5858
const existingRecordRef = useRef<any | undefined>(undefined);
59-
const sampingrateref = useRef<number>(500);
59+
const samplingrateref = useRef<number>(500);
6060
const recordingStartTimeRef = useRef<number>(0);
6161
const endTimeRef = useRef<number | null>(null); // Ref to store the end time of the recording
6262
const canvasElementCountRef = useRef<number>(1);
@@ -105,6 +105,10 @@ const NPG_Ble = () => {
105105
return; // Exit if the ref is null
106106
}
107107

108+
// Ensure dataPointCount is calculated from current sampling rate and timeBase
109+
const dpCount = samplingrateref.current * timeBase;
110+
dataPointCountRef.current = dpCount;
111+
108112
currentSweepPos.current = new Array(numChannels).fill(0);
109113
sweepPositions.current = new Array(numChannels).fill(0);
110114

@@ -137,7 +141,7 @@ const NPG_Ble = () => {
137141
const opacityDarkMinor = "0.05";
138142
const opacityLightMajor = "0.4";
139143
const opacityLightMinor = "0.1";
140-
const distanceminor = sampingrateref.current * 0.04;
144+
const distanceminor = samplingrateref.current * 0.04;
141145
const numGridLines = (500 * 4) / distanceminor;
142146

143147
for (let j = 1; j < numGridLines; j++) {
@@ -188,10 +192,10 @@ const NPG_Ble = () => {
188192
wglp.gScaleY = Zoom;
189193

190194

191-
const line = new WebglLine(getLineColor(channelNumber, theme), dataPointCountRef.current);
195+
const line = new WebglLine(getLineColor(channelNumber, theme), dpCount);
192196
wglp.gOffsetY = 0;
193197
line.offsetY = 0;
194-
line.lineSpaceX(-1, 2 / dataPointCountRef.current);
198+
line.lineSpaceX(-1, 2 / dpCount);
195199

196200
wglp.addLine(line);
197201
newLines.push(line);
@@ -314,7 +318,7 @@ const NPG_Ble = () => {
314318
forceUpdate(); // Trigger re-render
315319
};
316320
useEffect(() => {
317-
dataPointCountRef.current = (sampingrateref.current * timeBase);
321+
dataPointCountRef.current = (samplingrateref.current * timeBase);
318322
}, [timeBase]);
319323
const zoomRef = useRef(Zoom);
320324

@@ -338,13 +342,13 @@ const NPG_Ble = () => {
338342
const exgFiltersRef = useRef(Array.from({ length: maxCanvasElementCountRef.current }, () => new EXGFilter()));
339343
const pointoneFilterRef = useRef(Array.from({ length: maxCanvasElementCountRef.current }, () => new HighPassFilter()));
340344
notchFiltersRef.current.forEach((filter) => {
341-
filter.setbits(sampingrateref.current);
345+
filter.setbits(samplingrateref.current);
342346
});
343347
exgFiltersRef.current.forEach((filter) => {
344-
filter.setbits("12", sampingrateref.current);
348+
filter.setbits("12", samplingrateref.current);
345349
});
346350
pointoneFilterRef.current.forEach((filter) => {
347-
filter.setSamplingRate(sampingrateref.current);
351+
filter.setSamplingRate(samplingrateref.current);
348352
});
349353

350354
// Inside your component
@@ -1510,6 +1514,7 @@ const NPG_Ble = () => {
15101514
<div className="relative w-[28rem] flex items-center rounded-lg py-2 border border-gray-300 dark:border-gray-600">
15111515
{/* Button for setting Time Base to 1 */}
15121516
<button
1517+
type="button"
15131518
className="text-gray-700 dark:text-gray-400 mx-1 px-2 py-1 border rounded hover:bg-gray-200 dark:hover:bg-gray-700"
15141519
onClick={() => setTimeBase(1)}
15151520
>
@@ -1528,6 +1533,7 @@ const NPG_Ble = () => {
15281533
/>
15291534
{/* Button for setting Time Base to 10 */}
15301535
<button
1536+
type="button"
15311537
className="text-gray-700 dark:text-gray-400 mx-2 px-2 py-1 border rounded hover:bg-gray-200 dark:hover:bg-gray-700"
15321538
onClick={() => setTimeBase(10)}
15331539
>

src/components/Connection.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const Connection: React.FC<ConnectionProps> = ({
124124
const existingRecordRef = useRef<any | undefined>(undefined);
125125
const devicenameref = useRef<string>("");
126126
const [deviceReady, setDeviceReady] = useState(false);
127-
const sampingrateref = useRef<number>(0);
127+
const samplingrateref = useRef<number>(0);
128128
const [open, setOpen] = useState(false);
129129
const [openfft, setOpenfft] = useState(false);
130130
const [isPauseState, setIsPauseState] = useState(false);
@@ -358,7 +358,7 @@ const Connection: React.FC<ConnectionProps> = ({
358358
});
359359
}
360360
};
361-
const setCanvasCountInWorker = (canvasCount: number) => {
361+
const setCanvasCountInWorker = useCallback((canvasCount: number) => {
362362
if (!workerRef.current) {
363363
initializeWorker();
364364
}
@@ -370,13 +370,12 @@ const Connection: React.FC<ConnectionProps> = ({
370370

371371
// Send canvasCount independently to the worker
372372
workerRef.current?.postMessage({ action: 'setCanvasCount', canvasCount: canvasElementCountRef.current });
373-
};
373+
}, [selectedChannels, setCanvasCount]);
374374

375375
// Run the canvas count sync after render (and when selectedChannels change)
376376
useEffect(() => {
377377
setCanvasCountInWorker(canvasElementCountRef.current);
378-
// eslint-disable-next-line react-hooks/exhaustive-deps
379-
}, [selectedChannels.length]);
378+
}, [setCanvasCountInWorker]);
380379

381380
const setSelectedChannelsInWorker = (selectedChannels: number[]) => {
382381
if (!workerRef.current) {
@@ -576,7 +575,7 @@ const Connection: React.FC<ConnectionProps> = ({
576575

577576
if (sampling_rate) {
578577
setCurrentSamplingRate(sampling_rate);
579-
sampingrateref.current = sampling_rate;
578+
samplingrateref.current = sampling_rate;
580579
}
581580

582581
return {
@@ -1172,7 +1171,7 @@ const Connection: React.FC<ConnectionProps> = ({
11721171
maxCanvasElementCountRef.current = 3;
11731172
setSelectedChannel(1);
11741173
setCurrentSamplingRate(500);
1175-
sampingrateref.current = 500;
1174+
samplingrateref.current = 500;
11761175
setInterval(() => {
11771176
if (samplesReceivedRef.current === 0) {
11781177
disconnect();
@@ -1304,13 +1303,13 @@ const Connection: React.FC<ConnectionProps> = ({
13041303
const EXGFilters = Array.from({ length: maxCanvasElementCountRef.current }, () => new EXGFilter());
13051304
const pointoneFilter = Array.from({ length: maxCanvasElementCountRef.current }, () => new HighPassFilter());
13061305
notchFilters.forEach((filter) => {
1307-
filter.setbits(sampingrateref.current); // Set the bits value for all instances
1306+
filter.setbits(samplingrateref.current); // Set the bits value for all instances
13081307
});
13091308
pointoneFilter.forEach((filter) => {
1310-
filter.setSamplingRate(sampingrateref.current); // Set the bits value for all instances
1309+
filter.setSamplingRate(samplingrateref.current); // Set the bits value for all instances
13111310
});
13121311
EXGFilters.forEach((filter) => {
1313-
filter.setbits(detectedBitsRef.current.toString(), sampingrateref.current); // Set the bits value for all instances
1312+
filter.setbits(detectedBitsRef.current.toString(), samplingrateref.current); // Set the bits value for all instances
13141313
});
13151314
try {
13161315
while (isDeviceConnectedRef.current) {

0 commit comments

Comments
 (0)