Class: Sphero::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/sphero/request.rb

Direct Known Subclasses

SetPowerNotification, Sleep, Sphero

Defined Under Namespace

Classes: ConfigureCollisionDetection, Heading, Roll, SetDataStreaming, SetPowerNotification, Sleep, Sphero, Stabilization

Constant Summary collapse

SOP1 =
0xFF
SOP2 =
0xFF
SetBackLEDOutput =
make_command Sphero, 0x21
SetRotationRate =
make_command Sphero, 0x03
SetRGB =
make_command Sphero, 0x20
GetRGB =
make_command Sphero, 0x22
Ping =
make_command Request, 0x01
GetVersioning =
make_command Request, 0x02
GetBluetoothInfo =
make_command Request, 0x11
SetAutoReconnect =
make_command Request, 0x12
GetAutoReconnect =
make_command Request, 0x13
GetPowerState =
make_command Request, 0x20
GYRO_AXIS_H_FILTERED =
0x0000_0001
GYRO_AXIS_M_FILTERED =
0x0000_0002
GYRO_AXIS_L_FILTERED =
0x0000_0004
LEFT_MOTOR_BACK_EMF_FILTERED =
0x0000_0020
RIGHT_MOTOR_BACK_EMF_FILTERED =
0x0000_0040
MAGNETOMETER_AXIS_Z_FILTERED =
0x0000_0080
MAGNETOMETER_AXIS_Y_FILTERED =
0x0000_0100
MAGNETOMETER_AXIS_X_FILTERED =
0x0000_0200
GYRO_AXIS_Z_FILTERED =
0x0000_0400
GYRO_AXIS_Y_FILTERED =
0x0000_0800
GYRO_AXIS_X_FILTERED =
0x0000_1000
ACCELEROMETER_AXIS_Z_FILTERED =
0x0000_2000
ACCELEROMETER_AXIS_Y_FILTERED =
0x0000_4000
ACCELEROMETER_AXIS_X_FILTERED =
0x0000_8000
IMU_YAW_ANGLE_FILTERED =
0x0001_0000
IMU_ROLL_ANGLE_FILTERED =
0x0002_0000
IMU_PITCH_ANGLE_FILTERED =
0x0004_0000
LEFT_MOTOR_BACK_EMF_RAW =
0x0020_0000
RIGHT_MOTOR_BACK_EMF_RAW =
0x0040_0000
MAGNETOMETER_AXIS_Z_RAW =
0x0080_0000
MAGNETOMETER_AXIS_Y_RAW =
0x0100_0000
MAGNETOMETER_AXIS_X_RAW =
0x0200_0000
GYRO_AXIS_Z_RAW =
0x0400_0000
GYRO_AXIS_Y_RAW =
0x0800_0000
GYRO_AXIS_X_RAW =
0x1000_0000
ACCELEROMETER_AXIS_Z_RAW =
0x2000_0000
ACCELEROMETER_AXIS_Y_RAW =
0x4000_0000
ACCELEROMETER_AXIS_X_RAW =
0x8000_0000
QUATERNION_Q0 =
0x0000_0001
QUATERNION_Q1 =
0x0000_0002
QUATERNION_Q2 =
0x0000_0004
QUATERNION_Q3 =
0x0000_0008
ODOMETER_X =
0x0000_0010
ODOMETER_Y =
0x0000_0020
ACCELONE =
0x0000_0040
VELOCITY_X =
0x0000_0080
VELOCITY_Y =
0x0000_0100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seq, data = []) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
# File 'lib/sphero/request.rb', line 8

def initialize seq, data = []
  @seq    = seq
  @data   = data
  @did    = 0x00
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/sphero/request.rb', line 6

def data
  @data
end

#seqObject (readonly)

Returns the value of attribute seq.



6
7
8
# File 'lib/sphero/request.rb', line 6

def seq
  @seq
end

Class Method Details

.make_command(klass, cid, &block) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/sphero/request.rb', line 59

def self.make_command klass, cid, &block
  Class.new(klass) {
    define_method(:initialize) do |seq, *args|
      super(seq, args)
      @cid = cid
    end
  }
end

Instance Method Details

#bytesObject



44
45
46
# File 'lib/sphero/request.rb', line 44

def bytes
  packet_header + packet_body + checksum.chr
end

#checksumObject



40
41
42
# File 'lib/sphero/request.rb', line 40

def checksum
  ~((packet_header + packet_body).unpack('C*').drop(2).reduce(:+) % 256) & 0xFF
end

#dlenObject



48
49
50
# File 'lib/sphero/request.rb', line 48

def dlen
  packet_body.bytesize + 1
end

#headerObject



14
15
16
# File 'lib/sphero/request.rb', line 14

def header
  [SOP1, SOP2, @did, @cid, @seq, dlen]
end

#packet_bodyObject



36
37
38
# File 'lib/sphero/request.rb', line 36

def packet_body
  @data.pack 'C*'
end

#packet_headerObject



32
33
34
# File 'lib/sphero/request.rb', line 32

def packet_header
  header.pack 'CCCCCC'
end

#response(header, body) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/sphero/request.rb', line 23

def response header, body
  name = self.class.name.split('::').last
  if Response.const_defined?(name)
    Response.const_get(name).new header, body
  else
    Response.new header, body
  end
end

#to_strObject

The data to write to the socket



19
20
21
# File 'lib/sphero/request.rb', line 19

def to_str
  bytes
end