lib/arduino_firmata/arduino.rb in arduino_firmata-0.3.1 vs lib/arduino_firmata/arduino.rb in arduino_firmata-0.3.2
- old
+ new
@@ -3,11 +3,12 @@
class Arduino
include EventEmitter
attr_reader :version, :status, :nonblock_io, :eventmachine
- def initialize(serial_name, params)
+ def initialize(serialport_name, params)
+ @serialport_name = serialport_name
@nonblock_io = !!params[:nonblock_io]
@eventmachine = !!params[:eventmachine]
@read_byte_size = eventmachine ? 256 : 9600
@process_input_interval = eventmachine ? 0.0001 : 0.01
@status = Status::CLOSE
@@ -22,31 +23,18 @@
@digital_input_data = Array.new(16, 0)
@analog_input_data = Array.new(16, 0)
@version = nil
- @serial = SerialPort.new(serial_name, params[:bps], params[:bit], params[:stopbit], params[:parity])
- @serial.read_timeout = 3
- sleep 3
+ @serial = SerialPort.new(@serialport_name, params[:bps], params[:bit], params[:stopbit], params[:parity])
+ @serial.read_timeout = 10
+ sleep 3 if old_arduino_device?
@status = Status::OPEN
- trap 'SIGHUP' do
+ at_exit do
close
- exit
end
- trap 'SIGINT' do
- close
- exit
- end
- trap 'SIGKILL' do
- close
- exit
- end
- trap 'SIGTERM' do
- close
- exit
- end
@thread_status = false
run do
@thread_status = true
while status == Status::OPEN do
@@ -68,19 +56,23 @@
loop do
write REPORT_VERSION
sleep 0.5
break if @version
end
- sleep 0.5
+ sleep 0.5 if old_arduino_device?
end
def run(&block)
return unless block_given?
if eventmachine
EM::defer &block
else
Thread.new &block
end
+ end
+
+ def old_arduino_device?
+ File.basename(@serialport_name) !~ /^tty\.usbmodem/
end
def close
return if status == Status::CLOSE
@status = Status::CLOSE