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.
Direct Known Subclasses
Ardrone, ArdroneNavigation, ArdroneVideo, Button, Led, Motor, Passthru, Pinger, Pinger2, Roomba, Servo, Sphero, Wiidriver
Constant Summary collapse
- COMMANDS =
[].freeze
Instance Attribute Summary collapse
-
#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.
-
#start_driver ⇒ Object
Generic driver start.
Constructor Details
#initialize(params = {}) ⇒ Driver
Create new driver
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. Logger.error e.backtrace.inspect return nil end |
Instance Attribute Details
#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
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. Logger.error e.backtrace.inspect return nil end |
#commands ⇒ Collection
Returns commands.
50 51 52 |
# File 'lib/artoo/drivers/driver.rb', line 50 def commands self.class.const_get('COMMANDS') end |
#connection ⇒ Connection
Returns parent connection.
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.
45 46 47 |
# File 'lib/artoo/drivers/driver.rb', line 45 def event_topic_name(event) parent.event_topic_name(event) end |
#interval ⇒ String
Returns 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.
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 |
#pin ⇒ String
Returns parent pin.
30 31 32 |
# File 'lib/artoo/drivers/driver.rb', line 30 def pin parent.pin end |
#start_driver ⇒ Object
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 |