From be3f15eaceb483c0e28bb31f00417b4a20719c7f Mon Sep 17 00:00:00 2001 From: Jan-Henrik Bruhn Date: Sat, 6 Dec 2025 23:23:50 +0100 Subject: [PATCH] Fix TRIM command encoding in PES to PEN conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Correct the encoding of TRIM commands to use the proper bit flag (bit 1) instead of incorrectly treating them the same as JUMP commands (bit 0). The PEN format uses two separate flags in the Y-coordinate low byte: - Bit 0 (0x01): FEED_DATA - Jump stitch (move without stitching) - Bit 1 (0x02): CUT_DATA - Trim/cut thread command Previously, both JUMP and TRIM commands were encoded with bit 0, which prevented the machine from distinguishing between jumps and thread cuts. Changes: - Add PEN_CUT_DATA constant (0x02) for TRIM commands - Update encoding logic to set bit 0 for JUMP (cmd=1) - Update encoding logic to set bit 1 for TRIM (cmd=2) - Add detailed comments explaining the PEN format flags - Document that Y-coordinate flags can be combined - Document that X-coordinate flags are mutually exclusive This fix ensures thread cutting happens when intended, matching the behavior of the original C# implementation in the Asura app. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/utils/pystitchConverter.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/utils/pystitchConverter.ts b/src/utils/pystitchConverter.ts index 9a55338..8079060 100644 --- a/src/utils/pystitchConverter.ts +++ b/src/utils/pystitchConverter.ts @@ -1,9 +1,13 @@ import { pyodideLoader } from './pyodideLoader'; // PEN format flags -const PEN_FEED_DATA = 0x01; // Y-coordinate low byte, bit 0 (jump) -const PEN_COLOR_END = 0x03; // X-coordinate low byte, bits 0-2 -const PEN_DATA_END = 0x05; // X-coordinate low byte, bits 0-2 +// Y-coordinate low byte flags (can be combined) +const PEN_FEED_DATA = 0x01; // Bit 0: Jump stitch (move without stitching) +const PEN_CUT_DATA = 0x02; // Bit 1: Trim/cut thread command + +// X-coordinate low byte flags (bits 0-2, mutually exclusive) +const PEN_COLOR_END = 0x03; // Last stitch before color change +const PEN_DATA_END = 0x05; // Last stitch of entire pattern // Embroidery command constants (from pyembroidery) const MOVE = 0x10; @@ -153,10 +157,14 @@ for i, stitch in enumerate(pattern.stitches): let xEncoded = (absX << 3) & 0xFFFF; let yEncoded = (absY << 3) & 0xFFFF; - // Add jump flag if this is a JUMP (1) or TRIM (2) command + // Add command flags to Y-coordinate based on stitch type // PyStitch constants: STITCH=0, JUMP=1, TRIM=2 - if (cmd === 1 || cmd === 2) { + if (cmd === 1) { + // JUMP: Set bit 0 (FEED_DATA) - move without stitching yEncoded |= PEN_FEED_DATA; + } else if (cmd === 2) { + // TRIM: Set bit 1 (CUT_DATA) - cut thread command + yEncoded |= PEN_CUT_DATA; } // Check if this is the last stitch