Class: NXTBrick
- Inherits:
-
Object
- Object
- NXTBrick
- 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.
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
-
#a ⇒ Object
readonly
Accessors for output ports on the NXT brick.
-
#b ⇒ Object
readonly
Accessors for output ports on the NXT brick.
-
#c ⇒ Object
readonly
Accessors for output ports on the NXT brick.
-
#four ⇒ Object
readonly
Accessors for input ports on the NXT brick.
-
#interface ⇒ Object
Get the instance of the interface that this runner class is using to connect to the NXT brick.
-
#one ⇒ Object
readonly
Accessors for input ports on the NXT brick.
-
#port_identifiers ⇒ Object
readonly
We mandate that all added port connections have an identifier associated with it.
-
#three ⇒ Object
readonly
Accessors for input ports on the NXT brick.
-
#two ⇒ Object
readonly
Accessors for input ports on the NXT brick.
Instance Method Summary collapse
-
#add(port, identifier, klass) ⇒ Object
Add a new connector instance, binding a specific identifier to the given port.
-
#connect ⇒ Object
Connect using the given interface to the NXT brick.
-
#disconnect ⇒ Object
Close the connection to the NXT brick, and dispose of any resources that this instance of NXTBrick is using.
-
#initialize(interface_type, *interface_args) ⇒ NXTBrick
constructor
A new instance of NXTBrick.
-
#remove(identifier) ⇒ Object
Remove the assigned (if any) connector instance from the given identifier.interface.
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
#a ⇒ Object (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 |
#b ⇒ Object (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 |
#c ⇒ Object (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 |
#four ⇒ Object (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 |
#interface ⇒ Object
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 |
#one ⇒ Object (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_identifiers ⇒ Object (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 |
#three ⇒ Object (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 |
#two ⇒ Object (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.
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 |
#connect ⇒ Object
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 |
#disconnect ⇒ Object
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
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 |