Skip to content

Commit

Permalink
adding request objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jul 24, 2012
1 parent b0a5865 commit e761e5a
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 66 deletions.
13 changes: 3 additions & 10 deletions README.markdown
Expand Up @@ -15,26 +15,19 @@ provided by the bluetooth connection.


```ruby ```ruby
s = Sphero.new "/dev/tty.Sphero-PRG-RN-SPP" s = Sphero.new "/dev/tty.Sphero-PRG-RN-SPP"
p s.ping s.ping


# Roll 0 degrees, speed 125 # Roll 0 degrees, speed 125
p s.roll(125, 0) s.roll(125, 0)


trap(:INT) { # Turn 360 degrees, 30 degrees at a time
s.stop # Stop the ball
exit!
}

sleep 1
loop do
0.step(360, 30) { |h| 0.step(360, 30) { |h|
h = 0 if h == 360 h = 0 if h == 360


# Set the heading to h degrees # Set the heading to h degrees
s.heading = h s.heading = h
sleep 1 sleep 1
} }
end
sleep 1 sleep 1
s.stop s.stop
``` ```
Expand Down
89 changes: 35 additions & 54 deletions lib/sphero.rb
@@ -1,66 +1,38 @@
require 'serialport' require 'serialport'
require 'sphero/request'
require 'sphero/response'


class Sphero class Sphero
VERSION = '1.0.0' VERSION = '1.0.0'


class Response
SOP1 = 0
SOP2 = 1
MRSP = 2
SEQ = 3
DLEN = 4

CODE_OK = 0

attr_reader :body

def initialize header, body
@header = header
@body = body
end

def empty?
@header[DLEN] == 1
end

def success?
@header[MRSP] == CODE_OK
end

def seq
@header[SEQ]
end
end

def initialize dev def initialize dev
@sp = SerialPort.new dev, 115200, 8, 1, SerialPort::NONE @sp = SerialPort.new dev, 115200, 8, 1, SerialPort::NONE
@dev = 0x00 @dev = 0x00
@seq = 0x00 @seq = 0x00
end end


def ping def ping
write 0x01 write_packet Request::Ping.new(@seq)
end end


def version def version
write 0x02 write_packet Request::GetVersioning.new(@seq)
end end


def bluetooth_info def bluetooth_info
resp = write 0x11 write_packet Request::GetBluetoothInfo.new(@seq)
[resp.body.take(15).pack('C*'), resp.body.drop(15).pack('C*')]
end end


def auto_reconnect= time_s def auto_reconnect= time_s
write 0x12, [0x01, time_s] write_packet Request::SetAutoReconnect.new(@seq, time_s)
end end


def auto_reconnect def auto_reconnect
write(0x13).body[1] write_packet(Request::GetAutoReconnect.new(@seq)).time
end end


def disable_auto_reconnect def disable_auto_reconnect
write 0x12, [0x00, 0x05] write_packet Request::SetAutoReconnect.new(@seq, 0, false)
end end


def roll speed, heading, delay = 0x01 def roll speed, heading, delay = 0x01
Expand All @@ -78,6 +50,21 @@ def heading= h


private private


def write_packet packet
@sp.write packet.to_str
@seq += 1

header = @sp.read(5).unpack 'C5'
body = @sp.read header.last
response = packet.response header, body

if response.success?
response
else
raise response
end
end

def write cmd, data = [], did = @dev def write cmd, data = [], did = @dev
data_len = data.length + 1 data_len = data.length + 1


Expand All @@ -103,29 +90,23 @@ def write cmd, data = [], did = @dev


if $0 == __FILE__ if $0 == __FILE__
begin begin
s = Sphero.new "/dev/tty.Sphero-PRG-RN-SPP" s = Sphero.new "/dev/tty.Sphero-BRR-RN-SPP"
rescue Errno::EBUSY rescue Errno::EBUSY
p :wtf p :wtf
retry retry
end end


p s.ping 10.times {
p s.roll(125, 0) p s.ping

trap(:INT) {
s.stop
exit!
} }


sleep 1 obj = s.bluetooth_info
loop do p obj
0.step(360, 30) { |h| p obj.name
h = 0 if h == 360 p obj.bta

p s.auto_reconnect
s.heading = h s.auto_reconnect = 3
sleep 1 p s.auto_reconnect
} s.auto_reconnect = 0
end p s.auto_reconnect
sleep 1
s.stop
end end
85 changes: 85 additions & 0 deletions lib/sphero/request.rb
@@ -0,0 +1,85 @@
class Sphero
class Request
SOP1 = 0xFF
SOP2 = 0xFF

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
end

# The data to write to the socket
def to_str
bytes.pack @format
end

def response header, body
Response.new header, body
end

class Ping < Request
def initialize seq
super(seq, [])
@cid = 0x01
end
end

class GetVersioning < Request
def initialize seq
super(seq, [])
@cid = 0x02
end
end

class GetBluetoothInfo < Request
def initialize seq
super(seq, [])
@cid = 0x11
end

def response header, body
Response::GetBluetoothInfo.new header, body
end
end

class SetAutoReconnect < Request
def initialize seq, time = 7, enabled = true
super(seq, [enabled ? 0x01 : 0x00, time])
@cid = 0x12
end
end

class GetAutoReconnect < Request
def initialize seq
super(seq, [])
@cid = 0x13
end

def response header, body
Response::GetAutoReconnect.new header, body
end
end

class GetPowerState < Request
def initialize seq
super(seq, [])
@cid = 0x20
end
end
end
end
48 changes: 48 additions & 0 deletions lib/sphero/response.rb
@@ -0,0 +1,48 @@
class Sphero
class Response
SOP1 = 0
SOP2 = 1
MRSP = 2
SEQ = 3
DLEN = 4

CODE_OK = 0

def initialize header, body
@header = header
@body = body
end

def empty?
@header[DLEN] == 1
end

def success?
@header[MRSP] == CODE_OK
end

def seq
@header[SEQ]
end

def body
@body.unpack 'C*'
end

class GetAutoReconnect < Response
def time
body[1]
end
end

class GetBluetoothInfo < Response
def name
body.take(16).slice_before(0x00).first.pack 'C*'
end

def bta
body.drop(16).slice_before(0x00).first.pack 'C*'
end
end
end
end
5 changes: 3 additions & 2 deletions test/test_sphero.rb
Expand Up @@ -2,7 +2,8 @@
require 'sphero' require 'sphero'


class TestSphero < MiniTest::Unit::TestCase class TestSphero < MiniTest::Unit::TestCase
def test_sanity def test_ping_checksum
flunk "write tests or I will kneecap you" ping = Sphero::Request::Ping.new 1
assert_equal 1, ping.checksum
end end
end end

0 comments on commit e761e5a

Please sign in to comment.