Class: Artoo::Adaptors::Adaptor
- Inherits:
-
Object
- Object
- Artoo::Adaptors::Adaptor
- Includes:
- Celluloid::IO
- Defined in:
- lib/artoo/adaptors/adaptor.rb
Overview
The Adaptor class is the base class used to connect to a specific group of hardware devices. Examples would be an Arduino, a Sphero, or an ARDrone.
Derive a class from this class, in order to implement communication with a new type of hardware device.
Direct Known Subclasses
Ardrone, ArdroneNavigation, ArdroneVideo, Firmata, Loopback, Roomba, Sphero
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#connect ⇒ Boolean
Makes connected flag true.
-
#connect_to ⇒ TCPSocket, String
Connects to configured port.
-
#connect_to_serial(speed = 57600, data_bits = 8, stop_bits = 1, parity = ::SerialPort::NONE) ⇒ SerialPort
Creates serial connection.
-
#connect_to_tcp ⇒ TCPSocket
TCP socket connection.
-
#connect_to_udp ⇒ UDPSocket
UDP socket connection.
-
#connected? ⇒ Boolean
Connected flag status.
-
#disconnect ⇒ Boolean
Makes connected flag false.
-
#initialize(params = {}) ⇒ Adaptor
constructor
Initialize an adaptor.
-
#reconnect ⇒ Boolean
Makes connected flag true.
Constructor Details
#initialize(params = {}) ⇒ Adaptor
Initialize an adaptor
18 19 20 21 22 |
# File 'lib/artoo/adaptors/adaptor.rb', line 18 def initialize(params={}) @parent = params[:parent] @port = params[:port] @connected = false end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
12 13 14 |
# File 'lib/artoo/adaptors/adaptor.rb', line 12 def parent @parent end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/artoo/adaptors/adaptor.rb', line 12 def port @port end |
Instance Method Details
#connect ⇒ Boolean
Makes connected flag true
26 27 28 |
# File 'lib/artoo/adaptors/adaptor.rb', line 26 def connect @connected = true end |
#connect_to ⇒ TCPSocket, String
Connects to configured port
51 52 53 54 55 56 57 |
# File 'lib/artoo/adaptors/adaptor.rb', line 51 def connect_to if port.is_tcp? connect_to_tcp else port.port end end |
#connect_to_serial(speed = 57600, data_bits = 8, stop_bits = 1, parity = ::SerialPort::NONE) ⇒ SerialPort
Creates serial connection
74 75 76 77 78 79 80 |
# File 'lib/artoo/adaptors/adaptor.rb', line 74 def connect_to_serial(speed=57600, data_bits=8, stop_bits=1, parity=::SerialPort::NONE) require 'serialport' parity = ::SerialPort::NONE unless parity @sp = ::SerialPort.new(port.port, speed, data_bits, stop_bits, parity) rescue LoadError Logger.error "Please 'gem install hybridgroup-serialport' for serial port support." end |
#connect_to_tcp ⇒ TCPSocket
Returns TCP socket connection.
60 61 62 |
# File 'lib/artoo/adaptors/adaptor.rb', line 60 def connect_to_tcp @socket ||= TCPSocket.new(port.host, port.port) end |
#connect_to_udp ⇒ UDPSocket
Returns UDP socket connection.
64 65 66 |
# File 'lib/artoo/adaptors/adaptor.rb', line 64 def connect_to_udp @udp_socket ||= UDPSocket.new end |
#connected? ⇒ Boolean
Returns connected flag status.
44 45 46 |
# File 'lib/artoo/adaptors/adaptor.rb', line 44 def connected? @connected == true end |
#disconnect ⇒ Boolean
Makes connected flag false
32 33 34 35 |
# File 'lib/artoo/adaptors/adaptor.rb', line 32 def disconnect @connected = false true end |
#reconnect ⇒ Boolean
Makes connected flag true
39 40 41 |
# File 'lib/artoo/adaptors/adaptor.rb', line 39 def reconnect connect unless connected? end |