Sha256: 8c4e1faae2e16578416e7d54111e0956697a0daee58b0a6d9fad686a04c84051

Contents?: true

Size: 780 Bytes

Versions: 7

Compression:

Stored size: 780 Bytes

Contents

module NabaztagHackKit
  module Message
    extend self

    def build(*commands)
      commands = if commands.first.is_a?(Hash)
        commands.first
      elsif !commands.first.is_a?(Array)
        [commands]
      else
        commands
      end

      pack full_message commands.map { |cmd, *data|
        data = convert_data(data)
        [cmd] + to_3b(data.size) + data
      }
    end

    def to_3b(int)
      [int >> 16, int >> 8, int].map { |i| i & 0xFF }
    end

    def convert_data(data)
      if data.first.is_a?(String)
        data.first.each_byte.to_a
      else
        data.flatten
      end
    end

    private
    def full_message(*data)
      [0x7F] + data.flatten + [0xFF, 0x0A]
    end

    def pack(message)
      message.pack('c*')
    end
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
nabaztag_hack_kit-0.1.0.alpha7 lib/nabaztag_hack_kit/message.rb
nabaztag_hack_kit-0.1.0.alpha6 lib/nabaztag_hack_kit/message.rb
nabaztag_hack_kit-0.1.0.alpha5 lib/nabaztag_hack_kit/message.rb
nabaztag_hack_kit-0.1.0.alpha4 lib/nabaztag_hack_kit/message.rb
nabaztag_hack_kit-0.1.0.alpha3 lib/nabaztag_hack_kit/message.rb
nabaztag_hack_kit-0.1.0.alpha2 lib/nabaztag_hack_kit/message.rb
nabaztag_hack_kit-0.1.0.alpha1 lib/nabaztag_hack_kit/message.rb