Sha256: c34e5a58279f4d36df25a54f6a9345cf6c2c76ea25cc6989ef716008a45f8d24

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

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 GetPowerState < Response
      # constants for power_state
      CHARGING = 0x01
      OK       = 0x02
      LOW      = 0x03
      CRITICAL = 0x04

      def body
        @body.unpack 'CCnnnC'
      end

      def rec_ver
        body[0]
      end

      def power_state
        body[1]
      end

      # Voltage * 100
      def batt_voltage
        body[2]
      end

      def num_charges
        body[3]
      end

      # Time since awakened in seconds
      def time_since_charge
        body[4]
      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

    class GetRGB < Response
      def r; body[0]; end
      def g; body[1]; end
      def b; body[2]; end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
hybridgroup-sphero-1.1.6 lib/sphero/response.rb
hybridgroup-sphero-1.1.5 lib/sphero/response.rb
hybridgroup-sphero-1.1.4 lib/sphero/response.rb
hybridgroup-sphero-1.1.3 lib/sphero/response.rb
hybridgroup-sphero-1.1.2 lib/sphero/response.rb
hybridgroup-sphero-1.1.1 lib/sphero/response.rb
hybridgroup-sphero-1.1.0 lib/sphero/response.rb
hybridgroup-sphero-1.0.1 lib/sphero/response.rb
sphero-1.0.0 lib/sphero/response.rb