Permalink
Browse files

adding sleep command

  • Loading branch information...
tenderlove committed Jul 24, 2012
1 parent 6676d5d commit b797f0232766423cab9e44d2f0a251e6beca49db
Showing with 49 additions and 19 deletions.
  1. +6 −4 lib/sphero.rb
  2. +36 −13 lib/sphero/request.rb
  3. +7 −2 test/test_sphero.rb
View
@@ -39,6 +39,11 @@ def power_state
write_packet Request::GetPowerState.new(@seq)
end
+ def sleep wakeup = 0, macro = 0
+ obj = Request::Sleep.new(@seq, wakeup, macro)
+ write_packet obj
+ end
+
def roll speed, heading, delay = 0x01
cmd = [speed, heading >> 8, heading & 0xFF, delay]
write 0x30, cmd, 0x02
@@ -104,8 +109,5 @@ def write cmd, data = [], did = @dev
p s.ping
}
- obj = s.bluetooth_info
- ps = s.power_state
- p ps
- p ps.body
+ p s.sleep(10)
end
View
@@ -7,31 +7,41 @@ def initialize seq, data = []
@seq = seq
@data = data
@did = 0x00
- @dlen = @data.length + 1
- @format = 'C*'
end
- def data_bytes
- [SOP1, SOP2, @did, @cid, @seq, @dlen] + @data
- end
-
- def checksum
- ~(data_bytes.drop(2).reduce(:+) % 256) & 0xFF
- end
-
- def bytes
- data_bytes << checksum
+ def header
+ [SOP1, SOP2, @did, @cid, @seq, dlen]
end
# The data to write to the socket
def to_str
- bytes.pack @format
+ bytes
end
def response header, body
Response.new header, body
end
+ def packet_header
+ header.pack 'CCCCCC'
+ end
+
+ def packet_body
+ @data.pack 'C*'
+ end
+
+ def checksum
+ ~((packet_header + packet_body).unpack('C*').drop(2).reduce(:+) % 256) & 0xFF
+ end
+
+ def bytes
+ packet_header + packet_body + checksum.chr
+ end
+
+ def dlen
+ packet_body.bytesize + 1
+ end
+
class Ping < Request
def initialize seq
super(seq, [])
@@ -85,5 +95,18 @@ def response header, body
Response::GetPowerState.new header, body
end
end
+
+ class Sleep < Request
+ def initialize seq, wakeup, macro
+ super(seq, [wakeup, macro])
+ @cid = 0x22
+ end
+
+ private
+
+ def packet_body
+ @data.pack 'nC'
+ end
+ end
end
end
View
@@ -3,7 +3,12 @@
class TestSphero < MiniTest::Unit::TestCase
def test_ping_checksum
- ping = Sphero::Request::Ping.new 1
- assert_equal 1, ping.checksum
+ ping = Sphero::Request::Ping.new 0
+ assert_equal "\xFF\xFF\x00\x01\x00\x01\xFD", ping.to_str
+ end
+
+ def test_sleep_dlen
+ sleep = Sphero::Request::Sleep.new 0, 0, 0
+ assert_equal 0x04, sleep.dlen
end
end

0 comments on commit b797f02

Please sign in to comment.