set olay .overlay_screen.canvas
set overlay_top 0
set ov_screen_head 0
set ov_screen_arm 0
set ov_screen_arm_byte 0
set ov_screen_cam 0
set ov_screen_cam_byte 0
set ov_screen_auto_head 0
set ov_screen_auto_depth 0
set ov_screen_depth 0
set ov_screen_time 0
set ov_screen_cruise_speed 0
set ov_screen_cruise_setpoint 0
set ov_screen_annotate 0
set ov_screen_string 0
set overlay_text_message ""
set auto_angle_dvl 0
set overlay_string_enable 0

for {set i 0} {$i < 100} {incr i} {
     	set overlay_item($i) 0
        }

proc overlay_screen { } {

        global olay
        global env
        set env(DISPLAY) :0.2

        toplevel .overlay_screen -bg black
        wm geometry .overlay_screen 800x600-0-0
        canvas $olay -bg black -width 800 -height 600
        place $olay -x -1 -y -1

        #------------------------------------------------------#
        # Draw the heading of the vehicle on overlay screen
        #------------------------------------------------------#

        overlay_heading

}


proc overlay_heading { } {

	global olay angle angle_bak auto_angle overlay_top ov_screen_head
        global ov_screen_arm ov_screen_cam ov_screen_arm_byte ov_screen_cam_byte
        global ov_cam_enable ov_arm_enable
        global ov_screen_time
        global ov_screen_auto_head ov_screen_auto_depth ov_screen_depth
        global ov_screen_cruise_speed ov_screen_cruise_setpoint
        global cruise_setpoint cruise_foreward_speed
        global head_source_input
        global altitude_and_depth auto_angle_dvl auto_angle_bak
        global overlay_text_message ov_screen_annotate
        global ov_screen_string
        global overlay_string_enable overlaystring
        if ![winfo exists $olay] {return} else {

                $olay delete $overlay_top
                $olay delete $ov_screen_head
        	$olay delete $ov_screen_arm
        	$olay delete $ov_screen_cam
        	$olay delete $ov_screen_arm_byte
        	$olay delete $ov_screen_cam_byte
        	$olay delete $ov_screen_auto_head
        	$olay delete $ov_screen_auto_depth
        	$olay delete $ov_screen_depth
        	$olay delete $ov_screen_time        	
        	$olay delete $ov_screen_cruise_speed
        	$olay delete $ov_screen_cruise_setpoint
        	$olay delete $ov_screen_annotate
        	$olay delete $ov_screen_string
        	}

        set temp_angle 0
        if { $head_source_input == "FOG GYRO" } {
               	set temp_angle [lindex [ split $angle . ] 0]
        } elseif {
        	$head_source_input == "BACKUP COMPASS" } {
        	set temp_angle [lindex [ split $angle . ] 0]
        	if { ($temp_angle != 999) } {
        		set temp_angle [expr ($temp_angle%360) ]
        	       	}
        } elseif { $head_source_input == "DVL" } {
               	set angle_dvl [get_dvl_heading]
        	set temp_angle [lindex [ split $angle_dvl . ] 0]
        	}

        set x1 [expr {612 - (2.33 * ($temp_angle%360)) }]

        #------------------------------------------------------#
        # It takes approximately 21 spaces for 100 pixels
        # and for each 45 degrees angle
        #------------------------------------------------------#

        set overlay_top [$olay create text $x1 15 -fill white -font {-family Courier -size 21 -weight bold} \
        -text "N       |       E       |       S       |       W       |       N       |       E       |       S       |       W       |       N       |       E       |       S" ]
        set ov_screen_head [$olay create text 400 35 -font {-family Courier -size 21 -weight bold} -text $temp_angle -fill white ]
	
	if { [get_auto_head_status] == 1 }  {
	        if { $head_source_input == "FOG GYRO" } {
	               set temp_angle [lindex [ split $auto_angle . ] 0]
        	       set ov_screen_auto_head [$olay create text 400 55 -font {-family Courier -size 21 -weight bold} -text $temp_angle -fill white ]
	        } elseif { $head_source_input == "BACKUP COMPASS" } {
	               set temp_angle [lindex [ split $auto_angle_bak . ] 0]
        	       set ov_screen_auto_head [$olay create text 400 55 -font {-family Courier -size 21 -weight bold} -text $temp_angle -fill white ]
	        } elseif { $head_source_input == "DVL" } {
	               set temp_angle [lindex [ split $auto_angle_dvl . ] 0]
        	       set ov_screen_auto_head [$olay create text 400 55 -font {-family Courier -size 21 -weight bold} -text $temp_angle -fill white ]
	        }
   	} else {
   		$olay delete $ov_screen_auto_head
   		}
	
	if { [get_auto_cruise] == 1 }  {
		set ov_screen_cruise_speed [$olay create text 550 580 -font {-family Courier -size 21 -weight bold} -text $cruise_foreward_speed -fill white ]
		set ov_screen_cruise_setpoint [$olay create text 550 560 -font {-family Courier -size 21 -weight bold} -text $cruise_setpoint -fill white ]
		}
	
        set temp_angle [get_depth]
        set ov_screen_depth [$olay create text 75 35 -font {-family Courier -size 21 -weight bold} -text $temp_angle -fill white ]
   	
   	set temp_angle [get_auto_depth_setpt]
        if {[get_auto_depth_status] > 0}  {
   		set ov_screen_auto_depth [$olay create text 75 55 -font {-family Courier -size 21 -weight bold} -text $temp_angle -fill white ]
   	
   	} elseif {[get_auto_altitude_status] > 0 } {
   		set ov_screen_auto_depth [$olay create text 75 55 -font {-family Courier -size 21 -weight bold} -text $temp_angle -fill white ]
   	        				
   	}
        set current_time [get_current_time]
        set ov_screen_time [$olay create text 700 580 -font {-family Courier -size 21 -weight bold} -text $current_time -fill white ]
   	        	
   	        	
   	set temp [split [get_arm_display] |]
        set arm_string [lindex $temp 0]
        set arm_byte [lindex $temp 1]

   	set temp [split [get_cam_display] |]
        set cam_string [lindex $temp 0]
        set cam_byte [lindex $temp 1]

 	if { $ov_arm_enable == 1 } {
 	set ov_screen_arm [$olay create text 300 580 -font {-family Courier -size 21 -weight bold} -text $arm_string -fill white ]
 	set ov_screen_arm_byte [$olay create text 300 560 -font {-family Courier -size 21 -weight bold} -text $arm_byte -fill white ]
 	}
 	set ov_screen_cam [$olay create text 400 580 -font {-family Courier -size 21 -weight bold} -text $cam_string -fill white ]
 	set ov_screen_cam_byte [$olay create text 400 560 -font {-family Courier -size 21 -weight bold} -text $cam_byte -fill white ]
 	
 	set ov_screen_annotate [$olay create text 250 200 -text $overlay_text_message -font {-family Courier -size 21 -weight bold} -fill white -width 360 -justify left -anchor nw ]
 	
 	if { $overlay_string_enable == 1 } {
 	set ov_screen_string [$olay create text 400 560 -font {-family Courier -size 21 -weight bold} -text $overlaystring -fill white ]
 	}
 	#------------------------------------------------------#
        #  Check the overlay database to be displayed on the overlay screen
        #------------------------------------------------------#
        overlay_fields

        after 100 [list overlay_heading]
}



proc overlay_fields { } {

        global olay dive_num
        global overlay_item

        set overlay_size [get_ov_count]

        for {set i 0} {$i < $overlay_size} {incr i} {
	        if [winfo exists $olay] { $olay delete $overlay_item($i)
	        }

        	set temp [split [get_overlay_info $i] |]
        	set temp_type [lindex $temp 0]
        	set temp_channel [lindex $temp 1]
        	set temp_line [lindex $temp 2]
        	set temp_column [lindex $temp 3]
        	set temp_color [lindex $temp 4]
        	
             	
      	       #----------------------------------------#         	
               # Convert the line number to y coordinate to pixels coord
               # line goes from 5-20
               #----------------------------------------#

               set y_item [expr {105+($temp_line-4) * 30 } ]



      	       #----------------------------------------#         	
               # Convert the line number to x coordinate to pixels coord
               # column goes from 1-4
               #----------------------------------------#

               if {$temp_column < 3} {
                    set x_item [expr { 12 + ($temp_column * 70) } ]
               } else {
                    set x_item [expr { 480 + ($temp_column * 70) } ]
               }

               # get analog value from the vehicle

        	set value [get_element_name 0 0 $temp_channel]
        	
        	if {$temp_type == 0 } {set value [get_analog_value 0 $temp_channel]}
        	if {$temp_type == 1 } {set value "Dive# $dive_num"}
        	if {$temp_type == 2 } {
        		set temp_value [get_pitch]
        		set value "$temp_value\xa8"
        		}
        	if {$temp_type == 3 } {
        		set temp_value [get_roll]
        		set value "$temp_value\xa8"
        		}
        	if {$temp_type == 4 } {set value [dvl_altitude]}
        	
               switch $temp_color {
                    0 {set ovr_color yellow}
                    1 {set ovr_color red}
                    2 {set ovr_color green}
                    3 {set ovr_color yellow}
                    4 {set ovr_color blue}
                    5 {set ovr_color magenta}
                    6 {set ovr_color cyan}
                    7 {set ovr_color white}
                    default {set ovr_color white}
               }

               set overlay_item($i) [$olay create text $x_item $y_item -font {-family Courier -size 21 -weight bold} -text $value -fill $ovr_color ]
        	
         }
}

proc redisplay_overlay_screen { } {

	global olay
	
	if [winfo exists $olay] {destroy $olay}

        canvas $olay -bg black -width 800 -height 600
        place $olay -x -1 -y -1

        overlay_heading

}

overlay_screen


