

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

set depthx 0
set depthy 0
set depth_elem_num 0
set depth_name ""
set vf .ventana.editor.frame
set vd ""
set depth_check_var 1
set which_hist 1
set vsp ""
set currd ""
set curr_autod ""
set alti 0
set auto_alti 0
set dmax_text ""
set dmin_text ""
set dmid_text ""
set dup_text ""
set dlow_text ""
set depth_w1 50
set depth_h1 30
set depth_w2 70
set depth_h2 300
set depth_timer 0
set dmax 0.0
set dmin 200.0
set setpoint ""
set altitude_and_depth 0
set depth_loop_id 0

for {set i 0} { $i < 70 } {incr i } {
        set hl($i) 0
}


#----------------------------------------------------------------#
# "split" will parse the string according to the delimeter       #
# specified and insert it into list                              #
#----------------------------------------------------------------#

proc obtain_depth_hist { data } {
   set data_list [ split $data | ]

   depth $data_list

}

#---------------------------------------------------------------#
# This procedure sets the x y position of the depth 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_depth_pos { x y } {
   global depthx
   global depthy

   set depthx $x
   set depthy $y
}


#--------------------------------------------------------#
# Get the element number passed which can be used to     #
# define a new window name for more than one depthbars   #                                                            #
#--------------------------------------------------------#

proc get_depth_window_path_name {name} {

   global depth_name
   set depth_name $name
}


#-------------------------------------------------------#
# This will set which type of depth is going to be      #
# displayed. Each type of depth is different by the     #
# sampling time 					#
#-------------------------------------------------------#

proc set_which_hist { number } {

 	global which_hist
 	set which_hist $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 depth graph      #
#-------------------------------------------------------#

proc display_depth_or_not { flag elem_num } {
	
	# Declare variables
   	global depthx
   	global depthy
   	
   	global vf vd
   	global depth_elem_num
       	global depth_check_var

        	
      	set vf .ventana.editor.frame$elem_num
       	set depth_elem_num $elem_num
      	
      	
	# Kept on destroying it and redisplay the new values
	destroy $vf
      	
	
	if {$flag == 1  && $elem_num >= 1} { 		
		
		if [winfo exists $vf] { destroy $vf }
		set depth_check_var 1

		frame $vf -background black -width 220 -height 435
		
		place $vf -x $depthx -y $depthy
		
		#---------------------------------------#
   		# create the canvas  (outer rectangle)  #
  		#---------------------------------------#
    		set vd $vf.depth
  		canvas $vd -background black -width 220 -height 500
  		place $vd -x -1 -y -1

		#---------------------------------------------------#
    		# Draw the depth -> which will recursively call     #
    		# itself again to redraw the position               #
    		#---------------------------------------------------#
    	    	creating_depth_frame  	
   		depth
	} else {
		#----------------------------------------------------------------#
		# This will avoid the depth 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 $vf ] {
		set depth_check_var 0
 		destroy $vf
 		}
    	}

}


#-----------------------------------------------------------------------#
# This procedure will create the frame for the actual depth line to  	#
# be displayed. It is drawn in separate procedure because we do not  	#
# need to redraw this frame everytime we update the depth bar line.  	#
# Therefore, we just draw it once and leave it there until the user	#
# decided to remove the depth bar					#
#-----------------------------------------------------------------------#

proc creating_depth_frame { } {
   	global vd
   	global depth_w1 depth_h1 depth_w2 depth_h2
   	global depth_name vpmid vmax vmin vmid

  	#------------------------------------------------------#
 	# 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 depth line
   	set dbleft [ $vd create rectangle $depth_w1 $depth_h1 $depth_w2 $depth_h2 -outline white]

   	# Create the another rectangle on the left for the vertical speed
   	set dbright [ $vd create rectangle $depth_w2 $depth_h1 [expr {$depth_w2 + 80}] $depth_h2 -outline white]

   	# Create the another rectangle on the right for the depth history
   	set vspeed [ $vd create rectangle  [expr {$depth_w2 + 80}] $depth_h1 [expr {$depth_w2 + 100}] $depth_h2 -outline white]


   	# The title (element name)
   	$vd create text $depth_w1 [expr {$depth_h1 - 20}]  -text  $depth_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} {
            $vd 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} {
             $vd create line [expr {$depth_w2 + 100}] $i [expr {$depth_w2 + 105}] $i -fill white
   	}

   	#----------------------------#
   	# Creating the text display  #
   	#----------------------------#

   	
	# Depth
	label $vd.depth_label -text "Depth" -fg yellow -bg black -font {Halvetica 10}
	place $vd.depth_label -x 5 -y [expr {$depth_h2 + 20}]
	entry $vd.depth_entry -textvariable current -bg black \
		-fg white -relief flat -font {Halvetica 10} -width 10 -state disabled
	place $vd.depth_entry -x [expr {$depth_w1 + 50}] -y [expr {$depth_h2 + 20}]
	
	# Auto Depth
	label $vd.auto_dlabel -text "Setpoint" -fg blue -bg black -font {Halvetica 10}
	place $vd.auto_dlabel -x 5 -y [expr {$depth_h2 + 40}]
	entry $vd.auto_dentry -textvariable setpoint -bg black \
        	-fg white -relief flat -font {Halvetica 10} -width 10 -state disabled
	place $vd.auto_dentry -x [expr {$depth_w1 + 50}] -y [expr {$depth_h2 + 40}] -height 15

	# Altitude
	label $vd.altlabel -textvariable altitude_type -bg black -fg red -font {Halvetica 10}
	place $vd.altlabel -x 5 -y [expr {$depth_h2 + 60}]
	entry $vd.altentry -textvariable altitude -bg black \
		-fg white -relief flat -font {Halvetica 10} -width 10 -state disabled
	place $vd.altentry -x [expr {$depth_w1 + 50}] -y [expr {$depth_h2 + 60}]
	
	
    	# Vertical Speed
	label $vd.vsp_label -text "Vertical Speed" -fg green -bg black -font {Halvetica 10}
	place $vd.vsp_label -x 5 -y [expr {$depth_h2 + 80}]
	entry $vd.vsp_entry -textvariable vert_speed -bg black \
		-fg white -relief flat -font {Halvetica 10} -width 10 -state disabled
	place $vd.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 [ $vd create text $vtext $depth_h1 -text $vmax -fill white -font {Halvetica 10}]
   	set vup_text [ $vd create text $vtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h1) / 2 }] -text $vmid_up -fill white -font {Halvetica 10} ]
   	set vmid_text [ $vd create text $vtext [expr { ($depth_h1 + $depth_h2) / 2 }] -text $vmid -fill white -font {Halvetica 10} ]
   	set vlow_text [ $vd create text $vtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h2) / 2 }]  -text $vmid_low -fill white -font {Halvetica 10} ]
   	set vmin_text [ $vd 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 	#
   	#-------------------------------------------------------#	
   	
   	$vd create line $depth_w1 $depth_h1 [expr {$depth_w1 - 5}] $depth_h1 -fill white
   	$vd create line $depth_w1 $vpmid_up [expr {$depth_w1 - 5}] $vpmid_up  -fill white
   	$vd create line $depth_w1 $vpmid [expr {$depth_w1 - 5}] $vpmid -fill white
   	$vd create line $depth_w1 $vpmid_low [expr {$depth_w1 - 5}] $vpmid_low -fill white
   	$vd 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} {
             $vd 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 depth      #
# values history and also its current depth                	#
# 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 depth { } {

	#----------------------#
  	# Declare variables    #
   	#----------------------#
   	global vd vf vsp
   	global hist_list hl  currd curr_autod  alti auto_alti
   	global depth_check_var  depth_timer
   	global which_hist
   	global depth_w1 depth_w2 depth_h1 depth_h2
   	global current setpoint vert_speed altitude
	global dmax dmin dmax_text dmin_text dmid_text dup_text dlow_text
   	global altitude_and_depth
   	global temp_vsp
   	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_list [get_depth_hist $which_hist]
   	#---------------------------------------------------------------#
   	# Parse the string and put it into list                      	#
 	#---------------------------------------------------------------#

   	set alist [ split $hist_list | ]
   	#---------------------------------------------------------------#
   	# Get the current depth value from the list                  	#
   	#---------------------------------------------------------------#
   	#set raw_depth [get_raw_depth]
   	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_depth [expr $current + $altitude]
    	} else {
    	     	set altitude_and_depth  $current
    	}


   	#-------------------------------------------------#
   	# Delete the objects and redraw it again 	  #
   	#-------------------------------------------------#
   	
    	if { [winfo exists $vd] } {
   	
        	$vd delete $currd
        	$vd delete $vsp
        	$vd delete $curr_autod
        	$vd delete $alti
        	$vd delete $auto_alti
        	$vd delete $dmax_text
        	$vd delete $dmin_text
        	$vd delete $dmid_text
        	$vd delete $dlow_text
        	$vd delete $dup_text

        	for {set i 0} { $i < 50 } {incr i } {
                	$vd delete $hl($i)
        	}

   	}

        #-------------------------------------------------------#	
  	# Check if the user wants to remove it or not     	#
   	#-------------------------------------------------------#
   		
   	if {![winfo exists $vd] || $depth_check_var == 0} {
   		destroy $vf
   		return
   	}

  	
   	#---------------------------------------------------------#
   	# Checking the values for dmax and dmin, so that the      #
   	# current depth will always be inside the window          #
   	#---------------------------------------------------------#

   	#-------------------------------------------------------#	
   	# if curr altitude is lower than the bottom of the window,	#
   	# increase by 100     					#
   	#-------------------------------------------------------#	
   	
   	if {$altitude_and_depth >= $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_text [ $vd create text $xtext $depth_h1 -text $dmax -fill white -font {Halvetica 10}]
   	set dup_text [ $vd create text $xtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h1) / 2 }] -text $mid_up -fill white -font {Halvetica 10} ]
   	set dmid_text [ $vd create text $xtext [expr { ($depth_h1 + $depth_h2) / 2 }] -text $mid -fill white -font {Halvetica 10} ]
   	set dlow_text [ $vd create text $xtext [expr { (( ($depth_h1 + $depth_h2) / 2) + $depth_h2) / 2 }]  -text $mid_low -fill white -font {Halvetica 10} ]
   	set dmin_text [ $vd 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_depth - $dmin ) * $ratio ) } ]
   	set alti [$vd 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 currd [$vd 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 vsp [$vd 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_autod [$vd 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_autod [$vd create line $depth_w1 $auto_y $depth_w2 $auto_y -fill blue -width 3]
 		        				
   	} else {
   		set setpoint ""
   		$vd delete $curr_autod
  	}
   	
 	
   	#-------------------------------------------------------#
   	# Get data from the list and draw it to the graph  	#
   	#-------------------------------------------------------#

	set x 145
   	set count 0
   	   	
   	foreach hlist $alist {
                if {$hlist == "" || $hlist == " "} { break }


   		#-------------------------------------------------------#
  		# Translate each value of depth history to the canvas  	#
   		# coordinate                    			#
   		#-------------------------------------------------------#
	
   		set y1 [expr { $depth_h2 - ( ( $hlist - $dmin ) * $ratio ) } ]     	
  		
  		#-------------------------------------------------------#
   		# If depth is less than 0, set line to 0             	#
  		#-------------------------------------------------------#
		
		if { $y1 < $depth_h1 } { set y1 $depth_h1 }
  		
    	        #-------------------------------------------------------#
  		# Display it on the canvas				#
  		#-------------------------------------------------------#
	
  		set hl($count) [ $vd create line $x $y1 [expr {$x + 3}] $y1 -fill yellow -width 3 ]
   	
   		incr count
   		incr x -3

   	# end of foreach
   	}

   	global depth_loop_id depth_elem_num
  	set depth_loop_id [after 250 [list depth]]
}



proc redisplay_depth { } {

             global depth_check_var
             global depth_timer
             global vf
             global depth_elem_num

 		if {$depth_check_var == 1} {
 			set depth_timer 0
 			destroy $vf
  			display_depth_or_not  1 $depth_elem_num
  			return
    		}
 }


