From d7a2a204326ddfde5b257fc7350b015cafbc61b8 Mon Sep 17 00:00:00 2001 From: "Jim McCann (on silkmoth)" Date: Wed, 24 Apr 2024 16:51:29 -0400 Subject: [PATCH] use waste yarn from start and end tubes; clean up carrier in/out logic. Still untested. --- node_modules/autoknit-yarns.js | 188 +++++++++++++++++++-------------- 1 file changed, 111 insertions(+), 77 deletions(-) diff --git a/node_modules/autoknit-yarns.js b/node_modules/autoknit-yarns.js index f54c73a..6bbc10d 100644 --- a/node_modules/autoknit-yarns.js +++ b/node_modules/autoknit-yarns.js @@ -4,6 +4,7 @@ const fs = require('fs'); let StartRows = 10; let EndRows = 10; +let StartEndCarrier = "10"; let YarnInStitchNumber = 67; let CastOnStitchNumber = 67; @@ -65,12 +66,16 @@ Helpers.prototype.out = function out(str) { }; Helpers.prototype.write = function write() { + for (let cn of Object.keys(this.inYarns)) { + console.log(`Taking out yarn at end: ${cn}.`); + this.out(`outhook ${cn}`); + } if (OutFile !== "") { fs.writeFileSync(OutFile, this.knitout.join("\n") + "\n"); } }; -Helpers.prototype.start_tube = function end_tube(dir, bns, Carrier) { +function tubeNeedles(dir, bns) { let front = []; let back = []; bns.forEach(function(bn_str){ @@ -89,36 +94,6 @@ Helpers.prototype.start_tube = function end_tube(dir, bns, Carrier) { console.assert(front.length !== 0 && back.length !== 0, "should start a tube with at least a stitch on each bed."); - //do a tuck pattern to anchor yarn: - // v v v <-- - // v v --> - // ^------ first needle to be knit is here - let n = Math.max(front[front.length-1], back[back.length-1]); - let toDrop = []; - let me = this; - function initTuck(d, bn, c) { - me.tuck(d, bn, c); - toDrop.push(bn); - } - this.out("x-stitch-number " + YarnInStitchNumber); - if(!this.inYarns[Carrier]){ - this.out("inhook " + Carrier); - this.inYarns[Carrier] = true; - this.yarnUsedCount[Carrier] = 0; - - console.log("Bringing Carrier " + Carrier.toString() + " in."); - - } else { - console.warn("Bringing in carrier that was already in " + Carrier.toString()); - } - - initTuck('-', 'f' + n, Carrier); - initTuck('-', 'f' + (n-2), Carrier); - initTuck('-', 'f' + (n-4), Carrier); - initTuck('+', 'f' + (n-3), Carrier); - initTuck('+', 'f' + (n-1), Carrier); - this.out("releasehook " + Carrier); - //make list of needles and directions in tube order: let sts = []; if (dir === 'clockwise') { @@ -137,54 +112,107 @@ Helpers.prototype.start_tube = function end_tube(dir, bns, Carrier) { } } + return {front, back, sts}; +} + +Helpers.prototype.start_tube = function start_tube(dir, bns, Carrier) { + const {front, back, sts} = tubeNeedles(dir, bns); + + //---------------------------------------------------- + //start up a tube with a waste yarn carrier: //alternating tuck cast on: - this.out("x-stitch-number " + CastOnStitchNumber); + this.out(`inhook ${StartEndCarrier}`); + this.inYarns[StartEndCarrier] = true; + this.out(`x-stitch-number ${CastOnStitchNumber}`); sts.forEach(function(dbn, i) { - if (i%2 == 0) this.knit(dbn[0], dbn[1], Carrier); + if (i%2 == 0) this.knit(dbn[0], dbn[1], StartEndCarrier); }, this); sts.forEach(function(dbn, i) { - if (i%2 == 1) this.knit(dbn[0], dbn[1], Carrier); - }, this); - - //drop everything in 'toDrop' that wasn't part of alternating tucks: - toDrop.forEach(function(bn){ - //WARNING: this might actually drop **TOO MUCH** if the tucked needles overlap other existing stitches - let idx = bns.indexOf(bn); - if (idx === -1) { - this.drop(bn); - } + if (i%2 == 1) this.knit(dbn[0], dbn[1], StartEndCarrier); }, this); //knit some plain rows: - this.out("x-stitch-number " + PlainStitchNumber); + this.out(`x-stitch-number ${PlainStitchNumber}`); for (let row = 0; row < StartRows; ++row) { sts.forEach(function(dbn, i) { - this.knit(dbn[0], dbn[1], Carrier); + this.knit(dbn[0], dbn[1], StartEndCarrier); }, this); + if (row == 1) this.out(`releasehook ${StartEndCarrier}`); + } + this.out(`outhook ${StartEndCarrier}`); + delete this.inYarns[StartEndCarrier]; + //---------------------------------------------------- + + + let toDrop = []; + + { //bring in yarn and do tuck pattern: + let release = false; + + if(!this.inYarns[Carrier]){ + this.out("inhook " + Carrier); + this.inYarns[Carrier] = true; + this.yarnUsedCount[Carrier] = 0; + release = true; + + console.log("Bringing Carrier " + Carrier.toString() + " in."); + + } else { + console.warn("Bringing in carrier that was already in " + Carrier.toString()); + } + + this.out(`x-stitch-number ${YarnInStitchNumber}`); + + //do a tuck pattern to anchor yarn: + // v v v <-- + // v v --> + // ^------ first needle to be knit is here + const n = parseBedNeedle(bns[0]).needle; + const initTuck = (d, bn, cs) => { + this.tuck(d, bn, cs); + toDrop.push(bn); + }; + initTuck('-', 'f' + n, Carrier); + initTuck('-', 'f' + (n-2), Carrier); + initTuck('-', 'f' + (n-4), Carrier); + initTuck('+', 'f' + (n-3), Carrier); + initTuck('+', 'f' + (n-1), Carrier); + if (release) { + this.out("releasehook " + Carrier); + } } let first = 0; while (first < sts.length && sts[first][1] !== bns[0]) ++first; console.assert(first < sts.length, "First stitch from 'bns' should exist in 'sts'."); - //knit a bit extra to get aligned to the input bns: - for (let i = 0; i < first; ++i) { - let st = sts.shift(); - this.knit(st[0], st[1], Carrier); - sts.push(st); - } - - //alternating stitches to separate starting tube from knitting: - this.out("x-stitch-number " + CastOnStitchNumber); - sts.forEach(function(dbn, i) { - if (i%2 == 0) this.knit(dbn[0], dbn[1], Carrier); - }, this); - sts.forEach(function(dbn, i) { - if (i%2 == 1) this.knit(dbn[0], dbn[1], Carrier); - }, this); + //knit first row: + this.out(`x-stitch-number ${PlainStitchNumber}`); + bns.forEach((bn_str) => { + let d; + const bn = parseBedNeedle(bn_str) + if (bn.bed === 'f') { + d = (dir === 'clockwise' ? '-' : '+'); + } else if (bn.bed === 'b') { + d = (dir === 'clockwise' ? '+' : '-'); + } else { + console.assert("start_tube should only be called with 'f' or 'b' needles."); + } + this.knit(d, bn_str, Carrier); + }); - this.out("x-stitch-number " + PlainStitchNumber); + //drop any part of tuck pattern that needs dropping: + toDrop.forEach(function(bn){ + //WARNING: this might actually drop **TOO MUCH** if the tucked needles overlap other existing stitches + let idx = bns.indexOf(bn); + if (idx === -1) { + this.drop(bn); + console.log(`Dropping ${bn}`); //DEBUG + } else { + console.log(`Not dropping ${bn}`); //DEBUG + } + }, this); }; Helpers.prototype.bringIn = function bringIn(Carrier) { @@ -265,37 +293,43 @@ Helpers.prototype.end_tube = function end_tube(dir, bns, Carrier) { } } - //alternating stitches to separate ending tube from knitting: - this.out("x-stitch-number " + CastOnStitchNumber); - bns.forEach(function(bn, i) { - if (i%2 == 0) this.knit(d(bn), bn, Carrier); - }, this); + //final row of knitting: bns.forEach(function(bn, i) { - if (i%2 == 1) this.knit(d(bn), bn, Carrier); + this.knit(d(bn), bn, Carrier); }, this); - this.out("x-stitch-number " + PlainStitchNumber); - for (let row = 0; row < EndRows; ++row) { - bns.forEach(function(bn, i) { - this.knit(d(bn), bn, Carrier); - }, this); - } - + //take out the yarn: if(this.inYarns[Carrier]){ this.out("outhook " + Carrier); - this.inYarns[Carrier] = false; + delete this.inYarns[Carrier]; this.yarnUsedCount[Carrier] = 0; console.log("Taking Carrier " + Carrier.toString() + " out."); } else { console.warn("Taking out carrier that was never in : " + Carrier.toString()); } + //---------------------------------------- + //knit tube with a waste yarn carrier: + const {front, back, sts} = tubeNeedles(dir, bns); + + this.out(`inhook ${StartEndCarrier}`); + this.inYarns[StartEndCarrier] = true; + this.out(`x-stitch-number ${PlainStitchNumber}`); + for (let row = 0; row < EndRows; ++row) { + sts.forEach(function(dbn, i) { + this.knit(dbn[0], dbn[1], StartEndCarrier); + }, this); + if (row == 1) this.out(`releasehook ${StartEndCarrier}`); + } + this.out(`outhook ${StartEndCarrier}`); + delete this.inYarns[StartEndCarrier]; + //---------------------------------------- + + //drop the ending tube: bns.forEach(function(bn, i) { this.drop(bn); }, this); - - }; Helpers.prototype.xfer = function xfer(from, to) {