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.
-
#initialize(params = {}) ⇒ Adaptor
constructor
Initialize an adaptor.
-
#reconnect ⇒ Boolean
Makes connected flag true.
Constructor Details
#initialize(params = {}) ⇒ Adaptor
Initialize an adaptor
19 20 21 22 23 24 |
# File 'lib/artoo/adaptors/adaptor.rb', line 19 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.
12 13 14 |
# File 'lib/artoo/adaptors/adaptor.rb', line 12 def additional_params @additional_params end |
#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
28 29 30 |
# File 'lib/artoo/adaptors/adaptor.rb', line 28 def connect @connected = true end |
#connect_to ⇒ TCPSocket, String
Connects to configured port
53 54 55 56 57 58 59 |
# File 'lib/artoo/adaptors/adaptor.rb', line 53 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
77 78 79 80 81 82 83 |
# File 'lib/artoo/adaptors/adaptor.rb', line 77 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.
62 63 64 |
# File 'lib/artoo/adaptors/adaptor.rb', line 62 def connect_to_tcp @socket ||= TCPSocket.new(port.host, port.port) end |
#connect_to_udp ⇒ UDPSocket
Returns UDP socket connection.
67 68 69 |
# File 'lib/artoo/adaptors/adaptor.rb', line 67 def connect_to_udp @udp_socket ||= UDPSocket.new end |
#connected? ⇒ Boolean
Returns connected flag status.
46 47 48 |
# File 'lib/artoo/adaptors/adaptor.rb', line 46 def connected? @connected == true end |
#disconnect ⇒ Boolean
Makes connected flag false
34 35 36 37 |
# File 'lib/artoo/adaptors/adaptor.rb', line 34 def disconnect @connected = false true end |
#reconnect ⇒ Boolean
Makes connected flag true
41 42 43 |
# File 'lib/artoo/adaptors/adaptor.rb', line 41 def reconnect connect unless connected? end |