Merge pull request #91 from jhbruhn/fix/totalstitches

fix: total stitches from the machine are wrong, we need to use our own
This commit is contained in:
Jan-Henrik Bruhn 2026-03-29 22:16:09 +02:00 committed by GitHub
commit dc4fb0221c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 14 deletions

View file

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

View file

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