Class: Artoo::Drivers::Driver

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Notifications
Defined in:
lib/artoo/drivers/driver.rb

Overview

The Driver class is the base class used to implement behavior for a specific kind of hardware devices. Examples would be an Arduino, a Sphero, or an ARDrone.

Derive a class from this class, in order to implement behavior for a new type of hardware device.

Direct Known Subclasses

Counter, DeviceInfo, Passthru, Ping, Random, Test

Constant Summary collapse

COMMANDS =
[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Driver

Create new driver

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :parent (Object)
  • :additional_params (Object)


21
22
23
24
# File 'lib/artoo/drivers/driver.rb', line 21

def initialize(params={})
  @parent = params[:parent]
  @additional_params = params[:additional_params]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Sends missing methods to connection



85
86
87
88
89
90
91
# File 'lib/artoo/drivers/driver.rb', line 85

def method_missing(method_name, *arguments, &block)
  connection.send(method_name, *arguments, &block)
rescue Exception => e
  Logger.error e.message
  Logger.error e.backtrace.inspect
  return nil
end

Instance Attribute Details

#additional_paramsObject (readonly)

Returns the value of attribute additional_params.



13
14
15
# File 'lib/artoo/drivers/driver.rb', line 13

def additional_params
  @additional_params
end

#parentObject (readonly)

Returns the value of attribute parent.



13
14
15
# File 'lib/artoo/drivers/driver.rb', line 13

def parent
  @parent
end

Instance Method Details

#command(method_name, *arguments) ⇒ Object

Execute command

Parameters:

  • method_name (Symbol)
  • arguments (Array)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/artoo/drivers/driver.rb', line 59

def command(method_name, *arguments)
  known_command?(method_name)
  if arguments.first
    self.send(method_name, *arguments)
  else
    self.send(method_name)
  end
rescue Exception => e
  Logger.error e.message
  Logger.error e.backtrace.inspect
  return nil
end

#commandsCollection

Returns commands.

Returns:

  • (Collection)

    commands



52
53
54
# File 'lib/artoo/drivers/driver.rb', line 52

def commands
  self.class.const_get('COMMANDS')
end

#connectionConnection

Returns parent connection.

Returns:



27
28
29
# File 'lib/artoo/drivers/driver.rb', line 27

def connection
  parent.connection
end

#event_topic_name(event) ⇒ String

Returns parent topic name.

Returns:

  • (String)

    parent topic name



47
48
49
# File 'lib/artoo/drivers/driver.rb', line 47

def event_topic_name(event)
  parent.event_topic_name(event)
end

#intervalString

Returns parent interval.

Returns:

  • (String)

    parent interval



37
38
39
# File 'lib/artoo/drivers/driver.rb', line 37

def interval
  parent.interval
end

#known_command?(method_name) ⇒ Boolean

Returns True if command exists.

Returns:

  • (Boolean)

    True if command exists



73
74
75
76
77
78
# File 'lib/artoo/drivers/driver.rb', line 73

def known_command?(method_name)
  return true if commands.include?(method_name.intern)

  Logger.warn("Calling unknown command '#{method_name}'...")
  return false
end

#pinString

Returns parent pin.

Returns:

  • (String)

    parent pin



32
33
34
# File 'lib/artoo/drivers/driver.rb', line 32

def pin
  parent.pin
end

#require_interface(i) ⇒ Object



80
81
82
# File 'lib/artoo/drivers/driver.rb', line 80

def require_interface(i)
  parent.require_interface(i)
end

#start_driverObject

Generic driver start



42
43
44
# File 'lib/artoo/drivers/driver.rb', line 42

def start_driver
  Logger.info "Starting driver '#{self.class.name}'..."
end