Class: NXTBrick

Inherits:
Object
  • Object
show all
Includes:
NXT::Exceptions, NXT::Utils::Assertions
Defined in:
lib/nxt/nxt_brick.rb

Overview

This class is the entry point for end-users creating their own list of commands to execute remotely on a Lego NXT brick.

An instance of this class provides all the endpoints necessary to:

  • educate the API on the connected input and output devices; and,

  • access these input and output devices and run commands using them.

Examples:

Creating an instance using a block, with one motor output.


NXTBrick.new(interface) do |nxt|
  nxt.add_motor_output(:a, :front_left)
end

Creating an instance without a block, with one motor and one light sensor.


nxt = NXTBrick.new(interface)
nxt.add_motor_output(:a, :front_left)
# ...
nxt.disconnect

Constant Summary collapse

PORTS =

An enumeration of possible ports, both input and output, that the NXT brick can have connectors attached to.

%i[a b c one two three four].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NXT::Utils::Assertions

#assert_in, #assert_responds_to, #assert_type

Constructor Details

#initialize(interface_type, *interface_args) ⇒ NXTBrick

Returns a new instance of NXTBrick.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nxt/nxt_brick.rb', line 48

def initialize(interface_type, *interface_args)
  @port_identifiers = {}
  interface_type = interface_type.to_s.classify

  unless NXT::Interface.constants.include?(interface_type.to_sym)
    raise(InvalidInterfaceError, "There is no interface of type #{interface_type}.")
  end

  @interface = NXT::Interface.const_get(interface_type).new(*interface_args)

  return unless block_given?

  begin
    connect
    yield(self)
  ensure
    disconnect
  end
end

Instance Attribute Details

#aObject (readonly)

Accessors for output ports on the NXT brick. These will be populated with the appropriate instances of their respective output connectors.



37
38
39
# File 'lib/nxt/nxt_brick.rb', line 37

def a
  @a
end

#bObject (readonly)

Accessors for output ports on the NXT brick. These will be populated with the appropriate instances of their respective output connectors.



37
38
39
# File 'lib/nxt/nxt_brick.rb', line 37

def b
  @b
end

#cObject (readonly)

Accessors for output ports on the NXT brick. These will be populated with the appropriate instances of their respective output connectors.



37
38
39
# File 'lib/nxt/nxt_brick.rb', line 37

def c
  @c
end

#fourObject (readonly)

Accessors for input ports on the NXT brick. These will be populated with the appropriate instances of their respective input connectors.



41
42
43
# File 'lib/nxt/nxt_brick.rb', line 41

def four
  @four
end

#interfaceObject

Get the instance of the interface that this runner class is using to connect to the NXT brick.



33
34
35
# File 'lib/nxt/nxt_brick.rb', line 33

def interface
  @interface
end

#oneObject (readonly)

Accessors for input ports on the NXT brick. These will be populated with the appropriate instances of their respective input connectors.



41
42
43
# File 'lib/nxt/nxt_brick.rb', line 41

def one
  @one
end

#port_identifiersObject (readonly)

We mandate that all added port connections have an identifier associated with it. This is so that code is not fragile when port swapping needs to be done.



46
47
48
# File 'lib/nxt/nxt_brick.rb', line 46

def port_identifiers
  @port_identifiers
end

#threeObject (readonly)

Accessors for input ports on the NXT brick. These will be populated with the appropriate instances of their respective input connectors.



41
42
43
# File 'lib/nxt/nxt_brick.rb', line 41

def three
  @three
end

#twoObject (readonly)

Accessors for input ports on the NXT brick. These will be populated with the appropriate instances of their respective input connectors.



41
42
43
# File 'lib/nxt/nxt_brick.rb', line 41

def two
  @two
end

Instance Method Details

#add(port, identifier, klass) ⇒ Object

Add a new connector instance, binding a specific identifier to the given port.

If the given port already is bound, an exception will be thrown. The instance given though can be of any class, presuming it talks the correct language.

Parameters:

  • Symbol

    port The port to bind to.

  • Symbol

    identifier The identifier to associate with this port.

  • Class

    klass The Class to instantiate as the instance of this port. There is no limitation on what type this can be, though it must be able to hook in correctly with the NXT library.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/nxt/nxt_brick.rb', line 93

def add(port, identifier, klass)
  assert_in('port', port, PORTS)
  assert_responds_to('identifier', identifier, :to_sym)
  assert_type('klass', klass, Class)

  if respond_to?(identifier)
    if instance_variable_get(:"@#{port}").nil?
      raise(
        InvalidIdentifierError,
        "Cannot use identifier #{identifier}, a method on #{self.class} is already using it."
      )
    end

    raise(PortTakenError, "Port #{port} is already set, call remove first")
  end

  define_port_handler_method(port, identifier, klass)
end

#connectObject

Connect using the given interface to the NXT brick.



69
70
71
# File 'lib/nxt/nxt_brick.rb', line 69

def connect
  @interface.connect
end

#disconnectObject

Close the connection to the NXT brick, and dispose of any resources that this instance of NXTBrick is using. Any commands run against this runner after calling disconnect will fail.



76
77
78
# File 'lib/nxt/nxt_brick.rb', line 76

def disconnect
  @interface.disconnect
end

#remove(identifier) ⇒ Object

Remove the assigned (if any) connector instance from the given identifier.interface

Parameters:

  • Symbol

    identifier The identifier to search for and remove.



116
117
118
119
# File 'lib/nxt/nxt_brick.rb', line 116

def remove(identifier)
  assert_responds_to('identifier', identifier, :to_sym)
  !@port_identifiers.delete(identifier.to_sym).nil?
end