

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

# Initialize variables   #

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



set depth2x 0

set depth2y 0

set depth2_elem_num 0

set depth2_name ""

set v2f .ventana2.editor.frame

set v2d ""

set depth2_check_var 1

set which_hist2 1

set vsp2 ""

set currd2 ""

set curr_autod2 ""

set alti2 0

set auto_alti2 0

set dmax_text2 ""

set dmin_text2 ""

set dmid_text2 ""

set dup_text2 ""

set dlow_text2 ""

set depth_w1 50

set depth_h1 30

set depth_w2 70

set depth_h2 300

set depth2_timer 0

set dmax 0.0

set dmin 200.0

set setpoint ""

set altitude_and_depth2 0



for {set i 0} { $i < 70 } {incr i } {

        set h2l($i) 0

}





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

# "split" will parse the string according to the delimeter       #

# specified and insert it into list                              #

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



proc obtain_depth2_hist { data } {

   set data_list [ split $data | ]



   depth2 $data_list



}



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

# This procedure sets the x y position of the depth2 graph       #                          #

# It will be called from "elem_prop.tcl" file, where      	#

# the user can specify              				#

# the x and y coordinate of each graphic displayed      	#                                  #

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



proc set_depth2_pos { x y } {

   global depth2x

   global depth2y



   set depth2x $x

   set depth2y $y

}





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

# Get the element number passed which can be used to     #

# define a new window name for more than one depth2bars   #                                                            #

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



proc get_depth2_window_path_name {name} {



   global depth2_name

   set depth2_name $name

}





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

# This will set which type of depth2 is going to be      #

# displayed. Each type of depth2 is different by the     #

# sampling time 					#

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



proc set_which_hist2 { number } {



 	global which_hist2

 	set which_hist2 $number



}



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



proc random {{dmax 1.0} {dmin 0.0}} {

    global randomSeed



    set randomSeed [expr (7141*$randomSeed+54773) % 259200]

    set num  [expr $randomSeed/259200.0*($dmax-$dmin)+$dmin]

    return $num

}



set randomSeed 14823

set random_number 0



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









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

# This procedure will set the variables and then call   #

# another procedure that will draw the depth2 graph      #

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



proc display_depth2_or_not { flag elem_num } {

	

	# Declare variables

   	global depth2x

   	global depth2y

   	

   	global v2f v2d

   	global depth2_elem_num

       	global depth2_check_var



        	

      	set v2f .ventana2.editor.frame$elem_num

       	set depth2_elem_num $elem_num

      	

      	

	# Kept on destroying it and redisplay the new values

	destroy $v2f

	

	if {$flag == 1  && $elem_num >= 1} { 		

		

		if [winfo exists $v2f] { destroy $v2f }

		set depth2_check_var 1

		

		frame $v2f -background black -width 180 -height 435

		place $v2f -x $depth2x -y $depth2y

		

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

   		# create the canvas  (outer rectangle)  #

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

     		set v2d $v2f.depth2



   		canvas $v2d -background black -width 200 -height 500

   		place $v2d -x -1 -y -1





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

    		# Draw the depth2 -> which will recursively call     #

    		# itself again to redraw the position               #

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

    	    	creating_depth2_frame  	

   		depth2

		



	} else {

		

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

		# This will avoid the depth2 being deleted as well when           #

		# we just want to delete bargraph (called from check_graph_type) #

		# We check that if the window exists (depending on the element   #

		# number as part of the window name) then we delete it.          #

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

		

		if [ winfo exists $v2f ] {

		set depth2_check_var 0

 		destroy $v2f

 		}

    	}



}



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

# This procedure will create the frame for the actual depth2 line to  	#

# be displayed. It is drawn in separate procedure because we do not  	#

# need to redraw this frame everytime we update the depth2 bar line.  	#

# Therefore, we just draw it once and leave it there until the user	#

# decided to remove the depth2 bar					#

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



proc creating_depth2_frame { } {



   	global v2d

   	global depth_w1 depth_h1 depth_w2 depth_h2

   	global depth2_name





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

  	# Create the scale on the left hand side               #

  	# remember that (0,0) starts at top left corner and    #

  	# x going to right , y going down as they increases    #

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



   	# Create this rectangle to display the current depth2 line

   	set dbleft [ $v2d create rectangle $depth_w1 $depth_h1 $depth_w2 $depth_h2 -outline white]



   	# Create the another rectangle on the right for the depth2 history

   	set dbright [ $v2d create rectangle $depth_w2 $depth_h1 [expr {$depth_w2 + 80}] $depth_h2 -outline white]



   	# Create the another rectangle on the left for the vertical speed

   	set vspeed [ $v2d create rectangle  [expr {$depth_w2 + 80}] $depth_h1 [expr {$depth_w2 + 100}] $depth_h2 -outline white]





   	# The title (element name)

   	$v2d create text $depth_w1 [expr {$depth_h1 - 20}]  -text  $depth2_name  -fill white -font {Halvetica 10}



   	# Create the little mark/scale on the right side

   	for {set i  $depth_h1} { $i<= $depth_h2 } {incr i 10} {

            $v2d create line [expr {$depth_w2 + 100}] $i [expr {$depth_w2 + 102}] $i -fill white

   	}



   	# Create slightly longer mark/scale on the main right side

   	for {set i $depth_h1} { $i <= $depth_h2 } {incr i 50} {

             $v2d create line [expr {$depth_w2 + 100}] $i [expr {$depth_w2 + 105}] $i -fill white

   	}



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

   	# Creating the text display  #

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



   	

	# Depth

	label $v2d.depth2_label -text "Depth" -fg yellow -bg black -font {Halvetica 10}

	place $v2d.depth2_label -x 5 -y [expr {$depth_h2 + 20}]

	entry $v2d.depth2_entry -textvariable current -bg black \

		-fg white -relief flat -font {Halvetica 10} -width 10

	place $v2d.depth2_entry -x [expr {$depth_w1 + 50}] -y [expr {$depth_h2 + 20}]

	

	# Auto Depth

	label $v2d.auto_dlabel -text "Setpoint" -fg blue -bg black -font {Halvetica 10}

	place $v2d.auto_dlabel -x 5 -y [expr {$depth_h2 + 40}]

	entry $v2d.auto_dentry -textvariable setpoint -bg black \

		-fg white -relief flat -font {Halvetica 10} -width 10

	place $v2d.auto_dentry -x [expr {$depth_w1 + 50}] -y [expr {$depth_h2 + 40}] -height 15

	

	# Altitude

	label $v2d.altlabel -textvariable altitude_type -fg red -bg black -font {Halvetica 10}

	place $v2d.altlabel -x 5 -y [expr {$depth_h2 + 60}]

	entry $v2d.altentry -textvariable altitude -bg black \

		-fg white -relief flat -font {Halvetica 10} -width 10

	place $v2d.altentry -x [expr {$depth_w1 + 50}] -y [expr {$depth_h2 + 60}]

	

    	# Vertical Speed

	label $v2d.vsp_label -text "Vertical Speed" -fg green -bg black -font {Halvetica 10}

	place $v2d.vsp_label -x 5 -y [expr {$depth_h2 + 80}]

	entry $v2d.vsp_entry -textvariable vert_speed -bg black \

		-fg white -relief flat -font {Halvetica 10} -width 10

	place $v2d.vsp_entry -x [expr {$depth_w1 + 50}] -y [expr {$depth_h2 + 80}]

    	

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

   	# The text on the left for vert speed scale		#				#

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

   	global vmax vmin vmid vmid_up vmid_low

   	set vtext 20

   	set vmax 100

   	set vmin -100

   	set vmid [expr {($vmax + $vmin) / 2}]

   	set vmid_up [expr {($vmid + $vmax) / 2}]

   	set vmid_low [expr {($vmid + $vmin) /2}]

   	

   	set vmax_text [ $v2d create text $vtext $depth_h1 -text $vmax -fill white -font {Halvetica 10}]

   	set vup_text [ $v2d create text $vtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h1) / 2 }] -text $vmid_up -fill white -font {Halvetica 10} ]

   	set vmid_text [ $v2d create text $vtext [expr { ($depth_h1 + $depth_h2) / 2 }] -text $vmid -fill white -font {Halvetica 10} ]

   	set vlow_text [ $v2d create text $vtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h2) / 2 }]  -text $vmid_low -fill white -font {Halvetica 10} ]

   	set vmin_text [ $v2d create text $vtext $depth_h2  -text  $vmin  -fill white -font {Halvetica 10} ]

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

   	# Position of the scale mark				#

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

   	

   	set vpmid [expr { ($depth_h1 + $depth_h2) / 2 }]

   	set vpmid_up [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h1) / 2 }]

   	set vpmid_low [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h2) / 2 }]

   	

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

   	# Draw longer scale/mark for the vertical speed 	#

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

   	

   	$v2d create line $depth_w1 $depth_h1 [expr {$depth_w1 - 5}] $depth_h1 -fill white

   	$v2d create line $depth_w1 $vpmid_up [expr {$depth_w1 - 5}] $vpmid_up  -fill white

   	$v2d create line $depth_w1 $vpmid [expr {$depth_w1 - 5}] $vpmid -fill white

   	$v2d create line $depth_w1 $vpmid_low [expr {$depth_w1 - 5}] $vpmid_low -fill white

   	$v2d create line $depth_w1 $depth_h2 [expr {$depth_w1 - 5}] $depth_h2 -fill white

   	

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

   	# Draw smaller scale/mark for the vertical speed 	#

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

   	

   	set vinc [expr {( $vpmid_up - $depth_h1 ) / 5}]

   	for {set i $depth_h1} { $i <= $depth_h2 } {incr i 10} {

             $v2d create line $depth_w1 $i [expr {$depth_w1 - 2}] $i -fill white

   	}

   	

   	





}





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

# This procedure will draw the passed values to the widget   	#

# It takes one parameter which is a list  of all the depth2      #

# values history and also its current depth2                	#

# The current history is the first item on the list and the	#

# rest are the history starting from the most recent history    #

# to the least recent one                                       #

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



proc depth2 { } {





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

   	# Declare variables    #

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





   	global v2d v2f vsp2

   	global hist_list2 h2l  currd2 curr_autod2  alti2 auto_alti2

   	global depth2_check_var  depth2_timer

   	global which_hist2

   	global depth_w1 depth_w2 depth_h1 depth_h2

   	global current setpoint vert_speed altitude

   	global dmax dmin dmax_text2 dmin_text2 dmid_text2 dup_text2 dlow_text2

   	global altitude_and_depth2

   	global altimeter_input altitude_type

   	set count 0

   	set xtext 200



   	



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

        # Make a call to TCL procedure that return history         	#

        # for the depth							#

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

   	

   	set hist_list2 [get_depth_hist $which_hist2]

   	

   	

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

   	# Parse the string and put it into list                      	#

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



   	set alist [ split $hist_list2 | ]





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

   	# Get the current depth value from the list                  	#

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



   	set current [get_depth]

   	set altitude [get_altitude]

   	if {$altimeter_input == "MESOTECH"} {

        	set altitude_type "Mesotech"

        } elseif {$altimeter_input == "TRITECH"} {

          	set altitude_type "Tritech"

        } elseif {$altimeter_input == "DVL"} {

          	set altitude_type "DVL altitude"

        }

   	

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

   	# The vertical speed is obtained from 10 samples			#

   	# Vertical speed units is meter/minutes					#

   	# vert_speed = ( [s1 - s2] + [s2 - s3] + ...+ [s9 - s10] ) / 10 * 60   	#

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

   	

   	set vert_speed 0

   	for {set i 1} {$i <= 10} {incr i} {

   		set j [expr {$i - 1}]

   		set temp_currd [lindex $alist $j]

   		set temp_prevd [lindex $alist $i]

   		set diff [expr {$temp_currd - $temp_prevd }]

   		set vert_speed [expr {$vert_speed + $diff}]

   	                 	

   	}

   	

   	set vert_speed [expr {$vert_speed*6}]

   	



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

   	# The above number is too fast by factor of 2

	# Therefore, we divide it by 2 here

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

 

	set vert_speed [expr {$vert_speed/2}]





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

   	# Set this variable "altitude_and_depth" for drawing 	#

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

   	

   	

    	if {$altitude > 1} {

    		set altitude_and_depth2 [expr $current + $altitude]

    	} else {

    	     	set altitude_and_depth2  $current

    	}



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

   	# Delete the objects and redraw it again 	  #

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

   	

    	if { [winfo exists $v2d] } {

   	

        	$v2d delete $currd2

        	$v2d delete $vsp2

        	$v2d delete $curr_autod2

        	$v2d delete $alti2

        	$v2d delete $auto_alti2

        	$v2d delete $dmax_text2

        	$v2d delete $dmin_text2

        	$v2d delete $dmid_text2

        	$v2d delete $dlow_text2

        	$v2d delete $dup_text2



        	for {set i 0} { $i < 50 } {incr i } {

                	$v2d delete $h2l($i)

        	}



   	}



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

  	# Check if the user wants to remove it or not     	#

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

   	

  	if {![winfo exists $v2d] || $depth2_check_var == 0} {

   		destroy $v2f

   		return

   	}





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

   	# Checking the values for dmax and dmin, so that the      #

   	# current depth2 will always be inside the window          #

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



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

   	# if curr altitude is lower than the bottom of the window#

   	# increase by 100     					#

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

   	

   	if {$altitude_and_depth2 >= $dmin} {

   		set dmin [expr {$dmin  + 100 }]

   		set dmax [expr {$dmax  + 100 }]

   	}

   	

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

   	# It cannot go deeper than 2000 meter  			#

   	# (dmin is the bottom limit)  				#

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

   	

   	if {$dmin >= 2000.0} {

   		set dmin 2000.0

   		set dmax 1800.0

   	}



   	if {$current <= $dmax} {

   		set dmax [expr {$dmax  - 100 }]

   		set dmin [expr {$dmin  - 100 }]

   	}



   	

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

   	# It cannot go higher than 0 meter			#

   	# (dmax is the top limit)   				#

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

   	

   	if {$dmax <= 0} {

   		set dmax 0.0

   		set dmin 200.0

   	}

   	

   	

   	

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

   	# Create the text on the left of bar  			#

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

   	

   	set mid [expr {0.50 * ($dmax + $dmin)  }]

   	set mid_up  [expr {0.50 * ($dmax + $mid)  }]

   	set mid_low [expr {0.50 *  ( $mid + $dmin ) }]



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

   	# The text on the right 				#

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

   	

   	set dmax_text2 [ $v2d create text $xtext $depth_h1 -text $dmax -fill white -font {Halvetica 10}]

   	set dup_text2 [ $v2d create text $xtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h1) / 2 }] -text $mid_up -fill white -font {Halvetica 10} ]

   	set dmid_text2 [ $v2d create text $xtext [expr { ($depth_h1 + $depth_h2) / 2 }] -text $mid -fill white -font {Halvetica 10} ]

   	set dlow_text2 [ $v2d create text $xtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h2) / 2 }]  -text $mid_low -fill white -font {Halvetica 10} ]

   	set dmin_text2 [ $v2d create text $xtext $depth_h2  -text  $dmin  -fill white -font {Halvetica 10} ]





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

   	# Get all the values and map it according to the   	#

   	# canvas coordinates                             	#

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

   	

  	set ratio [ expr { ( $depth_h2 - $depth_h1 ) / ($dmax - $dmin) } ]



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

   	# Draw the altitude line       	                    	#

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

      	set yalt [expr { $depth_h2 - ( ( $altitude_and_depth2 - $dmin ) * $ratio ) } ]

   	set alti2 [$v2d create line [expr {$depth_w2 + 80}] $yalt [expr {$depth_w2 + 100}] $yalt -fill red -width 3]



    	

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

   	# Draw the current depth line                        	#

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

   	

   	set y [expr { $depth_h2 - ( ( $current - $dmin ) * $ratio ) } ]

   	

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

   	# If the depth is less than 0, then just draw          	#

   	# the depth to be 0					#

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

   	

   	if { $y < 0 } { set y 0	}                   	

   	

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

   	# Draw the current depth line                        	#

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

   	

   	set currd2 [$v2d create line [expr {$depth_w2 + 80}] $y [expr {$depth_w2 + 100}] $y -fill yellow -width 3]

   	

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

   	# Draw the vertical speed line                        	#

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

   	 global vpmid vmax vmin vmid

   	

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

   	# vratio = ($depth_h2 - $depth_h1) / ($vmax - $vmin) 	#

   	# 	 = 270 / ($vmid - $vmin)                  	#

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

   	

   	set vratio 1.35

   	set yvert [expr { $vpmid - ($vert_speed * $vratio) } ]

   	set vsp2 [$v2d create line $depth_w1 $yvert $depth_w2 $yvert -fill green -width 3]

   	

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

   	# Draw the SETPOINT               	               	#

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

   	

   	if {[get_auto_depth_status] > 0}  {

   	 	set setpoint [get_auto_depth_setpt]

   		set auto_y [expr { $depth_h2 - ( ( $setpoint - $dmin ) * $ratio ) } ]

   		set curr_autod2 [$v2d create line $depth_w1 $auto_y $depth_w2 $auto_y -fill blue -width 3]

 		

   	} elseif {[get_auto_altitude_status] > 0 } {

   	 	set setpoint [get_auto_depth_setpt]

   		set auto_y [expr { $depth_h2 - ( ( $setpoint - $dmin ) * $ratio ) } ]

   		set curr_autod2 [$v2d create line $depth_w1 $auto_y $depth_w2 $auto_y -fill blue -width 3]

 		        				

   	} else {

   	

   		set setpoint ""

   		$v2d delete $curr_autod2

   	}

   	

   		

   	









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

   	# Get data from the list and draw it to the graph  	#

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

	

   	set x 145

   	set count 0

   	

   	foreach h2list $alist {



   		if {$h2list == "" } { break }



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

   		# Translate each value of depth history to the canvas  	#

   		# coordinate                    			#

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

		

   		set y1 [expr { $depth_h2 - ( ( $h2list - $dmin ) * $ratio ) } ]     	

   	

   		

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

   		# If depth is less than 0, set line to 0             	#

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

		if { $y1 < $depth_h1 } {

   		 	set y1 $depth_h1

   		}

   		

   		if { $y1 > $depth_h2 } {

   			set y1 $depth_h2

   		}

   	

   		

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

   		# Display it on the canvas				#

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

	

   		set h2l($count) [ $v2d create line $x $y1 [expr {$x + 3}] $y1 -fill yellow -width 3 ]

   	

   		incr count

   		incr x -3



   	# end of foreach

   	}





   	



  	after 250 [list depth2]





}







proc redisplay_depth2 { } {



             global depth2_check_var

             global depth2_timer

             global v2f

             global depth2_elem_num



		if {$depth2_check_var == 1} {

			set depth2_timer 0

			destroy $v2f

 			display_depth2_or_not  1 $depth2_elem_num

 			return

   		}

}





