Class: Phidgets::Manager
- Inherits:
-
Object
- Object
- Phidgets::Manager
- Includes:
- Utility
- Defined in:
- lib/phidgets-ffi/manager.rb
Overview
This class represents a PhidgetManager.
Constant Summary collapse
- Klass =
Phidgets::FFI::CPhidgetManager
Instance Method Summary collapse
-
#close ⇒ Boolean
Closes and frees a PhidgetManager.
-
#devices ⇒ Array
Returns a list of the handles of all currently attached Phidgets, or raises an error.
-
#initialize(options = {}, &block) ⇒ Object
constructor
Initializes a PhidgetManager.
-
#on_attach(obj = nil, &block) ⇒ Boolean
Sets an attach handler callback function.
-
#on_detach(obj = nil, &block) ⇒ Boolean
Sets a detach handler callback function.
-
#on_error(obj = nil, &block) ⇒ Boolean
Sets a error handler callback function.
-
#on_server_connect(obj = nil, &block) ⇒ Boolean
Sets a server connect handler callback function.
-
#on_server_disconnect(obj = nil, &block) ⇒ Boolean
Sets a server disconnect handler callback function.
-
#server_address ⇒ String
Returns the address and port of a remotely opened PhidgetManager, or raises an error.
-
#server_id ⇒ String
Returns the server id of a remotely opened PhidgetManager, or raises an error.
-
#status ⇒ String
Returns the connected to server status, or raises an error.
Constructor Details
#initialize(options = {}, &block) ⇒ Object
Initializes a PhidgetManager. There are two methods that you can use to program the PhidgetManager.
First Method: You can program with a block.
options = {:address => 'localhost', :port => 5001, :server_id => nil, :password => nil}
Phidgets::Manager.new(options) do |man|
...
end
Second Method: You can program without a block
man = Phidgets::Manager.new
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/phidgets-ffi/manager.rb', line 23 def initialize(={}, &block) create password = ([:password].nil? ? nil : [:password].to_s) if ![:server_id].nil? or ![:address].nil? if ![:server_id].nil? open_remote(, password) else open_remote_ip(, password) end sleep 3 else open end if block_given? yield self close end end |
Instance Method Details
#close ⇒ Boolean
Closes and frees a PhidgetManager
75 76 77 78 79 80 81 |
# File 'lib/phidgets-ffi/manager.rb', line 75 def close remove_specific_event_handlers sleep 0.2 Klass.close(@handle) Klass.delete(@handle) true end |
#devices ⇒ Array
Returns a list of the handles of all currently attached Phidgets, or raises an error. You can use the handles with the Phidgets::Common class methods; See the example for more details.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/phidgets-ffi/manager.rb', line 84 def devices devices_ptr, count = ::FFI::MemoryPointer.new(:pointer, 300), ::FFI::MemoryPointer.new(:int) Klass.getAttachedDevices(@handle, devices_ptr, count) devices = devices_ptr.get_array_of_pointer(0, count.get_int(0)) device_array = [] count.get_int(0).times do |i| device_array[i] = devices[0].get_pointer(i*Phidgets::FFI::FFI_POINTER_SIZE) end #Klass.freeAttachedDevicesArray(devices_ptr) #error device_array end |
#on_attach(obj = nil, &block) ⇒ Boolean
Sets an attach handler callback function. This is called when a Phidget is plugged into the system.
As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
137 138 139 140 141 142 143 |
# File 'lib/phidgets-ffi/manager.rb', line 137 def on_attach(obj=nil, &block) @on_attach_obj = obj @on_attach = Proc.new { |handle, obj_ptr| yield handle, object_for(obj_ptr) } Klass.set_OnAttach_Handler(@handle, @on_attach, pointer_for(obj)) end |
#on_detach(obj = nil, &block) ⇒ Boolean
Sets a detach handler callback function. This is called when a Phidget is unplugged from the system.
As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
155 156 157 158 159 160 161 |
# File 'lib/phidgets-ffi/manager.rb', line 155 def on_detach(obj=nil, &block) @on_detach_obj = obj @on_detach = Proc.new { |handle, obj_ptr| yield handle, object_for(obj_ptr) } Klass.set_OnDetach_Handler(@handle, @on_detach, pointer_for(obj)) end |
#on_error(obj = nil, &block) ⇒ Boolean
Sets a error handler callback function. This is called when an asynchronous error occurs.
As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
209 210 211 212 213 214 215 |
# File 'lib/phidgets-ffi/manager.rb', line 209 def on_error(obj=nil, &block) @on_error_obj = obj @on_error = Proc.new { |handle, obj_ptr, code, description| yield self, object_for(obj_ptr), code, description } Klass.set_OnError_Handler(@handle, @on_error, pointer_for(obj)) end |
#on_server_connect(obj = nil, &block) ⇒ Boolean
Sets a server connect handler callback function. This is used for opening the PhidgetManager remotely, and is called when a connection to the server has been made.
As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
173 174 175 176 177 178 179 |
# File 'lib/phidgets-ffi/manager.rb', line 173 def on_server_connect(obj=nil, &block) @on_server_connect_obj = obj @on_server_connect = Proc.new { |handle, obj_ptr| yield self, object_for(obj_pointer) } Klass.set_OnServerConnect_Handler(@handle, @on_server_connect, pointer_for(obj)) end |
#on_server_disconnect(obj = nil, &block) ⇒ Boolean
Sets a server disconnect handler callback function. This is used for opening the PhidgetManager remotely, and is called when a connection to the server has been lost
As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.
191 192 193 194 195 196 197 |
# File 'lib/phidgets-ffi/manager.rb', line 191 def on_server_disconnect(obj=nil, &block) @on_server_disconnect_obj = obj @on_server_disconnect = Proc.new { |handle, obj_ptr| yield self, object_for(obj_ptr) } Klass.set_OnServerDisconnect_Handler(@handle, @on_server_disconnect, pointer_for(obj)) end |
#server_address ⇒ String
Returns the address and port of a remotely opened PhidgetManager, or raises an error. This will fail if the manager was opened locally.
111 112 113 114 115 116 117 118 |
# File 'lib/phidgets-ffi/manager.rb', line 111 def server_address str_ptr, int_ptr = ::FFI::MemoryPointer.new(:string), ::FFI::MemoryPointer.new(:int) Phidgets::FFI::Common.getServerID(@handle, str_ptr, int_ptr) strPtr = str_ptr.get_pointer(0) address = (strPtr.null? ? nil : strPtr.read_string) port = int_ptr.get_int(0) [address, port] end |
#server_id ⇒ String
Returns the server id of a remotely opened PhidgetManager, or raises an error. This will fail if the manager was opened locally.
103 104 105 106 107 108 |
# File 'lib/phidgets-ffi/manager.rb', line 103 def server_id ptr = ::FFI::MemoryPointer.new(:string) Phidgets::FFI::Common.getServerID(@handle, ptr) strPtr = ptr.get_pointer(0) strPtr.null? ? nil : strPtr.read_string end |
#status ⇒ String
Returns the connected to server status, or raises an error
121 122 123 124 125 |
# File 'lib/phidgets-ffi/manager.rb', line 121 def status ptr = ::FFI::MemoryPointer.new(:int) Klass.getServerStatus(@handle, ptr) Phidgets::FFI::ServerStatus[ptr.get_int(0)] end |