Skip to content

Commit

Permalink
Added Brick.reset_motor_position
Browse files Browse the repository at this point in the history
  • Loading branch information
docwhat committed Apr 22, 2012
1 parent a570794 commit ba00036
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
37 changes: 36 additions & 1 deletion lib/lego_nxt/brick.rb
Expand Up @@ -19,6 +19,7 @@ def initialize connection
#
# @param [Integer] frequency Range: 200-1400Hz
# @param [Integer] duration in milliseconds (1/1000 of a second)
# @return [nil]
def play_tone frequency, duration
raise ArgumentError.new("Frequency must be between 200-1400Hz; got #{frequency}") if frequency < 200 || frequency > 1400
transmit(
Expand All @@ -39,12 +40,27 @@ def battery_level
).unpack('S<')[0]
end

# Resets the tracking for the motor position.
#
# @param [Symbol] port The port the motor is attached to. Should be `:a`, `:b`, or `:c`
# @param [Boolean] set_relative Sets the position tracking to relative if true, otherwise absolute if false.
# @return [nil]
def reset_motor_position port, set_relative
transmit(
DirectOps::NO_RESPONSE,
DirectOps::RESETMOTORPOSITION,
normalize_motor_port(port),
normalize_boolean(set_relative)
)
end

# A wrapper around the transmit function for the connection.
#
# @param [LegoNXT::Type] bits A list of bytes.
# @return [nil]
def transmit *bits
connection.transmit(bits.map(&:byte_string).join(""))
bitstring = bits.map(&:byte_string).join("")
connection.transmit bitstring
end

# A wrapper around the transceive function for the connection.
Expand All @@ -64,5 +80,24 @@ def transceive *bits
return retval[3..-1]
end

# Converts a port symbol into the appropriate byte().
#
# @param [Symbol] port It should be `:a`, `:b`, or `:c` (which correspond to the markings on the brick)
# @return [UnsignedByte] The corresponding byte for the port.
def normalize_motor_port port
@portmap ||= { a: byte(0),
b: byte(1),
c: byte(2) }
raise ArgumentError.new("Motor ports must be #{@portmap.keys.inspect}: got #{port.inspect}") unless @portmap.include? port
@portmap[port]
end

# Converts a boolean value into `byte(0)` or `byte(1)`
#
# @param [Object] bool The value to convert.
# @return [UnsignedByte] Returns 0 or 1
def normalize_boolean bool
bool ? byte(1) : byte(0)
end
end
end
44 changes: 44 additions & 0 deletions spec/lib/lego_nxt/brick_spec.rb
Expand Up @@ -51,6 +51,32 @@
end
end

describe ".normalize_motor_port" do
[:a, :b, :c].each do |port|
context "with port #{port.inspect}" do
it "accepts port #{port.inspect}" do
subject.normalize_motor_port port
end
it "it returns a Type" do
subject.normalize_motor_port(port).should be_a_kind_of(LegoNXT::Type)
end
end
end

it "raises an error if it isn't a port symbol" do
expect { subject.normalize_motor_port(:d) }.to raise_error(ArgumentError)
end
end

describe ".normalize_boolean" do
it "returns byte(1) for true" do
subject.normalize_boolean(true).should == byte(1)
end
it "returns byte(0) for false" do
subject.normalize_boolean(false).should == byte(0)
end
end

describe ".play_tone" do
it "doesn't accept frequencies under 200" do
expect { subject.play_tone 199, 400 }.to raise_error(ArgumentError)
Expand Down Expand Up @@ -86,4 +112,22 @@
end
end

describe ".reset_motor_position" do
[:a, :b, :c].each do |port|
[true, false].each do |relative|
it "accepts port #{port.inspect}, relative #{relative.inspect}" do
subject.reset_motor_position port, relative
end
it "calls normalize_motor_port" do
subject.should_receive(:normalize_motor_port).with(port) { byte(0) }
subject.reset_motor_position port, relative
end
it "calls normalize_boolean" do
subject.should_receive(:normalize_boolean).with(relative) { byte(0) }
subject.reset_motor_position port, relative
end
end
end
end

end

0 comments on commit ba00036

Please sign in to comment.