# Bar Graphics use the element Number to display first the bar, then the line and value
# The line and value are destroyed and created new every second
# the alarm function has been disabled for now

for { set i 0 } { $i <= 64 } { incr i } {
	set bar2x($i) 0
	set bar2y($i) 0
	set bar2_check_var($i) 1
	set v2bf$i ""
	set l2bar($i) 0
	set v2text_id($i) 0
	set bar2_timer($i) 0
	set bar_min2($i) 0
	set bar_max2($i) 0
	set bar_average2($i) 0
	set bar_yrange2($i) 0
	set bar_rrange2($i) 0
}

#--------------------------------------------------------------------
# Make the Changes on the size of the bargraph here
# bar2_x_left_coord is the left x coordinate
# bar2_x_right_coord is the right x coordinate
# bar2_y_top_coord is the top y coordinate
# bar2_y_bottom_coord is the bottom y coordinate
#--------------------------------------------------------------------

set bar2_x_left_coord 30
set bar2_y_top_coord 40
set bar2_x_right_coord 40
set bar2_y_bottom_coord 140

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


# "elem_num" is the element number for vehicle analog in
proc display_bar2_or_not { flag elem_num } {
    	    	
   	global bar2_check_var
   	global bar2x bar2y
	global bar2_x_right_coord bar2_y_bottom_coord

        global env
	set env(DISPLAY) :0.1
	
	set v2bf .ventana2.editor.bframe$elem_num
	set v2bar $v2bf.barc
	
	if {$flag == 1 && $elem_num >= 1} {
   		if [winfo exists $v2bf] { destroy $v2bf }
		set bar2_check_var($elem_num) 1
		
		frame $v2bf -background black -width [expr {$bar2_x_right_coord + 15}] -height [expr {$bar2_y_bottom_coord + 25 }]
		place $v2bf -x $bar2x($elem_num) -y $bar2y($elem_num)
		
		# Create the canvas to draw the bar graph
   		canvas $v2bar -width 100 -height 250 -background black
   		place $v2bar -x -1 -y -1

		# Create the bar graph here
		creating_graphic2 $v2bar $elem_num
		    	
  	} else {
  		
  		set bar2_check_var($elem_num) 0
  		destroy $v2bf   	
  	}
}


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


proc creating_graphic2 {v2bar elem_num} {

   global bar_min2 bar_max2 bar_average2 bar_yrange2 bar_rrange2
   global bar2_x_left_coord bar2_y_top_coord bar2_x_right_coord bar2_y_bottom_coord
   global l2bar

   #-----------------------------------------
   # Create the outer rectangle x1 y1 x2 y2
   # remember that (0,0) starts at top left corner and
   # x going to right , y going down as they increases
   #-----------------------------------------

   set outer [ $v2bar create rectangle $bar2_x_left_coord $bar2_y_top_coord $bar2_x_right_coord $bar2_y_bottom_coord -outline white ]

   # Map the range according to the canvas coordinate	
   # Ratio is in Pixels per units (Volts, PSI, etc.)

   if { [expr { $bar_min2($elem_num) - $bar_max2($elem_num) } ] } {
   	set ratio [ expr { double(($bar2_y_bottom_coord - $bar2_y_top_coord)) / ($bar_min2($elem_num) - $bar_max2($elem_num)) } ]
   } else  {
   	set ratio 0
   	}
   # Bar Average is in pixels from top of graph
   set temp_bar_average2 [ expr {$bar2_y_bottom_coord - (($bar_average2($elem_num) - $bar_max2($elem_num)) * $ratio) } ]
 
  # Convert the units to pixels then add subtract from bar average

   set bar_low_yrange2 [ expr { $temp_bar_average2 - $bar_yrange2($elem_num) * $ratio} ]
   set bar_up_yrange2  [ expr { $temp_bar_average2 + $bar_yrange2($elem_num) * $ratio} ]
   set bar_low_rrange2 [ expr { $temp_bar_average2 - $bar_rrange2($elem_num) * $ratio} ]
   set bar_up_rrange2  [ expr { $temp_bar_average2 + $bar_rrange2($elem_num) * $ratio} ]

   #----------------------------------
   # Drawing the three color rectangles
   #----------------------------------

   # Draw the red rectangle
   set red_rect [ $v2bar create rectangle $bar2_x_left_coord $bar2_y_top_coord $bar2_x_right_coord $bar2_y_bottom_coord \
		-fill red -outline white ]
   # Draw the yellow rectangle	
   set yellow_rect [ $v2bar create rectangle $bar2_x_left_coord $bar_up_rrange2 $bar2_x_right_coord $bar_low_rrange2 \
		-fill yellow -outline white]

   # Draw the green rectangle
   set green_rect [ $v2bar create rectangle $bar2_x_left_coord $bar_up_yrange2 $bar2_x_right_coord $bar_low_yrange2 \
		-fill green -outline white]
   	

   #--------------------------------------------
   # Creating the line and its scales beside the bar graph
   #--------------------------------------------

   # Create the main line
   $v2bar create line $bar2_x_left_coord $bar2_y_top_coord $bar2_x_left_coord $bar2_y_bottom_coord -fill white

   # Create the little mark/scale on the main line every 5 pixels
   for {set i $bar2_y_top_coord} {$i <= $bar2_y_bottom_coord} {incr i 5} {
        $v2bar create line [expr {$bar2_x_left_coord - 2}] $i $bar2_x_left_coord $i -fill white
   }

   # Create a longer mark/scale on the main line every 25 pixels
   for {set i $bar2_y_top_coord} {$i <= $bar2_y_bottom_coord} {incr i 25} {
        $v2bar create line [expr {$bar2_x_left_coord - 5}] $i $bar2_x_left_coord $i -fill white
   }


   #--------------------------------------------
   # Create the text to be displayed beside the line
   #--------------------------------------------

   set minimum2 [expr {int($bar_min2($elem_num)) }]
   set maximum2 [expr {int($bar_max2($elem_num)) }]
   set middle2 [expr {int((($bar_min2($elem_num) - $bar_max2($elem_num))/2) +$bar_max2($elem_num)) }]

   # Bar graph Minimum
   label $v2bar.namemin -text $maximum2 -font {Halvetica 8 } -bg black -fg white
   place $v2bar.namemin -x  0 -y 130

    # Bar graph Maximum
   label $v2bar.namemax -text $minimum2 -font {Halvetica 8 } -bg black -fg white
   place $v2bar.namemax -x  0 -y 35

    # Bar graph Middle
   label $v2bar.namemiddle -text $middle2 -font {Halvetica 8 } -bg black -fg white
   place $v2bar.namemiddle -x  0 -y 85


   # Bar graph Title
   label $v2bar.name -text [get_element_name 0 0 $elem_num] \
   	-wraplength 50 -font {Halvetica 10 } -bg black -fg white
   place $v2bar.name -x  0 -y [expr {$bar2_y_top_coord - 40}]

   bar2 $elem_num

}



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


proc bar2 { elem_num } {
   
   global bar2_y_top_coord bar2_y_bottom_coord  bar2_x_left_coord bar2_x_right_coord
   global bar2_check_var l2bar  v2text_id  bar2_timer
   global bar_min2 bar_max2 bar_average2 bar_yrange2 bar_rrange2
   
   set volt [get_bar_voltage $elem_num]

   set v2bf .ventana2.editor.bframe$elem_num
   set v2bar .ventana2.editor.bframe$elem_num.barc

   if { ![winfo exists $v2bf ]  || $bar2_check_var($elem_num) == 0} { destroy $v2bf; return   }

   # Map the range according to the canvas coordinate
   # Check that Min and Max are Set and the Difference is Non Zero
   if { [expr { ($bar_min2($elem_num) - $bar_max2($elem_num)) } ] } {
   	set ratio [ expr { double(($bar2_y_bottom_coord - $bar2_y_top_coord)) / ($bar_max2($elem_num) - $bar_min2($elem_num)) } ]
   } else {
   	set ratio 0
   }
   if { $volt > $bar_min2($elem_num) } {
   	set y1 [expr { $bar2_y_bottom_coord - ( ( $bar_max2($elem_num) - $bar_min2($elem_num) ) * $ratio ) } ]
   	
   } elseif { $volt < $bar_max2($elem_num) } {
   	set y1 $bar2_y_bottom_coord
   	
   } else {
   	set y1 [expr { $bar2_y_bottom_coord + ( ( $volt - $bar_max2($elem_num) ) * $ratio ) } ]
   }

    # (Re)Draw the 'volt' line and its value
   if [winfo exists $v2bar] {
   	$v2bar delete $l2bar($elem_num)
   	$v2bar delete $v2text_id($elem_num)
   }

   set l2bar($elem_num) [ $v2bar create line [expr {$bar2_x_left_coord - 10}] $y1 [expr {$bar2_x_right_coord + 10}] $y1 -fill blue -width 3  -arrow both ]
   set v2text_id($elem_num) [$v2bar create text $bar2_x_left_coord  [expr {$bar2_y_bottom_coord + 10}] -text  "$volt" -fill white -font {Halvetica 10} ]


   # If the voltage is on red range, display an alarm
   #if { ( $y1 <= $bar2_y_bottom_coord && $y1 >= $bar_low_rrange2 ) || ( $y1 >= $bar2_y_top_coord && $y1 <= $bar_up_rrange2 ) } {
        #set_ana_alarm 1    	
   	#}

   after 1000 [list bar2 $elem_num]


}

proc redisplay_bar2 {elem_num} {
	global bar2_check_var
 	global bar2_timer
	global bar2x bar2y
 	#------------------------------------------------
	# Get the x y position for the bargraph going
	# to be redisplayed
	# get_xy_pos [element number] [screen]
	#------------------------------------------------
     	 	
	set position [get_xy_pos $elem_num 2]
	set bar2x($elem_num) [lindex $position 0]
 	set bar2y($elem_num) [lindex $position 1]

	set v2bf .ventana2.editor.bframe$elem_num
	
	if {$bar2_check_var($elem_num) == 1} {
		set bar2_timer($elem_num) 0
		destroy $v2bf
 		display_bar2_or_not  1 $elem_num
 		return
   	}
}

proc bar_detail2 {elem_num } {

global bar_min2 bar_max2 bar_average2 bar_yrange2 bar_rrange2
   
toplevel .bar_detail2 -width 65m -height 75m
set bard2 .bar_detail2
wm title $bard2 "Bar Setup"

    label $bard2.bargraph_title -foreground #0000f0 -height 0 -text {Bar Graph Detail}

    label $bard2.max_label -height 0 -text Minimum
    label $bard2.min_label -height 0 -text Maximum
    label $bard2.ave_label -height 0 -text Average
    label $bard2.yrange_label -height 0 -text "Yellow Range"
    label $bard2.rrange_label -height 0 -text "Red Range"

    entry $bard2.max_entry -background white -textvariable bar_max2($elem_num) 
    entry $bard2.min_entry -background white -textvariable bar_min2($elem_num) 
    entry $bard2.ave_entry -background white -textvariable bar_average2($elem_num) 
    entry $bard2.yrange_entry -background white -textvariable bar_yrange2($elem_num) 
    entry $bard2.rrange_entry -background white -textvariable bar_rrange2($elem_num) 

    button $bard2.ok \
        -text Ok -command {setup_bar2 $elem_num; destroy .bar_detail2}
    button $bard2.cancel \
        -text Cancel -command {destroy .bar_detail2}

    ###################
    # SETTING GEOMETRY
    ###################

    place $bard2.bargraph_title -x 15 -y 5 -anchor nw -bordermode ignore

    place $bard2.max_label -x 15 -y 40 -anchor nw -bordermode ignore
    place $bard2.min_label -x 15 -y 65 -anchor nw -bordermode ignore
    place $bard2.ave_label -x 15 -y 90 -anchor nw -bordermode ignore
    place $bard2.yrange_label -x 15 -y 115 -anchor nw -bordermode ignore
    place $bard2.rrange_label -x 15 -y 140 -anchor nw -bordermode ignore


    place $bard2.max_entry -x 100 -y 40 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard2.min_entry -x 100 -y 65 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard2.ave_entry -x 100 -y 90 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard2.yrange_entry -x 100 -y 115 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard2.rrange_entry -x 100 -y 140 -width 78 -height 22 -anchor nw -bordermode ignore

    place $bard2.ok \
        -x 30 -y 175 -anchor nw -bordermode ignore
    place $bard2.cancel \
        -x 80 -y 175 -anchor nw -bordermode ignore

}


proc setup_bar2 { elem_num } {

global bar_min2 bar_max2 bar_average2 bar_yrange2 bar_rrange2

if {$bar_min2($elem_num) == "" }  { set bar_min2($elem_num) 0 }
if {$bar_max2($elem_num) == "" }  { set bar_max2($elem_num) 5 }
if {$bar_average2($elem_num) == "" }  { set bar_average2($elem_num) 2.5 }
if {$bar_yrange2($elem_num) == "" }  { set bar_yrange2($elem_num) 1 }
if {$bar_rrange2($elem_num) == "" }  { set bar_rrange2($elem_num) 2 }

display_bar2_or_not 1 $elem_num

# set it to c++ database
setting_bar_detail $elem_num $bar_min2($elem_num) $bar_max2($elem_num) $bar_average2($elem_num) $bar_yrange2($elem_num) $bar_rrange2($elem_num)

}


