Class: Dino::TxRx::Base

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/dino/tx_rx/base.rb

Direct Known Subclasses

Serial, TCP

Instance Method Summary collapse

Instance Method Details

#close_readObject



21
22
23
24
25
# File 'lib/dino/tx_rx/base.rb', line 21

def close_read
  return nil if @thread.nil?
  Thread.kill(@thread)
  @thread = nil
end

#flush_readObject



55
56
57
# File 'lib/dino/tx_rx/base.rb', line 55

def flush_read
  gets until gets == nil
end

#getsObject



59
60
61
# File 'lib/dino/tx_rx/base.rb', line 59

def gets
  IO.select([io], nil, nil, 0.005) && io.gets
end

#handshakeObject

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dino/tx_rx/base.rb', line 36

def handshake
  flush_read
  100.times do
    begin
      write("!9000000.")
      Timeout::timeout(0.1) do
        line = gets.to_s
        if line.match /ACK:/
          flush_read
          return line.chop.split(/:/)[1].to_i
        end
      end
    rescue
      nil
    end
  end
 raise BoardNotFound
end

#readObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dino/tx_rx/base.rb', line 9

def read
  @thread ||= Thread.new do
    loop do
      line = gets
      if line && line.match(/\A\d+:/)
        pin, message = line.chop.split(/:/)
        pin && message && changed && notify_observers(pin, message)
      end
    end
  end
end

#write(message) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/dino/tx_rx/base.rb', line 27

def write(message)
  loop do
    if IO.select(nil, [io], nil)
      io.syswrite(message)
      break
    end
  end
end