
###########################
# Include the other source files #
##########################


source file_proc.tcl
source submenu/vehicle_submenu.tcl
source submenu/surface_submenu.tcl
source submenu/detritus_submenu.tcl
source submenu/instrument_submenu.tcl
source submenu/tool_submenu.tcl
source submenu/setting_submenu.tcl
source submenu/lightmenu_submenu.tcl
source submenu/lightisol_submenu.tcl
source submenu/log_field.tcl
source special_button.tcl
source alarm_list.tcl
source elem_prop.tcl
#source bar_detail.tcl
source depthbar.tcl
source overlay.tcl
source annotate_box.tcl
source initialize_surface.tcl
source list_all_element.tcl
source out_elem_prop.tcl
source overlay_screen.tcl
source sensor7_ethernet.tcl
source pilot_interface.tcl
sensor7_ethernet 2540

###########################
# Create the toplevel     #
###########################

set env(DISPLAY) :0
toplevel .ventana -bg black
set v .ventana
wm title $v "Ventana Screen 1"




########################
# Initialize variables #
########################

set old_telemetry_status 0
set tel_id 0
set bypass_id 0
set disklog_id 0
set teleos_id 0
set teleos_ethernet_id 0
set tms_id 0
set ship_gyro_id 0
set counter_var 0
set tel_counter_id 0
set telemetry_timer 0
set disk_status_inhibit 1	


#------------------------------------------------------------------------------------


#######################
# Create the menu bar       #
#######################

frame $v.bar -relief raised -bd 2 -width 20c
frame $v.editor -relief flat -width 1012 -height 710 -background black



###########################
# Create the menu buttons         #
###########################

menubutton $v.bar.file -text File -underline 0 \
	-menu $v.bar.file.menu
menubutton $v.bar.maintenance -text Maintenance \
	-menu $v.bar.maintenance.menu
menubutton $v.bar.lightisol -text "Light Isolation" \
	-menu $v.bar.lightisol.menu
menubutton $v.bar.instrument -text Instruments \
	-menu $v.bar.instrument.menu
menubutton $v.bar.tool -text Tools \
	-menu $v.bar.tool.menu
menubutton $v.bar.detritus -text Detritus \
	-menu $v.bar.detritus.menu
button $v.bar.overlay -text "Overlay" -relief flat \
	-command {overlay}
button $v.bar.annotate -text "Annotate" -relief flat \
	-command {annotate_box }
button $v.bar.alarm -text "Alarm List" -relief flat \
	-command {display_alarm_list }


	
#################################################
# Create the menu for the "File" button   	#
#################################################


#-------------------------------------------------------#
# The procedure to create new file, open, save,      	#
# and quit file are in "file_proc.tcl" file         	#
#-------------------------------------------------------#

menu $v.bar.file.menu

# New
$v.bar.file.menu add command -label "New" \
	-command newFile -underline 0

# Open
$v.bar.file.menu add command -label "Open" \
	-command {openFile } -underline 0
	
# Save
$v.bar.file.menu add command -label "Save" \
	-command  {saveFile} -underline 0

# Save As
$v.bar.file.menu add command -label "Save As" \
	-command {saveAs } -underline 5

$v.bar.file.menu add  separator


	
# Calling c++ function "download_init" inside tcl function "download_coonfirm"
$v.bar.file.menu add command -label "Download New Software" \
	-command download_confirm

$v.bar.file.menu add separator

# Quit
$v.bar.file.menu add command -label "Quit" \
	-command exit_program -underline 0

$v.bar.file.menu add separator

. configure -menu $v.bar.file
	


#-------------------------------------------------------#
# Create  Top Menus                                  	#
#-------------------------------------------------------#


# Create the menu for the "Lights Isolation" button
create_lightisol_submenu

# Create the menu for the "Instruments" button
create_instrument_submenu

# Create the menu for the "Tool" button
create_tool_submenu

# Create the menu for the "Detritus" button
create_detritus_submenu

#-------------------------------------------------------#
# Create the menu for the "Maintenance" button		#
# This one has "Setting" and "Element" submenu   	#
#-------------------------------------------------------#

menu $v.bar.maintenance.menu -tearoff 0

$v.bar.maintenance.menu add separator

#-------------------------------------------------------#
# Create the "Setting" submenu                       	#
#-------------------------------------------------------#
$v.bar.maintenance.menu add cascade -label Setting \
	-menu $v.bar.maintenance.menu.setting

$v.bar.maintenance.menu add separator
	

#-------------------------------------------------------#
# Create the "Element" submenu                      	#
#-------------------------------------------------------#

$v.bar.maintenance.menu add cascade -label Element \
	-menu $v.bar.maintenance.menu.element

$v.bar.maintenance.menu add separator


#-------------------------------------------------------#
# Create the menu for the "Setting"                 	#
#-------------------------------------------------------#
create_setting_submenu


#-------------------------------------------------------#
# Create the menu for the "Element"                 	#
# This one has submenu "Surface" and "Vehicle"     	#
#-------------------------------------------------------#
menu $v.bar.maintenance.menu.element


#-------------------------------------------------------#
# Create Display For All The Element    		#
#-------------------------------------------------------#
$v.bar.maintenance.menu.element add cascade -label "Display All Element" \
	-menu $v.bar.maintenance.menu.element.all_element
	

menu $v.bar.maintenance.menu.element.all_element
	
$v.bar.maintenance.menu.element.all_element add command \
	-label "Top Digital" -command list_dig_element

$v.bar.maintenance.menu.element.all_element add command \
	-label "Top Analog" -command list_ana_element

$v.bar.maintenance.menu.element.all_element add command \
	-label "Sub Digital" -command list_subdig_element

$v.bar.maintenance.menu.element.all_element add command \
	-label "Sub Analog" -command list_subana_element

$v.bar.maintenance.menu.element.all_element add command \
	-label "Top Analog Out" -command list_anaout_element
	
$v.bar.maintenance.menu.element.all_element add command \
	-label "Button List" -command list_button_element

$v.bar.maintenance.menu.element add separator

#-------------------------------------------------------#
# Create the "Surface" submenu                         	#
#-------------------------------------------------------#
create_surface_submenu


#-------------------------------------------------------#
# Create the "Vehicle" submenu                        	#
#-------------------------------------------------------#
create_vehicle_submenu

#------------------------------------------------------------------------------------




########################
# Pack all the widgets          #
########################


pack $v.bar.file $v.bar.maintenance \
$v.bar.lightisol $v.bar.instrument $v.bar.tool $v.bar.detritus $v.bar.annotate $v.bar.overlay $v.bar.alarm -in $v.bar -side left -padx 3m


pack $v.bar $v.editor -side top  -fill x




#########################################
# Create special button for arm,        #
# teleos and camera enable  		#
#########################################
create_special_button
# In Button.tcl





#-------------------------------------------------------#
# Make the popup menu stays on top              	#
#-------------------------------------------------------#

bind .ventana <ButtonPress-1> {
	
	# Arm Control , Auto Pilot, and Camera Control
        if [winfo exists .arm_control_popup ] { raise .arm_control_popup }
        if [winfo exists .auto_pilot_popup ] { raise .auto_pilot_popup }
        if [winfo exists .analog_input_list] { raise .analog_input_list }
	if [winfo exists .analog_output_list] { raise .analog_output_list }
	if [winfo exists .digital_input_list] { raise .digital_input_list }
	if [winfo exists .serial_input_list] { raise .serial_input_list }
	if [winfo exists .camera_popup] { raise .camera_popup}
     	

	# BPM and Output
       	if [winfo exists .bpm] { raise .bpm }
	if [winfo exists .add_output] { raise .add_output }
	if [winfo exists .button_add_output] { raise .button_add_output }
	if [winfo exists .slide_add_output] { raise .slide_add_output }
	if [winfo exists .but_small_popup] { raise .but_small_popup }
	if [winfo exists .slide_popup] { raise .slide_popup }
	if [winfo exists .out_bpm] { raise .out_bpm }
	
	
	# Tearoff
        if [winfo exists $v.tearoff1] { raise $v.tearoff1}
        if [winfo exists $v.tearoff2] { raise $v.tearoff2}
	if [winfo exists $v.tearoff3] { raise $v.tearoff3}
	if [winfo exists $v.tearoff4] { raise $v.tearoff4}
	if [winfo exists $v.tearoff5] { raise $v.tearoff5}
	if [winfo exists $v.tearoff6] { raise $v.tearoff6}
        if [winfo exists $v.tearoff7] { raise $v.tearoff7}
	if [winfo exists $v.tearoff8] { raise $v.tearoff8}
	if [winfo exists $v.tearoff9] { raise $v.tearoff9}
	if [winfo exists $v.tearoff10] { raise $v.tearoff10}
	if [winfo exists $v.tearoff11] { raise $v.tearoff11}
        if [winfo exists $v.tearoff12] { raise $v.tearoff12}
        if [winfo exists $v.tearoff13] { raise $v.tearoff13}
        if [winfo exists $v.tearoff14] { raise $v.tearoff14}
        if [winfo exists $v.tearoff15] { raise $v.tearoff15}

    	# Graphic Popup	
    	if [winfo exists .light_menu] { raise .light_menu}
        if [winfo exists .compass_popup] { raise .compass_popup}        	
        if [winfo exists .vehicle_popup] { raise .vehicle_popup}        	
     	if [winfo exists .but_small_popup] { raise .but_small_popup}
     	if [winfo exists .slide_popup] { raise .slide_popup}

        if [winfo exists .annotate_box] { raise .annotate_box }         	
     	if [winfo exists .overlay] { raise .overlay }        	
     	if [winfo exists .alarm_list] { raise .alarm_list}
     	if [winfo exists .analog_overlay] { raise .analog_overlay}
     	
     	if [winfo exists .save_or_not] { raise .save_or_not}
        if [winfo exists .tms_popup] { raise .tms_popup}
        if [winfo exists .gf] { raise .gf}
        if [winfo exists .dive] { raise .dive}
	if [winfo exists .auto_cruise] { raise .auto_cruise}
        if [winfo exists .teleos_setup] { raise .teleos_setup}
	if [winfo exists .latitude] { raise .latitude}
	if [winfo exists .logging] { raise .logging}
	if [winfo exists .software] { raise .software}
	
	# Diagnostic page
	if [winfo exists .dig_element] { raise .dig_element}
	if [winfo exists .ana_element] { raise .ana_element}
	if [winfo exists .subdig_element] { raise .subdig_element}
	if [winfo exists .subana_element] { raise .subana_element}
	if [winfo exists .top_ana_output1] { raise .top_ana_output1}
	if [winfo exists .top_dig_output(1)]  { raise .top_dig_output(1)}
     	if [winfo exists .top_dig_output(2)]  { raise .top_dig_output(2)}
     	if [winfo exists .top_dig_output(3)]  { raise .top_dig_output(3)}
     	if [winfo exists .all_anaout_element] { raise .all_anaout_element}
	if [winfo exists .all_analog_output] { raise .all_analog_output }
	if [winfo exists .top_ana_input1] { raise .top_ana_input1 }
	if [winfo exists .but_list_element] { raise .but_list_element }
	
		
        # Other Dialog Box
        if [winfo exists .__tk_filedialog] { raise .__tk_filedialog }
	if [winfo exists .save_or_not] { raise .save_or_not }
	if [winfo exists .wrong_ext] { raise .wrong_ext }	
	if [winfo exists .dlconf] { raise .dlconf }	
	
	#puts "%W"
}





#------------------------------------------------------------------------------------


##############################
# Create Telemetry button    #
##############################

proc check_telemetry_status { } {

 	
	global v disk_status
	global counter_var old_telemetry_status tel_id bypass_id disklog_id
	global tel_counter_id  telemetry_timer
	global teleos_status tms_status ship_gyro_status
	global teleos_id tms_id ship_gyro_id
	global teleos_ethernet_id teleos_ethernet_status
	global disk_status_inhibit
		
	if [winfo exists $v.editor.tframe] {	
		$v.editor.tframe delete $tel_id
		$v.editor.tframe delete $bypass_id
		$v.editor.tframe delete $disklog_id
		$v.editor.tframe delete $teleos_id
		$v.editor.tframe delete $teleos_ethernet_id
		$v.editor.tframe delete $ship_gyro_id
		$v.editor.tframe delete $tms_id
		$v.editor.tframe delete $tel_counter_id

	}
	
	#-----------------------------------------------------------------------#
	# This variable is incremented whenever we open a file                	#
	# and here we check if it is greater than one then we return          	#
	# from this procedure and re-run the loop again so it would          	#
	# not slow down the graphic                                   		#
	#-----------------------------------------------------------------------#           	
			
	if { ![winfo exists $v] } { return }
	
	
   	#--------------------#
   	# update the timer   #
   	#--------------------#
   	incr telemetry_timer
	
   	if { $telemetry_timer >= 200 } {
   	 	redisplay_telemetry
   	 	return
   	}
	#puts "$telemetry_timer"
	
	set tel_counter_id [ $v.editor.tframe create text 85 13 -text "$counter_var" \
			     -font {Halvetica 10} -fill white]
		
	# If we get the telemetry, display green button
	if {[get_telemetry_status] == 1 } {
		set counter_var [bad_packet_counter]
		set tel_id [ $v.editor.tframe create rectangle 120 5 130 15 -fill green]
			
		#---------------------------------------------------#
		# Check if the telemetry_is previously 0 (off) or   #
		# not the same as the current status, then 	    #
		# we need to update the analog and digitals	    #
		# according to the vehicle default state            #
		# "initialize_surface" will be called from file     #
		# "initialize_surface.tcl"			    #
		#---------------------------------------------------#
		
		if {$old_telemetry_status != [get_telemetry_status]} {
			initialize_surface
		
		}
		
		#---------------------------------------------------------#
		# Set the old_telemetry status to be 0 so that we WONT
		# update the surface when we get the telemetry back
		#---------------------------------------------------------#
		
		set old_telemetry_status 1
			
			
			
	} else {
		
		set counter_var [bad_packet_counter]
		
		# Change Telemetry Status To Red	
		#$v.editor.tframe delete $tel_id
		set tel_id [ $v.editor.tframe create rectangle 120 5 130 15 -fill red]
		
		#---------------------------------------------------------#
		# Set the old_telemetry status to be 0 so that we WILL
		# update the surface when we get the telemetry back
		#---------------------------------------------------------#
		
		set old_telemetry_status 0
	}
	
	#-------------------------------------------------------#
	# Here, we also check for the BYPASS status as well     #
	#-------------------------------------------------------# 	
	
	
	# Bypass = Surface Digital Element (Surface = 1, Channel 39)
	if { [get_digital_value 1 39] == 1 } {
		#$v.editor.tframe delete $bypass_id
	  	set bypass_id [ $v.editor.tframe create rectangle 120 20 130 30 -fill green]
			
	} else {
		#$v.editor.tframe delete $bypass_id
	  	set bypass_id [ $v.editor.tframe create rectangle 120 20 130 30 -fill red]
			
			
	}
	
	
	#-------------------------------------------------------#
	# Here, we also check for the Disk Logging status as    #
	# well. The variable "disk_status" is initialized and   #
	# declared inside file "log_field.tcl".			#
	# Note: when we update the variable "disk_status" , then the next time
	# we open "log_field.tcl", the status of the enable button will also
	# get updated depending on the value we set here
	#-------------------------------------------------------# 	
	
	if { $disk_status_inhibit == 1 } {
		set disk_status [get_disk_status]
		}
		
	if { $disk_status == 1 } {
		set disklog_id [ $v.editor.tframe create rectangle 120 35 130 45 -fill green]
	} else {
		set disklog_id [ $v.editor.tframe create rectangle 120 35 130 45 -fill red]
	}

	#-------------------------------------------------------#
	# Here, we also check for the Teleos status as          #
	# well. The variable "teleos_status" is initialized and #
	# declared inside file "commands.c".			#
	#-------------------------------------------------------# 	
	
	set teleos_status [get_teleos_status]
	
	if { $teleos_status == 1 } {
		set teleos_id [ $v.editor.tframe create rectangle 120 50 130 60 -fill green]
	} else {
		set teleos_id [ $v.editor.tframe create rectangle 120 50 130 60 -fill red]
	}

	#-------------------------------------------------------#
	# Here, we also check for the Teleos Ethernet status    #
	# well. The variable "teleos_status" is initialized and #
	# declared inside file "sensor7_ethernet.tcl".			#
	#-------------------------------------------------------# 	
	
	if { $teleos_ethernet_status == 1 } {
		set teleos_ehternet_id [ $v.editor.tframe create rectangle 120 65 130 75 -fill green]
	} else {
		set teleos_ethernet_id [ $v.editor.tframe create rectangle 120 65 130 75 -fill red]
	}

	#-------------------------------------------------------#
	# Here, we also check for the Ship gyro status as       #
	# well. The variable "ship_gyro_status" is initialized  #
	# and declared inside file "commands.c".		#
	#-------------------------------------------------------# 	
	
	set ship_gyro_status [get_ship_gyro_status]
	
	if { $ship_gyro_status == 1 } {
		set ship_gyro_id [ $v.editor.tframe create rectangle 120 80 130 90 -fill green]
			
	} else {
		set ship_gyro_id [ $v.editor.tframe create rectangle 120 80 130 90 -fill red]
	}
		
	#-------------------------------------------------------#
	# Here, we also check for the TMS String status as      #
	# well. The variable "TMS_status" is initialized and    #
	# declared inside file "commands.c".			#
	#-------------------------------------------------------# 	
	
	set tms_status [get_tms_status]
	
	if { $tms_status == 1 } {
		set tms_id [ $v.editor.tframe create rectangle 120 95 130 105 -fill green]
	} else {
		set tms_id [ $v.editor.tframe create rectangle 120 95 130 105 -fill red]

	}
	
		
	
	
	#########################################################
	# CATCH ALL THE ERROR WINDOW			    	#
	#########################################################
	
	#catch {destroy .bgerrorTrace}                         	
	#catch {destroy .bgerrorDialog}

	#########################################################
	
        after 500 check_telemetry_status

}


proc create_status_frame { } {
	
	global v

	# Create frame for Telemetry status
	if [winfo exists $v.editor.tframe] {destroy $v.editor.tframe}

	canvas $v.editor.tframe -bg black -width 160 -height 110
	place $v.editor.tframe -x 630 -y 0

	# The Telemetry and Bypass label
	label $v.editor.tframe.lab -text "Telemetry" -bg black -fg white \
		-font {Halvetica 10}
	place $v.editor.tframe.lab -x 5 -y 5	


	label $v.editor.tframe.bypass_lab -text "Bypass" -bg black -fg white \
		-font {Halvetica 10}
	place $v.editor.tframe.bypass_lab -x 5 -y 20
	
	# The Disk Logging label
	
	label $v.editor.tframe.disklog_lab -text "Disk Logging" -bg black -fg white \
		-font {Halvetica 10}
	place $v.editor.tframe.disklog_lab -x 5 -y 35
	
	# The Teleos label
	
	label $v.editor.tframe.teleos_lab -text "Teleos Serial" -bg black -fg white \
		-font {Halvetica 10}
	place $v.editor.tframe.teleos_lab -x 5 -y 50
	
	# The Teleos label
	
	label $v.editor.tframe.teleos_ethernet_lab -text "Teleos Ethernet" -bg black -fg white \
		-font {Halvetica 10}
	place $v.editor.tframe.teleos_ethernet_lab -x 5 -y 65
	
	# The Ship Gyro label
	
	label $v.editor.tframe.ship_gyro_lab -text "Ship Gyro" -bg black -fg white \
		-font {Halvetica 10}
	place $v.editor.tframe.ship_gyro_lab -x 5 -y 80
	
	# The TMS label
	
	label $v.editor.tframe.tms_lab -text "TMS" -bg black -fg white \
		-font {Halvetica 10}
	place $v.editor.tframe.tms_lab -x 5 -y 95
	
	
	# Call this only once in this function
	check_telemetry_status
	
}


#-------------------------------------------------------#
# Call this procedure in the beginning to display      	#
# telemetry status and also bypass status              	#
#-------------------------------------------------------#
create_status_frame



proc redisplay_telemetry { } {
	global telemetry_timer telemetry_loop_id
	
	set telemetry_timer 0
	
	#puts "redisplay telemetry status"
	#----------------------------------------------------------------#
	# Destroy the frame and recreate the graphics
	#----------------------------------------------------------------#
	
	create_status_frame
	

}

proc download_confirm { } {

	global env
	set env(DISPLAY) :0
    	toplevel .dlconf
    	wm title .dlconf "Download New Software ?"
    	wm geometry .dlconf 275x100+300+300

    	message .dlconf.lab \
    		-text "Are You Sure You Want To Download New Software ?" \
    		-justify center -width 7c
    	button .dlconf.yes -text "Yes, Download Now !" \
    		-width 20 -height 3 -command {download_init}
    	button .dlconf.cancel -text "Cancel" \
    		-width 15 -height 3 -command {destroy .dlconf}
    	
    	pack .dlconf.lab -side top  -padx 2m -pady 2m
    	pack .dlconf.yes .dlconf.cancel -side left -padx 2m -pady 2m	
    		
    	bind .dlconf <Return> \
        { .dlconf.cancel flash; destroy .dlconf }	

}