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
|