Class: IRobotCreate::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/irobotcreate/command.rb

Direct Known Subclasses

SensorCommand

Constant Summary collapse

@@commands =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, argument_options = []) ⇒ Command

Returns a new instance of Command.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/irobotcreate/command.rb', line 21

def initialize(code, argument_options = [])
  @code = code

  if argument_options != :inf
    case argument_options
    when Integer
      argument_options = [{ :type => :integer }] * argument_options
    when Symbol
      argument_options = [{ :type => argument_options }]
    when Array
      argument_options.map!{|ao| ao.is_a?(Hash) ? ao : { :type => ao } }
    when Hash
      argument_options = [argument_options]
    else
      raise "Can't handle argument options: #{argument_options}"
    end
  end

  @argument_options = argument_options
end

Instance Attribute Details

#argument_optionsObject (readonly)

Returns the value of attribute argument_options.



5
6
7
# File 'lib/irobotcreate/command.rb', line 5

def argument_options
  @argument_options
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/irobotcreate/command.rb', line 5

def code
  @code
end

Class Method Details

.[](name) ⇒ Object



15
16
17
# File 'lib/irobotcreate/command.rb', line 15

def [](name)
  @@commands[name.to_sym]
end

.register(name, *args) ⇒ Object



11
12
13
# File 'lib/irobotcreate/command.rb', line 11

def register(name, *args)
  @@commands[name] = new(*args)
end

Instance Method Details

#run(robot, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/irobotcreate/command.rb', line 42

def run(robot, *args)
  if argument_options == :inf
    array = args
  else
    if IRobotCreate::DEBUG
      puts "args: #{args.inspect}"
      puts "argument_options: #{argument_options.inspect}"
    end
    if args.size != argument_options.size
      raise ArgumentError, "Requires #{argument_options.size} arguments"
    end

    array = []
    args.each_with_index do |arg, i|
      opts = argument_options[i]

      value = arg
      puts "starting value: #{value.inspect}" if IRobotCreate::DEBUG

      size = opts[:size] || 1

      if opts[:inverse_map]
        opts[:map] = opts[:inverse_map]
        for k,v in opts[:inverse_map]
          opts[:map]["not_#{k}".to_sym] = 256**size - v
        end
        puts "inverse_map: #{opts[:map.inspect]}" if IRobotCreate::DEBUG
      end

      if opts[:map]
        value = opts[:map][value]
        puts "mapped: #{value}" if IRobotCreate::DEBUG
        raise "Couldn't map value" if value.nil?
      end

      if opts[:compound]
        raise "Compound size must be 1!" unless size == 1

        value_array = value.is_a?(Array) ? value : [value]
        value = 0

        compound_size = opts[:compound].size
        compound_size.times do |j|
          compound_value = opts[:compound][j]
          puts "checking for #{compound_value}" if IRobotCreate::DEBUG
          if compound_value && value_array.include?(compound_value)
            bit = (compound_size - 1) - j
            puts "Adding bit: #{bit}" if IRobotCreate::DEBUG
            value += 2 ** bit
          end
        end
      elsif opts[:type] == :boolean
        value = value ? 1 : 0
      end

      puts "preprocessed value: #{value.inspect}" if IRobotCreate::DEBUG

      if value < 0
        value += 256 ** size
        puts "converted negative: #{value.inspect}" if IRobotCreate::DEBUG
      end

      size.times do |j|
        base = 256 ** ((size - 1) - j)
        current = value / base
        value %= base
        array << current
      end

      puts "array: #{array.inspect}" if IRobotCreate::DEBUG
    end
  end

  puts "final array: #{array.inspect}" if IRobotCreate::DEBUG

  robot.send([code] + array)
end