Skip to content

Commit

Permalink
Move handshake into TxRx::Base
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Feb 15, 2013
1 parent 5e8ded0 commit d03e853
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
30 changes: 5 additions & 25 deletions lib/dino/board.rb
@@ -1,5 +1,3 @@
require 'timeout'

module Dino
class Board
attr_reader :digital_hardware, :analog_hardware, :analog_zero
Expand All @@ -8,7 +6,7 @@ class Board
def initialize(io)
@io, @digital_hardware, @analog_hardware = io, [], []
io.add_observer(self)
handshake
reset
end

def update(pin, msg)
Expand Down Expand Up @@ -39,6 +37,10 @@ def remove_analog_hardware(part)
@analog_hardware.delete(part)
end

def reset
@analog_zero = @io.handshake
end

def start_read
@io.read
end
Expand Down Expand Up @@ -75,10 +77,6 @@ def set_pin_mode(pin, mode, pullup=nil)
end
end

def reset
write("!9000000.", no_wrap: true)
end

def set_debug(on_off)
pin, value = normalize_pin(0), normalize_value(on_off == :on ? 1 : 0)
write("99#{pin}#{value}")
Expand Down Expand Up @@ -109,23 +107,5 @@ def normalize_value(value)
def normalize(pin, spaces)
pin.to_s.rjust(spaces, '0')
end

def handshake
50.times do
begin
reset
Timeout::timeout(0.1) do
line = @io.gets.to_s.chop
if line.match /ACK/
@analog_zero = line.split(/:/)[1].to_i
return @io.flush_read
end
end
rescue
nil
end
end
raise BoardNotFound
end
end
end
23 changes: 21 additions & 2 deletions lib/dino/tx_rx/base.rb
@@ -1,4 +1,5 @@
require 'observer'
require 'timeout'

module Dino
module TxRx
Expand Down Expand Up @@ -26,18 +27,36 @@ def close_read
def write(message)
loop do
if IO.select(nil, [io], nil)
io.write(message)
io.puts(message)
break
end
end
end

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

def flush_read
gets until gets == nil
end

def gets
IO.select([io], nil, nil, 0.01) && io.gets
IO.select([io], nil, nil, 0.005) && io.gets
end
end
end
Expand Down
10 changes: 4 additions & 6 deletions spec/lib/board_spec.rb
Expand Up @@ -3,7 +3,7 @@
module Dino
describe Dino::Board do
def io_mock(methods = {})
@io ||= mock(:io, {write: nil, add_observer: nil, gets: "ACK\n", flush_read: nil}.merge(methods))
@io ||= mock(:io, {write: nil, add_observer: nil, flush_read: nil, handshake: "14"}.merge(methods))
end

subject { Board.new(io_mock) }
Expand All @@ -21,11 +21,9 @@ def io_mock(methods = {})
end

it 'should initiate the handshake' do
io_mock.should_receive(:write).with("!9000000.")
io_mock.should_receive(:handshake)
subject
end

it 'should wait for acknowledgement'
end

describe '#update' do
Expand Down Expand Up @@ -220,7 +218,7 @@ def io_mock(methods = {})

describe '#reset' do
it 'should tell the board to reset to defaults' do
io_mock.should_receive(:write).with('!9000000.')
io_mock.should_receive(:handshake)
subject.reset
end
end
Expand All @@ -247,7 +245,7 @@ def io_mock(methods = {})
end

it 'should raise if a number larger than two digits are given' do
expect { subject.normalize_pin(1000) }.to raise_exception 'pins can only be two digits'
expect { subject.normalize_pin(1000) }.to raise_exception 'pin number must be in 0-99'
end
end

Expand Down

0 comments on commit d03e853

Please sign in to comment.