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.
Instance Attribute Summary collapse
-
#additional_params ⇒ Object
readonly
Returns the value of attribute additional_params.
-
#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 = nil) ⇒ 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.
-
#finalize ⇒ Boolean
Closes connection with device if connected.
-
#initialize(params = {}) ⇒ Adaptor
constructor
Initialize an adaptor.
-
#reconnect ⇒ Boolean
Makes connected flag true.
Constructor Details
#initialize(params = {}) ⇒ Adaptor
Initialize an adaptor
20 21 22 23 24 25 |
# File 'lib/artoo/adaptors/adaptor.rb', line 20 def initialize(params={}) @parent = params[:parent] @port = params[:port] @additional_params = params[:additional_params] @connected = false end |
Instance Attribute Details
#additional_params ⇒ Object (readonly)
Returns the value of attribute additional_params.
13 14 15 |
# File 'lib/artoo/adaptors/adaptor.rb', line 13 def additional_params @additional_params end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
13 14 15 |
# File 'lib/artoo/adaptors/adaptor.rb', line 13 def parent @parent end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
13 14 15 |
# File 'lib/artoo/adaptors/adaptor.rb', line 13 def port @port end |
Instance Method Details
#connect ⇒ Boolean
Makes connected flag true
35 36 37 |
# File 'lib/artoo/adaptors/adaptor.rb', line 35 def connect @connected = true end |
#connect_to ⇒ TCPSocket, String
Connects to configured port
60 61 62 63 64 65 66 |
# File 'lib/artoo/adaptors/adaptor.rb', line 60 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 = nil) ⇒ SerialPort
Creates serial connection
84 85 86 87 88 89 90 |
# File 'lib/artoo/adaptors/adaptor.rb', line 84 def connect_to_serial(speed=57600, data_bits=8, stop_bits=1, parity=nil) 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.
69 70 71 |
# File 'lib/artoo/adaptors/adaptor.rb', line 69 def connect_to_tcp @socket ||= TCPSocket.new(port.host, port.port) end |
#connect_to_udp ⇒ UDPSocket
Returns UDP socket connection.
74 75 76 |
# File 'lib/artoo/adaptors/adaptor.rb', line 74 def connect_to_udp @udp_socket ||= UDPSocket.new end |
#connected? ⇒ Boolean
Returns connected flag status.
53 54 55 |
# File 'lib/artoo/adaptors/adaptor.rb', line 53 def connected? @connected == true end |
#disconnect ⇒ Boolean
Makes connected flag false
41 42 43 44 |
# File 'lib/artoo/adaptors/adaptor.rb', line 41 def disconnect @connected = false true end |
#finalize ⇒ Boolean
Closes connection with device if connected
29 30 31 |
# File 'lib/artoo/adaptors/adaptor.rb', line 29 def finalize disconnect if connected? end |
#reconnect ⇒ Boolean
Makes connected flag true
48 49 50 |
# File 'lib/artoo/adaptors/adaptor.rb', line 48 def reconnect connect unless connected? end |