rubysphero 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rubysphero.rb +322 -0
  3. metadata +64 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95f29ec095f65e8310c4c3e235f4e6e852f70588
4
+ data.tar.gz: ac486753bb8b6997d813a631e707929c2aa0a689
5
+ SHA512:
6
+ metadata.gz: cdc28a6a59f6adf1c16de55a3284da3bf40db9a48d3d79d928057cabe34ea5ca59721d1afd81ff0ab2206c69c8e476e9b9818c24a2a196e497db819967912003
7
+ data.tar.gz: 39111cb9d6ab71c0564cf66d04cbae6b8535fe9849a9b4a51bf4592ed12126a74c34698dbb1fa4f07d9ac1aa14161f0513249771c9a553a5f6f458a82bce4b5e
data/lib/rubysphero.rb ADDED
@@ -0,0 +1,322 @@
1
+ require 'rubyserial'
2
+ require 'date'
3
+
4
+ module SpheroUtilities
5
+
6
+
7
+ def print_format_bytes(bytes)
8
+ return_str=""
9
+ bytes.each do |rsp_array|
10
+ return_str += rsp_array.to_s(16)
11
+ return_str += " "
12
+ end # each
13
+ return return_str
14
+ end # def
15
+
16
+
17
+ def do_checksum(bytes)
18
+ total=0
19
+ bytes.each do | a_byte |
20
+ total+=a_byte
21
+ end # each
22
+ logd "Checksum of these bytes: #{print_format_bytes(bytes)}"
23
+ chk1= (total%256)
24
+ chk2= (chk1 ^ 0b11111111)
25
+ logd "Checksum calculated: #{chk2.to_s(16)}"
26
+ return chk2
27
+
28
+ end # def
29
+
30
+ def add_checksum(full_bytes)
31
+ to_chk_bytes = full_bytes.drop(2)
32
+ full_bytes.push do_checksum(to_chk_bytes)
33
+ return full_bytes
34
+ end # def
35
+
36
+ def logd(log_str="")
37
+ puts DateTime.now.strftime("%Y-%m-%d %H:%M:%S.%L") + " " + log_str
38
+ end # def
39
+
40
+ end # mod
41
+ class SpheroClient
42
+
43
+ include SpheroUtilities
44
+
45
+ COLOURS = { :blue => [0x00,0x00,0xFF],
46
+ :green => [0x00,0xFF,0x00],
47
+ :red => [0xFF,0x00,0x00],
48
+ :orange => [0xFF,0x8C,0x00],
49
+ :yellow => [0xFF,0xFF,0x00],
50
+ :white => [0xFF,0xFF,0xFF],
51
+ :black => [0x00,0x00,0x00],
52
+ :pink => [0xFF,0x00,0xFF],
53
+ :grey => [0x80,0x80,0x80]}
54
+
55
+ def ping
56
+ logd()
57
+ logd "Building request: ping"
58
+ request=SpheroRequest.new()
59
+
60
+ request.sop1=0xFF
61
+ request.sop2=0xFF
62
+
63
+ request.did=0x00
64
+ request.cid=0x01 # Ping
65
+ request.seq=0x52
66
+ request.dlen=0x01
67
+ send_data(request.build_packet)
68
+
69
+ read_data
70
+ end # def
71
+
72
+ def send_data(data)
73
+ logd("Wire send next.")
74
+ @connection.write data
75
+
76
+ end # def
77
+
78
+ def read_data
79
+ bytes=@connection.read(7).unpack("C*")
80
+ logd("Wire read finished.")
81
+ response = SpheroResponse.new( bytes)
82
+
83
+ return response
84
+
85
+ end # def
86
+
87
+ def orientation
88
+
89
+ set_back_led_output(255)
90
+ set_colour(:black)
91
+ 1.step(720,5) do | heading |
92
+ roll(heading , 0)
93
+
94
+ inputted_text=gets.chomp
95
+
96
+ if inputted_text =="" then
97
+ # spin
98
+ elsif inputted_text==" "
99
+ set_back_led_output(0)
100
+ set_heading(heading)
101
+ set_colour(:white)
102
+ break
103
+ else
104
+ sleep 1
105
+ end # if
106
+ end # upto
107
+
108
+ end # def
109
+
110
+
111
+ def set_back_led_output(brightness)
112
+ logd()
113
+ logd "Building request: set back led output b"
114
+
115
+ request=SpheroRequest.new()
116
+
117
+ request.sop1=0xFF
118
+ request.sop2=0xFF
119
+
120
+ request.did=0x02
121
+ request.cid=0x21
122
+ request.seq=0x53
123
+ request.dlen=0x02
124
+ request.push_data brightness
125
+
126
+ send_data(request.build_packet)
127
+ read_data
128
+
129
+ end # def
130
+
131
+ def set_heading(heading_raw)
132
+ logd
133
+ logd( "Building request: Set Heading")
134
+
135
+ request=SpheroRequest.new()
136
+ heading = heading_raw%359
137
+ logd( "Heading: #{heading}")
138
+
139
+ request.sop1=0xFF
140
+ request.sop2=0xFF
141
+
142
+ request.did=0x02
143
+ request.cid=0x01
144
+ request.seq=0x55
145
+ request.dlen=0x03
146
+
147
+ request.push_data(heading , :a16bit)
148
+
149
+ send_data(request.build_packet)
150
+ read_data
151
+
152
+
153
+ end # def
154
+
155
+ def set_colour(chosen_colour)
156
+ logd
157
+ logd("Locating RGB values for: #{chosen_colour}")
158
+ set_colour_rgb(COLOURS[chosen_colour][0],COLOURS[chosen_colour][1],COLOURS[chosen_colour][2])
159
+ end # def
160
+
161
+
162
+ def set_colour_rgb(red_value,green_value,blue_value)
163
+ logd
164
+ logd "Building request: colour"
165
+
166
+ request=SpheroRequest.new()
167
+
168
+ request.sop1=0xFF
169
+ request.sop2=0xFF
170
+
171
+ request.did=0x02
172
+ request.cid=0x20 # Set RGB Output
173
+ request.seq=0x53
174
+ request.dlen=0x05
175
+ request.push_data red_value
176
+ request.push_data green_value
177
+ request.push_data blue_value
178
+ flag=0x01
179
+ request.push_data flag
180
+
181
+ send_data(request.build_packet)
182
+ read_data
183
+ end # def
184
+
185
+ def roll(heading_raw=0, speed=0xFF)
186
+ logd()
187
+ logd( "Building request: roll")
188
+
189
+ request=SpheroRequest.new()
190
+ heading = heading_raw%359
191
+ logd( "Heading: #{heading}")
192
+
193
+ request.sop1=0xFF
194
+ request.sop2=0xFF
195
+
196
+ request.did=0x02
197
+ request.cid=0x30 # Roll
198
+ request.seq=0x54
199
+ request.dlen=0x05
200
+
201
+ state=0x01
202
+
203
+ request.push_data speed
204
+ request.push_data(heading , :a16bit)
205
+ request.push_data state
206
+
207
+ send_data(request.build_packet)
208
+ read_data
209
+
210
+ end #def
211
+
212
+ def initialize(bluetooth_address)
213
+ return open(bluetooth_address)
214
+ end # class
215
+
216
+ def open(bluetooth_address)
217
+ begin
218
+ @connection = Serial.new bluetooth_address, 115200 ,8
219
+ rescue RubySerial::Exception
220
+ open bluetooth_address
221
+ end
222
+ return @connection
223
+ end # def
224
+
225
+ def close
226
+ @connection.close
227
+ end # def
228
+
229
+ end # class
230
+
231
+ class SpheroResponse
232
+
233
+ include SpheroUtilities
234
+
235
+ attr_accessor :calculated_checksum
236
+ attr_accessor :raw_checksum
237
+
238
+ attr_accessor :raw_data
239
+ attr_accessor :data
240
+ attr_accessor :valid
241
+
242
+
243
+ def initialize(raw_data)
244
+ @valid=false
245
+ @data=process_data(raw_data)
246
+ end # def
247
+
248
+ def process_data(bytes)
249
+ if bytes.length == 0
250
+ logd "Response: None received"
251
+ elsif bytes==nil
252
+ logd "Response: Was nil"
253
+ else
254
+ logd "Response data raw: #{print_format_bytes(bytes)}"
255
+ bytes.shift
256
+ bytes.shift
257
+ @raw_checksum=bytes.pop
258
+
259
+ @calculated_checksum=do_checksum( bytes )
260
+ logd "Response checksum: #{@calculated_checksum.to_s(16)}"
261
+ if @raw_checksum == @calculated_checksum then
262
+ logd("Response Checksum is good")
263
+ @valid=true
264
+ @data=bytes
265
+ end # if
266
+
267
+ end # else
268
+
269
+ end # def
270
+ end # class
271
+
272
+ class SpheroRequest
273
+ include SpheroUtilities
274
+
275
+ attr_accessor :sop1
276
+ attr_accessor :sop2
277
+ attr_accessor :did
278
+ attr_accessor :cid
279
+ attr_accessor :dlen
280
+ attr_accessor :seq
281
+ attr_accessor :checksum
282
+ #attr_accessor :payload_data
283
+
284
+
285
+ def initialize
286
+ @packet_data=Array.new
287
+ @payload_data=Array.new
288
+ end # def
289
+
290
+ def build_packet
291
+ packet_no_checksum=[@sop1, @sop2, @did, @cid, @seq, @dlen]
292
+
293
+ packet_no_checksum.concat @payload_data
294
+ packet_with_checksum=add_checksum(packet_no_checksum)
295
+ packet_packed=packet_with_checksum.pack("C*")
296
+ logd(print_format_bytes(packet_with_checksum))
297
+ @packet_data=packet_packed
298
+ return @packet_data
299
+ end # def
300
+
301
+ def push_data(data_input, length=:a8bit)
302
+ # 8bit and 16bit numbers
303
+ if data_input > 0xFF then
304
+ logd("Greater than 255, splitting into MSB and LSB)}")
305
+ logd("Masked: #{(data_input & 0b0000000100000000 ).to_s(2)}")
306
+ data_input_msb = (data_input & 0b0000000100000000) >> 8
307
+ data_input_lsb = data_input & 0b0000000011111111
308
+
309
+ logd("data_input MSB #{data_input_msb.to_s(2)}")
310
+ logd("data_input LSB #{data_input_lsb.to_s(2)}")
311
+ @payload_data.push data_input_msb
312
+ @payload_data.push data_input_lsb
313
+
314
+ else
315
+ if length==:a16bit
316
+ @payload_data.push 0x00
317
+ end #if
318
+ @payload_data.push data_input
319
+ end # else
320
+ end # def
321
+
322
+ end # class
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysphero
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pete Houghton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubyserial
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.4
33
+ description: A simple Sphero client library to control your Orbotic Sphero 2.0
34
+ email: pete@investigatingsoftware.co.uk
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/rubysphero.rb
40
+ homepage: https://github.com/phoughton/rubysphero
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.4.5.1
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: A simple Sphero client library.
64
+ test_files: []