phidgets-ffi 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +3 -0
  2. data/LICENSE +1 -1
  3. data/README.rdoc +92 -42
  4. data/examples/accelerometer.rb +39 -0
  5. data/examples/advanced_servo.rb +94 -0
  6. data/examples/analog.rb +43 -0
  7. data/examples/bridge.rb +57 -0
  8. data/examples/dictionary.rb +46 -31
  9. data/examples/encoder.rb +59 -0
  10. data/examples/frequency_counter.rb +63 -0
  11. data/examples/gps.rb +91 -0
  12. data/examples/interface_kit_with_block.rb +68 -0
  13. data/examples/interface_kit_without_block.rb +60 -0
  14. data/examples/ir.rb +157 -0
  15. data/examples/led.rb +36 -0
  16. data/examples/manager.rb +16 -10
  17. data/examples/motor_control.rb +108 -0
  18. data/examples/{ffi → raw-ffi}/dictionary.rb +11 -1
  19. data/examples/{ffi → raw-ffi}/interface_kit.rb +19 -2
  20. data/examples/{ffi → raw-ffi}/library_version.rb +0 -0
  21. data/examples/{ffi → raw-ffi}/log.rb +0 -0
  22. data/examples/{ffi → raw-ffi}/manager.rb +6 -3
  23. data/examples/rfid.rb +63 -0
  24. data/examples/servo.rb +45 -30
  25. data/examples/spatial.rb +75 -0
  26. data/examples/stepper.rb +87 -0
  27. data/examples/temperature_sensor.rb +49 -0
  28. data/examples/text_lcd.rb +101 -0
  29. data/lib/phidgets-ffi.rb +34 -3
  30. data/lib/phidgets-ffi/accelerometer.rb +122 -0
  31. data/lib/phidgets-ffi/advanced_servo.rb +304 -0
  32. data/lib/phidgets-ffi/analog.rb +111 -0
  33. data/lib/phidgets-ffi/bridge.rb +167 -0
  34. data/lib/phidgets-ffi/common.rb +506 -103
  35. data/lib/phidgets-ffi/dictionary.rb +136 -23
  36. data/lib/phidgets-ffi/encoder.rb +196 -0
  37. data/lib/phidgets-ffi/error.rb +8 -3
  38. data/lib/phidgets-ffi/ffi/accelerometer.rb +30 -0
  39. data/lib/phidgets-ffi/ffi/advanced_servo.rb +73 -0
  40. data/lib/phidgets-ffi/ffi/analog.rb +29 -0
  41. data/lib/phidgets-ffi/ffi/bridge.rb +44 -0
  42. data/lib/phidgets-ffi/ffi/common.rb +51 -34
  43. data/lib/phidgets-ffi/ffi/constants.rb +3 -1
  44. data/lib/phidgets-ffi/ffi/dictionary.rb +25 -20
  45. data/lib/phidgets-ffi/ffi/encoder.rb +32 -0
  46. data/lib/phidgets-ffi/ffi/frequency_counter.rb +38 -0
  47. data/lib/phidgets-ffi/ffi/gps.rb +32 -0
  48. data/lib/phidgets-ffi/ffi/interface_kit.rb +26 -23
  49. data/lib/phidgets-ffi/ffi/ir.rb +50 -0
  50. data/lib/phidgets-ffi/ffi/led.rb +40 -0
  51. data/lib/phidgets-ffi/ffi/log.rb +7 -6
  52. data/lib/phidgets-ffi/ffi/manager.rb +35 -20
  53. data/lib/phidgets-ffi/ffi/motor_control.rb +66 -0
  54. data/lib/phidgets-ffi/ffi/rfid.rb +36 -0
  55. data/lib/phidgets-ffi/ffi/servo.rb +16 -15
  56. data/lib/phidgets-ffi/ffi/spatial.rb +40 -0
  57. data/lib/phidgets-ffi/ffi/stepper.rb +56 -0
  58. data/lib/phidgets-ffi/ffi/temperature_sensor.rb +42 -0
  59. data/lib/phidgets-ffi/ffi/text_lcd.rb +55 -0
  60. data/lib/phidgets-ffi/frequency_counter.rb +148 -0
  61. data/lib/phidgets-ffi/gps.rb +181 -0
  62. data/lib/phidgets-ffi/interface_kit.rb +205 -92
  63. data/lib/phidgets-ffi/ir.rb +290 -0
  64. data/lib/phidgets-ffi/led.rb +112 -0
  65. data/lib/phidgets-ffi/log.rb +14 -2
  66. data/lib/phidgets-ffi/manager.rb +143 -26
  67. data/lib/phidgets-ffi/motor_control.rb +497 -0
  68. data/lib/phidgets-ffi/phidgets-ffi.rb +15 -2
  69. data/lib/phidgets-ffi/rfid.rb +220 -0
  70. data/lib/phidgets-ffi/servo.rb +103 -61
  71. data/lib/phidgets-ffi/spatial.rb +306 -0
  72. data/lib/phidgets-ffi/stepper.rb +370 -0
  73. data/lib/phidgets-ffi/temperature_sensor.rb +157 -0
  74. data/lib/phidgets-ffi/text_lcd.rb +298 -0
  75. data/lib/phidgets-ffi/version.rb +1 -1
  76. data/phidgets-ffi.gemspec +2 -2
  77. metadata +89 -76
  78. data/examples/ffi/servo.rb +0 -67
  79. data/examples/interface_kit.rb +0 -20
@@ -0,0 +1,111 @@
1
+ module Phidgets
2
+
3
+ # This class represents a PhidgetAnalog.
4
+ class Analog
5
+
6
+ Klass = Phidgets::FFI::CPhidgetAnalog
7
+ include Phidgets::Common
8
+
9
+ # Collection of analog outputs
10
+ # @return [AnalogOutputs]
11
+ attr_reader :outputs
12
+
13
+ attr_reader :attributes
14
+
15
+ # The attributes of a PhidgetAnalog
16
+ def attributes
17
+ super.merge({
18
+ :outputs => outputs.size
19
+ })
20
+ end
21
+
22
+ private
23
+
24
+ def load_device_attributes
25
+ load_outputs
26
+ end
27
+
28
+ def load_outputs
29
+ ptr = ::FFI::MemoryPointer.new(:int)
30
+ Klass.getOutputCount(@handle, ptr)
31
+ @outputs = []
32
+ ptr.get_int(0).times do |i|
33
+ @outputs << AnalogOutputs.new(@handle, i)
34
+ end
35
+ end
36
+
37
+ def remove_specific_event_handlers
38
+
39
+ end
40
+
41
+ # This class represents an analog output for a PhidgetAnalog. All the properties of an output are stored and modified in this class.
42
+ class AnalogOutputs
43
+ Klass = Phidgets::FFI::CPhidgetAnalog
44
+
45
+ private
46
+ def initialize(handle, index)
47
+ @handle, @index = handle, index.to_i
48
+ end
49
+
50
+ public
51
+
52
+ # Displays data for the analog output
53
+ def inspect
54
+ "#<#{self.class} @index=#{index}, @enabled=#{enabled}, @voltage=#{voltage}, @voltage_min=#{voltage_min}, @voltage_max=#{voltage_max}>"
55
+ end
56
+
57
+ # @return [Integer] returns the index of an output, or raises an error.
58
+ def index
59
+ @index
60
+ end
61
+
62
+
63
+ # @return [Boolean] returns the enabled state of an output, or raises an error.
64
+ def enabled
65
+ ptr = ::FFI::MemoryPointer.new(:int)
66
+ Klass.getEnabled(@handle, @index, ptr)
67
+ (ptr.get_int(0) == 0) ? false : true
68
+ end
69
+
70
+ # Sets the enabled state of an output, or raises an error.
71
+ # @param [Boolean] new_state new state
72
+ # @return [Boolean] returns the enabled state of an output, or raises an error.
73
+ def enabled=(new_state)
74
+ tmp = new_state ? 1 : 0
75
+ Klass.setEnabled(@handle, @index, tmp)
76
+ new_state
77
+ end
78
+
79
+ # @return [Float] returns the voltage of an output, in V, or raises an error.
80
+ def voltage
81
+ ptr = ::FFI::MemoryPointer.new(:double)
82
+ Klass.getVoltage(@handle, @index, ptr)
83
+ ptr.get_double(0)
84
+ end
85
+
86
+ # Sets the voltage of an output, in V, or raises an error.
87
+ # @param [Float] new_voltage new voltage
88
+ # @return [Float] returns the voltage of an output, in V, or raises an error.
89
+ def voltage=(new_voltage)
90
+ Klass.setVoltage(@handle, @index, new_voltage.to_f)
91
+ new_voltage
92
+ end
93
+
94
+ # @return [Float] returns the minimum supported output voltage, or raises an error.
95
+ def voltage_min
96
+ ptr = ::FFI::MemoryPointer.new(:double)
97
+ Klass.getVoltageMin(@handle, @index, ptr)
98
+ ptr.get_double(0)
99
+ end
100
+
101
+ # @return [Float] returns the maximum supported output voltage, or raises an error.
102
+ def voltage_max
103
+ ptr = ::FFI::MemoryPointer.new(:double)
104
+ Klass.getVoltageMax(@handle, @index, ptr)
105
+ ptr.get_double(0)
106
+ end
107
+
108
+ end
109
+ end
110
+ end
111
+
@@ -0,0 +1,167 @@
1
+ module Phidgets
2
+
3
+ # This class represents a PhidgetBridge.
4
+ class Bridge
5
+
6
+ Klass = Phidgets::FFI::CPhidgetBridge
7
+ include Phidgets::Common
8
+
9
+ # Collection of bridge inputs
10
+ # @return [BridgeInputs]
11
+ attr_reader :inputs
12
+
13
+ attr_reader :attributes
14
+
15
+ # The attributes of a PhidgetBridge
16
+ def attributes
17
+ super.merge({
18
+ :inputs => inputs.size,
19
+ })
20
+ end
21
+
22
+ # Sets a bridge data handler callback function. This is called at a set rate as defined by {Phidgets::Bridge#data_rate}
23
+ #
24
+ # @param [String] obj Object to pass to the callback function. This is optional.
25
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
26
+ # @example
27
+ # bridge.on_bridge_data do |device, input, value, obj|
28
+ # puts "Bridge Index #{input.index}, value: #{value}"
29
+ # end
30
+ # 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.
31
+ # @return [Boolean] returns true or raises an error
32
+ def on_bridge_data(obj=nil, &block)
33
+
34
+ @on_bridge_data_obj = obj
35
+ @on_bridge_data = Proc.new { |device, obj_ptr, index, value|
36
+ yield self, @inputs[index], value, object_for(obj_ptr)
37
+
38
+ }
39
+ Klass.set_OnBridgeData_Handler(@handle, @on_bridge_data, pointer_for(obj))
40
+ end
41
+
42
+ # @return [Integer] returns the data rate of the board, in milliseconds, or raises an error.
43
+ def data_rate
44
+ ptr = ::FFI::MemoryPointer.new(:int)
45
+ Klass.getDataRate(@handle, ptr)
46
+ ptr.get_int(0)
47
+ end
48
+
49
+ # Sets the data rate of the board, or raises an error.
50
+ # @param [Integer] new_data_rate data rate, in millilseconds
51
+ # @return [Integer] returns the data rate of the board, or raises an error.
52
+ def data_rate=(new_data_rate)
53
+ Klass.setDataRate(@handle, new_data_rate.to_i)
54
+ new_data_rate.to_i
55
+ end
56
+
57
+ # @return [Integer] returns maximum data rate of the board, in milliseconds, or raises an error.
58
+ def data_rate_max
59
+ ptr = ::FFI::MemoryPointer.new(:int)
60
+ Klass.getDataRateMax(@handle, ptr)
61
+ ptr.get_int(0)
62
+ end
63
+
64
+ # @return [Integer] returns minimum data rate of the board, in milliseconds, or raises an error.
65
+ def data_rate_min
66
+ ptr = ::FFI::MemoryPointer.new(:int)
67
+ Klass.getDataRateMin(@handle, ptr)
68
+ ptr.get_int(0)
69
+ end
70
+
71
+ # This class represents a bridge input for a PhidgetBridge. All the properties of an bridge input are stored and modified in this class.
72
+ class BridgeInputs
73
+ Klass = Phidgets::FFI::CPhidgetBridge
74
+
75
+ private
76
+ def initialize(handle, index)
77
+ @handle, @index = handle, index.to_i
78
+ end
79
+
80
+ public
81
+ # Displays data for the bridge input.
82
+ def inspect
83
+ "#<#{self.class} @index=#{index}, @bridge_value=#{bridge_value}, @bridge_min=#{bridge_min}, @bridge_max=#{bridge_max}, @enabled=#{enabled}, @gain=#{gain}>"
84
+ end
85
+
86
+ # @return [Integer] returns the index of the bridge input, or raises an error.
87
+ def index
88
+ @index
89
+ end
90
+
91
+ # @return [Float] returns the value of a bridge input, in mV/V, or raises an error.
92
+ def bridge_value
93
+ ptr = ::FFI::MemoryPointer.new(:double)
94
+ Klass.getBridgeValue(@handle, @index, ptr)
95
+ ptr.get_double(0)
96
+ end
97
+
98
+ # @return [Float] returns the maximum supported input value, in mV/V, or raises an error.
99
+ def bridge_max
100
+ ptr = ::FFI::MemoryPointer.new(:double)
101
+ Klass.getBridgeMax(@handle, @index, ptr)
102
+ ptr.get_double(0)
103
+ end
104
+
105
+ # @return [Float] returns the minimum supported input value, in mV/V, or raises an error.
106
+ def bridge_min
107
+ ptr = ::FFI::MemoryPointer.new(:double)
108
+ Klass.getBridgeMin(@handle, @index, ptr)
109
+ ptr.get_double(0)
110
+ end
111
+
112
+ # @return [Boolean] returns the enabled state of a bridge input, or raises an error.
113
+ def enabled
114
+ ptr = ::FFI::MemoryPointer.new(:int)
115
+ Klass.getEnabled(@handle, @index, ptr)
116
+ (ptr.get_int(0) == 0) ? false : true
117
+ end
118
+
119
+ # Sets the enabled state of a bridge input, or raises an error.
120
+ # @param [Boolean] new_state new state
121
+ # @return [Boolean] returns the enabled state of a bridge input, or raises an error.
122
+ def enabled=(new_state)
123
+ tmp = new_state ? 1 : 0
124
+ Klass.setEnabled(@handle, @index, tmp)
125
+ new_state
126
+ end
127
+
128
+ # @return [Phidgets::FFI::BridgeGains] returns the gain type of a bridge input, or raises an error.
129
+ def gain
130
+ ptr = ::FFI::MemoryPointer.new(:int)
131
+ Klass.getGain(@handle, @index, ptr)
132
+ Phidgets::FFI::BridgeGains[ptr.get_int(0)]
133
+ end
134
+
135
+ # Sets the gain type of a bridge input, or raises an error.
136
+ # @param [Phidgets::FFI::BridgeGains] new_gain new gain
137
+ # @return [Phidgets::FFI::BridgeGains] returns the gain type of a bridge input, or raises an error.
138
+ def gain=(new_gain)
139
+ ptr = ::FFI::MemoryPointer.new(:int)
140
+ Klass.setGain(@handle, @index, Phidgets::FFI::BridgeGains[new_gain])
141
+ new_gain
142
+ end
143
+
144
+ end #BridgeInputs
145
+
146
+ private
147
+
148
+ def load_device_attributes
149
+ load_inputs
150
+ end
151
+
152
+ def load_inputs
153
+ ptr = ::FFI::MemoryPointer.new(:int)
154
+ Klass.getInputCount(@handle, ptr)
155
+ @inputs = []
156
+ ptr.get_int(0).times do |i|
157
+ @inputs << BridgeInputs.new(@handle, i)
158
+ end
159
+
160
+ end
161
+
162
+ def remove_specific_event_handlers
163
+ Klass.set_OnBridgeData_Handler(@handle, nil, nil)
164
+ end
165
+ end
166
+
167
+ end
@@ -1,5 +1,8 @@
1
1
  module Phidgets
2
2
  module FFI
3
+ # Gets the library version. This contains a version number and a build date.
4
+ #
5
+ # @return [String]
3
6
  def self.library_version
4
7
  ptr = ::FFI::MemoryPointer.new(:string)
5
8
  Phidgets::FFI::Common.getLibraryVersion(ptr)
@@ -7,6 +10,10 @@ module Phidgets
7
10
  strPtr.null? ? nil : strPtr.read_string
8
11
  end
9
12
 
13
+ # Gets the description for an error code.
14
+ #
15
+ # @param [Fixnum] code The error code to get the description of.
16
+ # @return [String]
10
17
  def self.error_description(code)
11
18
  ptr = ::FFI::MemoryPointer.new(:string)
12
19
  Phidgets::FFI::Common.getErrorDescription(code, ptr)
@@ -15,20 +22,166 @@ module Phidgets
15
22
  end
16
23
  end
17
24
 
25
+ private
18
26
  module Utility
27
+
19
28
  private
29
+
30
+ # Given an FFI::MemoryPointer to an object_id, return a reference to it from the ObjectSpace
31
+ #
32
+ # @param [FFI::MemoryPointer] obj_ptr Pointer to an object_id
33
+ # @return [Object] Object referred to by the object_id
20
34
  def object_for(obj_ptr)
21
35
  (obj_ptr.null? ? nil : ObjectSpace._id2ref(obj_ptr.get_uint(0)))
22
36
  end
23
37
 
38
+ # Given an object, return a pointer to it's object_id in the ObjectSpace
39
+ #
40
+ # @param [Object] object for which to create a pointer reference
41
+ # @return [FFI::MemoryPointer] pointer to the object_id
24
42
  def pointer_for(obj)
25
43
  ::FFI::MemoryPointer.new(:uint).write_uint(obj.object_id)
26
44
  end
27
45
  end
28
46
 
47
+ # This is the base class from which all Phidget device classes derive.
29
48
  module Common
30
49
  include Utility
31
50
 
51
+ # Initializes a Phidget. There are two methods that you can use to program Phidgets.
52
+ #
53
+ # @return [Object]
54
+ #
55
+ # <b>First Method:</b> You can program with a block
56
+ # Phidgets::InterfaceKit.new do |ifkit|
57
+ # ...
58
+ # end
59
+ #
60
+ # <b>Second Method:</b> You can program without a block
61
+ #
62
+ # ifkit = Phidgets::InterfaceKit.new
63
+ #
64
+ # The constructor accepts optional arguments. Depending on what is specified as options, a Phidget will be opened in a different manner. The timeout is used for block programming; if no timeout is specified, the default timeout(1000 milliseconds) will be used. Please note that if you are not using the block method and specify a timeout, the timeout will be ignored.
65
+ #
66
+ # The first available Phidget that meets all the criteria will be opened. If there are two Phidgets of the same type attached to the system, you should specify a serial number or label, as there is no guarantee which Phidget will be selected by the call.
67
+ #
68
+ # @param [Integer] options The first available Phidget that meets all the criteria will be opened.
69
+ # @param [Proc] Block initialize will automatically will open and call wait_for_attachment on the Phidget. Then, the block will yield. Afterwards, close is automatically called.
70
+ #
71
+ # <b>Usage:</b>
72
+ #
73
+ # Open the first available Phidget with an optional timeout(milliseconds).
74
+ #
75
+ # Specify 0 for timeout to wait forever.
76
+ #
77
+ # <b>Parameters:</b> (Integer[optional])
78
+ # new({:timeout => 5000})
79
+ #
80
+ # <br/>
81
+ #
82
+ # Open a Phidget using a serial number, and an optional timeout(milliseconds).
83
+ #
84
+ # Specify -1 for serial number to open any. Specify 0 for timeout to wait forever.
85
+ #
86
+ # <b>Parameters:</b> (Integer, Integer[optional])
87
+ # new({:serial_number => -1, :timeout => 5000})
88
+ #
89
+ # <br/>
90
+ #
91
+ # Open a Phidget using a specific label, and an optional timeout(milliseconds).
92
+ #
93
+ # Specify nil for device label to open any. Specify 0 for timeout to wait forever.
94
+ #
95
+ # <b>Parameters:</b> (String, Integer[optional])
96
+ # new({:label => nil, :timeout => 5000})
97
+ #
98
+ # <br/>
99
+ #
100
+ # Open a Phidget remotely and securely, using a specific serial number, server id, password, and an optional timeout(milliseconds).
101
+ #
102
+ # Specify -1 for serial number to open any. Specify nil for password if it is not required. Specify 0 for timeout to wait forever.
103
+ #
104
+ # <b>Parameters:</b> (Integer, String, String, Integer[optional])
105
+ # new({:serial_number => -1, :server_id => 'phidgetsbc', :password => nil, :timeout => 5000})
106
+ #
107
+ # <br/>
108
+ #
109
+ # Open a Phidget remotely and securely, using a specific serial number, IP Address, port, password, and an optional timeout(milliseconds).
110
+ #
111
+ # Specify -1 for serial number to open any. Specify nil for password if it is not required. Specify 0 for timeout to wait forever.
112
+ #
113
+ # <b>Parameters:</b> (Integer, String, Integer, String, Integer[optional])
114
+ # new({:serial_number => -1, :server_address => 'localhost', :port => 5001, :password => nil, :timeout => 5000})
115
+ #
116
+ # <br/>
117
+ #
118
+ # Open a Phidget remotely and securely, using a specific device label, server id, password, and an optional timeout(milliseconds).
119
+ #
120
+ # Specify nil for device label to open any. Specify nil for password if it is not required. Specify 0 for timeout to wait forever.
121
+ #
122
+ # <b>Parameters:</b> (String, String, String, Integer[optional])
123
+ # new({:label => nil, :server_id => 'phidgetsbc', :password => nil, :timeout => 5000})
124
+ #
125
+ # <br/>
126
+ #
127
+ # Open a Phidget remotely and securely, using a specific label, server address, port, password, and an optional timeout(milliseconds).
128
+ #
129
+ # Specify nil for device label to open any. Specify nil for password if it is not required. Specify 0 for timeout to wait forever.
130
+ #
131
+ # <b>Parameters:</b> (String, String, Integer, String, Integer[optional])
132
+ # new({:label => nil, :server_address => 'localhost', :port => 5001, :password => nil, :timeout => 5000})
133
+ def initialize(options=nil, &block)
134
+ create
135
+ args_for_open = 0
136
+ with_wait_time = false
137
+ args_to_consider = 0
138
+
139
+ if !options.nil?
140
+
141
+ label = (options[:label].nil? ? nil : options[:label].to_s)
142
+ serial_number = (options[:serial_number].nil? ? -1 : options[:serial_number].to_i)
143
+ password = (options[:password].nil? ? nil : options[:password].to_s)
144
+
145
+ if !options[:server_id].nil?
146
+ if !label.nil?
147
+ open_label_remote(label, options[:server_id].to_s, password)
148
+ else
149
+ open_remote(serial_number, options[:server_id].to_s, password)
150
+ end
151
+ elsif !options[:address].nil?
152
+ if !label.nil?
153
+ open_label_remote_ip(label, options[:address].to_s, options[:port].to_i, password)
154
+ else
155
+ open_remote_ip(serial_number, options[:address].to_s, options[:port].to_i, password)
156
+ end
157
+ else #local
158
+
159
+ if !label.nil?
160
+ open_label(label)
161
+ else
162
+ open(serial_number)
163
+ end
164
+ end
165
+
166
+ else #option is not specified
167
+ open(-1)
168
+ end
169
+
170
+
171
+ if block_given?
172
+ if !options.nil? && !options[:timeout].nil? #timeout is given
173
+ wait_for_attachment options[:timeout].to_i
174
+ else
175
+ wait_for_attachment #use default wait time
176
+ end
177
+ yield self
178
+ close
179
+ end
180
+ end
181
+
182
+ private
183
+ # Create a pointer for this Device handle .. must be called before open or anything else.
184
+ # Called automatically when objects are instantiated in block form.
32
185
  def create
33
186
  ptr = ::FFI::MemoryPointer.new(:pointer, 1)
34
187
  self.class::Klass.create(ptr)
@@ -36,177 +189,414 @@ module Phidgets
36
189
  true
37
190
  end
38
191
 
192
+ # Opens a Phidget.
193
+ #
194
+ # @param [Integer] serial_number Serial number of the phidget to open, -1 means any.
195
+ # @return [Boolean] returns true or raises an error
39
196
  def open(serial_number=-1)
40
- Phidgets::FFI::Common.open(@handle, serial_number)
41
- true
197
+ Phidgets::FFI::Common.open(@handle, serial_number)
198
+ true
42
199
  end
43
-
44
- def open_label(str)
45
- Phidgets::FFI::Common.openLabel(@handle, str)
46
- true
200
+
201
+ # Opens a Phidget.
202
+ #
203
+ # @param [Integer] serial_number Serial number of the phidget to open, -1 means any.
204
+ # @return [Boolean] returns true or raises an error
205
+ def open_remote(serial_number=-1, server_id=nil, password=nil)
206
+ Phidgets::FFI::Common.openRemote(@handle, serial_number, server_id, password)
207
+ true
47
208
  end
48
-
209
+
210
+ # Opens a Phidget.
211
+ #
212
+ # @param [Integer] serial_number Serial number of the phidget to open, -1 means any.
213
+ # @return [Boolean] returns true or raises an error
214
+ def open_remote_ip(serial_number=-1, server_address=nil, port=5001, password=nil)
215
+ Phidgets::FFI::Common.openRemoteIP(@handle, serial_number, server_address, port, password)
216
+ true
217
+ end
218
+
219
+ # Opens a Phidget.
220
+ #
221
+ # @param [Integer] serial_number Serial number of the phidget to open, -1 means any.
222
+ # @return [Boolean] returns true or raises an error
223
+ def open_label_remote(label=nil, server_id=nil, password=nil)
224
+ Phidgets::FFI::Common.openLabelRemote(@handle, label, server_id, password)
225
+ true
226
+ end
227
+
228
+ # Opens a Phidget.
229
+ #
230
+ # @param [Integer] serial_number Serial number of the phidget to open, -1 means any.
231
+ # @return [Boolean] returns true or raises an error
232
+ def open_label_remote_ip(label=nil, server_address=nil, port=5001, password=nil)
233
+ Phidgets::FFI::Common.openLabelRemoteIP(@handle, label, server_address, port, password)
234
+
235
+ true
236
+ end
237
+
238
+ # Opens a Phidget.
239
+ #
240
+ # @param [Integer] serial_number Serial number of the phidget to open, -1 means any.
241
+ # @return [Boolean] returns true or raises an error
242
+ def open_label(label=nil)
243
+
244
+ Phidgets::FFI::Common.openLabel(@handle, label)
245
+ true
246
+ end
247
+
248
+ public
249
+
250
+ # Closes and frees a Phidget. This should be called before closing your application or things may not shut down cleanly.
251
+ #
252
+ # @return [Boolean] returns true or raises an error
49
253
  def close
50
- Phidgets::FFI::Common.close(@handle)
254
+ remove_common_event_handlers
255
+ remove_specific_event_handlers
256
+ sleep 0.2
257
+ Phidgets::FFI::Common.close(@handle)
258
+ delete
51
259
  true
52
260
  end
53
261
 
262
+
263
+ private
264
+ # Frees a Phidget handle.
265
+ #
266
+ # @return [Boolean] returns true or raises an error
54
267
  def delete
55
268
  Phidgets::FFI::Common.delete(@handle)
56
269
  true
57
270
  end
58
271
 
59
- def wait_for_attachment(milliseconds=10000)
60
- Phidgets::FFI::Common.waitForAttachment(@handle, milliseconds)
272
+ public
273
+ # This method can be called after the Phidget object has been created. This method blocks indefinitely until the Phidget becomes available. This can be quite some time (forever), if the Phidget is never plugged in. Please note that this call is not needed if you are programming inside a block. If you are programming inside a block, you can specify the wait time in the {Phidgets::Common#initialize} constructor
274
+ #
275
+ # @param [Integer] wait_time Time to wait for the attachment. Specify 0 to wait forever. This is Optional. The default wait time is 1000 milliseconds.
276
+ # @return [Boolean] returns true or raises an error
277
+ def wait_for_attachment(wait_time=1000)
278
+
279
+ Phidgets::FFI::Common.waitForAttachment(@handle, wait_time)
280
+ if attached?
281
+ load_device_attributes
282
+ end
283
+
61
284
  true
62
- end
63
-
64
- def self.name(handle)
65
- ptr = ::FFI::MemoryPointer.new(:string)
66
- Phidgets::FFI::Common.getDeviceName(handle, ptr)
67
- strPtr = ptr.get_pointer(0)
68
- strPtr.null? ? nil : strPtr.read_string
69
- end
70
- def name; Common.name(@handle); end
71
285
 
72
- def self.serial_number(handle)
73
- ptr = ::FFI::MemoryPointer.new(:int)
74
- Phidgets::FFI::Common.getSerialNumber(handle, ptr)
75
- ptr.get_int(0)
76
- end
77
- def serial_number; Common.serial_number(@handle); end
78
-
79
- def self.version(handle)
80
- ptr = ::FFI::MemoryPointer.new(:int)
81
- Phidgets::FFI::Common.getDeviceVersion(handle, ptr)
82
- ptr.get_int(0)
83
- end
84
- def version; Common.version(@handle); end
85
-
86
- def attached?
87
- ptr = ::FFI::MemoryPointer.new(:int)
88
- Phidgets::FFI::Common.getDeviceStatus(@handle, ptr)
89
- (ptr.get_int(0) == 0) ? false : true
90
286
  end
91
287
 
288
+ # Return the attached status of this Phidget, depending on whether the Phidget is physically plugged into the computer, initialized and is ready to use.
289
+ #
290
+ # @return [Boolean] returns the attached status
291
+ def attached?
292
+ Common.device_status(@handle);
293
+ end
294
+
295
+ # Return the detached status of this Phidget
296
+ #
297
+ # @return [Boolean] returns the detached status
92
298
  def detached?
93
299
  !attached?
94
300
  end
95
-
96
- def self.type(handle)
97
- ptr = ::FFI::MemoryPointer.new(:string)
98
- Phidgets::FFI::Common.getDeviceType(handle, ptr)
99
- strPtr = ptr.get_pointer(0)
100
- strPtr.null? ? nil : strPtr.read_string
101
- end
301
+
302
+ # Returns the name of the Phidget
303
+ #
304
+ # @return [String] returns the name of the Phidget, or raises an error
305
+ def name; Common.name(@handle); end
306
+
307
+ # Returns the serial number of the Phidget
308
+ #
309
+ # @return [Integer] returns the serial number of the Phidget, or raises an error
310
+ def serial_number; Common.serial_number(@handle); end
311
+
312
+ # Returns the device version of the Phidget
313
+ #
314
+ # @return [Integer] returns the device version of the Phidget, or raises an error
315
+ def version; Common.version(@handle); end
316
+
317
+ # Returns the device type of the Phidget
318
+ #
319
+ # @return [String] returns the device type of the Phidget, or raises an error
102
320
  def type; Common.type(@handle); end
321
+
322
+ # Returns the device id of the Phidget
323
+ #
324
+ # @return [String] returns the device id of the Phidget, or raises an error
325
+ def id; Common.device_id(@handle); end
326
+ #def device_id; Common.device_id(@handle); end #delete
103
327
 
104
- def self.label(handle)
105
- ptr = ::FFI::MemoryPointer.new(:string)
106
- Phidgets::FFI::Common.getDeviceLabel(handle, ptr)
107
- strPtr = ptr.get_pointer(0)
108
- strPtr.null? ? nil : strPtr.read_string
109
- end
328
+ # Returns the device class of the Phidget
329
+ #
330
+ # @return [String] returns the device class of the Phidget, or raises an error
331
+ def device_class; Common.device_class(@handle); end
332
+
333
+ # Returns the label of the Phidget
334
+ #
335
+ # @return [String] returns the label of the Phidget, or raises an error
110
336
  def label; Common.label(@handle); end
111
337
 
112
- def label=(new_label)
113
- Phidgets::FFI::Common.setDeviceLabel(@handle, new_label)
114
- new_label
115
- end
116
-
117
- def server_id
118
- ptr = ::FFI::MemoryPointer.new(:string)
119
- Phidgets::FFI::Common.getServerID(@handle, ptr)
120
- strPtr = ptr.get_pointer(0)
121
- strPtr.null? ? nil : strPtr.read_string
122
- end
123
-
124
- def server_address
125
- str_ptr, int_ptr = ::FFI::MemoryPointer.new(:string), ::FFI::MemoryPointer.new(:int)
126
- Phidgets::FFI::Common.getServerID(@handle, str_ptr, int_ptr)
127
- strPtr = str_ptr.get_pointer(0)
128
- address = (strPtr.null? ? nil : strPtr.read_string)
129
- port = int_ptr.get_int(0)
130
- [address, port]
131
- end
132
-
133
- def server_status
134
- ptr = ::FFI::MemoryPointer.new(:int)
135
- Phidgets::FFI::Common.getServerStatus(@handle, ptr)
136
- ptr.get_int(0)
137
- end
138
-
139
- def self.device_id(handle)
140
- ptr = ::FFI::MemoryPointer.new(:int)
141
- Phidgets::FFI::Common.getDeviceID(handle, ptr)
142
- Phidgets::FFI::DeviceID[ptr.get_int(0)]
338
+ # @return [String] returns the server id for a Phidget opened over the network, or raises an error
339
+ def server_id; Common.server_id(@handle); end
340
+
341
+ # @return [String, Integer] returns the Phidget WebService server address and port for a Phidget opened over the network, or raises an error
342
+ def server_address; Common.server_address(@handle); end
343
+
344
+ # @return [Boolean] returns the server status of this Phidget or raises an error
345
+ def attached_to_server?
346
+ Common.server_status(@handle)
143
347
  end
144
- def device_id; Common.device_id(@handle); end
145
348
 
146
- def self.device_class(handle)
147
- ptr = ::FFI::MemoryPointer.new(:int)
148
- Phidgets::FFI::Common.getDeviceClass(handle, ptr)
149
- Phidgets::FFI::DeviceClass[ptr.get_int(0)]
349
+ # @return [Boolean] returns the detached to server status of this Phidget or raises an error
350
+ def detached_to_server?
351
+ !attached_to_server?
150
352
  end
151
- def device_class; Common.device_class(@handle); end
152
-
353
+
354
+ # Sets an attach handler callback function. This is called when the Phidget is plugged into the system, and is ready for use.
355
+ #
356
+ # @param [String] obj Object to pass to the callback function. This is optional.
357
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
358
+ # @example
359
+ # ifkit.on_attach do |device, obj|
360
+ # puts "InterfaceKit attached #{device.attributes.inspect}"
361
+ # end
362
+ # 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.
363
+ # @return [Boolean] returns true or raises an error
153
364
  def on_attach(obj=nil, &block)
154
- @on_attach_obj = obj
365
+ @on_attach_obj = obj
155
366
  @on_attach = Proc.new { |handle, obj_ptr|
367
+ load_device_attributes
156
368
  yield self, object_for(obj_ptr)
157
- }
369
+ }
158
370
  Phidgets::FFI::Common.set_OnAttach_Handler(@handle, @on_attach, pointer_for(obj))
371
+ true
159
372
  end
160
373
 
374
+ # Sets a detach handler callback function. This is called when the Phidget is physically detached from the system, and is no longer available.
375
+ #
376
+ # @param [String] obj Object to pass to the callback function. This is optional.
377
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
378
+ # @example
379
+ # ifkit.on_detach do |device, obj|
380
+ # puts "InterfaceKit detached #{device.attributes.inspect}"
381
+ # end
382
+ # 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.
383
+ # @return [Boolean] returns true or raises an error
161
384
  def on_detach(obj=nil, &block)
162
385
  @on_detach_obj = obj
163
386
  @on_detach = Proc.new { |handle, obj_ptr|
164
387
  yield self, object_for(obj_ptr)
165
388
  }
166
389
  Phidgets::FFI::Common.set_OnDetach_Handler(@handle, @on_detach, pointer_for(obj))
390
+ true
167
391
  end
168
392
 
393
+ # Sets an error handler callback function. This is called when an asynchronous error occurs. This is generally used for network errors, and device hardware error messages.
394
+ #
395
+ # @param [String] obj Object to pass to the callback function. This is optional.
396
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
397
+ # @example
398
+ # ifkit.on_error do |device, obj, code, description|
399
+ # puts "Error - code #{code}, description #{description}"
400
+ # end
401
+ # 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.
402
+ # @return [Boolean] returns true or raises an error
403
+ def on_error(obj=nil, &block)
404
+ @on_error_obj = obj
405
+ @on_error = Proc.new { |handle, obj_ptr, code, description|
406
+ yield self, object_for(obj_ptr), code, description
407
+ }
408
+ Phidgets::FFI::Common.set_OnError_Handler(@handle, @on_error, pointer_for(obj))
409
+ true
410
+ end
411
+
412
+ # Sets a server connect handler callback function. This is called for network opened Phidgets when a connection to the PhidgetWebService has been established.
413
+ #
414
+ # @param [String] obj Object to pass to the callback function. This is optional.
415
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
416
+ # @example
417
+ # ifkit.on_server_connect do |device, obj|
418
+ # puts "Server connect #{device.attributes.inspect}"
419
+ # end
420
+ # 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.
421
+ # @return [Boolean] returns true or raises an error
169
422
  def on_server_connect(obj=nil, &block)
170
423
  @on_server_connect_obj = obj
171
424
  @on_server_connect = Proc.new { |handle, obj_ptr|
172
425
  yield self, object_for(obj_ptr)
173
426
  }
174
427
  Phidgets::FFI::Common.set_OnServerConnect_Handler(@handle, @on_server_connect, pointer_for(obj))
428
+ true
175
429
  end
176
430
 
431
+ # Sets a server disconnect handler callback function. This is called for network opened Phidgets when a connectiotn to the PhidgetWebService has been broken - either by calling close, or by network trouble, etc....
432
+ #
433
+ # @param [String] obj Object to pass to the callback function. This is optional.
434
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
435
+ # @example
436
+ # ifkit.on_server_disonnect do |device, obj|
437
+ # puts "Server disonnect #{device.attributes.inspect}"
438
+ # end
439
+ # 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.
440
+ # @return [Boolean] returns true or raises an error
177
441
  def on_server_disconnect(obj=nil, &block)
178
442
  @on_server_disconnect_obj = obj
179
443
  @on_server_disconnect = Proc.new { |handle, obj_ptr|
180
444
  yield self, object_for(obj_ptr)
181
445
  }
182
446
  Phidgets::FFI::Common.set_OnServerDisconnect_Handler(@handle, @on_server_disconnect, pointer_for(obj))
447
+ true
183
448
  end
184
449
 
185
- def on_error(obj=nil, &block)
186
- @on_error_obj = obj
187
- @on_error = Proc.new { |handle, obj_ptr, code, description|
188
- yield self, object_for(obj_ptr), code, description
189
- }
190
- Phidgets::FFI::Common.set_OnError_Handler(@handle, @on_error, pointer_for(obj))
191
- end
192
-
193
- def on_sleep(obj=nil, &block)
450
+ # Sets a sleep handler callback function. This is called when the MacOS X is entering sleep mode.
451
+ #
452
+ # @param [String] obj Object to pass to the callback function. This is optional.
453
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
454
+ # @example
455
+ # ifkit.on_sleep do |obj|
456
+ # puts "System sleeping"
457
+ # end
458
+ # 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.
459
+ # @return [Boolean] returns true or raises an error
460
+ # @note Used only in Mac OS X
461
+ def on_sleep(obj=nil, &block)
194
462
  @on_sleep_obj = obj
195
463
  @on_sleep = Proc.new { |obj_ptr|
196
464
  yield object_for(obj_ptr)
197
465
  }
198
466
  Phidgets::FFI::Common.set_OnWillSleep_Handler(@on_sleep, pointer_for(obj))
467
+ true
468
+
199
469
  end
200
-
470
+
471
+ # Sets a wake callback function. This is called when the MacOS X is waking up from sleep mode.
472
+ #
473
+ # @param [String] obj Object to pass to the callback function. This is optional.
474
+ # @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
475
+ # @example
476
+ # ifkit.on_wake do |obj|
477
+ # puts "System waking up"
478
+ # end
479
+ # 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.
480
+ # @return [Boolean] returns true or raises an error
481
+ # @note Used only in Mac OS X
201
482
  def on_wake(obj=nil, &block)
202
483
  @on_wake_obj = obj
203
484
  @on_wake = Proc.new { |obj_ptr|
204
485
  yield object_for(obj_ptr)
205
486
  }
206
487
  Phidgets::FFI::Common.set_OnWakeup_Handler(@on_wake, pointer_for(obj))
488
+ true
489
+ end
490
+
491
+ attr_reader :attributes
492
+
493
+ private
494
+
495
+ def self.device_status(handle)
496
+ ptr = ::FFI::MemoryPointer.new(:int)
497
+ Phidgets::FFI::Common.getDeviceStatus(handle, ptr)
498
+ (ptr.get_int(0) == 0) ? false : true
499
+ end
500
+
501
+ # Gets the specific name of a Phidget.
502
+ #
503
+ # @param [FFI::Pointer] pointer An attached phidget handle
504
+ # @return [String] returns the device name
505
+ def self.name(handle)
506
+ ptr = ::FFI::MemoryPointer.new(:string)
507
+ Phidgets::FFI::Common.getDeviceName(handle, ptr)
508
+ strPtr = ptr.get_pointer(0)
509
+ strPtr.null? ? nil : strPtr.read_string
207
510
  end
208
-
209
- def self.attributes(handle)
511
+
512
+ # Gets the serial number of a Phidget.
513
+ #
514
+ # @param [FFI::Pointer] pointer An attached phidget handle
515
+ # @return [Integer] returns the serial number of the phidget
516
+ def self.serial_number(handle)
517
+ ptr = ::FFI::MemoryPointer.new(:int)
518
+ Phidgets::FFI::Common.getSerialNumber(handle, ptr)
519
+ ptr.get_int(0)
520
+ end
521
+
522
+ # Gets the version of a Phidget.
523
+ #
524
+ # @param [FFI::Pointer] pointer An attached phidget handle
525
+ # @return [Integer] returns the version of a phidget
526
+ def self.version(handle)
527
+ ptr = ::FFI::MemoryPointer.new(:int)
528
+ Phidgets::FFI::Common.getDeviceVersion(handle, ptr)
529
+ ptr.get_int(0)
530
+ end
531
+
532
+ # Gets the type (class) of a Phidget.
533
+ #
534
+ # @param [FFI::Pointer] pointer An attached phidget handle
535
+ # @return [String] returns the type of a phidget
536
+ def self.type(handle)
537
+ ptr = ::FFI::MemoryPointer.new(:string)
538
+ Phidgets::FFI::Common.getDeviceType(handle, ptr)
539
+ strPtr = ptr.get_pointer(0)
540
+ strPtr.null? ? nil : strPtr.read_string
541
+ end
542
+
543
+ # Gets the label of a Phidget.
544
+ #
545
+ # @param [FFI::Pointer] pointer An attached phidget handle
546
+ # @return [String] returns the label of a phidget
547
+ def self.label(handle)
548
+ ptr = ::FFI::MemoryPointer.new(:string)
549
+ Phidgets::FFI::Common.getDeviceLabel(handle, ptr)
550
+ strPtr = ptr.get_pointer(0)
551
+ strPtr.null? ? nil : strPtr.read_string
552
+ end
553
+
554
+ public
555
+ # Sets the label of a Phidget. Note that this is not supported on very old Phidgets, and not yet supported in Windows. The label can be up to ten characters, and is stored in the Flash memory of newer Phidgets. This label can be set programatically, and is non-volatile - so it is remembered even if the Phidget is unplugged.
556
+ #
557
+ # @param [String] new_label New label
558
+ # @return [String] returns the label, or raises an error
559
+ def label=(new_label)
560
+ Phidgets::FFI::Common.setDevicelabel(@handle, new_label)
561
+ new_label
562
+ end
563
+
564
+ def self.device_id(handle)
565
+ ptr = ::FFI::MemoryPointer.new(:int)
566
+ Phidgets::FFI::Common.getDeviceID(handle, ptr)
567
+ Phidgets::FFI::DeviceID[ptr.get_int(0)]
568
+ end
569
+
570
+ def self.device_class(handle)
571
+ ptr = ::FFI::MemoryPointer.new(:int)
572
+ Phidgets::FFI::Common.getDeviceClass(handle, ptr)
573
+ Phidgets::FFI::DeviceClass[ptr.get_int(0)]
574
+ end
575
+
576
+ def self.server_id(handle)
577
+ ptr = ::FFI::MemoryPointer.new(:string)
578
+ Phidgets::FFI::Common.getServerID(handle, ptr)
579
+ strPtr = ptr.get_pointer(0)
580
+ strPtr.null? ? nil : strPtr.read_string
581
+ end
582
+
583
+ def self.server_address(handle)
584
+ str_ptr, int_ptr = ::FFI::MemoryPointer.new(:string), ::FFI::MemoryPointer.new(:int)
585
+ Phidgets::FFI::Common.getServerAddress(handle, str_ptr, int_ptr)
586
+ strPtr = str_ptr.get_pointer(0)
587
+ address = (strPtr.null? ? nil : strPtr.read_string)
588
+ port = int_ptr.get_int(0)
589
+ [address, port]
590
+ end
591
+
592
+ def self.server_status(handle)
593
+ ptr = ::FFI::MemoryPointer.new(:int)
594
+ Phidgets::FFI::Common.getServerStatus(handle, ptr)
595
+ #(ptr.get_int(0) == 0) ? false : true
596
+ Phidgets::FFI::ServerStatus[ptr.get_int(0)]
597
+ end
598
+
599
+ def self.attributes(handle)
210
600
  {
211
601
  :type => type(handle),
212
602
  :name => name(handle),
@@ -217,6 +607,19 @@ module Phidgets
217
607
  :device_id => device_id(handle),
218
608
  }
219
609
  end
610
+
611
+ public
612
+
613
+ #The attributes of a Phidget
220
614
  def attributes; Common.attributes(@handle); end
615
+
616
+ private
617
+ def remove_common_event_handlers
618
+ Phidgets::FFI::Common.set_OnDetach_Handler(@handle, nil, nil)
619
+ Phidgets::FFI::Common.set_OnAttach_Handler(@handle, nil, nil)
620
+ Phidgets::FFI::Common.set_OnServerConnect_Handler(@handle, nil, nil)
621
+ Phidgets::FFI::Common.set_OnServerDisconnect_Handler(@handle, nil, nil)
622
+ Phidgets::FFI::Common.set_OnError_Handler(@handle, nil, nil)
623
+ end
221
624
  end
222
- end
625
+ end