Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactoring the factory method on the request

  • Loading branch information...
commit 31a5e27dec3ed94130d40551758c2edb7afa273a 1 parent 19f9a55
@tenderlove authored
Showing with 37 additions and 48 deletions.
  1. +19 −1 lib/sphero.rb
  2. +18 −47 lib/sphero/request.rb
View
20 lib/sphero.rb
@@ -54,6 +54,11 @@ def stop
end
def heading= h
+ p :heading => h
+ heading = Request::Heading.new(@seq, h)
+ p Request::Heading
+ p heading
+ p heading.packet_body
write Request::Heading.new(@seq, h)
end
@@ -114,14 +119,27 @@ def write packet
trap(:INT) {
s.stop
+ exit!
}
- s.roll 100, 0
+ #s.roll 100, 0
+ p s.auto_reconnect
+ exit
loop do
[0, 180].each do |dir|
s.heading = dir
+ sleep 10
end
+
+ #[
+ # [0, 0, 0xFF],
+ # [0xFF, 0, 0],
+ # [0, 0xFF, 0],
+ #].each do |color|
+ # s.rgb(*color)
+ # sleep 5
+ #end
end
#36.times {
View
65 lib/sphero/request.rb
@@ -3,6 +3,8 @@ class Request
SOP1 = 0xFF
SOP2 = 0xFF
+ attr_reader :data
+
def initialize seq, data = []
@seq = seq
@data = data
@@ -19,7 +21,12 @@ def to_str
end
def response header, body
- Response.new header, body
+ name = self.class.name.split('::').last
+ klass = if Response.const_defined?(name)
+ Response.const_get(name).new header, body
+ else
+ Response.new header, body
+ end
end
def packet_header
@@ -49,48 +56,24 @@ def initialize seq, data = []
end
end
- class Heading < Sphero
- def initialize seq, heading
- super(seq, [heading])
- @cid = 0x01
- end
-
- private
- def packet_body
- @data.pack 'n'
- end
- end
-
- class SetBackLEDOutput < Sphero
- def initialize seq, brightness
- super(seq, [brightness])
- @cid = 0x21
- end
+ def self.make_command klass, cid, &block
+ Class.new(klass) {
+ define_method(:initialize) do |seq, *args|
+ super(seq, args)
+ @cid = cid
+ end
+ }
end
- class SetRotationRate < Sphero
- def initialize seq, rate
- super(seq, [rate])
- @cid = 0x03
- end
- end
-
- class SetRGB < Sphero
- def initialize seq, r, g, b, persistant
- super(seq, [r, g, b, persistant])
- @cid = 0x20
- end
- end
+ SetBackLEDOutput = make_command Sphero, 0x21
+ SetRotationRate = make_command Sphero, 0x03
+ SetRGB = make_command Sphero, 0x20
class GetRGB < Sphero
def initialize seq
super(seq, [])
@cid = 0x22
end
-
- def response header, body
- Response::GetRGB.new header, body
- end
end
class Roll < Sphero
@@ -124,10 +107,6 @@ def initialize seq
super(seq, [])
@cid = 0x11
end
-
- def response header, body
- Response::GetBluetoothInfo.new header, body
- end
end
class SetAutoReconnect < Request
@@ -142,10 +121,6 @@ def initialize seq
super(seq, [])
@cid = 0x13
end
-
- def response header, body
- Response::GetAutoReconnect.new header, body
- end
end
class GetPowerState < Request
@@ -153,10 +128,6 @@ def initialize seq
super(seq, [])
@cid = 0x20
end
-
- def response header, body
- Response::GetPowerState.new header, body
- end
end
class Sleep < Request
Please sign in to comment.
Something went wrong with that request. Please try again.