Class: Artoo::Drivers::Driver
- Inherits:
-
Object
- Object
- Artoo::Drivers::Driver
- 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
-
#additional_params ⇒ Object
readonly
Returns the value of attribute additional_params.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#command(method_name, *arguments) ⇒ Object
Execute command.
-
#commands ⇒ Collection
Commands.
-
#connection ⇒ Connection
Parent connection.
-
#event_topic_name(event) ⇒ String
Parent topic name.
-
#initialize(params = {}) ⇒ Driver
constructor
Create new driver.
-
#interval ⇒ String
Parent interval.
-
#known_command?(method_name) ⇒ Boolean
True if command exists.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Sends missing methods to connection.
-
#pin ⇒ String
Parent pin.
- #require_interface(i) ⇒ Object
-
#start_driver ⇒ Object
Generic driver start.
Constructor Details
#initialize(params = {}) ⇒ Driver
Create new driver
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. Logger.error e.backtrace.inspect return nil end |
Instance Attribute Details
#additional_params ⇒ Object (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 |
#parent ⇒ Object (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
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. Logger.error e.backtrace.inspect return nil end |
#commands ⇒ Collection
Returns commands.
52 53 54 |
# File 'lib/artoo/drivers/driver.rb', line 52 def commands self.class.const_get('COMMANDS') end |
#connection ⇒ Connection
Returns parent connection.
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.
47 48 49 |
# File 'lib/artoo/drivers/driver.rb', line 47 def event_topic_name(event) parent.event_topic_name(event) end |
#interval ⇒ String
Returns 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.
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 |
#pin ⇒ String
Returns 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_driver ⇒ Object
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 |