Class: NXTBrick
- Inherits:
-
Object
- Object
- NXTBrick
- Includes:
- NXT::Exceptions
- 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.
[:a, :b, :c, :one, :two, :three, :four]
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.
Constructor Details
#initialize(interface_type, *interface_args) ⇒ NXTBrick
Returns a new instance of NXTBrick.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/nxt/nxt_brick.rb', line 45 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.new("There is no interface of type #{interface_type}.") end self.interface = NXT::Interface.const_get(interface_type).new(*interface_args) if block_given? begin self.connect yield(self) rescue Exception => e binding.pry ensure self.disconnect end 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.
34 35 36 |
# File 'lib/nxt/nxt_brick.rb', line 34 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.
34 35 36 |
# File 'lib/nxt/nxt_brick.rb', line 34 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.
34 35 36 |
# File 'lib/nxt/nxt_brick.rb', line 34 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.
38 39 40 |
# File 'lib/nxt/nxt_brick.rb', line 38 def four @four end |
#interface ⇒ Object
Get the instance of the interface that this runner class is using to connect to the NXT brick.
30 31 32 |
# File 'lib/nxt/nxt_brick.rb', line 30 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.
38 39 40 |
# File 'lib/nxt/nxt_brick.rb', line 38 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.
43 44 45 |
# File 'lib/nxt/nxt_brick.rb', line 43 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.
38 39 40 |
# File 'lib/nxt/nxt_brick.rb', line 38 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.
38 39 40 |
# File 'lib/nxt/nxt_brick.rb', line 38 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.
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 119 120 121 122 123 |
# File 'lib/nxt/nxt_brick.rb', line 92 def add(port, identifier, klass) raise TypeError.new('Expected port to be a Symbol') unless port.is_a?(Symbol) raise TypeError.new('Expected identifier to be a Symbol') unless identifier.is_a?(Symbol) raise TypeError.new('Expected klass to be a Class') unless klass.is_a?(Class) unless PORTS.include?(port) raise TypeError.new("Expected port to be one of: :#{PORTS.join(', :')}") end port_variable = :"@#{port}" if !self.respond_to?(identifier) # Makes a new instance of the class and pushes it into our instance variable # for the given port. self.instance_variable_set(port_variable, klass.new(port, self.interface)) # Given that that succeeded, all that remains is to add the identifier # to our lookup Hash. We'll use this Hash later on within method_missing. @port_identifiers[identifier] = port # Define a method on the eigenclass of this instance. (class << self; self; end).send(:define_method, identifier) do self.instance_variable_get(port_variable) end else if !self.instance_variable_get(port_variable).nil? raise PortTakenError.new("Port #{port} is already set, call remove first") else raise InvalidIdentifierError.new("Cannot use identifier #{identifier}, a method on #{self.class} is already using it.") end end end |
#connect ⇒ Object
Connect using the given interface to the NXT brick.
68 69 70 |
# File 'lib/nxt/nxt_brick.rb', line 68 def connect self.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.
75 76 77 |
# File 'lib/nxt/nxt_brick.rb', line 75 def disconnect self.interface.disconnect end |
#remove(identifier) ⇒ Object
Remove the assigned (if any) connector instance from the given identifier.
129 130 131 132 |
# File 'lib/nxt/nxt_brick.rb', line 129 def remove(identifier) raise TypeError.new('Expected identifier to be a Symbol') unless identifier.is_a?(Symbol) !!@port_identifiers.delete(identifier) end |