#!/usr/bin/python
import serial, reprap, time, sys

reprap.openSerial( 0, 19200, 60 )

# these devices are present in network
reprap.cartesian.x.active = True
reprap.cartesian.y.active = True
reprap.cartesian.z.active = True
reprap.extruder.active = True
reprap.cartesian.x.setNotify()
reprap.cartesian.y.setNotify()
reprap.cartesian.z.setNotify()
reprap.cartesian.x.limit = 2700
reprap.cartesian.y.limit = 2500
reprap.cartesian.y.limit = 2000

def printHelp():
	print "\nreprapcontrol command [args]"
	print "    Commands:"
	print "        stop                 Stop all axies"
	print "        reset                Send all axies to home"
	print "        pos                  Print current position"
	print "        goto [x] [y] [z]     Go to specified position"
	print "        power [0 to 63]      Set power level"
	print "        speed [0 to 255]     Set steper speed"
	print "        go                   Test routine"
	print "        free                 Power off steper motors"
	print "        run [x/y] [speed]    Spin motor forwards"
	print "        runb [x/y] [speed]   Spin motor backwards"
	print "        step [x/y/z]         Step motor forward"
	print "        stepb [x/y/z]        Step motor backwards"

def printPos():
	x, y, z = reprap.cartesian.getPos()
	print "Location [" + str(x) + ", " + str(y) + ", " + str(z) + "]"

print "================================================================"

if len(sys.argv) < 2:
	printHelp()



########### Cartesian control commands #########

# Stop all steppers
if sys.argv[1] == "stop":
	reprap.cartesian.stop()

# Reset all axies
elif sys.argv[1] == "reset":
	reprap.cartesian.homeReset( waitArrival = True, speed = 220 )

# Print current positon
elif sys.argv[1] == "pos":
	printPos()

# Goto a specific location
elif sys.argv[1] == "goto":
	reprap.cartesian.seek( ( int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]) ), speed = 200, waitArrival = True)

# Set stepper power (%)
elif sys.argv[1] == "power":
	reprap.cartesian.setPower( int( sys.argv[2] ) )	# This is a value from 0 to 63 (6 bits)

# Free motors (switch off all coils)
elif sys.argv[1] == "free":
	reprap.cartesian.free()

# Test routine
elif sys.argv[1] == "go":
	reprap.cartesian.seek( (1000, 1000, 0), 200, True )	
	time.sleep(2)
	reprap.cartesian.seek( (500, 1000, 0), 200, True )
	time.sleep(2)
	reprap.cartesian.seek( (500, 500, 0), 200, True )
	time.sleep(2)
	reprap.cartesian.seek( (10, 10, 0), 200, True )



############## control of individual steppers #############

# Spin stepper forwards
elif sys.argv[1] == "run":	# run axis
	if sys.argv[2] == "x":
		reprap.cartesian.x.forward( int(sys.argv[3]) )
	elif sys.argv[2] == "y":
		reprap.cartesian.y.forward( int(sys.argv[3]) )
	elif sys.argv[2] == "z":
		reprap.cartesian.z.forward( int(sys.argv[3]) )

# Spin stepper backwards
elif sys.argv[1] == "runb":	#runb axis
	if sys.argv[2] == "x":
		reprap.cartesian.x.backward( int(sys.argv[3]) )
	elif sys.argv[2] == "y":
		reprap.cartesian.y.backward( int(sys.argv[3]) )
	elif sys.argv[2] == "z":
		reprap.cartesian.z.backward( int(sys.argv[3]) )

# Step motor forwards
elif sys.argv[1] == "step":
	if sys.argv[2] == "x":
		reprap.cartesian.x.forward1()
	elif sys.argv[2] == "y":
		reprap.cartesian.y.forward1()	
	elif sys.argv[2] == "z":
		reprap.cartesian.z.forward1()	

# Step motor backwards
elif sys.argv[1] == "stepb":
	if sys.argv[2] == "x":
		reprap.cartesian.x.backward1()
	elif sys.argv[2] == "y":
		reprap.cartesian.y.backward1()	
	elif sys.argv[2] == "z":
		reprap.cartesian.z.backward1()	



################# control of extruder #####################

# Test extrder motor
elif sys.argv[1] == "motor":
	nn = 0
	while 1:
		if nn > 0:
			nn = 0
		else:
			nn = 150
		reprap.extruder.setMotor(reprap.CMD_REVERSE, nn)
		time.sleep(1)

elif sys.argv[1] == "getinfo":
	mtype = reprap.extruder.getModuleType()
	version = reprap.extruder.getVersion()
	print "module", mtype, "version", version

elif sys.argv[1] == "heat":
	reprap.extruder.setHeat(255, 255, 255, 255)

#setHeat(self, lowHeat, highHeat, tempTarget, tempMax

elif sys.argv[1] == "temp":
	print "Temp is ", reprap.extruder.getTemp()



############### scan network for devices ###################

# Scan snap network
elif sys.argv[1] == "scan":
	reprap.scanNetwork()

# Test comms
elif sys.argv[1] == "test":
	reprap.testComms()


else:
	printHelp()

reprap.closeSerial()