#------------------------------------------------------------#
# Include source files that contain some of the procedure    			#
# definition we are going to use in this file                				#
#------------------------------------------------------------#


source input.tcl
source submenu/surface_submenu.tcl
source submenu/vehicle_submenu.tcl

#-----------------------------------------------------#
# Initialize all the variables that are going to be   			#
# sent down to C++ database                          			 #
#-----------------------------------------------------#




set anaout_elem_num ""
set anaout_elem_name ""
set calc "0:0:0:0"
set default_type 0
set default1value 0
set default2value 0
set default3value 0
set default4value 0
set anaout_enable_channel 1
set anaout_enable_auto 1
set enable_graphic 1
set digout_value 1
set anaout_value ""
set anaout_raw ""
set ana_out_offset ""
set ana_out_gain ""
set maxautocalibrate ""
set minautocalibrate ""
set graph_type ""
set color ""
set xpos 0
set ypos 0
set vga 1
set max_auto_loop_var 0
set min_auto_loop_var 0
set  io_elem_timer 0
set obpm .out_bpm
set selected ""



#-----------------------------------------------------------------------------#
# This procedure will popup a Big Popup Menu where the user can enter         #
# information and set or define their configuration file                      #
# It takes 3 parameter passed from the "vehicle" and "surface" submenu        #
# files when user click on the element                                        #
#                                                                             #
# "surf_vehicle" = 1 / 0 ----> Surface / Vehicle                              #
# "dig_ana" = 1 / 0 ---> Digital / Analog                                     #
# "anaout_elem_number" = #any_number ---> The element number                              #
#-----------------------------------------------------------------------------#

proc out_popup {surf_vehicle dig_ana anaout_elem_number } {

   global obpm

   #--------------------------------------------#
   # Create a new window
   #--------------------------------------------#

   if [winfo exists $obpm] {destroy $obpm}


   global env
   set env(DISPLAY) :0
   toplevel $obpm  -width 20c -height 16c
   wm title $obpm "Element Properties"
   focus $obpm



   #--------------------------------------------------------------------------------#
   # Need to declare these variables as global, otherwise when we                   #
   # set them to different values it won't keep the new value and still             #
   # using the old values which is the value we set when we initialize them above   #
   #--------------------------------------------------------------------------------#
   global anaout_elem_num
   global anaout_elem_name
   global xpos
   global ypos
   global digout_value
   global anaout_value
   global anaout_raw
   global io_elem_timer


   #-----------------------------------------------------#
   # copy the parameter to the global variable so that   #
   # it can be used by any procedure inside 'popup'      #
   #-----------------------------------------------------#
   set anaout_elem_num $anaout_elem_number


    ##############
    # Input	#
    ##############

    label $obpm.input_label \
        -text Input
    listbox $obpm.input_text \
        -bg white -height 10  \
        -font {Halvetica 10}


   #--------------------------------------------------------#
   # Now , get all the values 				    		#
   # for this element from default C++ configuration files.				#
   # This one call "ret_all_info" which returns a string    				#
   # containing configuration values, and this procedure    			#
   # will parse it  and will set the each of the          				#
   # value to the global one so that we can use it          			#
   # anywhere here                                          				#
   #--------------------------------------------------------#
   my_ret_anaout_info


   ##################
   # Element name   #
   ##################

   label $obpm.anaout_elem_name_label \
        -text {Element Name}
   entry $obpm.anaout_elem_name_entry -textvariable anaout_elem_name \
        -background white -width 213
   focus $obpm.anaout_elem_name_entry

   place $obpm.anaout_elem_name_label \
        -x 20 -y 15 -anchor nw -bordermode ignore
   place $obpm.anaout_elem_name_entry \
        -x 125 -y 10 -width 213 -height 25 -anchor nw -bordermode ignore


    #############
    # Graphic   #
    #############

    label $obpm.graph_info_label \
        -text "Graphic Information"
    frame $obpm.graph_info \
        -borderwidth 2 -height 110 -relief groove -width 225
    label $obpm.graph_info.type_label \
        -text Type
    label $obpm.graph_info.xlabel \
    	-text X(0-800)
    label $obpm.graph_info.ylabel \
    	-text Y(0-600)

    entry $obpm.graph_info.type \
    	-width 13 -state disabled
    entry  $obpm.graph_info.xentry \
    	-width 13 -state disabled
    entry  $obpm.graph_info.yentry \
    	-width 13 -state disabled

    ############################
    # Place them in window     #
    ############################

    place $obpm.graph_info \
        -x 20 -y 70 -width 225 -height 110 -anchor nw -bordermode ignore
    place $obpm.graph_info_label \
        -x 20 -y 45 -anchor nw -bordermode ignore
    place $obpm.graph_info.type_label \
        -x 10 -y 20 -anchor nw -bordermode ignore


    place $obpm.graph_info.xlabel \
    	-x 10 -y 45  -anchor nw -bordermode ignore
    place $obpm.graph_info.ylabel \
    	-x 10 -y 70 -anchor nw -bordermode ignore	

    place  $obpm.graph_info.type \
    	-x 80 -y 20
    place  $obpm.graph_info.xentry \
    	-x 80 -y 45
    place  $obpm.graph_info.yentry \
    	-x 80 -y 70


###############################################################################
    #############
    # Enable    #
    #############

    label $obpm.enable_label \
        -height 0 -text Enable -width 50
    frame $obpm.enable \
        -borderwidth 2 -height 75 -relief groove -width 195

    checkbutton $obpm.enable.anaout_enable_channel \
         -text "Enable Channel" \
        -variable anaout_enable_channel  -state disabled

    checkbutton $obpm.enable.anaout_enable_auto \
        -text "Enable Autocalibrate" \
        -variable anaout_enable_auto


	
    place $obpm.enable \
        -x 20 -y 210 -width 225 -height 75 -anchor nw -bordermode ignore
    place $obpm.enable.anaout_enable_channel \
        -x 20 -y 15 -anchor nw -bordermode ignore
    place $obpm.enable.anaout_enable_auto \
        -x 20 -y 35 -anchor nw -bordermode ignore
    place $obpm.enable_label \
        -x 20 -y 185 -width 50 -height 20 -anchor nw -bordermode ignore


     #####################
     # Autocalibrate Control   #
     #####################

     label $obpm.ac_label \
        -text "Autocalibrate Setup"
     frame $obpm.ac_frame \
        -borderwidth 2 -height 200 -relief groove -width 195
     label $obpm.ac_frame.maxlabel \
        -text Max
     label $obpm.ac_frame.minlabel \
        -text Min
     entry $obpm.ac_frame.maxauto \
        -textvariable maxautocalibrate -width 13 -bg white
     entry $obpm.ac_frame.minauto \
        -textvariable minautocalibrate -width 13 -bg white
     button $obpm.ac_frame.auto_button \
        -command out_autocalibrate -text Autocalibrate

     place $obpm.ac_label \
        -x 255 -y 45 -anchor nw -bordermode ignore
     place $obpm.ac_frame \
        -x 260 -y 70 -width 195 -height 110 -anchor nw -bordermode ignore
     place $obpm.ac_frame.maxlabel \
        -x 12 -y 20 -anchor nw -bordermode ignore
     place $obpm.ac_frame.minlabel \
        -x 12 -y 45 -anchor nw -bordermode ignore
     place $obpm.ac_frame.maxauto \
        -x 70 -y 20  -anchor nw -bordermode ignore
     place $obpm.ac_frame.minauto \
        -x 70 -y 45  -anchor nw -bordermode ignore
     place $obpm.ac_frame.auto_button \
        -x 70 -y 75 -anchor nw -bordermode ignore

  ###########################
    # Analog Setup		          #
    ###########################


    label $obpm.as_label \
        -text "Analog Setup"
    frame $obpm.as_frame \
        -borderwidth 2 -relief groove -width 195 -height 75
    label $obpm.as_frame.gainlabel \
        -text Gain
    label $obpm.as_frame.offsetlabel \
        -text Offset
    entry $obpm.as_frame.gainentry \
        -background white -textvariable ana_out_gain -width 13
    entry $obpm.as_frame.offsetentry \
        -background white -textvariable ana_out_offset -width 13

    bind $obpm.as_frame.gainentry <Return> {
    	global default1value default2value default3value default4value

    	# Pack the 4 default values to a single string
        	set calc "$default1value:$default2value:$default3value:$default4value"

    	# Send Input to IO database -> this will update the element in database
        send_io_elem_info 1 3 $anaout_elem_num $anaout_elem_name $calc $default_type $anaout_enable_channel $anaout_enable_auto $enable_graphic $digout_value $anaout_value $anaout_raw $ana_out_offset $ana_out_gain $maxautocalibrate $minautocalibrate $graph_type $color $xpos $ypos $vga			
        # Send the Input List to IO database
        #send_out_list_info 1 3 $anaout_elem_num [get_input]
	
    }

    bind $obpm.as_frame.offsetentry <Return> {
    	global default1value default2value default3value default4value

    	# Pack the 4 default values to a single string
        set calc "$default1value:$default2value:$default3value:$default4value"

    	# Send Input to IO database -> this will update the element in database
        send_io_elem_info 1 3 $anaout_elem_num $anaout_elem_name $calc $default_type $anaout_enable_channel $anaout_enable_auto $enable_graphic $digout_value $anaout_value $anaout_raw $ana_out_offset $ana_out_gain $maxautocalibrate $minautocalibrate $graph_type $color $xpos $ypos $vga			
        # Send the Input List to IO database
        #send_out_list_info 1 3 $anaout_elem_num [get_input]
	
    }

     place $obpm.as_label \
        -x 255 -y 185 -anchor nw -bordermode ignore
     place $obpm.as_frame \
        -x 260 -y 210  -anchor nw -bordermode ignore
     place $obpm.as_frame.gainlabel \
        -x 12 -y 10 -anchor nw -bordermode ignore
     place $obpm.as_frame.offsetlabel \
        -x 12 -y 35 -anchor nw -bordermode ignore
     place $obpm.as_frame.gainentry \
        -x 70 -y 10  -anchor nw -bordermode ignore
     place $obpm.as_frame.offsetentry\
        -x 70 -y 35  -anchor nw -bordermode ignore



    ################
    # Value        #
    ################

    label $obpm.value_label \
        -text Value
    frame $obpm.value\
        -borderwidth 2 -height 130 -relief groove -width 225

    place $obpm.value\
        -x 20 -y 315 -width 225 -height 130 -anchor nw -bordermode ignore
    place $obpm.value_label \
        -x 20 -y 290 -anchor nw -bordermode ignore

    ##########
    # Analog #
    ##########

    entry $obpm.value.analog_entry -state disabled \
        -background white -textvariable anaout_value -width 108
    entry $obpm.value.analog_raw_entry -state disabled \
        -background white -textvariable anaout_raw -width 108

    label $obpm.value.analog_label \
        -text Analog -width 60
    label $obpm.value.ana_raw_label \
        -text {Analog Raw} -width 91


    place $obpm.value.analog_entry \
        -x 105 -y 45 -width 108 -height 22 -anchor nw -bordermode ignore
    place $obpm.value.analog_raw_entry \
        -x 105 -y 70 -width 108 -height 22 -anchor nw -bordermode ignore

    place $obpm.value.analog_label \
        -x 10 -y 45 -width 60 -height 20 -anchor nw -bordermode ignore
    place $obpm.value.ana_raw_label \
        -x 10 -y 70 -width 91 -height 20 -anchor nw -bordermode ignore

    ###########
    # Digital #
    ###########

    checkbutton $obpm.value.digital -text "Digital Value"  -variable digout_value
    place $obpm.value.digital -x 10 -y 10 -anchor nw -bordermode ignore



    ##############
    # DEFAULT    #
    ##############

    label $obpm.default_label \
        -height 0 -text Default -width 0
    place $obpm.default_label \
        -x 255 -y 290 -anchor nw -bordermode ignore
    frame $obpm.default_frame \
        -borderwidth 2 -height 130 -relief groove -width 225
    place $obpm.default_frame\
        -x 260 -y 315 -width 195 -height 130 -anchor nw -bordermode ignore

    label $obpm.default_frame.default1label \
        -text "Power Up"
    label $obpm.default_frame.default2label \
        -text "On Deck"
    label $obpm.default_frame.default3label \
        -text "10-100 M"
    label $obpm.default_frame.default4label \
        -text ">100 M"

    entry $obpm.default_frame.default1 \
        -textvariable default1value -state disabled
    entry $obpm.default_frame.default2 \
        -textvariable default2value -state disabled
    entry $obpm.default_frame.default3 \
        -textvariable default3value  -state disabled
    entry $obpm.default_frame.default4 \
        -textvariable default4value -state disabled


    place $obpm.default_frame.default1label \
        -x 5 -y 20 -width 80 -anchor nw -bordermode ignore
    place $obpm.default_frame.default2label \
        -x 5 -y 45 -width 80 -anchor nw -bordermode ignore
    place $obpm.default_frame.default3label \
        -x 5 -y 70 -width 80  -anchor nw -bordermode ignore
    place $obpm.default_frame.default4label \
        -x 5 -y 95 -width 80  -anchor nw -bordermode ignore

    place $obpm.default_frame.default1 \
        -x 80 -y 20 -width 90  -anchor nw -bordermode ignore
    place $obpm.default_frame.default2 \
        -x 80 -y 45 -width 90  -anchor nw -bordermode ignore
    place $obpm.default_frame.default3 \
        -x 80 -y 70 -width 90 -anchor nw -bordermode ignore
    place $obpm.default_frame.default4 \
        -x 80 -y 95 -width 90 -anchor nw -bordermode ignore


###############################################################################



    #---------------------------------------------------------
    # When we click on the input, we bind it to a variable, so that when we click on
    # 'delete' button, it will remove the correct selected line
    #---------------------------------------------------------

    global selected
    bind $obpm.input_text <ButtonRelease-1> {
    	set selected  [.out_bpm.input_text curselection]
    	puts "$selected"
    	
    }




    #--------------------------------------------------------------#
    # Create "Add" and "Delete" buttons
    # The command "add_input" and "delete_input" are called from
    # "input.tcl"
    #--------------------------------------------------------------#

    button $obpm.input_add  -text Add -command {add_input $surveh}
    button $obpm.input_delete -text Delete \
    	-command {
    			global selected
    			if {$selected == ""} { set selected end }
    			delete_input $selected
    			
    			
    		 }

    place $obpm.input_label \
        -x 470 -y 45 -anchor nw -bordermode ignore
    place $obpm.input_text \
        -x 470 -y 70 -width 100  -anchor nw -bordermode ignore
    place $obpm.input_add \
        -x 470 -y 215 -width 50
    place $obpm.input_delete \
         -x 520 -y 215  -width 50


###############################################################################
	

	
     ####################
     # OK Button        #
     ####################


     button $obpm.ok\
        -height 0 -text "OK"  \
        -command {	set io_elem_timer  0
 			ok_command     }

		
     place $obpm.ok \
        -x 470 -y 380 -anchor nw -bordermode ignore  -width 100



     ####################
     # Cancel Button    #
     ####################

      button $obpm.cancel \
         -command { destroy $obpm ; destroy .add_input} -text "Cancel"

      place $obpm.cancel \
         -x 470 -y 415 -anchor nw -bordermode ignore -width 100



    # Loop to update the analog and digital value
    set io_elem_timer 1
    loop_analog_out_value

# end of procedure "popup"
}



#-----------------------------------------------------------------------------


proc loop_analog_out_value { } {

	global obpm surveh diganal anaout_elem_num	
	global io_elem_timer digout_value anaout_value anaout_raw
 	global anaout_enable_channel
	
 	if {![winfo exists $obpm] || $io_elem_timer == 0 } {
 		return
 	}

 	
      	#------------------------------------------------------------------#
      	# Calling C++ function that reads the value for analog out channel
      	# connected to input from surface (anaout_surveh = 1) or Sub (anaout_surveh = 0)
      	#
      	#------------------------------------------------------------------#
      	
      	set values [ret_io_elem_value 1 3 $anaout_elem_num]

      	
      	#------------------------------------------------------------------#
      	# Now parse the string "values" and set it to "values_list"
     	#------------------------------------------------------------------#
      	
     	set values_list [ split $values | ]
 	#puts "values_list : $values_list"
 	set digout_value [lindex $values_list 0]
   	set anaout_value [lindex $values_list 1]
      	set anaout_raw [lindex $values_list 2]
 	

      after 500 [list  loop_analog_out_value ]
	

}




#-----------------------------------------------------------------------------



proc send_input_to_bottom { } {
         	
        global surveh
        global diganal
        global anaout_elem_num


        # This will set the value of "input" from listbox
   	set input [get_input]
   	   	
   	send_input_down $surveh $diganal $anaout_elem_num
   	
   }


#-----------------------------------------------------------------------------

   ###############################################################################

   proc my_ret_anaout_value { } {

      global surveh
      global diganal
      global anaout_elem_num
      global anaout_enable_channel
      global digout_value
      global anaout_value
      global anaout_raw
      global ana_out_gain ana_out_offset maxautocalibrate minautocalibrate



     		# set variable "values" to a string returned from C++ function
      		# "ret_io_elem_info"
      		set values [ret_io_elem_value 1 3 $anaout_elem_num]

      		# Now parse the string "values" and set it to "values_list"
      		set values_list [ split $values | ]
                      		
      		set digout_value [lindex $values_list 0]
      		set anaout_value [lindex $values_list 1]
      		set anaout_raw [lindex $values_list 2]
      		set ana_out_gain [lindex $values_list 3]
		set ana_out_offset [lindex $values_list 4]
		set maxautocalibrate [lindex $values_list 5]
		set minautocalibrate [lindex $values_list 6]
     	


   # end of procedure "my_ret_anaout_value"
   }


#-----------------------------------------------------------------------------


proc my_ret_anaout_info { } {


      global anaout_elem_num
      global anaout_elem_name


      # set the string returned to be "all_info"
      set all_info [ret_all_info 1 3 $anaout_elem_num 1]

      #puts "out_elem_prop.tcl: $all_info"

      # now parse it and set it to "all_info_list"
      set all_info_list [ split $all_info | ]

      # Set a checking variable "temp"
      set temp [lindex $all_info_list 0]

      if { $temp == -1 } {
	
      	set anaout_elem_name ""
      	set calc 0:0:0:0
      	set default_type 0
      	set anaout_enable_channel 1
      	set anaout_enable_auto 1
     	set enable_graphic 1
      	set digout_value ""
      	set anaout_value ""
      	set anaout_raw ""
      	set ana_out_offset 0
      	set ana_out_gain 0
      	set maxautocalibrate 0
      	set minautocalibrate  0
      	set graph_type None
      	set color ""
      	set xpos 0
      	set ypos 0
      	set vga 1
      	puts "Element does not exist, set everything to 0 or Null "

        	send_io_elem_info 1 3 $anaout_elem_num $anaout_elem_name \
                          $calc $default_type $anaout_enable_channel \
                          $anaout_enable_auto $enable_graphic $digout_value \
                          $anaout_value $anaout_raw $ana_out_offset $ana_out_gain \
                          $maxautocalibrate $minautocalibrate $graph_type $color \
                          $xpos $ypos 1

        my_ret_anaout_value

      } else {

      #-------------------------------------------------------------------
      # Now use the temporary variable to keep the value returned for
      # surface/vehicle, digital/analog, and element number
      #-------------------------------------------------------------------

      	      	#puts "out_elem_prop.tcl : all : $all_info_list"
      		
      		# Now set it to global variables available
      		set surveh [ lindex $all_info_list 0 ]
      		set diganal [ lindex $all_info_list 1 ]
      		set anaout_elem_num [ lindex $all_info_list 2 ]
      		set anaout_elem_name [ lindex $all_info_list 3 ]
		
      		set digout_value [ lindex $all_info_list 9 ]
      		set anaout_value [ lindex $all_info_list 10 ]
      		set anaout_raw [ lindex $all_info_list 11 ]
      		
      		set ana_out_offset [ lindex $all_info_list 12 ]
      		set ana_out_gain [ lindex $all_info_list 13 ]
      		set maxautocalibrate [ lindex $all_info_list 14 ]
      		set minautocalibrate  [ lindex $all_info_list 15 ]

		
      		# we have to get the input nodes from db and display it back in GUI
      		display_input_in_listbox

      		my_ret_anaout_value
		
        # end of else
        }



# end of procedure "my_ret_anaout_info"
}

proc display_input_in_listbox { } {
	
        global anaout_elem_num
        global SERIAL

        #----------------------------------------#
        # Calling C++ function "display_output"
        #----------------------------------------#

        set all_input [split [display_output 1 3 $anaout_elem_num] |] 		
        #puts "all input -----------$all_input"

        set surveh [lindex $all_input 0]
        set elem_num [lindex $all_input 1]

        #----------------------------------------------------------#
        # Now display it in listbox
        #----------------------------------------------------------#

        if {$surveh == $SERIAL } {
              	
              	switch $elem_num {
      		
      		# Heading
      		1 { .out_bpm.input_text insert 0 "Heading"}
      		
        	# Depth
      		2 { .out_bpm.input_text insert 0 "Depth"}

      		# Pitch
      		3 { .out_bpm.input_text insert 0 "Pitch"}
      		
      		# Roll
      		4 { .out_bpm.input_text insert 0 "Roll"}
      		
      		}	
         	
        }  else {
        	
        	.out_bpm.input_text insert end "[get_element_name $surveh 0 $elem_num]"
	}		
		
}



proc ok_command { } {

    global anaout_elem_num anaout_elem_name calc default_type anaout_enable_channel anaout_enable_auto enable_graphic digout_value anaout_value anaout_raw ana_out_offset ana_out_gain maxautocalibrate minautocalibrate graph_type color xpos ypos vga			
    global default1value default2value default3value default4value
       	
    	#----------------------------------------------------------#
       	# Ask user to enter a name for the element
       	#----------------------------------------------------------#
       	
       	if {$anaout_elem_name == "" } {
		 	
		#----------------------------------------------------------#
		# Element name can't be empty
		#----------------------------------------------------------#
		set status [tk_dialog  .warning { } {Please insert name in element name field. } warning 0 {Close } ]
       	       	switch $status {0 {puts "Close"; return} }             	
        			
	}

	#----------------------------------------------------------#
        # Pack the 4 default values to a single string
        #----------------------------------------------------------#
        set calc "0:0:0:0"

        #----------------------------------------------------------#
        # Check element name existence
        #----------------------------------------------------------#
        #for {set i 1} {$i <= 4 }  {incr i} {
         	
        #	if {$anaout_elem_name == [get_element_name 1 3 $i] } {
        #		set status [tk_dialog  .warning { } {The name you entered has already exist. Please enter a different name. } warning 0 {Close } ]
       	#      		switch $status {0 {puts "Close"; return} }             	
        		
        #	}
        #}
	
        #----------------------------------------------------------#
        # Send Input to IO database -> this will update the element in database
        # And also send down the value for gain and ana_out_offset to the sub
        #----------------------------------------------------------#

        send_io_elem_info 1 3 $anaout_elem_num $anaout_elem_name $calc $default_type $anaout_enable_channel $anaout_enable_auto $enable_graphic $digout_value $anaout_value $anaout_raw $ana_out_offset $ana_out_gain $maxautocalibrate $minautocalibrate $graph_type $color $xpos $ypos $vga			

        #----------------------------------------------------------#
        # Display the new name to the submenu list
        #----------------------------------------------------------#

        display_new_anaout_name
		
	#----------------------------------------------------------#
	# Destroy the window
	#----------------------------------------------------------#
	
	destroy .out_bpm
	destroy .add_input		

}



#----------------------------------------------------------#
# This function will update the element name on the pull down menu         		
# coming from :
#                                                                                                                   	
# 	surface -> analog in -> ...                                                            	
# 	surface -> digital in  -> ...                                                         	
# 	surface -> analog out -> ...                                                           	
# 	vehicle ->  digital in -> ...                                                             	
# 	vehicle ->  analog in -> ...                                                          	
#                                                                                                            	
# Note : Update needs to be done for BOTH screen                                      	
#                                                                                                            	
#----------------------------------------------------------#

proc display_new_anaout_name { } {

        global anaout_elem_num anaout_elem_name

        	
       	#------------#
        # Screen 1	
        #------------#

       	if ![winfo exists .ventana] {
         	puts "out_elem_prop.tcl : Can Not Find First Screen "
             	return;
       }

        	
	set win .ventana.bar.maintenance.menu.element.surface.anaout
	$win entryconfigure $anaout_elem_num -label "$anaout_elem_num $anaout_elem_name"
        	
	#------------#
	# Screen 2	
	#------------#

 	if ![winfo exists .ventana2] {
            	puts "out_elem_prop.tcl : Can Not Find Second Screen "
             	return
	}

        set win2 .ventana2.bar.maintenance.menu.element.surface.anaout
        $win2 entryconfigure $anaout_elem_num -label "$anaout_elem_num $anaout_elem_name"
        	
}

#------------------------ end of display_submenu proc ----------------------------#


	

proc out_autocalibrate { } {
	
	
	global anaout_elem_num maxautocalibrate minautocalibrate max_auto_loop_var
	global anaout_enable_auto
		
	#--------------------------------------------------------#
	# Enable autocalibrate button has to be enabled
	#--------------------------------------------------------#
	
	if {$anaout_enable_auto == 0} {
		return
	}
	
	set max_auto_loop_var 1
	
	button .out_bpm.auto -text  "Please Move The Analog Raw To MAXIMUM. Press Here When Finished. " \
 		-wraplength 100 -bg red -fg white -command {set max_auto_loop_var 0}
 	place .out_bpm.auto -x 200 -y 200
 	grab .out_bpm.auto
 	
	anaout_max_loop
	
	
	
}
 	

proc anaout_max_loop { } {

	global anaout_elem_num maxautocalibrate
	global max_auto_loop_var  min_auto_loop_var
	global ana_out_gain ana_out_offset
	
	if {![winfo exists .out_bpm.auto] || $max_auto_loop_var == 0 } {
		
		#--------------------------------------------------------#
		# Getting ready to display the Button that asks user to get min value
		#--------------------------------------------------------#
		
		set min_auto_loop_var 1
		
		button .out_bpm.automin -text  "Please Move The Analog Raw To MINIMUM. Press Here When Finished. " \
 			-wraplength 100 -bg blue -fg white \
 			-command {	set min_auto_loop_var 0
 					set values [split [ret_io_elem_value 1 3 $anaout_elem_num] | ]
 					set ana_out_gain [lindex $values 3]
 					set ana_out_offset [lindex $values 4] }
 					
 		place .out_bpm.automin -x 200 -y 200
 	             grab .out_bpm.automin
 	
		anaout_min_loop
		
		destroy .out_bpm.auto
		return
	} 	

 	set max_auto_value [ auto_calibrate_max 1 3 $anaout_elem_num $maxautocalibrate ]
 	 	
 	if {  $max_auto_value == -1 } {
 		puts "out_elem_prop.tcl : Error in reading maximum autocalibrate value" 	
 	}
 	
 	if {  $max_auto_value == -2 } {
 	
 		set status [tk_dialog  .elem_warning2 { } {The Autocalibrate is not enabled.} warning 0 {Close } ]
       		switch $status {   0 { return } }
 	
 	}
 	
 	puts "out_elem_prop.tcl: Waiting for MAX value........."		
            after 250 [list anaout_max_loop]	

}


proc anaout_min_loop { } {

	global anaout_elem_num minautocalibrate
	global min_auto_loop_var
	
         	if {![winfo exists .out_bpm.automin] || $min_auto_loop_var == 0 } {
		destroy .out_bpm.automin
		return
	}
		
 	set min_auto_value [ auto_calibrate_min 1 3 $anaout_elem_num $minautocalibrate ]
 	 	
 	if {  $min_auto_value == -1 } {
 		puts "out_elem_prop.tcl : Error in reading minimum autocalibrate value" 	
 	}
 	
 	if {  $min_auto_value == -2 } {
 	
 		set status [tk_dialog  .elem_warning2 { } {The Autocalibrate is not enabled.} warning 0 {Close } ]
       		switch $status {   0 { return } }
 	
 	}
 	
  	puts "out_elem_prop.tcl: Waiting for MIN value........."
        after 250 [list anaout_min_loop]	


}