Class: AnalogInput

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'lib/analog_input.rb', line 2

def data
  @data
end

#pinObject (readonly)

Returns the value of attribute pin.



2
3
4
# File 'lib/analog_input.rb', line 2

def pin
  @pin
end

#valueObject (readonly)

Returns the value of attribute value.



2
3
4
# File 'lib/analog_input.rb', line 2

def value
  @value
end

#visibleObject

Returns the value of attribute visible.



3
4
5
# File 'lib/analog_input.rb', line 3

def visible
  @visible
end

#windowObject (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

.allObject



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

#drawObject



46
47
48
49
# File 'lib/analog_input.rb', line 46

def draw
  draw_line if visible
  draw_label
end

#draw_labelObject



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_lineObject



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

#updateObject



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