Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0a5865
commit e761e5a
Showing
5 changed files
with
174 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters