lib/sphero.rb in sphero-1.5.0 vs lib/sphero.rb in sphero-1.5.1
- old
+ new
@@ -1,12 +1,11 @@
require 'sphero/request'
require 'sphero/response'
require 'thread'
+require 'rubyserial'
class Sphero
- VERSION = '1.5.0'
-
FORWARD = 0
RIGHT = 90
BACKWARD = 180
LEFT = 270
@@ -19,21 +18,23 @@
retries_left = DEFAULT_RETRIES
begin
sphero = self.new dev
if (block_given?)
begin
- sphero.instance_eval(&block)
+ sphero.instance_eval(&block)
ensure
- sphero.close
+ sphero.close
end
return nil
end
- return sphero
- rescue Errno::EBUSY
- puts retries_left
- retries_left = retries_left - 1
- retry unless retries_left < 0
+ return sphero
+ rescue RubySerial::Exception => e
+ if e.message == 'EBUSY'
+ puts retries_left
+ retries_left = retries_left - 1
+ retry unless retries_left < 0
+ end
end
end
end
def initialize dev
@@ -59,11 +60,11 @@
loop do
@responses << @response_queue.pop
end
}
end
-
+
def close
return if @sp.nil? || @sp.closed?
begin
stop
rescue Exception => e
@@ -168,19 +169,19 @@
end
# configure data streaming notification messages
def set_data_streaming n, m, mask, pcnt, mask2
queue_packet Request::SetDataStreaming.new(@seq, limit2(n), limit2(m),
- limit4(mask), limit1(pcnt), limit4(mask2) )
+ limit4(mask), limit1(pcnt), limit4(mask2) )
end
# configure collision detection messages
def configure_collision_detection meth, x_t, y_t, x_spd, y_spd, dead
queue_packet Request::ConfigureCollisionDetection.new(@seq, limit1(meth),
- limit1(x_t), limit1(y_t),
- limit1(x_spd), limit1(y_spd),
- limit1(dead) )
+ limit1(x_t), limit1(y_t),
+ limit1(x_spd), limit1(y_spd),
+ limit1(dead) )
end
private
def sync_response seq
@@ -229,16 +230,16 @@
limit value, 0xFFFFFFFF
end
def flag(value)
case value
- when true
- 0x01
- when false
- 0x00
- else
- value
+ when true
+ 0x01
+ when false
+ 0x00
+ else
+ value
end
end
def is_windows?
os = RUBY_PLATFORM.split("-")[1]
@@ -248,41 +249,30 @@
false
end
end
def initialize_serialport dev
- require 'serialport'
- @sp = SerialPort.new dev, 115200, 8, 1, SerialPort::NONE
- if is_windows?
- @sp.read_timeout=1000
- @sp.write_timeout=0
- @sp.initial_byte_offset=5
- end
- rescue LoadError
- puts "Please 'gem install hybridgroup-serialport' for serial port support."
+ @sp = Serial.new dev, 115200
+ rescue RubySerial::Exception => e
+ retry if e.message == 'EBUSY'
end
def queue_packet packet
@packets << packet
end
def write packet
header, body = nil
- IO.select([], [@sp], [], 1)
- @lock.synchronize do
- @sp.write packet.to_str
- @seq += 1
- end
- IO.select([@sp], [], [], 1)
+ @sp.write packet.to_str
+ @seq += 1
header = read_header(true)
body = read_body(header.last, true) if header
# pick off asynch packets and store, till we get to the message response
while header && Response.async?(header)
messages << Response::AsyncResponse.response(header, body)
- IO.select([@sp], [], [], 1)
header = read_header(true)
if header
body = read_body(header.last, true)
else
body = nil
@@ -292,22 +282,22 @@
response = packet.response header, body
if response.success?
@response_queue << response
else
- raise "Unable to write to Sphero!"
+ puts "Unable to write to Sphero!"
end
end
def read_header(blocking=false)
header = nil
begin
data = read_next_chunk(5, blocking)
- return nil unless data
+ return nil unless data && data.length == 5
header = data.unpack 'C5'
- rescue Errno::EBUSY
- retry
+ rescue RubySerial::Exception => e
+ retry if e.message == 'EBUSY'
rescue Exception => e
puts e.message
puts e.backtrace.inspect
retry
end
@@ -318,12 +308,12 @@
def read_body(len, blocking=false)
data = nil
begin
data = read_next_chunk(len, blocking)
return nil unless data && data.length == len
- rescue Errno::EBUSY
- retry
+ rescue RubySerial::Exception => e
+ retry if e.message == 'EBUSY'
rescue Exception => e
puts e.message
puts e.backtrace.inspect
retry
end
@@ -332,18 +322,16 @@
end
def read_next_chunk(len, blocking=false)
data = nil
begin
- @lock.synchronize do
- if blocking || is_windows?
- data = @sp.read(len)
- else
- data = @sp.read_nonblock(len)
- end
+ if blocking || is_windows?
+ data = @sp.read(len)
+ else
+ data = @sp.read_nonblock(len)
end
- rescue Errno::EBUSY
- retry
+ rescue RubySerial::Exception => e
+ retry if e.message == 'EBUSY'
rescue Exception => e
puts e.message
puts e.backtrace.inspect
return nil
end