From 7caa0eb117142fa3da11a7efac63a84fc40ce2e5 Mon Sep 17 00:00:00 2001 From: Icedsoda <29167146+1cedsoda@users.noreply.github.com> Date: Thu, 15 May 2025 20:44:15 +0200 Subject: [PATCH] Fix Spool Speed --- electron/src/machines/winder/Spool.tsx | 23 ++++++++----------- .../winder/winder2/Winder2Settings.tsx | 10 ++++---- server/src/machines/winder2/new.rs | 10 ++++---- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/electron/src/machines/winder/Spool.tsx b/electron/src/machines/winder/Spool.tsx index a8f46c31..fc16e9da 100644 --- a/electron/src/machines/winder/Spool.tsx +++ b/electron/src/machines/winder/Spool.tsx @@ -6,7 +6,7 @@ type Props = { export function Spool({ rpm }: Props) { const [rotationState, setRotationState] = React.useState({ - rotation: 0, + rotations: 0, lastRpm: undefined as number | undefined, lastRpmTime: undefined as number | undefined, }); @@ -14,28 +14,25 @@ export function Spool({ rpm }: Props) { useEffect(() => { if (rpm !== undefined) { const now = Date.now(); - setRotationState((prev) => { - const newState = { ...prev, lastRpm: rpm, lastRpmTime: now }; - - if (prev.lastRpmTime !== undefined && prev.lastRpm !== undefined) { - const deltaTime = now - prev.lastRpmTime; - const deltaRotation = (-rpm * deltaTime) / 360; - newState.rotation = prev.rotation + deltaRotation; - } - - return newState; + // Calculate the new rotation based on the RPM and time elapsed + const dt = (now - (rotationState.lastRpmTime || now)) / 1000; // Convert ms to seconds + const newRotations = ((rpm / 60) * dt) % 1; // Convert RPM to rotations per second + setRotationState({ + rotations: (rotationState.rotations + newRotations) % 1, // Keep it within 0-1 + lastRpm: rpm, + lastRpmTime: now, }); } }, [rpm]); - const { rotation } = rotationState; + const { rotations } = rotationState; return (