Class: Adept::Device
- Inherits:
-
Object
- Object
- Adept::Device
- Defined in:
- lib/adept/device.rb
Overview
Basic interface to a Digilent Adept device.
Direct Known Subclasses
Constant Summary collapse
- DeviceManager =
Get an easy reference to the adept Device Manager API.
LowLevel::DeviceManager
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
Allow access to the device’s handle, for now.
Class Method Summary collapse
-
.by_name(name) ⇒ Object
Factory method which returns a connection to the device with the given name, or nil if no devices with that name could be found.
-
.connected_devices ⇒ Object
Returns an array of information regarding connected devices.
-
.open_first_connected_device ⇒ Object
Returns a new connection to the first connected device.
-
.path_to(name) ⇒ Object
Returns the path to first device with the given name, or nil if no devices with that name could be found.
Instance Method Summary collapse
-
#close(check_handle = true) ⇒ Object
Closes the given device.
-
#ensure_handle_is_valid ⇒ Object
TODO: throw an exception of our handle is nil.
-
#initialize(path) ⇒ Device
constructor
Creates a new connection to a Digilent adept device.
-
#supported_connections ⇒ Object
Returns a list of connection methods supported by the current device.
Constructor Details
#initialize(path) ⇒ Device
Creates a new connection to a Digilent adept device.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/adept/device.rb', line 58 def initialize(path) #Open the device, and store its handle. @handle = DeviceManager::open_device(path) #If we didn't get a valid handle, raise the relevant exception. if @handle.nil? raise DeviceManager::last_error end end |
Instance Attribute Details
#handle ⇒ Object (readonly)
Allow access to the device’s handle, for now.
24 25 26 |
# File 'lib/adept/device.rb', line 24 def handle @handle end |
Class Method Details
.by_name(name) ⇒ Object
Factory method which returns a connection to the device with the given name, or nil if no devices with that name could be found.
30 31 32 33 34 35 36 37 38 |
# File 'lib/adept/device.rb', line 30 def self.by_name(name) #Find path to the first device with the given name. path = path_to(name) #Return a new connection to the target device, or nil if no path was found. path ? self.new(path) : nil end |
.connected_devices ⇒ Object
Returns an array of information regarding connected devices. Each array member is a hash, with three members:
-:name, the device's shortname
-:connection, the device's connection
-:transport, a code indicating the method by which we're connecting to the device
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/adept/device.rb', line 100 def self.connected_devices #Populate the internal device list, retrieving the amount of connected devices. count = DeviceManager::populate_device_list #Get the device information for each of the given devices. devices = (0...count).map { |index| DeviceManager::get_device_info(index) } #Free the internal device list. DeviceManager::free_device_list #and return the list of connected devices devices end |
.open_first_connected_device ⇒ Object
Returns a new connection to the first connected device.
119 120 121 122 |
# File 'lib/adept/device.rb', line 119 def self.open_first_connected_device device = connected_devices.first new(device[:path]) end |
.path_to(name) ⇒ Object
Returns the path to first device with the given name, or nil if no devices with that name could be found.
44 45 46 47 48 49 50 51 52 |
# File 'lib/adept/device.rb', line 44 def self.path_to(name) #Find the first device with the given name. target_device = connected_devices.find { |device| device[:name] == name } #Return the path to the target device. target_device ? target_device[:path] : nil end |
Instance Method Details
#close(check_handle = true) ⇒ Object
Closes the given device.
74 75 76 77 |
# File 'lib/adept/device.rb', line 74 def close(check_handle=true) ensure_handle_is_valid if check_handle DeviceManager::close_device(@handle) end |
#ensure_handle_is_valid ⇒ Object
TODO: throw an exception of our handle is nil
82 83 84 |
# File 'lib/adept/device.rb', line 82 def ensure_handle_is_valid end |
#supported_connections ⇒ Object
Returns a list of connection methods supported by the current device.
89 90 91 |
# File 'lib/adept/device.rb', line 89 def supported_connections ConnectionProvider.providers.select { |connection| connection.supported_by?(@handle) } end |