Class: Cosmos::TrendbarWidget

Inherits:
LimitsbarWidget show all
Defined in:
lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb

Constant Summary collapse

MAX_TREND_SECONDS =
3600.0

Instance Attribute Summary

Attributes included from Widget

#item, #item_name, #limits_set, #limits_state, #packet, #packet_name, #polling_period, #screen, #settings, #target_name, #value, #value_type

Instance Method Summary collapse

Methods inherited from LimitsbarWidget

#paint_implementation, takes_value?

Methods inherited from LimitsWidget

#calculate_widths, #get_limits, #paintEvent

Methods included from Widget

#context_menu, included, #set_setting, #set_subsetting, #shutdown, #update_widget

Constructor Details

#initialize(parent_layout, target_name, packet_name, item_name, value_type = :CONVERTED, trend_seconds = 60.0, width = 160, height = 25, trend_value_handle = nil) ⇒ TrendbarWidget

Returns a new instance of TrendbarWidget.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb', line 23

def initialize (parent_layout, target_name, packet_name, item_name, value_type = :CONVERTED, trend_seconds = 60.0, width = 160, height = 25, trend_value_handle = nil)
  #  @trend_seconds needs to be set before super because super will call get_tooltip_text
  @trend_seconds = trend_seconds.to_f
  @trend_seconds = MAX_TREND_SECONDS if @trend_seconds > MAX_TREND_SECONDS
  super(parent_layout, target_name, packet_name, item_name, value_type, width, height)
  @trend_value_handle = ConfigParser.handle_nil(trend_value_handle)

  # Track the total number of samples received.  This will help us to know how many
  # trendlines to plot before we reach the maximum.
  @samples_received = 0

  # The actual difference (delta) between the current value and the desired trend value.
  @trend_delta = nil
  @trend_points = 1
  @trend_data = [0]
end

Instance Method Details

#additional_drawing(dc) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb', line 76

def additional_drawing (dc)
  trendline_pos = (@x_pad + ((@trend_data[@trend_points]) - @low_value) / @bar_scale * @bar_width).to_i
  trendline_pos = @x_pad if trendline_pos < @x_pad
  trendline_pos = @bar_width + @x_pad if trendline_pos > @x_pad + @bar_width

  # Create the horizontal trendline and circle indicating the trend value.
  # The desired diameter of the circle is 0.35 * bar_height.
  bar_y_mid = @height / 2
  radius = 0.175 * @bar_height

  if @samples_received > @trend_points

    # Set the value for the widget that displays the trend_delta
    if @trend_value_handle
      trend_val = @value - @trend_data[@trend_points]

      # Round to 2 digits
      @trend_value_handle.setText(sprintf("%0.2f", trend_val))
    end

    dc.addLineColor(trendline_pos, bar_y_mid, @line_pos, bar_y_mid)

    # Put a circle in the middle of the bar at the trendline point.
    #
    # Note: The documentation claims the (x, y) given is the center of the
    # circle but it doesn't seem right => we shift the circle (x,y) to what
    # looks correct experimentally since this is just a rough visual indicator.
    #~ dc.fillCircle(trendline_pos + radius/2, bar_y_mid + radius/2, radius)
    dc.addEllipseColorFill(trendline_pos - 2, bar_y_mid - radius/2, radius*2, radius*2)
  end
end

#adjust_trend_points_and_dataObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb', line 58

def adjust_trend_points_and_data
  @trend_points = ((1.0 / @polling_period.to_f) * @trend_seconds).to_i + 1
  # Time history of TLM item values with oldest -> newest reading left[0]
  # -> right[max_trend_points - 1]
  @trend_data = [0] * @trend_points

  # Subtract one since we'll use this as an array index and will precompute so we don't have
  # to take a computation hit at every data update.
  @trend_points -= 1
  @trend_points = 0 if @trend_points < 0
end

#get_tooltip_textObject



70
71
72
73
74
# File 'lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb', line 70

def get_tooltip_text
  tooltip_text  = super()
  tooltip_text += "\nTrending Value = (Current Value) - (Value #{@trend_seconds} Seconds Ago)"
  return tooltip_text
end

#polling_period=(polling_period) ⇒ Object



40
41
42
43
# File 'lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb', line 40

def polling_period=(polling_period)
  super(polling_period)
  adjust_trend_points_and_data()
end

#process_settingsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb', line 108

def process_settings
  super()

  # Handle the global TREND_SECONDS setting which will do a global change of
  # all widgets of this type in a given screen definition file.
  # Usage (in screen def file):
  #                  GLOBAL_SETTING TRENDLIMITSBAR TREND_SECONDS <num_seconds>
  if @settings.include?('TREND_SECONDS')
    @trend_seconds = @settings['TREND_SECONDS'][0].to_f

    # Make sure maximum is still enforced
    if @trend_seconds > MAX_TREND_SECONDS
      @trend_seconds = MAX_TREND_SECONDS
    end

    adjust_trend_points_and_data()
  end
end

#value=(data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb', line 45

def value= (data)
  @value = data.to_f

  @samples_received += 1

  # Remove the oldest value off the end (right) and insert on the
  # front (left) of the array.
  @trend_data.pop
  @trend_data.insert(0, @value)

  update()
end