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.

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)


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

def initialize(params={})
  @parent = params[:parent]
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



79
80
81
82
83
84
85
# File 'lib/artoo/drivers/driver.rb', line 79

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

#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)


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

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



50
51
52
# File 'lib/artoo/drivers/driver.rb', line 50

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

#connectionConnection

Returns parent connection.

Returns:



25
26
27
# File 'lib/artoo/drivers/driver.rb', line 25

def connection
  parent.connection
end

#event_topic_name(event) ⇒ String

Returns parent topic name.

Returns:

  • (String)

    parent topic name



45
46
47
# File 'lib/artoo/drivers/driver.rb', line 45

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

#intervalString

Returns parent interval.

Returns:

  • (String)

    parent interval



35
36
37
# File 'lib/artoo/drivers/driver.rb', line 35

def interval
  parent.interval
end

#known_command?(method_name) ⇒ Boolean

Returns True if command exists.

Returns:

  • (Boolean)

    True if command exists



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

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



30
31
32
# File 'lib/artoo/drivers/driver.rb', line 30

def pin
  parent.pin
end

#start_driverObject

Generic driver start



40
41
42
# File 'lib/artoo/drivers/driver.rb', line 40

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