#title: Cast-On Sampler (python with knitout module)
# Creates several sheets, all with different cast-ons.

import knitout
K = knitout.Writer('1 2 3 4 5 6');
K.addHeader('Machine', 'Kniterate');

#Parameters:
carrier = "3" #carrier name
min = 1 #needle number of left edge
max = 20 #needle number of right edge
rows = 20 #rows of knitting in each sample
cordWidth = 4 #cord width (for I-cord cast-on)

# === Alternating Tucks === 
#Bring in carrier:
K.ingripper(carrier)

#On Kniterate machines, carriers start on the left,
#so will start tucking onto needles left-to-right,
#and will be sure to tuck the leftmost needle in the first pass:
for n in range(min, max+1, 1):
	#tuck alternating needles, making sure to do the left edge:
	if (n - min) % 2 == 0:
		K.tuck("+", 'f' + str(n), carrier)
#now, moving right-to-left, tuck the needles that were not tucked on the first pass:
for n in range(max, min-1, -1):
	if (n - min) % 2 != 0:
		K.tuck("-", 'f' + str(n), carrier)

#knit three plain rows to allow cast-on stitches to relax:
# three isn't set in stone here -- it's just convenient
# for this example code to have the carrier end up on the right.
for n in range(min, max+1, 1):
	K.knit("+", 'f' + str(n), carrier)
for n in range(max, min-1, -1):
	K.knit("-", 'f' + str(n), carrier)
for n in range(min, max+1, 1):
	K.knit("+", 'f' + str(n), carrier)
# ---- single jersey sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#left-going row:
		for n in range(max, min-1, -1):
			K.knit("-", 'f' + str(n), carrier)
	else:
		#right-going row:
		for n in range(min, max+1, 1):
			K.knit("+", 'f' + str(n), carrier)

# ---- take carrier out and drop remaining loops ----
#Send carrier back to its parking location:
K.outgripper(carrier)

#drop loops:
for n in range(min, max+1, 1):
	K.drop('f' + str(n))

# === All-Needle Welt === 
#Bring in carrier:
K.ingripper(carrier)

#On Kniterate machines, carriers start on the left,
#so will start by tucking all needles left-to-right,
#and will be sure to tuck the rightmost back-bed needle last.

#Need to use quarter-pitch racking to tuck all needles in one pass:
K.rack(0.5)

for n in range(min, max+1, 1):
	K.tuck("+", 'f' + str(n), carrier)
	K.tuck("+", 'b' + str(n), carrier)

#Return to aligned racking:
K.rack(0)

#Do a row of plain knitting on the front and back:
for n in range(max, min-1, -1):
	K.knit("-", 'f' + str(n), carrier)
for n in range(min, max+1, 1):
	K.knit("+", 'b' + str(n), carrier)
#One can knit more on the front or back beds here to make a larger "welt" at the edge.

#Stack back-bed stitches with front-bed stitches:
for n in range(min, max+1, 1):
	K.xfer('b' + str(n), 'f' + str(n))

#knit two plain rows through the stacked stitches to return carrier to the right edge:
for n in range(max, min-1, -1):
	K.knit("-", 'f' + str(n), carrier)
for n in range(min, max+1, 1):
	K.knit("+", 'f' + str(n), carrier)

# ---- single jersey sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#left-going row:
		for n in range(max, min-1, -1):
			K.knit("-", 'f' + str(n), carrier)
	else:
		#right-going row:
		for n in range(min, max+1, 1):
			K.knit("+", 'f' + str(n), carrier)

# ---- take carrier out and drop remaining loops ----
#Send carrier back to its parking location:
K.outgripper(carrier)

#drop loops:
for n in range(min, max+1, 1):
	K.drop('f' + str(n))

# === Twisted Tucks === 
#Bring in carrier:
K.ingripper(carrier)

#Use a twisted tuck to cast on every stitch.
#Note: this requires a new pass for every stitch, so is pretty slow!
#There are both faster and fancier cast-ons.
#On Kniterate machines, carriers start on the left, so will start with leftmost needle.
for n in range(min, max+1, 1):
	#tuck alternating needles, making sure to do the left edge:
	K.tuck("-", 'f' + str(n), carrier)

#knit two plain rows to allow cast-on stitches to relax and bring carrier to the right:
for n in range(max, min-1, -1):
	K.knit("-", 'f' + str(n), carrier)
for n in range(min, max+1, 1):
	K.knit("+", 'f' + str(n), carrier)
# ---- single jersey sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#left-going row:
		for n in range(max, min-1, -1):
			K.knit("-", 'f' + str(n), carrier)
	else:
		#right-going row:
		for n in range(min, max+1, 1):
			K.knit("+", 'f' + str(n), carrier)

# ---- take carrier out and drop remaining loops ----
#Send carrier back to its parking location:
K.outgripper(carrier)

#drop loops:
for n in range(min, max+1, 1):
	K.drop('f' + str(n))

# === I-Cord === 
#; not supported at full guage on kniterate


K.write('cast-on-sampler.kniterate.k')
