Permalink
Browse files

The board can now read good

  • Loading branch information...
austinbv committed Sep 17, 2012
1 parent 064c13e commit f5eba336f4024a4419136d113a96d1b2fdc14b57
Showing with 298 additions and 46 deletions.
  1. +17 −12 lib/dino/board.rb
  2. +12 −23 lib/dino/tx_rx.rb
  3. +18 −11 spec/lib/board_spec.rb
  4. +251 −0 src/du.ino
View
@@ -18,22 +18,17 @@ def read!
def write(msg, opts = {})
formatted_msg = opts.delete(:no_wrap) ? msg : "!#{msg}."
- @io.puts(formatted_msg).nil?
+ @io.write(formatted_msg).nil?
end
- def digital_write(pin, value)
+ def digital_write(pin, value)'/dev/tty.usbmodem1411'
pin, value = normalize_pin(pin), normalize_value(value)
write("01#{pin}#{value}")
end
- def normalize_pin(pin)
- raise Exception.new('pins can only be two digits') if pin.to_s.length > 2
- normalize(pin, 2)
- end
-
- def normalize_value(value)
- raise Exception.new('values are limited to three digits') if value.to_s.length > 3
- normalize(value, 3)
+ def digital_read(pin)
+ pin, value = normalize_pin(pin), normalize_value(0)
+ write("02#{pin}#{value}")
end
def set_pin_mode(pin, mode)
@@ -42,8 +37,18 @@ def set_pin_mode(pin, mode)
end
def set_debug(on_off)
- value = normalize_value(on_off == :on ? 1 : 0)
- write("9900#{value}")
+ pin, value = normalize_pin(0), normalize_value(on_off == :on ? 1 : 0)
+ write("99#{pin}#{value}")
+ end
+
+ def normalize_pin(pin)
+ raise Exception.new('pins can only be two digits') if pin.to_s.length > 2
+ normalize(pin, 2)
+ end
+
+ def normalize_value(value)
+ raise Exception.new('values are limited to three digits') if value.to_s.length > 3
+ normalize(value, 3)
end
private
View
@@ -6,50 +6,39 @@ class TxRx
include Celluloid
BAUD = 115200
- def arduino
- @arduino ||= tty_devices.map do |device|
+ def io
+ @io ||= tty_devices.map do |device|
next if device.match /^cu/
begin
- SerialPort.new(device, BAUD)
+ SerialPort.new("/dev/#{device}", BAUD)
rescue
nil
end
end.compact.first
end
- def arduino=(device)
- @arduino = SerialPort.new(device, BAUD)
+ def io=(device)
+ @io = SerialPort.new(device, BAUD)
end
def read
- message, started = [], false
-
loop do
- input = @arduino.getc
-
- if input == '!' || started
- message << input
- started = true
- end
-
- if input == '.'
- started = false
- puts input
+ if IO.select([io], nil, nil, 0.05)
+ puts io.gets
end
+ sleep 0.001
end
end
- def puts(message)
- @arduino.puts(message)
- end
-
- def serial_messages
+ def write(message)
+ IO.select(nil, [io], nil, 0.05)
+ io.puts(message)
end
private
def tty_devices
- exec('ls /dev | grep usb').split /\n/
+ `ls /dev | grep usb`.split(/\n/)
end
end
end
View
@@ -3,7 +3,7 @@
module Dino
describe Board do
def io_mock(methods = {})
- @io ||= mock(:io, {puts: nil}.merge(methods))
+ @io ||= mock(:io, {write: nil}.merge(methods))
end
subject { Board.new(io_mock) }
@@ -20,7 +20,7 @@ def io_mock(methods = {})
end
it 'should send 8 bits first if this is the first write' do
- io_mock.should_receive(:puts).with('00000000')
+ io_mock.should_receive(:write).with('00000000')
Board.new(io_mock)
end
end
@@ -43,31 +43,38 @@ def io_mock(methods = {})
describe '#write' do
it 'should return true if the write succeeds' do
- board = Board.new(mock(:io, puts: nil))
+ board = Board.new(mock(:io, write: nil))
board.write("message").should == true
end
it 'should wrap the message in a ! and a . by default' do
- io_mock.should_receive(:puts).with('!hello.')
+ io_mock.should_receive(:write).with('!hello.')
subject.write('hello')
end
it 'should not wrap the message if no_wrap is set to true' do
io = io_mock
board = Board.new(io)
- io.should_receive(:puts).with('hello')
+ io.should_receive(:write).with('hello')
board.write('hello', no_wrap: true)
end
end
describe '#digital_write' do
it 'should append a append a write to the pin and value' do
- io = mock(:io, puts: nil)
- io.should_receive(:puts).with('!0101003.')
+ io = mock(:io, write: nil)
+ io.should_receive(:write).with('!0101003.')
Board.new(io).digital_write(01, 003)
end
end
+ describe '#digital_read' do
+ it 'should tell the board to start reading from the given pin' do
+ io_mock.should_receive(:write).with('!0213000.')
+ subject.digital_read(13)
+ end
+ end
+
describe '#normalize_pin' do
it 'should normalize numbers so they are two digits' do
subject.normalize_pin(1).should == '01'
@@ -98,24 +105,24 @@ def io_mock(methods = {})
describe '#set_pin_mode' do
it 'should send a value of 1 if the pin mode is set to out' do
- io_mock.should_receive(:puts).with('!0013001.')
+ io_mock.should_receive(:write).with('!0013001.')
subject.set_pin_mode(13, :out)
end
it 'should send a value of 0 if the pin mode is set to in' do
- io_mock.should_receive(:puts).with('!0013000.')
+ io_mock.should_receive(:write).with('!0013000.')
subject.set_pin_mode(13, :in)
end
end
describe '#set_debug' do
it 'should set the boards debug on when passed on' do
- io_mock.should_receive(:puts).with('!9900001.')
+ io_mock.should_receive(:write).with('!9900001.')
subject.set_debug(:on)
end
it 'should set the boards debug off when passed off' do
- io_mock.should_receive(:puts).with('!9900000.')
+ io_mock.should_receive(:write).with('!9900000.')
subject.set_debug(:off)
end
end
Oops, something went wrong.

0 comments on commit f5eba33

Please sign in to comment.