
#---------------------------------------------------------------#
# Initialize variables	                      			#
#---------------------------------------------------------------#

# toplevel to display the top digital output
set dig_out(1) .none
set dig_out(2) .none
set dig_out(3) .none

# toplevel to display the sub output
set all_aout .all_analog_output


#---------------------------------------------------------------#
# Helper function that bind two listboxes to a scrollbar  	#
#---------------------------------------------------------------#

proc bind_yview { lists args } {
        foreach l $lists {
        	eval {$l yview } $args
        }
	
}



#---------------------------------------------------------------#
# TOP DIGITAL LIST	                      			#
#---------------------------------------------------------------#


proc list_dig_element { } {

	global dig top_dname top_dval top_dname2 top_dval2 top_dname3 top_dval3
	
	
	set dig .dig_element
	set top_dname $dig.top_name
	set top_dval $dig.top_value
	set top_dname2 $dig.top_name2
	set top_dval2 $dig.top_value2
	set top_dname3 $dig.top_name3
	set top_dval3 $dig.top_value3
	
	set top_dscroll $dig.top_scroll

        global env
	set env(DISPLAY) :0
	
	toplevel $dig -width 10c -height 10c
	wm title $dig "Top Digital Input"
	
	#---------------------------------------#
 	# First Column 				#
	#---------------------------------------#
	
	listbox $top_dname -yscroll "$top_dscroll set" \
		-relief flat -width 35 -height 32 \
		-font {Halvetica 10} -bg white

	listbox $top_dval  -yscroll "$top_dscroll set" \
		-relief flat -width 5 \
		-font {Halvetica 10} -bg white

	#---------------------------------------#
	# Second Column 			#
	#---------------------------------------#
	listbox $top_dname2 -yscroll "$top_dscroll set" \
		-relief flat -width 35 -height 32 \
		-font {Halvetica 10}

	listbox $top_dval2  -yscroll "$top_dscroll set" \
		-relief flat -width 5 \
		-font {Halvetica 10}

	
	
	#---------------------------------------#
	# Third Column 				#
	#---------------------------------------#
	listbox $top_dname3 -yscroll "$top_dscroll set" \
		-relief flat -width 35 -height 32 \
		-font {Halvetica 10} -bg white

	listbox $top_dval3  -yscroll "$top_dscroll set" \
		-relief flat -width 5 \
		-font {Halvetica 10} -bg white

	

	scrollbar $top_dscroll -orient vertical  \
		-command [list bind_yview [list $top_dname $top_dval $top_dname2 $top_dval2 $top_dname3 $top_dval3]]
	
		$top_dname insert end  "1 (TB1-47) [get_element_name 1 1 1]"
		$top_dname insert end  "2 (TB1-45) [get_element_name 1 1 2]"
		$top_dname insert end  "3 (TB1-43) [get_element_name 1 1 3]"
		$top_dname insert end  "4 (TB1-41) [get_element_name 1 1 4]"
		$top_dname insert end  "5 (TB1-39) [get_element_name 1 1 5]"
		$top_dname insert end  "6 (TB1-37) [get_element_name 1 1 6]"
		$top_dname insert end  "7 (TB1-35) [get_element_name 1 1 7]"
		$top_dname insert end  "8 (TB1-33) [get_element_name 1 1 8]"
		$top_dname insert end  "9 (TB1-31) [get_element_name 1 1 9]"
		$top_dname insert end  "10(TB1-29) [get_element_name 1 1 10]"
		$top_dname insert end  "11(TB1-27) [get_element_name 1 1 11]"
		$top_dname insert end  "12(TB1-25) [get_element_name 1 1 12]"
		$top_dname insert end  "13(TB1-23) [get_element_name 1 1 13]"
		$top_dname insert end  "14(TB1-21) [get_element_name 1 1 14]"
		$top_dname insert end  "15(TB1-19) [get_element_name 1 1 15]"
		$top_dname insert end  "16(TB1-17) [get_element_name 1 1 16]"
		$top_dname insert end  "17(TB1-15) [get_element_name 1 1 17]"
		$top_dname insert end  "18(TB1-13) [get_element_name 1 1 18]"
		$top_dname insert end  "19(TB1-11) [get_element_name 1 1 19]"
		$top_dname insert end  "20(TB1-9)  [get_element_name 1 1 20]"
		$top_dname insert end  "21(TB1-7)  [get_element_name 1 1 21]"
		$top_dname insert end  "22(TB1-5)  [get_element_name 1 1 22]"
		$top_dname insert end  "23(TB1-3)  [get_element_name 1 1 23]"
		$top_dname insert end  "24(TB1-1)  [get_element_name 1 1 24]"
		$top_dname insert end  "25(TB2-47) [get_element_name 1 1 25]"
		$top_dname insert end  "26(TB2-45) [get_element_name 1 1 26]"
		$top_dname insert end  "27(TB2-43) [get_element_name 1 1 27]"
		$top_dname insert end  "28(TB2-41) [get_element_name 1 1 28]"
		$top_dname insert end  "29(TB2-39) [get_element_name 1 1 29]"
		$top_dname insert end  "30(TB2-37) [get_element_name 1 1 30]"
		$top_dname insert end  "31(TB2-35) [get_element_name 1 1 31]"
		$top_dname insert end  "32(TB2-33) [get_element_name 1 1 32]"

		$top_dname2 insert end  "33(TB2-31) [get_element_name 1 1 33]"		
		$top_dname2 insert end  "34(TB2-29) [get_element_name 1 1 34]"
		$top_dname2 insert end  "35(TB2-27) [get_element_name 1 1 35]"
		$top_dname2 insert end  "36(TB2-25) [get_element_name 1 1 36]"
		$top_dname2 insert end  "37(TB2-23) [get_element_name 1 1 37]"
		$top_dname2 insert end  "38(TB2-21) [get_element_name 1 1 38]"
		$top_dname2 insert end  "39(TB2-19) [get_element_name 1 1 39]"
		$top_dname2 insert end  "40(TB2-17) [get_element_name 1 1 40]"
		$top_dname2 insert end  "41(TB2-15) [get_element_name 1 1 41]"
		$top_dname2 insert end  "42(TB2-13) [get_element_name 1 1 42]"
		$top_dname2 insert end  "43(TB2-11) [get_element_name 1 1 43]"
		$top_dname2 insert end  "44(TB2-9)  [get_element_name 1 1 44]"
		$top_dname2 insert end  "45(TB2-7)  [get_element_name 1 1 45]"
		$top_dname2 insert end  "46(TB2-5)  [get_element_name 1 1 46]"
		$top_dname2 insert end  "47(TB2-3)  [get_element_name 1 1 47]"
		$top_dname2 insert end  "48(TB2-1)  [get_element_name 1 1 48]"
		$top_dname2 insert end  "49(TB3-47) [get_element_name 1 1 49]"
		$top_dname2 insert end  "50(TB3-45) [get_element_name 1 1 50]"
		$top_dname2 insert end  "51(TB3-43) [get_element_name 1 1 51]"
		$top_dname2 insert end  "52(TB3-41) [get_element_name 1 1 52]"
		$top_dname2 insert end  "53(TB3-39) [get_element_name 1 1 53]"
		$top_dname2 insert end  "54(TB3-37) [get_element_name 1 1 54]"
		$top_dname2 insert end  "55(TB3-35) [get_element_name 1 1 55]"
		$top_dname2 insert end  "56(TB3-33) [get_element_name 1 1 56]"
		$top_dname2 insert end  "57(TB3-31) [get_element_name 1 1 57]"
		$top_dname2 insert end  "58(TB3-29) [get_element_name 1 1 58]"
		$top_dname2 insert end  "59(TB3-27) [get_element_name 1 1 59]"
		$top_dname2 insert end  "60(TB3-25) [get_element_name 1 1 60]"
		$top_dname2 insert end  "61(TB3-23) [get_element_name 1 1 61]"
		$top_dname2 insert end  "62(TB3-21) [get_element_name 1 1 62]"
		$top_dname2 insert end  "63(TB3-19) [get_element_name 1 1 63]"
		$top_dname2 insert end  "64(TB3-17) [get_element_name 1 1 64]"

		$top_dname3 insert end  "65(TB3-15) [get_element_name 1 1 65]"		
		$top_dname3 insert end  "66(TB3-13) [get_element_name 1 1 66]"
		$top_dname3 insert end  "67(TB3-11) [get_element_name 1 1 67]"
		$top_dname3 insert end  "68(TB3-9)  [get_element_name 1 1 68]"
		$top_dname3 insert end  "69(TB3-7)  [get_element_name 1 1 69]"
		$top_dname3 insert end  "70(TB3-5)  [get_element_name 1 1 70]"
		$top_dname3 insert end  "71(TB3-3)  [get_element_name 1 1 71]"
		$top_dname3 insert end  "72(TB3-1)  [get_element_name 1 1 72]"
		$top_dname3 insert end  "73(TB4-47) [get_element_name 1 1 73]"
		$top_dname3 insert end  "74(TB4-45) [get_element_name 1 1 74]"
		$top_dname3 insert end  "75(TB4-43) [get_element_name 1 1 75]"
		$top_dname3 insert end  "76(TB4-41) [get_element_name 1 1 76]"
		$top_dname3 insert end  "77(TB4-39) [get_element_name 1 1 77]"
		$top_dname3 insert end  "78(TB4-37) [get_element_name 1 1 78]"
		$top_dname3 insert end  "79(TB4-35) [get_element_name 1 1 79]"
		$top_dname3 insert end  "80(TB4-33) [get_element_name 1 1 80]"
		$top_dname3 insert end  "81(TB4-31) [get_element_name 1 1 81]"
		$top_dname3 insert end  "82(TB4-29) [get_element_name 1 1 82]"
		$top_dname3 insert end  "83(TB4-27) [get_element_name 1 1 83]"
		$top_dname3 insert end  "84(TB4-25) [get_element_name 1 1 84]"
		$top_dname3 insert end  "85(TB4-23) [get_element_name 1 1 85]"
		$top_dname3 insert end  "86(TB4-21) [get_element_name 1 1 86]"
		$top_dname3 insert end  "87(TB4-19) [get_element_name 1 1 87]"
		$top_dname3 insert end  "88(TB4-17) [get_element_name 1 1 88]"
		$top_dname3 insert end  "89(TB4-15) [get_element_name 1 1 89]"
		$top_dname3 insert end  "90(TB4-13) [get_element_name 1 1 90]"
		$top_dname3 insert end  "91(TB4-11) [get_element_name 1 1 91]"
		$top_dname3 insert end  "92(TB4-9)  [get_element_name 1 1 92]"
		$top_dname3 insert end  "93(TB4-7)  [get_element_name 1 1 93]"
		$top_dname3 insert end  "94(TB4-5)  [get_element_name 1 1 94]"
		$top_dname3 insert end  "95(TB4-3)  [get_element_name 1 1 95]"
		$top_dname3 insert end  "96(TB4-1)  [get_element_name 1 1 96]"
	
	#---------------------------------------#	
	# Update all the values			#
	#---------------------------------------#
	update_all_topdigital
	
	#---------------------------------------#	
	# Close Button				#
	#---------------------------------------#
	frame $dig.fr
	button $dig.fr.close -text "Close" -width 5 -height 3 \
		-command {destroy $dig}
	
	#---------------------------------------#		
	# Button to Show the Outputs
	#---------------------------------------#	
	button $dig.fr.out1 -text "Output (1-32)" -width 10 -height 3 \
		-wraplength 50 -command {list_dig_output 1}
	button $dig.fr.out2 -text "Output (33-64)" -width 10 -height 3 \
		-wraplength 50 -command {list_dig_output 2}
	button $dig.fr.out3 -text "Output (65-96)" -width 10 -height 3 \
		-wraplength 50 -command {list_dig_output 3}
	
	#---------------------------------------#	
	# Pack Widget Up			#
	#---------------------------------------#
	
	pack $dig.fr.close $dig.fr.out1 $dig.fr.out2 $dig.fr.out3 -side left -pady 2m -padx 2m
	pack $dig.fr -pady 2m -side bottom	
	pack $top_dname $top_dval $top_dname2 $top_dval2 $top_dname3 $top_dval3 -side left -fill y
	
	
	
}

proc update_all_topdigital { } {
	global dig top_dname top_dval top_dname2 top_dval2 top_dname3 top_dval3
	
	if { ! [winfo exists $dig] } {return}
	
	# else
	$top_dval delete 0 32
	$top_dval2 delete 0 32
	$top_dval3 delete 0 32
		
	for {set i 1} { $i <= 32 } { incr i } {

		#---------------------------------------#
		# Get the analog value 			#
		# Parameter :  surface/vehicle = 1/0	#
		# 		channel number		#
		#---------------------------------------#
		set i2 [expr {$i + 32 } ]
		set i3 [expr {$i + 64 } ]

		set j [expr {$i - 1}]
		set j2 [expr {$j + 32}]
		set j3 [expr {$j + 64}]
		
		set val($j) [get_digital_value 1 $i]
		set val($j2) [get_digital_value 1 $i2 ]
		set val($j3) [get_digital_value 1 $i3 ]
		
		#-----------------------------------------------#
		# First Column                                 	#
		#-----------------------------------------------#
		
		if {$val($j) == 0} {
			set val($j) "Off"
		} elseif { $val($j) == 1 } {
			set val($j) "On"
		} else {
			set val($j) "None"
		}
		
		#-----------------------------------------------#
		# Second Column                                 #
		#-----------------------------------------------#
		
		if {$val($j2) == 0} {
			set val($j2) "Off"
		} elseif { $val($j2) == 1 } {
			set val($j2) "On"
		} else {
			set val($j2) "None"
		}
		
		#-----------------------------------------------#
		# Third Column                                 	#
		#-----------------------------------------------#
		
		if {$val($j3) == 0} {
			set val($j3) "Off"
		} elseif { $val($j3) == 1 } {
			set val($j3) "On"
		} else {
			set val($j3) "None"
		}
		
		# Notice we use "j" because it is our index
	        $top_dval insert $j "$val($j)"
                $top_dval2 insert $j "$val($j2)"
		$top_dval3 insert $j "$val($j3)"

        }

	
	after 250 [list update_all_topdigital]


}



##################################################################################




#---------------------------------------------------------------#
# TOP ANALOG LIST	                      			#
#---------------------------------------------------------------#

proc list_ana_element { } {

	global ana top_aname top_aval top_rawval
	
	
	set ana .ana_element
	set top_aname $ana.top_name
	set top_aval $ana.top_value
	set top_rawval $ana.top_rawvalue
	
        global env
	set env(DISPLAY) :0
	
	toplevel $ana -width 10c -height 10c
	wm title $ana "Top Analog Input"
	
	#---------------------------------------#
	# First Column 				#
	#---------------------------------------#
	
	listbox $top_aname \
		-relief flat -width 50 -height 32 \
		-font {Halvetica 10}
	listbox $top_aval  \
		-relief flat -width 10 -bg white \
		-font {Halvetica 10}
	listbox $top_rawval  \
		-relief flat -width 10 -bg white \
		-font {Halvetica 10}
		
	$top_aname insert end  "1 (TB5-37) [get_element_name 1 0 1]" 			
	$top_aname insert end  "2 (TB5-36) [get_element_name 1 0 2]" 
	$top_aname insert end  "3 (TB5-35) [get_element_name 1 0 3]" 			
	$top_aname insert end  "4 (TB5-34) [get_element_name 1 0 4]" 
	$top_aname insert end  "5 (TB5-33) [get_element_name 1 0 5]" 			
	$top_aname insert end  "6 (TB5-32) [get_element_name 1 0 6]" 
	$top_aname insert end  "7 (TB5-31) [get_element_name 1 0 7]" 			
	$top_aname insert end  "8 (TB5-30) [get_element_name 1 0 8]"
	$top_aname insert end  "9 (TB5-18) [get_element_name 1 0 9]" 			
	$top_aname insert end  "10(TB5-17) [get_element_name 1 0 10]" 
	$top_aname insert end  "11(TB5-16) [get_element_name 1 0 11]" 			
	$top_aname insert end  "12(TB5-15) [get_element_name 1 0 12]" 
	$top_aname insert end  "13(TB5-14) [get_element_name 1 0 13]" 			
	$top_aname insert end  "14(TB5-13) [get_element_name 1 0 14]" 
	$top_aname insert end  "15(TB5-12) [get_element_name 1 0 15]" 			
	$top_aname insert end  "16(TB5-11) [get_element_name 1 0 16]"	
	$top_aname insert end  "17(TB6-37) [get_element_name 1 0 17]" 			
	$top_aname insert end  "18(TB6-36) [get_element_name 1 0 18]" 
	$top_aname insert end  "19(TB6-35) [get_element_name 1 0 19]" 			
	$top_aname insert end  "20(TB6-34) [get_element_name 1 0 20]" 
	$top_aname insert end  "21(TB6-33) [get_element_name 1 0 21]" 			
	$top_aname insert end  "22(TB6-32) [get_element_name 1 0 22]" 
	$top_aname insert end  "23(TB6-31) [get_element_name 1 0 23]" 			
	$top_aname insert end  "24(TB6-30) [get_element_name 1 0 24]"
	$top_aname insert end  "25(TB6-18) [get_element_name 1 0 25]" 			
	$top_aname insert end  "26(TB6-17) [get_element_name 1 0 26]" 
	$top_aname insert end  "27(TB6-16) [get_element_name 1 0 27]" 			
	$top_aname insert end  "28(TB6-15) [get_element_name 1 0 28]" 
	$top_aname insert end  "29(TB6-14) [get_element_name 1 0 29]" 			
	$top_aname insert end  "30(TB6-13) [get_element_name 1 0 30]" 
	$top_aname insert end  "31(TB6-12) [get_element_name 1 0 31]" 			
	$top_aname insert end  "32(TB6-11) [get_element_name 1 0 32]"	

	#---------------------------------------#	
	# Update all the values			#
	#---------------------------------------#
	update_all_topanalog
	
	#---------------------------------------#	
	# Close Button				#
	#---------------------------------------#
	frame $ana.fr
	button $ana.fr.close -text "Close" -width 5 -height 3 \
		-command {destroy $ana}
	
	#---------------------------------------#	
	# Button to Show all Analog Outputs		#
	#---------------------------------------#
	button $ana.fr.out -text "Show Output" -width 10 -height 3 \
		-command {list_ana_output}
	
	#---------------------------------------#	
	# Pack Widget Up			#
	#---------------------------------------#
	
	pack $ana.fr.close $ana.fr.out -padx 2m -pady 2m -side left
	pack $ana.fr -pady 2m -side bottom	
	pack $top_aname $top_aval $top_rawval -side left -fill y
	
	
	
}


proc update_all_topanalog { } {
	global ana top_aname top_aval top_rawval
	
	if { ! [winfo exists $ana] } {return}
	
	# else
	$top_aval delete 0 32
		
	for {set i 1} { $i <= 32 } { incr i } {

		#---------------------------------------#
		# Get the analog value 			#
		# Parameter :  surface/vehicle = 1/0	#
		# 		channel number		#
		#---------------------------------------#
		
		set j [expr {$i - 1}]
		set val($j) [get_analog_value 1 $i]
		#---------------------------------------#
		# Get the raw analog value 		#
		# Parameter :  	surface/vehicle = 1/0	#
		#		digital/analog = 1/0	#
		# 		channel number		#
		#---------------------------------------#
		set rawvalue($j) [get_raw_analog_value 1 0 $i]   
				
		#-----------------------------------------------#
		# First Column                                 	#
		#-----------------------------------------------#
				
		# Notice we use "j" because it is our index
	        $top_aval insert $j "$val($j)"
	        $top_rawval insert $j "$rawvalue($j)"
        }


	after 250 [list update_all_topanalog]

}


proc list_ana_output { } {

	global ana_out1 top_aname_out top_aout
	
	
	set ana_out1 .top_ana_output1
	set ayout_scroll $ana_out1.yscroll
	
	
	# first frame -> name
	set out_name $ana_out1.name
	set top_aname_out $out_name.top_name
	set axout_scroll1 $out_name.xscroll1
	
        	# second frame -> output
        	set out_aval $ana_out1.output
        	set top_aout $out_aval.top_value
        	
	set axout_scroll2 $out_aval.xscroll2
        	
 	global env
	set env(DISPLAY) :0
	
	if [winfo exists $ana_out1] {destroy $ana_out1}
	toplevel $ana_out1 -width 20c -height 10c
	wm title $ana_out1 "Sub Analog Output"
	
	#---------------------------------------#
	# Element name an its Output			#
	#---------------------------------------#
	frame  $out_name
 	frame $out_aval
        	
	listbox $top_aname_out -yscroll "$ayout_scroll set"  -xscroll "$axout_scroll1 set"  \
		-relief flat -width 45 -height 16 \
		-font {Halvetica 10} -bg white

	listbox $top_aout  -yscroll "$ayout_scroll set"  -xscroll "$axout_scroll2 set"  \
		-relief flat -width 25 -height 16 \
		-font {Halvetica 10} -bg white


	#---------------------------------------#
	# Vertical scrollbar for both listbox		#
	#---------------------------------------#
	
 	scrollbar $ayout_scroll -orient vertical  \
		-command [list bind_yview [list $top_aname_out $top_aout]]
	
	
	#---------------------------------------#
	# Horizontal scrollbar for both listbox		#
	#---------------------------------------#
	
	
	scrollbar $axout_scroll1 -orient horizontal  \
		-command { $top_aname_out xview }
	scrollbar $axout_scroll2 -orient horizontal  \
		-command { $top_aout xview }
	
		
	#-------------------------------------------------------------------#
	# Start inserting name and outputs to listbox	
	#-------------------------------------------------------------------#
		
				
	$top_aname_out insert end  "(TB4-1)  Port Thurst" 			
	$top_aname_out insert end  "(TB4-2)  Lat Thrust" 			
	$top_aname_out insert end  "(TB4-3)  Spare Servo 1" 			
	$top_aname_out insert end  "(TB4-4)  Stbd Thurst" 			
	$top_aname_out insert end  "(TB4-5)  Vert Thrust" 			
	$top_aname_out insert end  "(TB4-6)  Cam Pan" 			
	$top_aname_out insert end  "(TB4-7)  Cam Tilt" 			
	$top_aname_out insert end  "(TB4-8)  Cam Shoulder" 			
	$top_aname_out insert end  "(TB4-9)  Spare 3000 Servo" 			
	$top_aname_out insert end  "(TB4-10) Spare 3000 Servo" 			
	$top_aname_out insert end  "(TB4-11) Manip Swing" 			
	$top_aname_out insert end  "(TB4-12) Manip Shoulder" 			
	$top_aname_out insert end  "(TB4-13) Manip Elbow" 			
	$top_aname_out insert end  "(TB4-14) Manip Pitch" 			
	$top_aname_out insert end  "(TB4-15) Manip Yaw" 			
	$top_aname_out insert end  "(TB4-16) Spare LP Servo"
	
	

        #---------------------------------------------------#	
	# Create a Close button				    #
	#---------------------------------------------------#	
	button $ana_out1.close -text "Close" -width 5 -height 3 \
		-command {destroy $ana_out1}
	
	
	#---------------------------------------------------#	
	# We pack this into separate frames if we want to   #
	# have scrollbar for each listbox                   #
	#---------------------------------------------------#
		
	pack $ana_out1.close -side bottom -pady 2m
	pack  $top_aname_out -side top -fill x
	pack  $top_aout  -side top -fill x
	
	pack $out_name $out_aval  $ayout_scroll  -side left -fill y
	
	#-----------------------------------------------#
	# Now update all the Sub Analog Output		#
	#-----------------------------------------------#
	update_ana_subout
	
}



#-----------------------------------------------------------------#
# This will update the value obtained from sub analog output
# which is send back up through sub analog input channel
# 49-64. Therefore, we can also see this values by opening the
# diagnostic page for "Sub Analog"
#-----------------------------------------------------------------#

proc update_ana_subout { } {
	global ana_out1 top_aname_out top_aout
	
	if { ! [winfo exists $ana_out1] } {return}
	
	# else
	$top_aout delete 0 16
	
	#---------------------------------------#
	# Get the analog value from sub output	#
	# Parameter :  surface/vehicle = 1/0	#
	# 		channel number		#
	#---------------------------------------#
			
	for {set i 49} { $i <= 64 } { incr i } {

		
		set j [expr {$i - 49}]
		set val($j) [get_sub_output $j]
		
				
		# Notice we use "j" because it is our index
	        $top_aout insert $j "$val($j)"

        }

	
	after 250 [list update_ana_subout]


}


##################################################################################


#---------------------------------------------------------------#
# SUB DIGITAL LIST	                      			#
#---------------------------------------------------------------#

proc list_subdig_element { } {

	global subdig sub_dname sub_dval
	
	
	set subdig .subdig_element
	set sub_dname $subdig.top_name
	set sub_dval $subdig.top_value

        global env
	set env(DISPLAY) :0
	
	toplevel $subdig -width 10c -height 10c
	wm title $subdig "Sub Digital Input"
	
	#---------------------------------------#
	# First Column 				#
	#---------------------------------------#
	
	listbox $sub_dname \
		-relief flat -width 50 -height 32 \
		-font {Halvetica 10}
	listbox $sub_dval  \
		-relief flat -width 10 \
		-font {Halvetica 10}
			
	$sub_dname insert end  "1 (TB3-1)  [get_element_name 0 1 1]" 			
	$sub_dname insert end  "2 (TB3-2)  [get_element_name 0 1 2]" 			
	$sub_dname insert end  "3 (TB3-3)  [get_element_name 0 1 3]" 			
	$sub_dname insert end  "4 (TB3-4)  [get_element_name 0 1 4]" 			
	$sub_dname insert end  "5 (TB3-5)  [get_element_name 0 1 5]" 			
	$sub_dname insert end  "6 (TB3-6)  [get_element_name 0 1 6]" 			
	$sub_dname insert end  "7 (TB3-7)  [get_element_name 0 1 7]" 			
	$sub_dname insert end  "8 (TB3-8)  [get_element_name 0 1 8]" 			
	$sub_dname insert end  "9 (TB3-9)  [get_element_name 0 1 9]" 			
	$sub_dname insert end  "10(TB3-10) [get_element_name 0 1 10]" 			
	$sub_dname insert end  "11(TB3-11) [get_element_name 0 1 11]" 			
	$sub_dname insert end  "12(TB3-12) [get_element_name 0 1 12]" 			
	$sub_dname insert end  "13(TB3-13) [get_element_name 0 1 13]" 			
	$sub_dname insert end  "14(TB3-14) [get_element_name 0 1 14]" 			
	$sub_dname insert end  "15(TB3-15) [get_element_name 0 1 15]" 			
	$sub_dname insert end  "16(TB3-16) [get_element_name 0 1 16]" 			
	$sub_dname insert end  "17(TB3-42) [get_element_name 0 1 17]" 			
	$sub_dname insert end  "18(TB3-41) [get_element_name 0 1 18]" 			
	$sub_dname insert end  "19(TB3-40) [get_element_name 0 1 19]" 			
	$sub_dname insert end  "20(TB3-39) [get_element_name 0 1 20]" 			
	$sub_dname insert end  "21(TB3-38) [get_element_name 0 1 21]" 			
	$sub_dname insert end  "22(TB3-37) [get_element_name 0 1 22]" 			
	$sub_dname insert end  "23(TB3-36) [get_element_name 0 1 23]" 			
	$sub_dname insert end  "24(TB3-35) [get_element_name 0 1 24]" 
	$sub_dname insert end  "25(TB3-34) [get_element_name 0 1 25]" 			
	$sub_dname insert end  "26(TB3-33) [get_element_name 0 1 26]" 			
	$sub_dname insert end  "27(TB3-32) [get_element_name 0 1 27]" 			
	$sub_dname insert end  "28(TB3-31) [get_element_name 0 1 28]" 			
	$sub_dname insert end  "29(TB3-30) [get_element_name 0 1 29]" 			
	$sub_dname insert end  "30(TB3-29) [get_element_name 0 1 30]" 			
	$sub_dname insert end  "31(TB3-28) [get_element_name 0 1 31]" 			
	$sub_dname insert end  "32(TB3-27) [get_element_name 0 1 32]" 	

	#---------------------------------------#	
	# Update all the values			#
	#---------------------------------------#
	update_all_subdigital
	
	#---------------------------------------#	
	# Close Button				#
	#---------------------------------------#
	button $subdig.close -text "Close" -width 5 -height 3 \
		-command {destroy $subdig}
	
	#---------------------------------------#	
	# Pack Widget Up			#
	#---------------------------------------#
	
	pack $subdig.close -pady 2m -side bottom	
	pack $sub_dname $sub_dval -side left -fill y
	
	
	
}


proc update_all_subdigital { } {
	global subdig sub_dname sub_dval
	
	if { ! [winfo exists $subdig] } {return}
	
	# else
	$sub_dval delete 0 32
		
	for {set i 1} { $i <= 32 } { incr i } {

		#-----------------------------------------------#
		# For the digital sub input, we will check the	#
		# status in the same way as we check for the 	#
		# alarm status                                  #
		# check_dig_status [ channel # ]		#
		# Note  : channel number needed for this 	#
		# function starts from 0			#
		#-----------------------------------------------#
		
		set j [expr {$i - 1}]
		#set val($j) [get_digital_status 1 $i]
		set val($j) [check_dig_status $j]
		
		#-----------------------------------------------#
		# First Column                                 	#
		#-----------------------------------------------#
		
		if {$val($j) == 0} {
			set val($j) "Off"
		} elseif { $val($j) == 1 } {
			set val($j) "On"
		} else {
			set val($j) "None"
		}
				
		# Notice we use "j" because it is our index
	        $sub_dval insert $j "$val($j)"

        }

	
	after 250 [list update_all_subdigital]


}




#--------------------------------------------------------------
# This procedure list all the top digital ouputs
#--------------------------------------------------------------

proc list_dig_output { num } {

	global dig_out top_dname_out top_dout top_dout_stat
	
	
	set dig_out($num) .top_dig_output($num)
	set yout_scroll $dig_out($num).yscroll
	
	
	# first frame -> name
	set out_name $dig_out($num).name
	set top_dname_out $out_name.top_name
	set xout_scroll1 $out_name.xscroll1
	
        # second frame -> output connection (which board)
        set out_dval $dig_out($num).output
        set top_dout $out_dval.top_value
	set xout_scroll2 $out_dval.xscroll2
	
	# third culomn -> output value from the sub
	set top_dout_stat $dig_out($num).top_dout_stat


		
 	global env
	set env(DISPLAY) :0
	if [winfo exists $dig_out($num)] {destroy $dig_out($num)}
	toplevel $dig_out($num) -width 20c -height 10c
	wm title $dig_out($num) "Sub Digital Output"
	
	#---------------------------------------#
	# Element name and its Output			#
	#---------------------------------------#
	frame  $out_name
        frame $out_dval
        	
	listbox $top_dname_out -yscroll "$yout_scroll set"  -xscroll "$xout_scroll1 set"  \
		-relief flat -width 25 -height 32 \
		-font {Halvetica 10} -bg white

	listbox $top_dout  -yscroll "$yout_scroll set"  -xscroll "$xout_scroll2 set"  \
		-relief flat -width 45 -height 32 \
		-font {Halvetica 10} -bg white

	listbox $top_dout_stat -yscroll "$yout_scroll set"   \
		-relief flat -width 5 -height 32 \
		-font {Halvetica 10} -bg white

	
	
	#---------------------------------------#
	# Vertical scrollbar for both listbox					#
	#---------------------------------------#
	
	scrollbar $yout_scroll -orient vertical  \
		-command [list bind_yview [list $top_dname_out $top_dout $top_dout_stat]]
	
	
	#---------------------------------------#
	# Horizontal scrollbar for both listbox		#
	#---------------------------------------#
	
	
	scrollbar $xout_scroll1 -orient horizontal  \
		-command { $top_dname_out xview }
	scrollbar $xout_scroll2 -orient horizontal  \
		-command { $top_dout xview }
	
	
	 	
	 if {$num == 1 } {
	 	button $dig_out(1).close -text "Close" \
	 		-width 5 -height 3 -command {destroy $dig_out(1)}
	} elseif {$num == 2 } {
	    	button $dig_out(2).close -text "Close" \
	 		-width 5 -height 3 -command {destroy $dig_out(2)}
	} elseif {$num == 3 } {
	    	button $dig_out(3).close -text "Close" \
	 		-width 5 -height 3 -command {destroy $dig_out(3)}
	}
	
	 pack $dig_out($num).close -side bottom -pady 2m
	 pack  $top_dname_out  -side top -fill x
	 pack  $top_dout   -side top -fill x
	 pack $out_name $out_dval $top_dout_stat -side left -fill y
	
	
	 update_dig_subout $num
	
	
}

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

proc get_close_wname { } {
     	global dig_out
     	
     	if [winfo exists $dig_out(1)] {
     	bind $dig_out(1).close <ButtonPress-1> {return 1}
	} elseif [winfo exists $dig_out(2)] {
     	bind $dig_out(2).close <ButtonRelease-1> {return 2}
     	} elseif [winfo exists $dig_out(3)] {     	
     	bind $dig_out(3).close <ButtonRelease-1> {return 3}
     	}

}




proc update_dig_subout { num } {
	global dig_out top_dname_out top_dout top_dout_stat
	                                                                                                                	
	
	if {$num == 1} {
		 #destroy $dig_out(2)
   	 	 #destroy $dig_out(3)
   	 	
		if ![winfo exists $dig_out(1)] {return} 	
		set top_dout_stat $dig_out(1).top_dout_stat
		$top_dout_stat delete 0 32
		
		for {set i 1} { $i <= 32 } { incr i } {
			inserting_data $i
		}

        	
        } elseif { $num == 2 } {
        	#destroy $dig_out(1)
   	 	#destroy $dig_out(3)
   	 	
        	if ![winfo exists $dig_out(2)] {return}
        	set top_dout_stat $dig_out(2).top_dout_stat
        	$top_dout_stat delete 0 32
			
		for {set i 33} { $i <= 64 } { incr i } {
			inserting_data $i
	  	}
        	
        	
   	} else {
   	 	#destroy $dig_out(1)
   	 	#destroy $dig_out(2)
   	 	
   		if ![winfo exists $dig_out(3)] {return}
   		set top_dout_stat $dig_out(3).top_dout_stat
   	   	$top_dout_stat delete 0 32
			
		for {set i 65} { $i <= 96 } { incr i } {
			inserting_data $i
        	}
        	
   	}


	
	after 250 [list update_dig_subout $num]


}


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

proc get_dig_out { elem_num } {

	#---------------------------------------------------#
	# note :                                                                                                         	#
	# display_output [ surface / vehicle ] [ digital / analog ] [ element number ]      	#
	#---------------------------------------------------#
	
        set all_output_info [display_output 1 1 $elem_num]
	set all_output [split $all_output_info |] 		
	#puts "all output-----------$all_output_info"
		
	set temp_count [llength $all_output]
	set count [incr temp_count -1]
	set dout ""	
	
	for {set i 0 } {$i <= $count } {incr i 2} {
			
			# Dig Gesout
			if { [lindex $all_output $i] == 0 } {
				set e_num [lindex $all_output [expr {$i + 1}]]
				if { $e_num == 1}  { lappend dout "(TB1-1)  GESOUT1 $e_num" }
			        if { $e_num == 2}  { lappend dout "(TB1-2)  GESOUT1 $e_num" }
				if { $e_num == 3}  { lappend dout "(TB1-3)  GESOUT1 $e_num" }
				if { $e_num == 4}  { lappend dout "(TB1-4)  GESOUT1 $e_num" }
				if { $e_num == 5}  { lappend dout "(TB1-5)  GESOUT1 $e_num" }
				if { $e_num == 6}  { lappend dout "(TB1-6)  GESOUT1 $e_num" }
				if { $e_num == 7}  { lappend dout "(TB1-7)  GESOUT1 $e_num" }
				if { $e_num == 8}  { lappend dout "(TB1-8)  GESOUT1 $e_num" }		
				if { $e_num == 9}  { lappend dout "(TB1-19) GESOUT1 $e_num" }
				if { $e_num == 10} { lappend dout "(TB1-20) GESOUT1 $e_num" }
				if { $e_num == 11} { lappend dout "(TB1-21) GESOUT1 $e_num" }
				if { $e_num == 12} { lappend dout "(TB1-22) GESOUT1 $e_num" }
				if { $e_num == 13} { lappend dout "(TB1-23) GESOUT1 $e_num" }
				if { $e_num == 14} { lappend dout "(TB1-24) GESOUT1 $e_num" }
				if { $e_num == 15} { lappend dout "(TB1-25) GESOUT1 $e_num" }
				if { $e_num == 16} { lappend dout "(TB1-26) GESOUT1 $e_num" }			
			
				if { $e_num == 17} { lappend dout "(TB1-27) GESOUT1 $e_num" }
				if { $e_num == 18} { lappend dout "(TB1-28) GESOUT1 $e_num" }
				if { $e_num == 19} { lappend dout "(TB1-29) GESOUT1 $e_num" }
				if { $e_num == 20} { lappend dout "(TB1-30) GESOUT1 $e_num" }
				if { $e_num == 21} { lappend dout "(TB1-31) GESOUT1 $e_num" }
				if { $e_num == 22} { lappend dout "(TB1-32) GESOUT1 $e_num" }
				if { $e_num == 23} { lappend dout "(TB1-33) GESOUT1 $e_num" }
				if { $e_num == 24} { lappend dout "(TB1-34) GESOUT1 $e_num" }		
				if { $e_num == 25} { lappend dout "(TB1-45) GESOUT1 $e_num" }
				if { $e_num == 26} { lappend dout "(TB1-46) GESOUT1 $e_num" }
				if { $e_num == 27} { lappend dout "(TB1-47) GESOUT1 $e_num" }
				if { $e_num == 28} { lappend dout "(TB1-48) GESOUT1 $e_num" }
				if { $e_num == 29} { lappend dout "(TB1-49) GESOUT1 $e_num" }
				if { $e_num == 30} { lappend dout "(TB1-50) GESOUT1 $e_num" }
				if { $e_num == 31} { lappend dout "(TB1-51) GESOUT1 $e_num" }
				if { $e_num == 32} { lappend dout "(TB1-52) GESOUT1 $e_num" }			
			
				if { $e_num == 33} { lappend dout "(TB2-1)  GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 34} { lappend dout "(TB2-2)  GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 35} { lappend dout "(TB2-3)  GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 36} { lappend dout "(TB2-4)  GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 37} { lappend dout "(TB2-5)  GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 38} { lappend dout "(TB2-6)  GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 39} { lappend dout "(TB2-7)  GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 40} { lappend dout "(TB2-8)  GESOUT2 [expr {$e_num-32}]" }		
				if { $e_num == 41} { lappend dout "(TB2-19) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 42} { lappend dout "(TB2-20) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 43} { lappend dout "(TB2-21) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 44} { lappend dout "(TB2-22) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 45} { lappend dout "(TB2-23) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 46} { lappend dout "(TB2-24) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 47} { lappend dout "(TB2-25) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 48} { lappend dout "(TB2-26) GESOUT2 [expr {$e_num-32}]" }		
			
				if { $e_num == 49} { lappend dout "(TB2-27) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 50} { lappend dout "(TB2-28) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 51} { lappend dout "(TB2-29) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 52} { lappend dout "(TB2-30) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 53} { lappend dout "(TB2-31) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 54} { lappend dout "(TB2-32) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 55} { lappend dout "(TB2-33) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 56} { lappend dout "(TB2-34) GESOUT2 [expr {$e_num-32}]" }		
				if { $e_num == 57} { lappend dout "(TB2-45) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 58} { lappend dout "(TB2-46) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 59} { lappend dout "(TB2-47) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 60} { lappend dout "(TB2-48) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 61} { lappend dout "(TB2-49) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 62} { lappend dout "(TB2-50) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 63} { lappend dout "(TB2-51) GESOUT2 [expr {$e_num-32}]" }
				if { $e_num == 64} { lappend dout "(TB2-52) GESOUT2 [expr {$e_num-32}]" }	
			
			}
			
			# Dig MPL
			if { [lindex $all_output $i] == 1 } {
				set e_num [lindex $all_output [expr {$i + 1}]]
			  	if { $e_num == 1 }  { lappend dout "(TB8-1,2,3) MPL1 $e_num"}
			        if { $e_num == 2 }  { lappend dout "(TB8-4,5,6)    MPL1 $e_num"}	
			        if { $e_num == 3 }  { lappend dout "(TB8-7,8,9)    MPL1 $e_num"}
			 	if { $e_num == 4 }  { lappend dout "(TB8-10,11,12) MPL1 $e_num"}
			 	if { $e_num == 5 }  { lappend dout "(TB8-13,14,15) MPL1 $e_num"}
			 	if { $e_num == 6 }  { lappend dout "(TB8-16,17,18) MPL1 $e_num"}
			 	if { $e_num == 7 }  { lappend dout "(TB8-19,20,21) MPL1 $e_num"}
			  	if { $e_num == 8 }  { lappend dout "(TB8-22,23,24) MPL1 $e_num"}
			 	if { $e_num == 9 }  { lappend dout "(TB8-25,26,27) MPL1 $e_num"}
			 	if { $e_num == 10 } { lappend dout "(TB8-28,29,30) MPL1 $e_num"}
			 	if { $e_num == 11 } { lappend dout "(TB8-31,32,33) MPL1 $e_num"}
			 	if { $e_num == 12 } { lappend dout "(TB8-34,35,36) MPL1 $e_num"}
			 	if { $e_num == 13 } { lappend dout "(TB8-37,38,39) MPL1 $e_num"}
			 	if { $e_num == 14 } { lappend dout "(TB8-40,41,42) MPL1 $e_num"}			
			  	if { $e_num == 15 } { lappend dout "(TB8-43,44,45) MPL1 $e_num"}
			 	if { $e_num == 16 } { lappend dout "(TB8-46,47,48) MPL1 $e_num"}
			 	
			 	if { $e_num == 17 } { lappend dout "(TB9-1,2,3)    MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 18 } { lappend dout "(TB9-4,5,6)    MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 19 } { lappend dout "(TB9-7,8,9)    MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 20 } { lappend dout "(TB9-10,11,12) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 21 } { lappend dout "(TB9-13,14,15) MPL2 [expr {$e_num-16}]"}
			  	if { $e_num == 22 } { lappend dout "(TB9-16,17,18) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 23 } { lappend dout "(TB9-19,20,21) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 24 } { lappend dout "(TB9-22,23,24) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 25 } { lappend dout "(TB9-25,26,27) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 26 } { lappend dout "(TB9-28,29,30) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 27 } { lappend dout "(TB9-31,32,33) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 28 } { lappend dout "(TB9-34,35,36) MPL2 [expr {$e_num-16}]"}
			  	if { $e_num == 29 } { lappend dout "(TB9-37,38,39) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 30 } { lappend dout "(TB9-40,41,42) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 31 } { lappend dout "(TB9-43,44,45) MPL2 [expr {$e_num-16}]"}
			 	if { $e_num == 32 } { lappend dout "(TB9-46,47,48) MPL2 [expr {$e_num-16}]"}
			 	
			 	if { $e_num == 33 } { lappend dout "(TB10-1,2,3)    MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 34 } { lappend dout "(TB10-4,5,6)    MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 35 } { lappend dout "(TB10-7,8,9)    MPL3 [expr {$e_num-32}]"}	
			  	if { $e_num == 36 } { lappend dout "(TB10-10,11,12) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 37 } { lappend dout "(TB10-13,14,15) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 38 } { lappend dout "(TB10-16,17,18) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 39 } { lappend dout "(TB10-19,20,21) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 40 } { lappend dout "(TB10-22,23,24) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 41 } { lappend dout "(TB10-25,26,27) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 42 } { lappend dout "(TB10-28,29,30) MPL3 [expr {$e_num-32}]"} 	
			 	if { $e_num == 43 } { lappend dout "(TB10-31,32,33) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 44 } { lappend dout "(TB10-34,35,36) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 45 } { lappend dout "(TB10-37,38,39) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 46 } { lappend dout "(TB10-40,41,42) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 47 } { lappend dout "(TB10-43,44,45) MPL3 [expr {$e_num-32}]"}
			 	if { $e_num == 48 } { lappend dout "(TB10-46,47,48) MPL3 [expr {$e_num-32}]"} 	
			 	
			} 	
		
			# Ana Gesdac
			if { [lindex $all_output $i] == 2 } {
				set e_num [lindex $all_output [expr {$i + 1}]]
				lappend dout "Gesdac $e_num"
			
			}
	# end of for loop
	}
	
	return $dout		
	
}


#######################################################################


#-------------------------------------------------------------------------------#
# This procedure returns the digital value from sub output                	#
# Given the channel number from top to find its output connection         	#
# to the sub                                                       		#
#-------------------------------------------------------------------------------#

proc get_dig_subout_status { top_channel } {

	set board_name [lindex  [lindex [get_dig_out $top_channel]  0] 0]
			set elem_num [lindex  [lindex [get_dig_out $top_channel]  0] 1]
			
			if {$board_name == "Gesout"} {set board 0} elseif {$board_name == "MPL" } {set board 1} else {set board -1}
			#puts "loop : board_name : $board_name = $board $elem_num"
			
			set status [get_digital_status $board $elem_num ]
			
			 if { $status == 0 } {
			 	set status "Off"
			 	# if element doesn't exist
			 	if { $board == -1 } { set status "None" }
			 	
			 } elseif { $status == 1 } {
			 	set status "On"
			 } else {
				set status "None"
			 }
			
			 return $status
}


proc inserting_data { i } {

	global top_dname_out top_dout top_dout_stat
	
			set j [expr {$i - 1}]
			
			if [winfo exists $top_dname_out] {$top_dname_out insert end  "$i [get_element_name 1 1 $i]"}
			if [winfo exists $top_dout]	{ $top_dout insert end "$i [get_dig_out $i]" }	 
			
			# The status of digital channel on the sub			
			set val($j) [get_dig_subout_status $i]
				
			 #  we use "j" because it is our index
			if [winfo exists $top_dout_stat] {$top_dout_stat insert $j "$val($j)"}
        	
}



#---------------------------------------------------------------#
# ANALOG OUT LIST								                      			#
#---------------------------------------------------------------#

proc list_anaout_element { } {

	global all_anaout top_aout_name top_aval_name top_raw_name
	
	
	set all_anaout .all_anaout_element
	set top_aout_name $all_anaout.top_name
	set top_aval_name $all_anaout.top_value
	set top_raw_name $all_anaout.top_raw_value

	
        global env
	set env(DISPLAY) :0
	
	if [winfo exists $all_anaout] { destroy $all_anaout }
	toplevel $all_anaout -width 10c -height 10c
	wm title $all_anaout "Top Analog Output"
	
	#---------------------------------------#
	# First Column 				#
	#---------------------------------------#
	
	listbox $top_aout_name \
		-relief flat -width 50 -height 4 \
		-font {Halvetica 10}
	listbox $top_aval_name  \
		-relief flat -width 10 -bg white \
		-font {Halvetica 10}
	listbox $top_raw_name  \
		-relief flat -width 10 -bg white \
		-font {Halvetica 10}
					
		$top_aout_name insert end  "1 TB5-9 [get_element_name 1 3 1]" 			
		$top_aout_name insert end  "2 TB5-27[get_element_name 1 3 2]"		
		$top_aout_name insert end  "3 TB6-9 [get_element_name 1 3 3]" 			
		$top_aout_name insert end  "4 TB6-27[get_element_name 1 3 4]"

	
	#---------------------------------------#	
	# Update all the values			#
	#---------------------------------------#
	update_all_anaout
	
	#---------------------------------------#	
	# Close Button				#
	#---------------------------------------#
	frame $all_anaout.fr
	button $all_anaout.fr.close -text "Close" -width 5 -height 3 \
		-command {destroy $all_anaout}
	
	#---------------------------------------#	
	# Button to Show all Analog Outputs		#
	#---------------------------------------#
	button $all_anaout.fr.out -text "Show Input" -width 10 -height 3 \
		-command {list_sub_ana_input}
	
	#---------------------------------------#	
	# Pack Widget Up			#
	#---------------------------------------#
	
	pack $all_anaout.fr.close $all_anaout.fr.out -padx 2m -pady 2m -side left
	pack $all_anaout.fr -pady 2m -side bottom	
	pack $top_aout_name $top_aval_name $top_raw_name -side left -fill y
	
	
}


proc update_all_anaout { } {
	global all_anaout top_aout_name top_aval_name top_raw_name
	
	if { ! [winfo exists $all_anaout] } {return}
	
	# else
	$top_aval_name delete 0 4
	#$top_raw_name delete 0 4
	
	for {set i 1} { $i <= 4 } { incr i } {

		#---------------------------------------#
		# Get the top analog value 		#
		# Parameter :  	surface/vehicle = 1/0	#
		#		digital/analog = 1/0	#
		# 		channel number		#
		#---------------------------------------#
		
		set j [expr {$i - 1}]
		set val($j) [get_top_analog_value 1 3 $i]
	
		#---------------------------------------#
		# Get the raw analog value 		#
		# Parameter :  	surface/vehicle = 1/0	#
		#		digital/analog = 1/0	#
		# 		channel number		#
		#---------------------------------------#
		set rawvalue($j) [get_raw_analog_value 1 3 $i]   
	
	
		#-----------------------------------------------#
		# First Column                                 	#
		#-----------------------------------------------#
				
		# Notice we use "j" because it is our index
	        $top_aval_name insert $j "$val($j)"
	        $top_raw_name insert $j "$rawvalue($j)"
        }

	
	after 250 [list update_all_anaout]


}



proc list_sub_ana_input { } {

	global ana_in1 top_aout_name_in top_ain 	
	
	set ana_in1 .top_ana_input1
	set ayin_scroll $ana_in1.yscroll
	
	
	# first frame -> name
	set in_name $ana_in1.name
	set top_aout_name_in $in_name.top_name
		
	set axin_scroll1 $in_name.xscroll1
	
        	# second frame -> input
        set in_aval $ana_in1.input
        set top_ain $in_aval.top_value
        	
	set axin_scroll2 $in_aval.xscroll2
        	
 	global env
	set env(DISPLAY) :0
	
	if [winfo exists $ana_in1] {destroy $ana_in1}
	toplevel $ana_in1 -width 30c -height 10c
	wm title $ana_in1 "Sub Analog Input"
	
	#---------------------------------------#
	# Element name an its input			#
	#---------------------------------------#
	frame  $in_name
 	frame $in_aval

        	
	listbox $top_aout_name_in -yscroll "$ayin_scroll set"  -xscroll "$axin_scroll1 set"  \
		-relief flat -width 25 -height 4 \
		-font {Halvetica 10} -bg white

	listbox $top_ain  -yscroll "$ayin_scroll set"  -xscroll "$axin_scroll2 set"  \
		-relief flat -width 25 -height 4 \
		-font {Halvetica 10} -bg white

	#---------------------------------------#
	# Vertical scrollbar for both listbox		#
	#---------------------------------------#
	
 	scrollbar $ayin_scroll -orient vertical  \
		-command [list bind_yview [list $top_aout_name_in $top_ain]]
	
	
	#---------------------------------------#
	# Horizontal scrollbar for both listbox		#
	#---------------------------------------#
	
	
	scrollbar $axin_scroll1 -orient horizontal  \
		-command { $top_aout_name_in xview }
	scrollbar $axin_scroll2 -orient horizontal  \
		-command { $top_ain xview }
		
	#------------------------------------#
	# Start inserting name and inputs to listbox	#
	#------------------------------------#
		
	for {set i 1} {$i <= 4} {incr i} {   		
				
		set the_input [split [display_output 1 3 $i] | ]
		set input [lindex $the_input 0]
		set elem_num [lindex $the_input 1]
		
		#---------------------------------------------------------------------#
		# If it is coming from serial port (heading, depth, etc) 
		# input = SERIAL = 5
		#---------------------------------------------------------------------#
		
		if {$input == 5} {
			
			switch $elem_num {
      		
				# Heading
      				1 { $top_aout_name_in insert end "$i Heading"}
      		
        			# Depth
      				2 { $top_aout_name_in insert end "$i Depth"}

      				# Pitch
      				3 { $top_aout_name_in insert end "$i Pitch"}
      		
      				# Roll
      				4 { $top_aout_name_in insert end "$i Roll"}
      			
      			}
  
  		
  		#---------------------------------------------------------------------#
		# Else, it is coming from sub analog input
		# input = 0
		#---------------------------------------------------------------------#
		    		
      		} else {
      				
			$top_aout_name_in insert end  "$i [get_element_name 0 0 $elem_num] " 			

	}	
	}

        #---------------------------------------------------#	
	# Create a Close button				    #
	#---------------------------------------------------#	
	button $ana_in1.close -text "Close" -width 5 -height 3 \
		-command {destroy $ana_in1}
	
	
	#---------------------------------------------------#	
	# We pack this into separate frames if we want to   #
	# have scrollbar for each listbox                   #
	#---------------------------------------------------#
		
	pack $ana_in1.close -side bottom -pady 2m
	pack  $top_aout_name_in -side top -fill x
	pack  $top_ain -side top -fill x
	
	pack $in_name $in_aval  $ayin_scroll  -side left -fill y
	
	#-----------------------------------------------#
	# Now update all the Sub Analog input		#
	#-----------------------------------------------#
	update_anaout_in
	
	
}



#-----------------------------------------------------------------#
# This will update the value obtained from sub analog input
#-----------------------------------------------------------------#

proc update_anaout_in { } {
	
	global ana_in1 top_aout_name_in top_ain
	
	if { ! [winfo exists $ana_in1] } {return}
		
	# else
	$top_ain delete 0 4
	
	#---------------------------------------#
	# Get the analog value from sub input	
	#---------------------------------------#
			
	for {set i 1} { $i <= 4 } { incr i } {

		set the_input [split [display_output 1 3 $i] | ]
		set input [lindex $the_input 0]
		set elem_num [lindex $the_input 1]
		
		#---------------------------------------------------------------------#
		# If it is coming from serial port (heading, depth, etc) 
		# input = SERIAL = 5
		#---------------------------------------------------------------------#
		
		if {$input == 5} {
			
			
			switch $elem_num {
      		
				# Heading
      				1 { $top_ain insert $i [get_head] }
      		
        			# Depth
      				2 { $top_ain insert $i [get_depth] }

      				# Pitch
      				3 { $top_ain insert $i [get_pitch] }
      		
      				# Roll
      				4 { $top_ain insert $i [get_roll] }
      			
      			}
  	
		
		#---------------------------------------------------------------------#
		# Else, it is coming from sub analog input
		# input = 0
		#---------------------------------------------------------------------#
		      		
      		} else {
				
			if { $elem_num != "" } {
				
				#---------------------------------------------------------------------#
				# get the analog value from sub analog input
				#---------------------------------------------------------------------#
				
				set values [split [ret_io_elem_value 0 0 $elem_num] | ]
				set val($i) [lindex $values 1]

				#---------------------------------------------------------------------#
				# insert it to the listbox at each index i
				#---------------------------------------------------------------------#
				$top_ain insert $i "$val($i)"
				}
   			
   		# end else
   		}
   		
   	# end for loop
        }

	
	after 250 [list update_anaout_in]


}











##################################################################################




#---------------------------------------------------------------#
# SUB ANALOG LIST	                      			#
#---------------------------------------------------------------#


proc list_subana_element { } {

	global subana sub_aname sub_aval sub_rawaval sub_aname2 sub_aval2 sub_rawaval2 all_aout
	
	
	set subana .subana_element
	set sub_aname $subana.sub_name
	set sub_aval $subana.sub_value
	set sub_rawaval $subana.sub_rawvalue
        set sub_aname2 $subana.sub_name2
	set sub_aval2 $subana.sub_value2
	set sub_rawaval2 $subana.sub_rawvalue2	
	
        global env
	set env(DISPLAY) :0
	
	toplevel $subana -width 10c -height 10c
	wm title $subana "Sub Analog Input"
	
	#---------------------------------------#
	# First Column 				#
	#---------------------------------------#
	
	listbox $sub_aname \
		-relief flat -width 30 -height 32 \
		-font {Halvetica 10}
	listbox $sub_aval  \
		-relief flat -width 10 \
		-font {Halvetica 10}
	listbox $sub_rawaval  \
		-relief flat -width 10 \
		-font {Halvetica 10}

	#---------------------------------------#
	# Second Column 			#
	#---------------------------------------#
	
	listbox $sub_aname2 \
		-relief flat -width 30 -height 32 \
		-font {Halvetica 10}
	listbox $sub_aval2  \
		-relief flat -width 10 \
		-font {Halvetica 10}
	listbox $sub_rawaval2  \
		-relief flat -width 10 \
		-font {Halvetica 10}
		
	$sub_aname insert end  "1 (AI1-29) [get_element_name 0 0 1]" 			
	$sub_aname insert end  "2 (AI1-32) [get_element_name 0 0 2]" 
	$sub_aname insert end  "3 (AI1-35) [get_element_name 0 0 3]" 			
	$sub_aname insert end  "4 (AI1-38) [get_element_name 0 0 4]" 
	$sub_aname insert end  "5 (AI1-41) [get_element_name 0 0 5]" 			
	$sub_aname insert end  "6 (AI1-44) [get_element_name 0 0 6]" 
	$sub_aname insert end  "7 (AI1-47) [get_element_name 0 0 7]" 			
	$sub_aname insert end  "8 (AI1-50) [get_element_name 0 0 8]" 
	$sub_aname insert end  "9 (AI1-3)  [get_element_name 0 0 9]" 			
	$sub_aname insert end  "10(AI1-6)  [get_element_name 0 0 10]" 
	$sub_aname insert end  "11(AI1-9)  [get_element_name 0 0 11]" 			
	$sub_aname insert end  "12(AI1-12) [get_element_name 0 0 12]" 
	$sub_aname insert end  "13(AI1-15) [get_element_name 0 0 13]" 			
	$sub_aname insert end  "14(AI1-18) [get_element_name 0 0 14]" 
	$sub_aname insert end  "15(AI1-21) [get_element_name 0 0 15]" 			
	$sub_aname insert end  "16(AI1-24) [get_element_name 0 0 16]" 	
			
	$sub_aname insert end  "17(AI2-29) [get_element_name 0 0 17]" 			
	$sub_aname insert end  "18(AI2-32) [get_element_name 0 0 18]" 
	$sub_aname insert end  "19(AI2-35) [get_element_name 0 0 19]" 			
	$sub_aname insert end  "20(AI2-38) [get_element_name 0 0 20]" 
	$sub_aname insert end  "21(AI2-41) [get_element_name 0 0 21]" 			
	$sub_aname insert end  "22(AI2-44) [get_element_name 0 0 22]" 
	$sub_aname insert end  "23(AI2-47) [get_element_name 0 0 23]" 			
	$sub_aname insert end  "24(AI2-50) [get_element_name 0 0 24]" 
	$sub_aname insert end  "25(AI2-3)  [get_element_name 0 0 25]" 			
	$sub_aname insert end  "26(AI2-6)  [get_element_name 0 0 26]" 
	$sub_aname insert end  "27(AI2-9)  [get_element_name 0 0 27]" 			
	$sub_aname insert end  "28(AI2-12) [get_element_name 0 0 28]" 
	$sub_aname insert end  "29(AI2-15) [get_element_name 0 0 29]" 			
	$sub_aname insert end  "30(AI2-18) [get_element_name 0 0 30]" 
	$sub_aname insert end  "31(AI2-21) [get_element_name 0 0 31]" 			
	$sub_aname insert end  "32(AI2-24) [get_element_name 0 0 32]" 		

	$sub_aname2 insert end  "33(TB6-1)  [get_element_name 0 0 33]" 			
	$sub_aname2 insert end  "34(TB6-2)  [get_element_name 0 0 34]" 
	$sub_aname2 insert end  "35(TB6-3)  [get_element_name 0 0 35]" 			
	$sub_aname2 insert end  "36(TB6-4)  [get_element_name 0 0 36]" 
	$sub_aname2 insert end  "37(TB6-5)  [get_element_name 0 0 37]" 			
	$sub_aname2 insert end  "38(TB6-6)  [get_element_name 0 0 38]" 
	$sub_aname2 insert end  "39(TB6-7)  [get_element_name 0 0 39]" 			
	$sub_aname2 insert end  "40(TB6-8)  [get_element_name 0 0 40]" 
	$sub_aname2 insert end  "41(TB6-10) [get_element_name 0 0 41]" 			
	$sub_aname2 insert end  "42(TB6-11) [get_element_name 0 0 42]" 
	$sub_aname2 insert end  "43(TB6-12) [get_element_name 0 0 43]" 			
	$sub_aname2 insert end  "44(TB6-13) [get_element_name 0 0 44]" 
	$sub_aname2 insert end  "45(TB6-14) [get_element_name 0 0 45]" 			
	$sub_aname2 insert end  "46(TB6-15) [get_element_name 0 0 46]" 
	$sub_aname2 insert end  "47(TB6-16) [get_element_name 0 0 47]" 			
	$sub_aname2 insert end  "48(TB6-17) [get_element_name 0 0 48]" 
			
	#-------------------------------------------------------------------#
	# Inserting DAC name to sub input second listbox	
	#-------------------------------------------------------------------#
				
	$sub_aname2 insert end  "49(TB7-1)  Port Thurst" 			
	$sub_aname2 insert end  "50(TB7-2)  Lat Thrust" 			
	$sub_aname2 insert end  "51(TB7-3)  Spare Servo 1" 			
	$sub_aname2 insert end  "52(TB7-4)  Stbd Thurst" 			
	$sub_aname2 insert end  "53(TB7-5)  Vert Thrust" 			
	$sub_aname2 insert end  "54(TB7-6)  Cam Pan" 			
	$sub_aname2 insert end  "55(TB7-7)  Cam Tilt" 			
	$sub_aname2 insert end  "56(TB7-8)  Cam Shoulder" 			
	$sub_aname2 insert end  "57(TB7-10) Spare 3000 Servo" 			
	$sub_aname2 insert end  "58(TB7-11) Spare 3000 Servo" 			
	$sub_aname2 insert end  "59(TB7-12) Manip Swing" 			
	$sub_aname2 insert end  "60(TB7-13) Manip Shoulder" 			
	$sub_aname2 insert end  "61(TB7-14) Manip Elbow" 			
	$sub_aname2 insert end  "62(TB7-15) Manip Pitch" 			
	$sub_aname2 insert end  "63(TB7-16) Manip Yaw" 			
	$sub_aname2 insert end  "64(TB7-17) Spare LP Servo"
	
	
	#---------------------------------------#	
	# Update all the values			#
	#---------------------------------------#
	update_all_subanalog
	

	#---------------------------------------#	
	# Frame for Close & Show Output buttons				
	#---------------------------------------#
	
	frame $subana.fr

	#---------------------------------------#	
	# Close Button				#
	#---------------------------------------#
	button $subana.fr.close -text "Close" -width 5 -height 3 \
		-command {destroy $subana}
	
	#---------------------------------------#	
	# Button to Show all Top Analog Outputs		#
	#---------------------------------------#
	button $subana.fr.out -text "Show Output" -width 10 -height 3 \
		-command {list_sub_in}
	
	#---------------------------------------#	
	# Pack Widget Up			#
	#---------------------------------------#
	
	pack $subana.fr.close $subana.fr.out -pady 2m -side left
	pack $subana.fr -pady 2m -padx 2m -side bottom	
	pack $sub_aname $sub_aval $sub_rawaval $sub_aname2 $sub_aval2 $sub_rawaval2 -side left -fill y
	
	
	
}


proc update_all_subanalog { } {
	global subana sub_aname sub_aval sub_rawaval sub_aname2 sub_aval2 sub_rawaval2
	
	if { ! [winfo exists $subana] } {return}
	
	# else
	$sub_aval delete 0 32
	$sub_aval2 delete 0 32
	$sub_rawaval delete 0 32
	$sub_rawaval2 delete 0 32
			
	for {set i 1} { $i <= 32 } { incr i } {

		#---------------------------------------#
		# Get the analog value 			#
		# Parameter :  surface/vehicle = 1/0	#
		# 		channel number		#
		#---------------------------------------#
		#---------------------------------------#
		# Get the raw analog value 		#
		# Parameter :  	surface/vehicle = 1/0	#
		#		digital/analog = 1/0	#
		# 		channel number		#
		#---------------------------------------#
		set j [expr {$i - 1}]
		set j2 [expr {$j + 32 }]
		set i2 [expr {$i + 32 }]
		
		set val($j) [get_analog_value 0 $i]
		set rawval($j) [get_raw_analog_value 0 0 $i]   
		
		if {$i2 < 49} {
			set val($j2) "[get_analog_value 0 $i2]"
			#set rawval($j2) 3
			set rawval($j2) "[get_raw_analog_value 0 0 $i2]"   
						
		} else {
			# NOTE: both analog values are equivalent
			set val($j2) "[get_sub_output [expr {$i2 - 49}]]"
			set rawval($j2) $val($j2)   
 
		}
		
		#-----------------------------------------------#
		# First Column                                 	#
		#-----------------------------------------------#
				
		# Notice we use "j" because it is our index
	        $sub_aval insert $j "$val($j)"
                $sub_aval2 insert $j "$val($j2)"
	        $sub_rawaval insert $j "$rawval($j)"
                $sub_rawaval2 insert $j "$rawval($j2)"
        }

	
	after 250 [list update_all_subanalog]


}

	
proc list_sub_in { } {

	global all_aout sub_in sub_out
	
	
	# first frame -> name
	set out_name $all_aout.name
	set sub_in $out_name.top_name
	
        # second frame -> input
        set out_val $all_aout.input
        set sub_out $out_val.top_value
        	
		
 	global env
	set env(DISPLAY) :0
	
	if [winfo exists $all_aout] {destroy $all_aout}
	toplevel $all_aout -width 20c -height 10c
	wm title $all_aout "Top Analog Output"
		
	#---------------------------------------#
	# Element name an its input			#
	#---------------------------------------#
	frame  $out_name
 	frame $out_val
        	
	listbox $sub_in  \
		-relief flat -width 25 -height 4 \
		-font {Halvetica 10} -bg white

	listbox $sub_out \
		-relief flat -width 25 -height 4 \
		-font {Halvetica 10} -bg white


	#------------------------------------#
	# Start inserting name and inputs to listbox	#
	#------------------------------------#
		
	for {set i 1} {$i <= 64} {incr i} {   		
		                                          	
		set the_output [split [display_output 0 0 $i] | ]
		set outp [lindex $the_output 0]
	
		#------------------------------------------------------------------------------#
		# If it is connected to analog output element (digital / analog type  == 3)
		#------------------------------------------------------------------------------#
		
		if {$outp == 3} {
			set elem_num [lindex $the_output 1]
			$sub_in insert end  "$i [get_element_name 1 3 $elem_num] " 			
		} 
	
	# end for loop
	}
	
	

        #---------------------------------------------------#	
	# Create a Close button				    #
	#---------------------------------------------------#	
	button $all_aout.close -text "Close" -width 5 -height 3 \
		-command {destroy $all_aout}
	
	
	#---------------------------------------------------#	
	# We pack this into separate frames if we want to   #
	# have scrollbar for each listbox                   #
	#---------------------------------------------------#
		
	pack $all_aout.close -side bottom -pady 2m
	pack  $sub_in -side top -fill x
	pack  $sub_out  -side top -fill x
	
	pack $out_name $out_val  -side left -fill y
	
	#-----------------------------------------------#
	# Now update all the Sub Analog input		#
	#-----------------------------------------------#
	update_ana_subin
	
}



#-----------------------------------------------------------------#
# This will update the value obtained from sub analog input
#-----------------------------------------------------------------#

proc update_ana_subin { } {
	global all_aout sub_in sub_out
	
	if { ! [winfo exists $all_aout] } {return}
	
	# else
	$sub_out delete 0 4
	
	#---------------------------------------#
	# Get the analog value from sub input	 	
	#---------------------------------------#
			
        		for {set i 1} {$i <= 64} {incr i} {   		
		                                          	
		set the_output [split [display_output 0 0 $i] | ]
		set outp [lindex $the_output 0]
	
		#------------------------------------------------------------------------------#
		# If it is connected to analog output element (digital / analog type  == 3)
		#------------------------------------------------------------------------------#
		
		if {$outp == 3} {
			set elem_num [lindex $the_output 1]
			
			# get the analog value from sub analog input
			set values [split [ret_io_elem_value 1 3 $elem_num] | ]
			set val($i) [lindex $values 1]
      	        	$sub_out insert $i "$val($i)"
		} 
	
	# end for loop
	}
	
        

	
	after 250 [list update_ana_subin]


}





#---------------------------------------------------------------#
# BUTTON LIST	                      			
#---------------------------------------------------------------#


proc list_button_element { } {

	global but_list top_butname top_butval top_butname2 top_butval2 top_butname3 top_butval3 top_butname4 top_butval4   
		
	
	set but_list .but_list_element
	if [winfo exists $but_list] {destroy $but_list}
	
	set top_butname $but_list.top_name
	set top_butval $but_list.top_value
	set top_butname2 $but_list.top_name2
	set top_butval2 $but_list.top_value2
	set top_butname3 $but_list.top_name3
	set top_butval3 $but_list.top_value3	
	set top_butname4 $but_list.top_name4
	set top_butval4 $but_list.top_value4
		
	set but_scroll $but_list.top_scroll

        global env
	set env(DISPLAY) :0
	
	toplevel $but_list -width 10c -height 10c
	wm title $but_list "Button List"
	
	#------------------------------------------------------------------------------#
	# First Frame - GESOUT
	#------------------------------------------------------------------------------#
	
	listbox $top_butname -xscroll "$but_scroll set" \
		-relief flat -width 25 -height 32 \
		-font {Halvetica 10} -bg white
			
	listbox $top_butval  -xscroll "$but_scroll set" \
		-relief flat -width 5 \
		-font {Halvetica 10} -bg white
		
	#------------------------------------------------------------------------------#
	# Second Frame - GESOUT
	#------------------------------------------------------------------------------#
	
	listbox $top_butname2 -xscroll "$but_scroll set" \
		-relief flat -width 25 -height 32 \
		-font {Halvetica 10} -bg grey

	listbox $top_butval2  -xscroll "$but_scroll set" \
		-relief flat -width 5 \
		-font {Halvetica 10} -bg grey

	#------------------------------------------------------------------------------#
	# Third Frame - MPL
	#------------------------------------------------------------------------------#
		
	listbox $top_butname3 -xscroll "$but_scroll set" \
		-relief flat -width 25 -height 32 \
		-font {Halvetica 10} -bg white

	listbox $top_butval3  -xscroll "$but_scroll set" \
		-relief flat -width 5 \
		-font {Halvetica 10} -bg white

	#------------------------------------------------------------------------------#
	# Fourth Frame - MPL
	#------------------------------------------------------------------------------#
		
	listbox $top_butname4 -xscroll "$but_scroll set" \
		-relief flat -width 25 -height 32 \
		-font {Halvetica 10} -bg grey

	listbox $top_butval4  -xscroll "$but_scroll set" \
		-relief flat -width 5 \
		-font {Halvetica 10} -bg grey

	#------------------------------------------------------------------------------#
	# Scrollbar
	#------------------------------------------------------------------------------#

	scrollbar $but_scroll -orient horizontal  \
		-command [list bind_yview [list $top_butname $top_butval $top_butname2 $top_butval2 $top_butname3 $top_butval3 $top_butname4 $top_butval4]]
	
	
	#------------------------------------------------------------------------------#
	# Insert the GESOUT button names 
	#------------------------------------------------------------------------------#
	
	for {set i 1} {$i <= 32} {incr i} {
		
		set i2 [expr {$i + 32}]
		
		$top_butname insert end "GESOUT $i : [get_button_name 0 $i]"
		$top_butname2 insert end "GESOUT $i2 : [get_button_name 0 $i2]"	
		$top_butname3 insert end "MPL $i : [get_button_name 1 $i]"
		$top_butname4 insert end "MPL $i2 : [get_button_name 1 $i2]"
	}
	

	#---------------------------------------#	
	# Update all the values			#
	#---------------------------------------#
	update_all_but_list
	
	#---------------------------------------#	
	# Close Button				#
	#---------------------------------------#
	frame $but_list.fr
	button $but_list.fr.close -text "Close" -width 5 -height 3 \
		-command {destroy $but_list}
		
	#---------------------------------------#	
	# Pack Widget Up			#
	#---------------------------------------#
		
	pack $but_list.fr.close -side left -pady 2m -padx 2m
	pack $but_list.fr -pady 2m -side bottom
	pack $top_butname $top_butval $top_butname2 $top_butval2 \
		$top_butname3 $top_butval3 $top_butname4 $top_butval4 -side left -fill y
	
	
	
}

proc update_all_but_list { } {

	global but_list top_butname top_butval  top_butval2 top_butval3 top_butname4 top_butval4
	
	if { ! [winfo exists $but_list] } {return}
	
	#------------------------------------------------------------------------------#
	# Else, Refresh the screen	
	#------------------------------------------------------------------------------#
	
	$top_butval delete 0 32
	$top_butval2 delete 0 32
	$top_butval3 delete 0 32
	$top_butval4 delete 0 32	
		
	for {set i 1} { $i <= 32 } { incr i } {

		set j [expr {$i - 1}]
		
		#------------------------------------------------------------------------------#
		# First Column - GESOUT                                	
		#------------------------------------------------------------------------------#
		
		set val($j) [get_digital_status 0 $i]
		
		if {$val($j) == 0} {
			set val($j) "Off"
		} elseif { $val($j) == 1 } {
			set val($j) "On"
		} else {
			set val($j) "None"
		}
		
		# Notice we use "j" for our index
	        $top_butval insert $j "$val($j)"
	        
	        #------------------------------------------------------------------------------#
		# Second Column - GESOUT                                	
		#------------------------------------------------------------------------------#
		
		set val2($j) [get_digital_status 0 [expr {$i + 32}] ]
		
		if {$val2($j) == 0} {
			set val2($j) "Off"
		} elseif { $val2($j) == 1 } {
			set val2($j) "On"
		} else {
			set val2($j) "None"
		}
		
		$top_butval2 insert $j "$val2($j)"
  		
  		#------------------------------------------------------------------------------#
		# Third Column - MPL
		#------------------------------------------------------------------------------#
		
		set val3($j) [get_digital_status 1 $i ]
		
		if {$val3($j) == 0} {
			set val3($j) "Off"
		} elseif { $val3($j) == 1 } {
			set val3($j) "On"
		} else {
			set val3($j) "None"
		}
		
		$top_butval3 insert $j "$val3($j)"
  
  		
  		#------------------------------------------------------------------------------#
		# Third Column - MPL
		#------------------------------------------------------------------------------#
		
		set val4($j) [get_digital_status 1 [expr {$i + 32}] ]
		
		if {$val4($j) == 0} {
			set val4($j) "Off"
		} elseif { $val4($j) == 1 } {
			set val4($j) "On"
		} else {
			set val4($j) "None"
		}
		
		$top_butval4 insert $j "$val4($j)"
  
        }

	
	after 250 [list update_all_but_list]


}


#list_button_element

