

proc ground_fault2 { } {

	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
	set gf_value1 0
	set gf_value2 0


	global env
	set env(DISPLAY) :0.1

	if [winfo exists .gf2] { destroy .gf2 }


	#--------------#
	# First board  #
	#--------------#
	#toplevel .gf2 -width 55m -height 8c
	toplevel .gf2 -width 11c -height 8c
        wm title .gf2 "Ground Fault"

	label .gf2.title -text "Ground Fault Board #2" -relief groove  -fg blue
	label .gf2.ch -text "Channel #"
	entry .gf2.ch_ent -relief flat -width 3 -textvariable gf_ch1
        label .gf2.ch_name -textvariable gf_chname1 -font {Halvetica 10} -fg blue

	entry  .gf2.ent -width 17 -bg white -textvariable gf_value1
	button .gf2.up -text Up -width 5 -height 2 \
		-command increase21
		
	button .gf2.down -text Down -width 5 -height 2 \
		-command decrease21
	
	button .gf2.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 .gf2.close -text Close -width 5 -height 2 \
		-command {destroy .gf2}


	#---------------------#
	# Setting geometry    #
	#---------------------#

	# First board
	place .gf2.title -x 10 -y 10
	place .gf2.ch -x 10 -y 35
	place .gf2.ch_ent -x 70 -y 35
	place .gf2.ch_name -x 10 -y 53 	
	place .gf2.ent -x 10 -y 70
	place .gf2.up -x 10 -y 100
	place .gf2.down -x 70 -y 100
	place .gf2.auto -x 10 -y 150
	place .gf2.close -x 230 -y 150
	



	#---------------#
	# Second board  #
	#---------------#

	label .gf2.title2 -text "Ground Fault Board #1" -relief groove -fg blue
	label .gf2.ch2 -text "Channel #"
	entry .gf2.ch_ent2 -relief flat -width 3 -textvariable gf_ch2
	label .gf2.ch_name2 -textvariable gf_chname2 -font {Halvetica 10} -fg blue
	
	entry  .gf2.ent2 -width 17 -bg white -textvariable gf_value2
	button .gf2.up2 -text Up -width 5 -height 2 \
		-command increase22
		
	button .gf2.down2 -text Down -width 5 -height 2 \
		-command decrease22
	
	#------------------------------------- NOT NEEDED -------------------------------------
	
	button .gf2.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 .gf2.close2 -text Close -width 5 -height 2 \
		-command {destroy .gf2}


	#---------------------#
	# Setting geometry    #
	#---------------------#

	#---------------#
	# Second board  #
	#---------------#
	place .gf2.title2 -x 170 -y 10
	place .gf2.ch2 -x 170 -y 35
	place .gf2.ch_ent2 -x 230 -y 35
	place .gf2.ch_name2 -x 170 -y 53
	place .gf2.ent2 -x 170 -y 70
	place .gf2.up2 -x 170 -y 100
	place .gf2.down2 -x 230 -y 100

	#-----------------------------------------------#
        # Updating the analog values for GF Boards    	#
        #-----------------------------------------------#

	update_value2

}


proc increase21 { } {
 	
 	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 decrease21 { } {
 	
 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto1 gf_chname1 gf_chname2

 	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 increase22 { } {
 	
 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto2 gf_chname1 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 decrease22 { } {
 	
 	global gf_ch1 gf_ch2 gf_value1 gf_value2 auto2  gf_chname1 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 autogf21 { } {

        global gf_ch1 gf_ch2 gf_value1 gf_value2 auto1 gf_chname1 gf_chname2

        	
        if {! [winfo exists .gf2] || $auto1 == 0} {return}

        if {$gf_ch1 >= 16} {
		set gf_ch1 1
 	} else {
 	 	incr gf_ch1
 	}

        after 1000 [list autogf21]

        #puts "auto mode 1"
}


proc autogf22 { } {

        global gf_ch1 gf_ch2 gf_value1 gf_value2 auto2 gf_chname1 gf_chname2
	

	if {! [winfo exists .gf2] || $auto2 == 0} {return}


        if {$gf_ch2 >= 8} {
		set gf_ch2 1
 	} else {
 	 	incr gf_ch2
 	}
 	

        after 1000 [list autogf22]

        #puts "ground.tcl : auto mode 2"


}

#-------------------------------------------------------#
# This function will update the analog value for the 	#
# ground fault every 250 ms                             #
#-------------------------------------------------------#

proc update_value2 { } {

 	global gf_ch1 gf_ch2 gf_value1 gf_value2  auto1 auto2  gf_chname1 gf_chname2

	
	if {! [winfo exists .gf2] } {return}
	
	#----------------#
	# 16 channels 	 #
	#----------------#
	
	#---------------------------------------------#
	# 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_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_value2]

}

#ground_fault2
