rubysphero 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rubysphero.rb +62 -46
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 634d5c52ce52dece3c540e662dbfef7f7aa50fb9
|
4
|
+
data.tar.gz: 721f4e07381929fd20fc64c2a381d6a5e17a7a46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 914745898bccac50217daab9c34d1768d9eb98fefb724f5a5e4e84d3271af9243818a3e5a4a3efdba27b9db9752451e6ba5c3e871a2729f6ee4f226244285173
|
7
|
+
data.tar.gz: d16fca27c0aaed518fb6657f4c00212f57f009fc022e814b4bbb1bc0b0b1d5e3a21e576294b37396bf64006ee59329b9185d571376fff328446722d0f12e380f
|
data/lib/rubysphero.rb
CHANGED
@@ -38,10 +38,12 @@ module SpheroUtilities
|
|
38
38
|
end # def
|
39
39
|
|
40
40
|
end # mod
|
41
|
+
|
41
42
|
class SpheroClient
|
42
43
|
|
43
44
|
include SpheroUtilities
|
44
|
-
|
45
|
+
COMMS_RETRY=5
|
46
|
+
|
45
47
|
COLOURS = { :blue => [0x00,0x00,0xFF],
|
46
48
|
:green => [0x00,0xFF,0x00],
|
47
49
|
:red => [0xFF,0x00,0x00],
|
@@ -55,33 +57,41 @@ class SpheroClient
|
|
55
57
|
def ping
|
56
58
|
logd()
|
57
59
|
logd "Building request: ping"
|
58
|
-
request=SpheroRequest.new()
|
59
|
-
|
60
|
-
request.sop1=0xFF
|
61
|
-
request.sop2=0xFF
|
60
|
+
request=SpheroRequest.new(:synchronous)
|
62
61
|
|
63
62
|
request.did=0x00
|
64
63
|
request.cid=0x01 # Ping
|
65
|
-
request.seq=
|
64
|
+
request.seq=get_sequence
|
66
65
|
request.dlen=0x01
|
67
|
-
send_data(request.build_packet)
|
68
66
|
|
69
|
-
|
67
|
+
return send_and_check(request)
|
70
68
|
end # def
|
71
69
|
|
70
|
+
def send_and_check(request)
|
71
|
+
send_data(request.build_packet)
|
72
|
+
|
73
|
+
COMMS_RETRY.times do
|
74
|
+
response = read_data(request.length)
|
75
|
+
if request.seq == response.echoed_seq then
|
76
|
+
logd("Sent and received Sequences MATCH.")
|
77
|
+
return true
|
78
|
+
end # if
|
79
|
+
logd("Sequences do not match. Sent:#{request.seq} Rec:#{response.echoed_seq} ")
|
80
|
+
end # times
|
81
|
+
return false
|
82
|
+
end # data
|
83
|
+
|
72
84
|
def send_data(data)
|
73
85
|
logd("Wire send next.")
|
74
86
|
@connection.write data
|
75
|
-
|
76
87
|
end # def
|
77
88
|
|
78
|
-
def read_data
|
79
|
-
bytes=@connection.read(
|
89
|
+
def read_data(length)
|
90
|
+
bytes=@connection.read(length).unpack("C*")
|
80
91
|
logd("Wire read finished.")
|
81
92
|
response = SpheroResponse.new( bytes)
|
82
93
|
|
83
94
|
return response
|
84
|
-
|
85
95
|
end # def
|
86
96
|
|
87
97
|
def orientation
|
@@ -99,12 +109,12 @@ class SpheroClient
|
|
99
109
|
set_back_led_output(0)
|
100
110
|
set_heading(heading)
|
101
111
|
set_colour(:white)
|
102
|
-
|
112
|
+
return true
|
103
113
|
else
|
104
114
|
sleep 1
|
105
115
|
end # if
|
106
116
|
end # upto
|
107
|
-
|
117
|
+
return false
|
108
118
|
end # def
|
109
119
|
|
110
120
|
|
@@ -112,19 +122,15 @@ class SpheroClient
|
|
112
122
|
logd()
|
113
123
|
logd "Building request: set back led output b"
|
114
124
|
|
115
|
-
request=SpheroRequest.new()
|
116
|
-
|
117
|
-
request.sop1=0xFF
|
118
|
-
request.sop2=0xFF
|
125
|
+
request=SpheroRequest.new(:synchronous)
|
119
126
|
|
120
127
|
request.did=0x02
|
121
128
|
request.cid=0x21
|
122
|
-
request.seq=
|
129
|
+
request.seq=get_sequence
|
123
130
|
request.dlen=0x02
|
124
131
|
request.push_data brightness
|
125
132
|
|
126
|
-
|
127
|
-
read_data
|
133
|
+
return send_and_check(request)
|
128
134
|
|
129
135
|
end # def
|
130
136
|
|
@@ -132,30 +138,25 @@ class SpheroClient
|
|
132
138
|
logd
|
133
139
|
logd( "Building request: Set Heading")
|
134
140
|
|
135
|
-
request=SpheroRequest.new()
|
141
|
+
request=SpheroRequest.new(:synchronous)
|
136
142
|
heading = heading_raw%359
|
137
143
|
logd( "Heading: #{heading}")
|
138
|
-
|
139
|
-
request.sop1=0xFF
|
140
|
-
request.sop2=0xFF
|
141
144
|
|
142
145
|
request.did=0x02
|
143
146
|
request.cid=0x01
|
144
|
-
request.seq=
|
147
|
+
request.seq=get_sequence
|
145
148
|
request.dlen=0x03
|
146
149
|
|
147
150
|
request.push_data(heading , :a16bit)
|
148
151
|
|
149
|
-
|
150
|
-
read_data
|
151
|
-
|
152
|
+
return send_and_check(request)
|
152
153
|
|
153
154
|
end # def
|
154
155
|
|
155
156
|
def set_colour(chosen_colour)
|
156
157
|
logd
|
157
158
|
logd("Locating RGB values for: #{chosen_colour}")
|
158
|
-
set_colour_rgb(COLOURS[chosen_colour][0],COLOURS[chosen_colour][1],COLOURS[chosen_colour][2])
|
159
|
+
return set_colour_rgb(COLOURS[chosen_colour][0],COLOURS[chosen_colour][1],COLOURS[chosen_colour][2])
|
159
160
|
end # def
|
160
161
|
|
161
162
|
|
@@ -163,14 +164,12 @@ class SpheroClient
|
|
163
164
|
logd
|
164
165
|
logd "Building request: colour"
|
165
166
|
|
166
|
-
request=SpheroRequest.new()
|
167
|
+
request=SpheroRequest.new(:synchronous)
|
167
168
|
|
168
|
-
request.sop1=0xFF
|
169
|
-
request.sop2=0xFF
|
170
169
|
|
171
170
|
request.did=0x02
|
172
171
|
request.cid=0x20 # Set RGB Output
|
173
|
-
request.seq=
|
172
|
+
request.seq=get_sequence
|
174
173
|
request.dlen=0x05
|
175
174
|
request.push_data red_value
|
176
175
|
request.push_data green_value
|
@@ -178,24 +177,21 @@ class SpheroClient
|
|
178
177
|
flag=0x01
|
179
178
|
request.push_data flag
|
180
179
|
|
181
|
-
|
182
|
-
|
180
|
+
return send_and_check(request)
|
181
|
+
|
183
182
|
end # def
|
184
183
|
|
185
184
|
def roll(heading_raw=0, speed=0xFF)
|
186
185
|
logd()
|
187
186
|
logd( "Building request: roll")
|
188
187
|
|
189
|
-
request=SpheroRequest.new()
|
188
|
+
request=SpheroRequest.new(:synchronous)
|
190
189
|
heading = heading_raw%359
|
191
190
|
logd( "Heading: #{heading}")
|
192
|
-
|
193
|
-
request.sop1=0xFF
|
194
|
-
request.sop2=0xFF
|
195
191
|
|
196
192
|
request.did=0x02
|
197
193
|
request.cid=0x30 # Roll
|
198
|
-
request.seq=
|
194
|
+
request.seq=get_sequence
|
199
195
|
request.dlen=0x05
|
200
196
|
|
201
197
|
state=0x01
|
@@ -204,12 +200,21 @@ class SpheroClient
|
|
204
200
|
request.push_data(heading , :a16bit)
|
205
201
|
request.push_data state
|
206
202
|
|
207
|
-
|
208
|
-
read_data
|
203
|
+
return send_and_check(request)
|
209
204
|
|
210
205
|
end #def
|
211
206
|
|
207
|
+
def get_sequence
|
208
|
+
@sequence_val+=1
|
209
|
+
if @sequence_val > 255
|
210
|
+
@sequence_val=0
|
211
|
+
end # if
|
212
|
+
logd("Getting seq: #{@sequence_val}")
|
213
|
+
return @sequence_val
|
214
|
+
end # def
|
215
|
+
|
212
216
|
def initialize(bluetooth_address)
|
217
|
+
@sequence_val=0
|
213
218
|
return open(bluetooth_address)
|
214
219
|
end # class
|
215
220
|
|
@@ -234,7 +239,7 @@ class SpheroResponse
|
|
234
239
|
|
235
240
|
attr_accessor :calculated_checksum
|
236
241
|
attr_accessor :raw_checksum
|
237
|
-
|
242
|
+
attr_accessor :echoed_seq
|
238
243
|
attr_accessor :raw_data
|
239
244
|
attr_accessor :data
|
240
245
|
attr_accessor :valid
|
@@ -261,6 +266,8 @@ class SpheroResponse
|
|
261
266
|
if @raw_checksum == @calculated_checksum then
|
262
267
|
logd("Response Checksum is good")
|
263
268
|
@valid=true
|
269
|
+
logd("Response data:#{bytes}")
|
270
|
+
@echoed_seq=bytes[1]
|
264
271
|
@data=bytes
|
265
272
|
end # if
|
266
273
|
|
@@ -279,13 +286,22 @@ class SpheroRequest
|
|
279
286
|
attr_accessor :dlen
|
280
287
|
attr_accessor :seq
|
281
288
|
attr_accessor :checksum
|
282
|
-
|
289
|
+
attr_accessor :sequence
|
283
290
|
|
284
291
|
|
285
|
-
def initialize
|
292
|
+
def initialize(type=:synchronous)
|
293
|
+
if type==:synchronous
|
294
|
+
@sop1=0xFF
|
295
|
+
@sop2=0xFF
|
296
|
+
end # if
|
297
|
+
|
286
298
|
@packet_data=Array.new
|
287
299
|
@payload_data=Array.new
|
288
300
|
end # def
|
301
|
+
|
302
|
+
def length
|
303
|
+
return @packet_data.length
|
304
|
+
end # def
|
289
305
|
|
290
306
|
def build_packet
|
291
307
|
packet_no_checksum=[@sop1, @sop2, @did, @cid, @seq, @dlen]
|