Module: NabaztagHackKit::Message

Extended by:
Message
Included in:
Message
Defined in:
lib/nabaztag_hack_kit/message.rb,
lib/nabaztag_hack_kit/message/api.rb,
lib/nabaztag_hack_kit/message/helper.rb

Defined Under Namespace

Modules: Api, Helper

Instance Method Summary collapse

Instance Method Details

#build(*commands) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nabaztag_hack_kit/message.rb', line 7

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 do |cmd, *data|
    data = convert_data(data)
    [cmd.to_i] + to_3b(data.size) + data.map(&:to_i)
  end)
end

#convert_data(data) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/nabaztag_hack_kit/message.rb', line 26

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

#to_3b(int) ⇒ Object



22
23
24
# File 'lib/nabaztag_hack_kit/message.rb', line 22

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