Class: AnalogInput
- Inherits:
-
Object
- Object
- AnalogInput
- Defined in:
- lib/analog_input.rb
Constant Summary collapse
- COLORS =
[Gosu::Color::GREEN,Gosu::Color::CYAN,Gosu::Color::YELLOW,Gosu::Color::FUCHSIA,Gosu::Color::BLUE,Gosu::Color::WHITE,Gosu::Color::GRAY]
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#pin ⇒ Object
readonly
Returns the value of attribute pin.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
-
#visible ⇒ Object
Returns the value of attribute visible.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Class Method Summary collapse
Instance Method Summary collapse
- #callback(v) ⇒ Object
- #draw ⇒ Object
- #draw_label ⇒ Object
- #draw_line ⇒ Object
-
#initialize(args = {}) ⇒ AnalogInput
constructor
A new instance of AnalogInput.
- #update ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ AnalogInput
Returns a new instance of AnalogInput.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/analog_input.rb', line 17 def initialize(args={}) @@inputs ||=[] @window = args[:window] @pin = args[:pin] @data = [] @value = 0 @visible = args[:visible]||false @font_large ||= Gosu::Font.new(30) @font_small ||= Gosu::Font.new(20) @yscale = @window.yscale @height = @window.height @@inputs << self end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'lib/analog_input.rb', line 2 def data @data end |
#pin ⇒ Object (readonly)
Returns the value of attribute pin.
2 3 4 |
# File 'lib/analog_input.rb', line 2 def pin @pin end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
2 3 4 |
# File 'lib/analog_input.rb', line 2 def value @value end |
#visible ⇒ Object
Returns the value of attribute visible.
3 4 5 |
# File 'lib/analog_input.rb', line 3 def visible @visible end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
2 3 4 |
# File 'lib/analog_input.rb', line 2 def window @window end |
Class Method Details
.[](p) ⇒ Object
7 8 9 10 |
# File 'lib/analog_input.rb', line 7 def self.[](p) i=@@inputs.select {|x| x.pin == p }.first return i || nil end |
.all ⇒ Object
12 13 14 15 |
# File 'lib/analog_input.rb', line 12 def self.all @@inputs ||= [] @@inputs end |
Instance Method Details
#callback(v) ⇒ Object
33 34 35 |
# File 'lib/analog_input.rb', line 33 def callback(v) @value = v end |
#draw ⇒ Object
46 47 48 49 |
# File 'lib/analog_input.rb', line 46 def draw draw_line if visible draw_label end |
#draw_label ⇒ Object
51 52 53 54 55 56 |
# File 'lib/analog_input.rb', line 51 def draw_label margin = 60*self.pin @font_large.draw("Analog #{self.pin}",@window.scope_width+5,10+margin,0,1.0,1.0, COLORS[self.pin]) @font_small.draw("#{sprintf('%.2fv',@data[-1][:voltage])} #{ self.visible ? @data[-1][:value] : "(hidden)"}",@window.scope_width+5,40+margin,0,1.0,1.0, COLORS[self.pin]) return true end |
#draw_line ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/analog_input.rb', line 58 def draw_line (0..self.data.length - 2).each do |i| x1=self.data[i][:time] - self.data[0][:time] y1=@height-(self.data[i][:voltage]*@yscale) x2=@data[i+1][:time] - @data[0][:time] y2=@height-(self.data[i+1][:voltage]*@yscale) @window.draw_line(x1,y1,COLORS[self.pin],x2,y2,COLORS[self.pin]) end end |
#update ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/analog_input.rb', line 37 def update @data.shift if @data.count > (@window.scope_width / @window.update_interval) @data << { voltage: @value/204.6, value: @value, time: Gosu::milliseconds } end |