Class: Phidgets::Manager

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • options (String) (defaults to: {})

    Information required to connect to the WebService. This is optional. If no option is specified, it is assumed that the address is localhost and port is 5001

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.



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(options={}, &block)
	  create
  
	  password = (options[:password].nil? ? nil : options[:password].to_s)
	  if !options[:server_id].nil? or !options[:address].nil?
		if !options[:server_id].nil?
			open_remote(options, password)
		else
			open_remote_ip(options, password)
		end
		sleep 3
	  else
		open
	  end
  if block_given?
    yield self
    close
  end
end

Instance Method Details

#closeBoolean

Closes and frees a PhidgetManager

Returns:

  • (Boolean)

    returns true or raises an error



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

#devicesArray

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.

Returns:

  • (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.

Examples:

manager.on_attach do |device_ptr, obj|
  puts "Attaching #{Phidgets::Common.name(device_ptr)}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



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.

Examples:

manager.on_detach do |device_ptr, obj|
  puts "Detaching #{Phidgets::Common.name(device_ptr)}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



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.

Examples:

manager.on_error do |obj|
  puts "Error (#{code}): #{reason}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



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.

Examples:

manager.on_server_connect do |obj|
   puts 'Connected'
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



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.

Examples:

manager.on_server_disconnect do |obj|
  puts 'Disconnected'
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



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_addressString

Returns the address and port of a remotely opened PhidgetManager, or raises an error. This will fail if the manager was opened locally.

Returns:

  • (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_idString

Returns the server id of a remotely opened PhidgetManager, or raises an error. This will fail if the manager was opened locally.

Returns:

  • (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

#statusString

Returns the connected to server status, or raises an error

Returns:

  • (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