

proc ground_fault { } {



	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto1 auto2 gf_chname1 gf_chname2



	#-----------------------------#

	# Initialize the variables    #

	#-----------------------------#

	set gf_ch1 1

	set gf_ch2 1

	set auto1 1

	set auto2 1





	global env

	set env(DISPLAY) :0



	if [winfo exists .gf] { destroy .gf }





	#--------------#

	# First board  #

	#--------------#

	#toplevel .gf -width 55m -height 8c

	

	toplevel .gf -width 11c -height 8c

	wm title .gf "Ground Fault"



	label .gf.title -text "Ground Fault Board #2" -relief groove -fg blue

	label .gf.ch -text "Channel #"

	entry .gf.ch_ent -relief flat -width 3 -textvariable gf_ch1

        label .gf.ch_name -textvariable gf_chname1 -font {Halvetica 10} -fg blue





	entry  .gf.ent -width 17 -bg white -textvariable gf_value1

	button .gf.up -text Up -width 5 -height 2 \

		-command increase1

		

	button .gf.down -text Down -width 5 -height 2 \

		-command decrease1

		

	button .gf.auto -text Auto  -width 5 -height 2 \

		-command {	set auto1 1

				

				#-------------------------------------------------------#

 				# Tell the sub that automatic mode (7) is selected 	#

 				#-------------------------------------------------------#

 				select_ground_mode 7

			   }

	

	button .gf.close -text Close -width 5 -height 2 \

		-command {destroy .gf}

	



	#---------------------#

	# Setting geometry    #

	#---------------------#



	#---------------#

	# First board   #

	#---------------#

	place .gf.title -x 10 -y 10

	place .gf.ch -x 10 -y 35

	place .gf.ch_ent -x 70 -y 35 	

	place .gf.ch_name -x 10 -y 53 	

	place .gf.ent -x 10 -y 70

	place .gf.up -x 10 -y 100

	place .gf.down -x 70 -y 100

	place .gf.auto -x 10 -y 150

	place .gf.close -x 230 -y 150

	







	#---------------#

	# Second board  #

	#---------------#

	

	label .gf.title2 -text "Ground Fault Board #1" -relief groove  -fg blue

	label .gf.ch2 -text "Channel #"

	entry .gf.ch_ent2 -relief flat -width 3 -textvariable gf_ch2

 	label .gf.ch_name2 -textvariable gf_chname2 -font {Halvetica 10} -fg blue

	

	

	entry  .gf.ent2 -width 17 -bg white -textvariable gf_value2

	button .gf.up2 -text Up -width 5 -height 2 \

		-command increase2

		

	button .gf.down2 -text Down -width 5 -height 2 \

		-command decrease2

	

	#------------------------------------- NOT NEEDED -------------------------------------

	

	button .gf.auto2 -text Auto  -width 5 -height 2 \

		-command {	set auto2 1

			 	

				#---------------------------------------------------#

 				# Tell the sub that automatic mode (7) is selected  #		#

 				#---------------------------------------------------#

 				select_ground_mode 7                				

			    }

	#--------------------------------------------------------------------------------

	

	button .gf.close2 -text Close -width 5 -height 2 \

		-command {destroy .gf}

	

	#---------------------#

	# Setting geometry    #

	#---------------------#



	#---------------#

	# Second board  #

	#---------------#

	place .gf.title2 -x 170 -y 10

	place .gf.ch2 -x 170 -y 35

	place .gf.ch_ent2 -x 230 -y 35

	place .gf.ch_name2 -x 170 -y 53

	place .gf.ent2 -x 170 -y 70

	place .gf.up2 -x 170 -y 100

	place .gf.down2 -x 230 -y 100

	



	#-----------------------------------------------#

        # Set the name for the second GF Board	    	#

        # because we do not have auto function that	#

        # can update names like the first board		#

        #-----------------------------------------------#

        	

	set gf_chname2 [get_element_name 0 1 [expr {$gf_ch2 }]]

	

        #-----------------------------------------------#

        # Updating the analog values for GF Boards    	#

        #-----------------------------------------------#



	update_value



}







proc increase1 { } {

 	

 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto1 gf_chname1 gf_chname2





 	if {$gf_ch1 >= 16} {

		set gf_ch1 1

 	} else {

 	 	incr gf_ch1

 	}



 	set auto1 0

 	

 	#-------------------------------------------------------#

 	# Tell the sub that manual mode (6) is selected 	#

 	#-------------------------------------------------------#

 	select_ground_mode 6

 	

                                                                          	

 	

}



proc decrease1 { } {

 	

 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto1 gf_chname1





 	if {$gf_ch1 <= 1} {

		set gf_ch1 16

 	} else {

 	 	incr gf_ch1 -1

 	}

 	

 	set auto1 0

 	

 	#-------------------------------------------------------#

 	# Tell the sub that manual mode (6) is selected 	#

 	#-------------------------------------------------------#

 	select_ground_mode 6



 	

 	                     	

}





proc increase2 { } {

 	

 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto2 gf_chname2



 	if {$gf_ch2 >= 8} {

		set gf_ch2 1

 	} else {

 	 	incr gf_ch2

 	}

 	

 	#-------------------------------------------------------#

 	# This c++ function below will be responsible for 	#

 	# turning on/off mpl channel 41-43        		#

 	#-------------------------------------------------------#

 	 	

 	set gf_value2 [get_8_ground_value $gf_ch2]

	

	if {$gf_ch2 != 0} {

		set gf_chname2 [get_element_name 0 1 [expr {$gf_ch2 }]]

	}

	

}



proc decrease2 { } {

 	

 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto2 gf_chname2





 	if {$gf_ch2 <= 1} {

		set gf_ch2 8

 	} else {

 	 	incr gf_ch2 -1

 	}

 	

 	#-------------------------------------------------------#

 	# This c++ function below will be responsible for 	#

 	# turning on/off mpl channel 41-43        		#

 	#-------------------------------------------------------#

 	

 	set gf_value2 [get_8_ground_value $gf_ch2]

	

	if {$gf_ch2 != 0} {

		set gf_chname2 [get_element_name 0 1 [expr {$gf_ch2 }]]

	}

 	

 	

}



proc autogf1 { } {



        global gf_ch1 gf_ch2 gf_value1 gf_value2 auto1 gf_chname1 gf_chname2





        	

        if {! [winfo exists .gf] || $auto1 == 0} {return}



        if {$gf_ch1 >= 16} {

		set gf_ch1 1

 	} else {

 	 	incr gf_ch1

 	}



        after 250 [list autogf1]



        #puts "auto mode 1"

}





proc autogf2 { } {



        global gf_ch1 gf_ch2 gf_value1 gf_value2 auto2  gf_chname1 gf_chname2



	



	if {! [winfo exists .gf] || $auto2 == 0} {return}





        if {$gf_ch2 >= 8} {

		set gf_ch2 1

 	} else {

 	 	incr gf_ch2

 	}

 	



        after 250 [list autogf2]



        #puts "ground.tcl : auto mode 2"





}



#---------------------------------------------------------------#

# This function will update the analog value for the 		#

# ground fault every 250 ms                             	#

#---------------------------------------------------------------#



proc update_value { } {



 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto1 auto2 gf_chname1 gf_chname2





	

	if {! [winfo exists .gf] } {return}

	

	#---------------#

	# 16 channe		#

	#---------------#

	

	

	#---------------------------------------------------------------#

	# If the automatic mode (7) is selected     	 		#

	# Here, the function get_16_ground_value will return 2     	#

	# values which are the channel number and the ground value   	#

	# separated by "|"						#

	#---------------------------------------------------------------#

	if { $auto1 == 1 } {

		set gf_ch1 [lindex [split [get_16_ground_value 7 -1]  | ] 0 ]

		set gf_value1 [lindex [split [get_16_ground_value 7 -1]  | ] 1 ]

		  			

	}  else {

	

	#---------------------------------------------------------------#

	# If the manual mode (6) is selected     		 	#

	# Here, inside the "get_16_ground_value", we will have to    	#

	# turn the channel manually by passing it as one of the		#

	# parameter, and then read the ground value / analog value    	#

	# from channel 38						#

	#---------------------------------------------------------------#

	

		set gf_value1 [get_16_ground_value 6 $gf_ch1]

	        		

	}

	

	#------------#

	# 16 channels #

	#------------#

	#set gf_value2 [get_8_ground_value $gf_ch2]

	set gf_value2 [get_analog_value 0 37]

	

	

	if {$gf_ch1 == 1}  { set gf_chname1 "Arm FB +5 Power" }

	if {$gf_ch1 == 2}  { set gf_chname1 "Cam FB +5 Power" }

	if {$gf_ch1 == 3}  { set gf_chname1 "Depth +12 Power" }

	if {$gf_ch1 == 4}  { set gf_chname1 "DVL +24 Power" }

	if {$gf_ch1 == 5}  { set gf_chname1 "Altimeter +24 Power" }

	if {$gf_ch1 == 6}  { set gf_chname1 "Oil Press +5 Power" }

	if {$gf_ch1 == 7}  { set gf_chname1 "ISIT +24,+12,-12 Power" }

	if {$gf_ch1 == 8}  { set gf_chname1 "Spare GFI" }

	if {$gf_ch1 == 9}  { set gf_chname1 "Spare GFI" }

	if {$gf_ch1 == 10} { set gf_chname1 "Spare GFI" }

	if {$gf_ch1 == 11} { set gf_chname1 "Spare GFI" }

	if {$gf_ch1 == 12} { set gf_chname1 "Spare GFI" }	

	if {$gf_ch1 == 13} { set gf_chname1 "Spare GFI" }

	if {$gf_ch1 == 14} { set gf_chname1 "Spare GFI" }

	if {$gf_ch1 == 15} { set gf_chname1 "Spare GFI" }

	if {$gf_ch1 == 16} { set gf_chname1 "Spare GFI" }	





	after 250 [list update_value]



}







#ground_fault