#!/usr/bin/python3
########    VFD Spindle Commands        ########
####     Creator: Piet van Rensburg        #####
####     Company: Craftsman CNC            #####
####     wwww.craftsmancnc.co.nz           #####
################################################

import sys, os
import gettext
BASE = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))
gettext.install("linuxcnc", localedir=os.path.join(BASE, "share", "locale"), unicode=True)

#sys.path.append('/home/craftsmancnc/linuxcnc/configs/craftsmancnc/')
sys.path.append('./')

import linuxcnc, hal, time

import serial

import easygui

import vfd_controller

running = False
speed_set = False
current_speed = 0

def exception_format(e):
	info = "".join(traceback.format_tb(sys.exc_info()[2]))
	return str(e) + "\n\n" + info
	
	##used like this
	##try:
	##	......
	##except Exception as e:
	##	easygui.msgbox(exception_format(e), title="Exception")


def start_spindle_fwd():
	global running 
	global speed_set	 	 
	try:
		vfd_controller.start_spindle_cw()
		running = True	
	except Exception as e:
		#easygui.msgbox(exception_format(e), title="VFD Exception")
		easygui.msgbox("Unable to start the spindle!", title="Error")	

def start_spindle_rev():
	global running 
	global speed_set
	try:
		vfd_controller.start_spindle_ccw()
		running = True	
		speed_set = False			
	except Exception as e:
		#easygui.msgbox(exception_format(e), title="VFD Exception")
		easygui.msgbox("Unable to start the spindle!", title="Error")	
	
def change_spindle_speed(speed):
	global speed_set	 
	global current_speed
	try:
		vfd_controller.set_spindle_speed(speed)
		speed_set = True		
		current_speed = float(speed)
	except Exception as e:
		#easygui.msgbox(exception_format(e), title="Exception")
		easygui.msgbox("Unable to change the spindle's speed!", title="Error")	
	
def stop_spindle():
	global running
	global speed_set
	global current_speed
	running = False
	speed_set = False
	current_speed =0
	try:
		vfd_controller.stop_spindle()	
	except Exception as e:
		#easygui.msgbox(exception_format(e), title="VFD Exception")
		easygui.msgbox("Unable to stop the spindle!", title="Error")	


h = hal.component("vfd_spindle")
h.newpin("spindle_fwd", hal.HAL_BIT, hal.HAL_IN)
h.newpin("spindle_rev", hal.HAL_BIT, hal.HAL_IN)
h.newpin("spindle_on", hal.HAL_BIT, hal.HAL_IN)
h.newpin("spindle_stop", hal.HAL_BIT, hal.HAL_IN)
h.newpin("spindle_speed", hal.HAL_FLOAT, hal.HAL_IN)
h.ready()

try:
	while 1:		
		if h.spindle_fwd and not running :             
			change_spindle_speed('%.0f' % h.spindle_speed)
			start_spindle_fwd()
		elif h.spindle_rev and not running :
			change_spindle_speed('%.0f' % h.spindle_speed)
			speed_set = False
			start_spindle_rev()
		elif not h.spindle_on and running :
			stop_spindle()
		elif h.spindle_speed >= 3000 and not speed_set :
			change_spindle_speed('%.0f' % h.spindle_speed)
		elif '%.0f' % h.spindle_speed	<> '%.0f' % current_speed and running and  speed_set :
			change_spindle_speed('%.0f' % h.spindle_speed)

except KeyboardInterrupt:
    pass
