;Original Code by Dustin Neumann, Modified by Dane Ocampo for 2GBP

G21 (mm)
G90 (Absolute distance mode)
G55 (Machine Plane Coordinates) 
G64 (Turn off exact stop check)

o<noext> call


;#######################################[ P A R A M E T E R S ]########################################

; Movement speed 
#<_speed> = 1100
#<_mss> = 1.25 (1.28) (Margin Speed Scalar: the return speed on the margin)

#<_ext_rate> = 0.000000000025 ;extrude 
#<_purge_ext_rate> = 0.001 ;purge extrusion rate
#<_reverse> = 0.005 (extruder back up, used before inter-layer pauses)

#<_corners> = 0 (1 extrude on corners, 0 no extrude on corners)
#<_margin> = 8
#<_lo> = 2 ; loop offset, used to prevent errors from turn-arounds at row ends	

#<_pause> = .0001 (wait time, used at corners) ;originally .0001

;work dimensions
#<_l> = 36 (mm length)
#<_width> = 36 (mm width)
#<_delta> = .75 (mm inter-row&column spacing)
#<_layerH> = .08 (mm half-layer height)


;#######################################[ I N I T I A L I Z E ]########################################

;coordinates
#<_z0> = 3  (Initial Height of Needle Tip)
#<_zz> = #<_z0>

#<_x0> = 0 (initial x)
#<_xx> = #<_x0> (x)

#<_y0> = 0 (initial y)
#<_yy> = #<_y0> (y) 

;counters
#<_zstep> = 0
#<_step> = 0
#<_done> = 0

;counter maximums
#<_layers> = 26
#<_ystepf> = FIX[#<_l>/#<_delta>]	(FIX operator rounds the argument down to integer)
#<_xstepf> = FIX[#<_width>/#<_delta>]

;######################[ M A T R I X  C O D E ]################################

G1 X[#<_xx>] Y[#<_yy>] Z[#<_zz>] F#<_speed>
;M68 E0 Q2			;turn on HV
o110 while [#<_zstep> LE FIX[[#<_layers> - 1]/2]]
;#########[ P U R G E  C O D E 		]#######################

	#<_yy> = [#<_yy> + #<_l> + 20]     ;position purge routine (y)
	#<_xx> = [#<_xx> - #<_width> + 7]  ;position purge routine (x)
	G0 X[#<_xx>] Y[#<_yy>] Z10         ;position purge routine

	o<ep> call [#<_purge_ext_rate>] [1]     ; 2nd param: 0=nohv 1=hv
	o105 while [#<_step> LE 20]
		;#<_yy> = [#<_yy> + #<_delta>] (shows iterations for debugging)
		#<_xx> = [#<_xx> + 0.5*#<_width>*[2*[#<_step> MOD 2]-1]]	;note: [2*[#<_step> 	MOD 2]-1]] alternates between +/-1 with each step
		G1 X[#<_xx>] F#<_speed>
		#<_step>=[#<_step> + 1]
	o105 endwhile
	o<noext> call
;###############[ E N D  P U R G E  C O D E ] ########################
	;reset variables assumed to have previously stated initial values
	#<_zz> = [#<_zz>+#<_layerH>]
	
	#<_xx> = #<_x0>
	#<_yy> = #<_y0>
	#<_step> = 0
	G0 Z#<_zz>

	#<_xx> = [#<_l> + #<_margin>*0.5]
	#<_yy> = [#<_y0> + [[0.5*#<_delta>]*[#<_zstep> MOD 2]]] ; causes layers to stagger

	o<ep> call [#<_ext_rate>] [1]   ; second parameter: 0=nohv, 1=hv
	o115 while [#<_step> LE [[#<_ystepf>/2]-1]]
		#<_step> = [#<_step> + 1]
		G1 X[[#<_xx> - #<_width> - [#<_margin>]]] F#<_speed>
		#<_yy> = [#<_yy>+[0.5*#<_l>]]
		
		G4 P[#<_pause>]
		;margin
		G0 Y[#<_yy>] X[[#<_xx> - #<_width> - [#<_margin>]*1.5]] F[#<_speed> * #<_mss>]
		G4 P#<_pause>
		
		G1 X[#<_xx> ] F#<_speed>
		#<_yy> = [#<_yy>-[[0.5*#<_l>]-#<_delta>]]
		
		G4 P#<_pause>
		;margin
		G0 Y[#<_yy>] X[#<_xx> + [#<_margin>/2]] F[#<_speed>*#<_mss>]
		G4 P#<_pause>
		
	o115 endwhile
;#########[ P U R G E  C O D E 		]#######################

	#<_yy> = [#<_yy> + #<_l> + 20]     ;position purge routine (y)
	#<_xx> = [#<_xx> - #<_width> + 7]  ;position purge routine (x)
	G0 X[#<_xx>] Y[#<_yy>] Z10         ;position purge routine

	o<ep> call [#<_purge_ext_rate>] [1]     ; 2nd param: 0=nohv 1=hv
	o120 while [#<_step> LE [[#<_ystepf>/2]-1+20]]
		;#<_yy> = [#<_yy> + #<_delta>] (shows iterations for debugging)
		#<_xx> = [#<_xx> + 0.5*#<_width>*[2*[#<_step> MOD 2]-1]]	;note: [2*[#<_step> 	MOD 2]-1]] alternates between +/-1 with each step
		G1 X[#<_xx>] F#<_speed>
		#<_step>=[#<_step> + 1]
	o120 endwhile
	o<noext> call
;###############[ E N D  P U R G E  C O D E ] ########################
	
	#<_zz> = [#<_zz>+#<_layerH>]
	G0 Z[#<_zz>]
	
	#<_xx> = #<_x0>
	#<_xx> = [#<_x0> + [[0.5*#<_delta>]*[#<_zstep> MOD 2]]] ; causes layers to stager
	#<_yy> = [#<_y0> - #<_margin>]

	#<_step> = 0
	o<ep> call [#<_ext_rate>] [1]   ; second parameter: 0=nohv, 1=hv
	o125 while[#<_step> LE [[#<_xstepf>/2]-1]]
		#<_step> = [#<_step> + 1]
		G1 Y[[#<_yy> + #<_l> +[#<_margin>*1.5]]] F#<_speed>
		#<_xx> = [#<_xx>+[0.5*#<_width>]]
		
		G4 P[#<_pause>/2]
		;margin		
		G0 X[#<_xx>] Y[[#<_yy> + #<_l> + [2*#<_margin>]]] F[#<_speed>*#<_mss>]

		;G4 P#<_pause>		
		G1 Y[#<_yy> + [#<_margin>/2]] F#<_speed>
		
		#<_xx> = [#<_xx>-[[0.5*#<_width>]-#<_delta>]]
		
		G4 P#<_pause>
		;margin		
		G0 X[#<_xx>] Y[#<_yy>] F[#<_speed>*#<_mss>]
		
	o125 endwhile
		
	#<_zz> = [#<_zz>+#<_layerH>]
	G0 Z[#<_zz>]

	#<_step> = 0
	

#<_zstep> = [#<_zstep> + 1]
o<noext> call

;============================[ E L E C T R O S P U N  L A Y E R ]================================
	o111 if [#<_zstep> MOD 3 EQ 0]		;electrospin 1 layer for every 3 printed layers
		G1 Z[#<_zz> + 200.1] F2000	;165.1 mm = 6.5 in
		#<_yy> = [#<_yy> + #<_l>/2]     ;position purge routine (y)
		;#<_xx> = [#<_xx> - #<_width>/2] ;position purge routine (x)
		G0 Y[#<_yy>]			;move y-axis to center of matrix
		o<es> call [1.5] [1]		;extrude at 1.5mL/hr with hv on
		G4 P300				;electrospin for 600s (10 min)
		o<noext> call			;stop extruding and hv off
		G1 Z[#<_zz>] F2000		;quick move back to where it was (G0 causes error)
	o111 endif
;================================================================================================

o110 endwhile	
o<noext> call
M68 E0 Q2			;turn on HV
;################################[ F I N I S H I N G  S E Q U E N C E ]###################################

;move out of the way
G0 X-25 Y150 Z80

;;back up extruder to relieve excess pressure and prevent drips
;M4 S0.05 
;o101 while [#<_done> LE 0]	('timer')
;	G1 X[#<_xx>] F#<_speed>
;	#<_done>=[#<_done> + 1]
;o101 endwhile

; retract by 1uL to relieve pressure and prevent drips
;o<ep> call [-3.6] [0]
;G4 P1
;o<noext> call

M2	;End Program