fix: total stitches from the machine are wrong, we need to use our own

This commit is contained in:
Jan-Henrik 2026-03-29 22:14:28 +02:00
parent 270a464742
commit 7431327c1c
2 changed files with 16 additions and 14 deletions

View file

@ -71,12 +71,12 @@ export function ProgressMonitor() {
const isMaskTraceComplete =
machineStatus === MachineStatus.MASK_TRACE_COMPLETE;
// Use PEN stitch count as fallback when machine reports 0 total stitches
const totalStitches = patternInfo
? patternInfo.totalStitches === 0 && displayPattern?.penStitches
? displayPattern.penStitches.stitches.length
: patternInfo.totalStitches
: 0;
// Use our own PEN stitch count as the source of truth for total stitches.
// The machine's patternInfo.totalStitches uses a different counting method than
// currentStitch (e.g. excludes lock stitches), so it can't be used as the max.
const totalStitches = displayPattern?.penStitches
? displayPattern.penStitches.stitches.length
: (patternInfo?.totalStitches ?? 0);
// Use adjustedStitchIndex (from step control) when available, otherwise machine-reported
const currentStitch =
@ -156,8 +156,10 @@ export function ProgressMonitor() {
totalStitches={totalStitches}
lastRolledBackError={lastRolledBackError}
colorBlocks={colorBlocks}
onAdjustPosition={adjustStitchPosition}
onSetPosition={setStitchPosition}
onAdjustPosition={(offset) =>
adjustStitchPosition(offset, totalStitches)
}
onSetPosition={(index) => setStitchPosition(index, totalStitches)}
/>
)}

View file

@ -65,8 +65,8 @@ interface MachineState {
startSewing: () => Promise<void>;
resumeSewing: () => Promise<void>;
deletePattern: () => Promise<void>;
setStitchPosition: (index: number) => Promise<void>;
adjustStitchPosition: (offset: number) => Promise<void>;
setStitchPosition: (index: number, maxStitches?: number) => Promise<void>;
adjustStitchPosition: (offset: number, maxStitches?: number) => Promise<void>;
// Initialization
initialize: () => void;
@ -338,11 +338,11 @@ export const useMachineStore = create<MachineState>((set, get) => ({
},
// Set stitch position to an absolute index
setStitchPosition: async (index: number) => {
setStitchPosition: async (index: number, maxStitches?: number) => {
const { isConnected, service, patternInfo } = get();
if (!isConnected) return;
const totalStitches = patternInfo?.totalStitches || 0;
const totalStitches = maxStitches ?? patternInfo?.totalStitches ?? 0;
const clamped = Math.max(0, Math.min(index, totalStitches));
try {
@ -359,11 +359,11 @@ export const useMachineStore = create<MachineState>((set, get) => ({
},
// Adjust stitch position by a relative offset
adjustStitchPosition: async (offset: number) => {
adjustStitchPosition: async (offset: number, maxStitches?: number) => {
const { sewingProgress, adjustedStitchIndex } = get();
const currentIndex =
adjustedStitchIndex ?? sewingProgress?.currentStitch ?? 0;
await get().setStitchPosition(currentIndex + offset);
await get().setStitchPosition(currentIndex + offset, maxStitches);
},
// Handle automatic stitch rollback for thread errors