Class: LibIXM::Adapters::SFBProg
- Inherits:
-
Object
- Object
- LibIXM::Adapters::SFBProg
- Defined in:
- lib/adapters/sfbprog.rb
Instance Attribute Summary collapse
-
#packet_callbacks ⇒ Object
Returns the value of attribute packet_callbacks.
Instance Method Summary collapse
- #build_line ⇒ Object
- #every_packet(&callback) ⇒ Object
-
#initialize(options) ⇒ SFBProg
constructor
A new instance of SFBProg.
-
#respond_to(packet) ⇒ Object
On IO read fire @reflexes for each packet.
- #send_packet(packet) ⇒ Object (also: #<<)
- #start ⇒ Object
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() = { :sfbprog_path => 'sfbprog', :sfbprog_args => '', :sfbprog_device => '/dev/ttyUSB0', :sfbprog_sketch => File.join( File.dirname(__FILE__), '..', '..', 'sketches', 'packet_echo.hex' ) }.merge() @sfbprog = IO.popen( " #{[:sfbprog_path]} \ -n #{[:sfbprog_device]} \ -S - \ -t 0 \ #{[:sfbprog_args]} \ -f #{[:sfbprog_sketch]}", 'w+' ) @packet_callbacks = [] start end |
Instance Attribute Details
#packet_callbacks ⇒ Object
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_line ⇒ Object
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 |
#start ⇒ Object
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 |