hybridgroup-sphero 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ require 'sphero/response'
3
3
  require 'thread'
4
4
 
5
5
  class Sphero
6
- VERSION = '1.3.0'
6
+ VERSION = '1.4.0'
7
7
 
8
8
  FORWARD = 0
9
9
  RIGHT = 90
@@ -12,7 +12,7 @@ class Sphero
12
12
 
13
13
  DEFAULT_RETRIES = 3
14
14
 
15
- attr_accessor :connection_types, :async_messages
15
+ attr_accessor :connection_types, :messages
16
16
 
17
17
  class << self
18
18
  def start(dev, &block)
@@ -46,11 +46,15 @@ class Sphero
46
46
  @dev = 0x00
47
47
  @seq = 0x00
48
48
  @lock = Mutex.new
49
- @async_messages = []
49
+ @messages = Queue.new
50
50
  end
51
51
 
52
52
  def close
53
- @lock.synchronize do
53
+ begin
54
+ stop
55
+ rescue Exception => e
56
+ puts e.message
57
+ ensure
54
58
  @sp.close
55
59
  end
56
60
  end
@@ -145,26 +149,7 @@ class Sphero
145
149
  def configure_collision_detection meth, x_t, y_t, x_spd, y_spd, dead
146
150
  write Request::ConfigureCollisionDetection.new(@seq, meth, x_t, y_t, x_spd, y_spd, dead)
147
151
  end
148
-
149
- # read all outstanding async packets and store in async_responses
150
- # would not do well to receive simple responses this way...
151
- def read_async_messages
152
- header, body = nil
153
- new_responses = []
154
-
155
- @lock.synchronize do
156
- header, body = read_next_response
157
-
158
- while header && Response.async?(header)
159
- new_responses << Response::AsyncResponse.response(header, body)
160
- header, body = read_next_response
161
- end
162
- end
163
-
164
- async_messages.concat(new_responses) unless new_responses.empty?
165
- return !new_responses.empty?
166
- end
167
-
152
+
168
153
  private
169
154
 
170
155
  def is_windows?
@@ -191,24 +176,27 @@ class Sphero
191
176
  def write packet
192
177
  header, body = nil
193
178
 
179
+ IO.select([], [@sp], [], 20)
180
+
194
181
  @lock.synchronize do
195
- rs, ws = IO.select([], [@sp], [], 20)
196
182
  @sp.write packet.to_str
197
183
  @seq += 1
184
+ end
198
185
 
199
- header = nil
200
- loop do
201
- header = read_header(true)
202
- break if header
203
- end
186
+ IO.select([@sp], [], [], 20)
187
+ header = read_header(true)
188
+ body = read_body(header.last, true) if header
204
189
 
205
- body = read_body(header.last, true) if header
190
+ # pick off asynch packets and store, till we get to the message response
191
+ while header && Response.async?(header)
192
+ messages << Response::AsyncResponse.response(header, body)
206
193
 
207
- # pick off asynch packets and store, till we get to the message response
208
- while header && Response.async?(header)
209
- async_messages << Response::AsyncResponse.response(header, body)
210
- header = read_header(true)
211
- body = read_body(header.last, true) if header
194
+ IO.select([@sp], [], [], 20)
195
+ header = read_header(true)
196
+ if header
197
+ body = read_body(header.last, true)
198
+ else
199
+ body = nil
212
200
  end
213
201
  end
214
202
 
@@ -222,9 +210,10 @@ class Sphero
222
210
  end
223
211
 
224
212
  def read_header(blocking=false)
213
+ header = nil
225
214
  begin
226
215
  data = read_next_chunk(5, blocking)
227
- return nil unless data && data.length == 5
216
+ return nil unless data
228
217
  header = data.unpack 'C5'
229
218
  rescue Errno::EBUSY
230
219
  retry
@@ -238,6 +227,7 @@ class Sphero
238
227
  end
239
228
 
240
229
  def read_body(len, blocking=false)
230
+ data = nil
241
231
  begin
242
232
  data = read_next_chunk(len, blocking)
243
233
  return nil unless data && data.length == len
@@ -253,12 +243,14 @@ class Sphero
253
243
  end
254
244
 
255
245
  def read_next_chunk(len, blocking=false)
246
+ data = nil
256
247
  begin
257
- if blocking || is_windows?
258
- data = @sp.read(len)
259
- return nil unless data && data.length == len
260
- else
261
- data = @sp.read_nonblock(len)
248
+ @lock.synchronize do
249
+ if blocking || is_windows?
250
+ data = @sp.read(len)
251
+ else
252
+ data = @sp.read_nonblock(len)
253
+ end
262
254
  end
263
255
  rescue Errno::EBUSY
264
256
  retry
@@ -22,7 +22,7 @@ class Sphero
22
22
 
23
23
  def response header, body
24
24
  name = self.class.name.split('::').last
25
- klass = if Response.const_defined?(name)
25
+ if Response.const_defined?(name)
26
26
  Response.const_get(name).new header, body
27
27
  else
28
28
  Response.new header, body
@@ -4,7 +4,8 @@ require 'sphero'
4
4
  class TestSpheroRequest < MiniTest::Unit::TestCase
5
5
  def test_ping_checksum
6
6
  ping = Sphero::Request::Ping.new 0
7
- assert_equal "\xFF\xFF\x00\x01\x00\x01\xFD", ping.to_str
7
+ expected_bytes = "\xFF\xFF\x00\x01\x00\x01\xFD".force_encoding(Encoding::ASCII_8BIT)
8
+ assert_equal expected_bytes, ping.to_str
8
9
  end
9
10
 
10
11
  def test_sleep_dlen
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hybridgroup-sphero
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-10 00:00:00.000000000 Z
12
+ date: 2013-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc