Class: Artoo::Adaptors::Adaptor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Adaptor

Returns a new instance of Adaptor.



14
15
16
17
18
# File 'lib/artoo/adaptors/adaptor.rb', line 14

def initialize(params={})
  @parent = params[:parent]
  @port = params[:port]
  @connected = false
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



12
13
14
# File 'lib/artoo/adaptors/adaptor.rb', line 12

def parent
  @parent
end

#portObject (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

#connectObject



20
21
22
# File 'lib/artoo/adaptors/adaptor.rb', line 20

def connect
  @connected = true
end

#connect_toObject



37
38
39
40
41
42
43
# File 'lib/artoo/adaptors/adaptor.rb', line 37

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) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/artoo/adaptors/adaptor.rb', line 53

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_tcpObject



45
46
47
# File 'lib/artoo/adaptors/adaptor.rb', line 45

def connect_to_tcp
  @socket ||= TCPSocket.new(port.host, port.port)
end

#connect_to_udpObject



49
50
51
# File 'lib/artoo/adaptors/adaptor.rb', line 49

def connect_to_udp
  @udp_socket ||= UDPSocket.new
end

#connected?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/artoo/adaptors/adaptor.rb', line 33

def connected?
  @connected == true
end

#disconnectObject



24
25
26
27
# File 'lib/artoo/adaptors/adaptor.rb', line 24

def disconnect
  @connected = false
  true
end

#reconnectObject



29
30
31
# File 'lib/artoo/adaptors/adaptor.rb', line 29

def reconnect
  connect unless connected?
end