Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplify Serial TxRx. Configurable baud rate. Other minor fixes.

  • Loading branch information...
commit 215043df87ffe668a1fdace1f8dd46820819ac21 1 parent 9784361
@vickash vickash authored
View
2  bin/dino
@@ -14,7 +14,7 @@ def usage
$stderr.puts "Usage: #{File.basename($0)} COMMAND [command-specific-options]"
$stderr.puts
$stderr.puts "Commands:"
- $stderr.puts " generate-sketch"
+ $stderr.puts " generate-sketch SKETCH [options]"
$stderr.puts
$stderr.puts " Sketches:"
$stderr.puts " serial"
View
2  examples/ser2net.rb
@@ -9,7 +9,7 @@
# Note that we're using ser2net in raw TCP mode and not telnet mode which is more common.
#
# Replace /dev/cu.usbmodem621 with your Arduino device.
-# Arduino UNOs are usually /dev/ACM0 under Linux.
+# Arduino UNOs are usually /dev/ttyACM0 under Linux.
#
# ser2net is preinstalled on many Linuxes. Install ser2net at the Mac Terminal with:
# brew install ser2net
View
6 lib/dino/components/rgb_led.rb
@@ -27,9 +27,9 @@ def after_initialize(options={})
COLORS.each_key do |color|
define_method(color) do
- analog_write(COLORS[color][0], pins[:red])
- analog_write(COLORS[color][1], pins[:green])
- analog_write(COLORS[color][2], pins[:blue])
+ analog_write(pins[:red], COLORS[color][0])
+ analog_write(pins[:green], COLORS[color][1])
+ analog_write(pins[:blue], COLORS[color][2])
end
end
View
35 lib/dino/tx_rx/usb_serial.rb
@@ -5,42 +5,27 @@ module TxRx
class USBSerial < Base
BAUD = 115200
- def initialize(device = nil)
+ def initialize(device = nil, options={})
@device = device
+ @baud = options[:baud] || BAUD
@first_write = true
end
def io
- raise BoardNotFound unless @io ||= find_arduino
- @io
- end
-
- def io=(device)
- @io = SerialPort.new(device, BAUD)
+ @io ||= connect
end
private
- def tty_devices
- return [@device] if @device
- if RUBY_PLATFORM.include?("mswin") || RUBY_PLATFORM.include?("mingw")
- com_ports = []
- 1.upto(9) { |n| com_ports << "COM#{n}" }
- com_ports
- else
- `ls /dev`.split("\n").grep(/usb|ACM/).map{|d| "/dev/#{d}"}
- end
+ def connect
+ tty_devices.each { |device| return SerialPort.new(device, @baud) rescue nil }
+ raise BoardNotFound
end
- def find_arduino
- tty_devices.map do |device|
- next if device.match /^cu/
- begin
- SerialPort.new(device, BAUD)
- rescue
- nil
- end
- end.compact.first
+ def tty_devices
+ return [@device] if @device
+ return (1..9).map { |n| "COM#{n}" } if RUBY_PLATFORM.match /mswin|mingw/i
+ `ls /dev`.split("\n").grep(/usb|ACM/).map{ |d| "/dev/#{d}" }
end
end
end
View
36 spec/lib/tx_rx/usb_serial_spec.rb
@@ -12,14 +12,15 @@ module Dino
describe '#io' do
context "on windows" do
- it 'should instantiate a new SerialPort for each usb tty device found' do
+ it 'should instantiate a new SerialPort for the first available tty device' do
original_platform = RUBY_PLATFORM
Constants.redefine(:RUBY_PLATFORM, "mswin", :on => Object)
- subject.should_receive(:tty_devices).and_return(["COM1", "COM2", "COM3", "COM4"])
- SerialPort.should_receive(:new).with('COM1', TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
- SerialPort.should_receive(:new).with('COM2', TxRx::USBSerial::BAUD).and_return(mock)
- SerialPort.should_receive(:new).with('COM3', TxRx::USBSerial::BAUD).and_return(mock)
- SerialPort.should_receive(:new).with('COM4', TxRx::USBSerial::BAUD).and_return(mock)
+ subject.should_receive(:tty_devices).and_return(["COM1", "COM2", "COM3"])
+
+ # COM2 is chosen as available for this test.
+ SerialPort.should_receive(:new).with("COM1", TxRx::USBSerial::BAUD).and_raise
+ SerialPort.should_receive(:new).with("COM2", TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
+ SerialPort.should_not_receive(:new).with("COM3", TxRx::USBSerial::BAUD)
subject.io.should == mock_serial
Constants.redefine(:RUBY_PLATFORM, original_platform, :on => Object)
@@ -27,21 +28,22 @@ module Dino
end
context "on unix" do
- it 'should instantiate a new SerialPort for each usb tty device found' do
- subject.should_receive(:tty_devices).and_return(['/dev/tty1.usb', '/dev/tty1.usb', '/dev/tty.ACM0'])
- SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
- SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::USBSerial::BAUD).and_return(mock)
- SerialPort.should_receive(:new).with('/dev/tty.ACM0', TxRx::USBSerial::BAUD).and_return(mock)
+ it 'should instantiate a new SerialPort for the first available tty device' do
+ subject.should_receive(:tty_devices).and_return(['/dev/ttyACM0', '/dev/tty.usbmodem1'])
+
+ # /dev/ttyACM0 is chosen as available for this test.
+ SerialPort.should_receive(:new).with('/dev/ttyACM0', TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
+ SerialPort.should_not_receive(:new).with('/dev/tty.usbmodem1', TxRx::USBSerial::BAUD)
subject.io.should == mock_serial
end
end
it 'should use the existing io instance if set' do
- subject.should_not_receive(:tty_devices)
+ subject.should_receive(:tty_devices).once.and_return(['/dev/tty.ACM0', '/dev/tty.usbmodem1'])
SerialPort.stub(:new).and_return(mock_serial = mock)
- subject.io = '/dev/tty1.usb'
+ 3.times { subject.io }
subject.io.should == mock_serial
end
@@ -51,14 +53,6 @@ module Dino
end
end
- describe '#io=' do
- it 'should set io to a new serial port with the specified device' do
- SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
- subject.io = '/dev/tty1.usb'
- subject.instance_variable_get(:@io).should == mock_serial
- end
- end
-
describe '#read' do
it 'should create a new thread' do
Thread.should_receive :new
Please sign in to comment.
Something went wrong with that request. Please try again.