Class: LibIXM::Adapters::SFBProg

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/sfbprog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SFBProg

Returns a new instance of SFBProg.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/adapters/sfbprog.rb', line 8

def initialize(options)
  options = {
    :sfbprog_path => 'sfbprog',
    :sfbprog_args => '',
    :sfbprog_device => '/dev/ttyUSB0',
    :sfbprog_sketch => File.join( File.dirname(__FILE__), '..', '..', 'sketches', 'packet_echo.hex' )
  }.merge(options)

  @sfbprog = IO.popen( "
#{options[:sfbprog_path]} \
-n #{options[:sfbprog_device]} \
-S - \
-t 0 \
#{options[:sfbprog_args]} \
-f #{options[:sfbprog_sketch]}",
    'w+'
  )

  @packet_callbacks = []
  start
end

Instance Attribute Details

#packet_callbacksObject

Returns the value of attribute packet_callbacks.



6
7
8
# File 'lib/adapters/sfbprog.rb', line 6

def packet_callbacks
  @packet_callbacks
end

Instance Method Details

#build_lineObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/adapters/sfbprog.rb', line 43

def build_line
  line = String.new
  loop {
    begin
      char = @sfbprog.read_nonblock(1)
      break if char == "\n"
      line << char
    rescue Errno::EAGAIN
    end
  }
  line
end

#every_packet(&callback) ⇒ Object



56
57
58
# File 'lib/adapters/sfbprog.rb', line 56

def every_packet( &callback )
  @packet_callbacks << callback
end

#respond_to(packet) ⇒ Object

On IO read fire @reflexes for each packet



68
69
70
71
72
# File 'lib/adapters/sfbprog.rb', line 68

def respond_to( packet )
  @packet_callbacks.each { |callback|
    callback.call( packet )
  }
end

#send_packet(packet) ⇒ Object Also known as: <<



60
61
62
# File 'lib/adapters/sfbprog.rb', line 60

def send_packet( packet )
  @sfbprog.puts packet.chomp
end

#startObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/adapters/sfbprog.rb', line 30

def start
  3.times { build_line }
  send_packet('') # SFBProg needs a dummy packet first.
  sleep 2 # Disgusting, but it takes time.
  @read_thread = Thread.new {
    loop {
      line = build_line
      respond_to( line )
      Thread.pass
    }
  }
end