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,157 @@
1
+ module Phidgets
2
+
3
+ # This class represents a PhidgetTemperatureSensor.
4
+ class TemperatureSensor
5
+
6
+ Klass = Phidgets::FFI::CPhidgetTemperatureSensor
7
+ include Phidgets::Common
8
+
9
+ # Collection of temperature sensor sensors
10
+ # @return [TemperatureSensorSensors]
11
+ attr_reader :thermocouples
12
+
13
+ attr_reader :attributes
14
+
15
+ # The attributes of a PhidgetTemperatureSensor
16
+ def attributes
17
+ super.merge({
18
+ :thermocouples => thermocouples.size,
19
+ })
20
+ end
21
+
22
+ # Sets a temperature change handler callback function. This is called when when the temperature has changed atleast by the sensitivity value that has been set.
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
+ # temp.on_temperature_change do |device, input, temperature, obj|
28
+ # puts "Input #{input.index}'s temperature changed to #{temperature}"
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_temperature_change(obj=nil, &block)
33
+ @on_temperature_change_obj = obj
34
+ @on_temperature_change = Proc.new { |device, obj_ptr, index, temperature|
35
+ yield self, @thermocouples[index], temperature, object_for(obj_ptr)
36
+ }
37
+ Klass.set_OnTemperatureChange_Handler(@handle, @on_temperature_change, pointer_for(obj))
38
+ end
39
+
40
+
41
+ # This class represents a thermocouple sensor attached for a PhidgetTemperatureSensor. All the properties of an thermocouple sensor are stored and modified in this class.
42
+ class TemperatureSensorSensors
43
+ Klass = Phidgets::FFI::CPhidgetTemperatureSensor
44
+
45
+ private
46
+ def initialize(handle, index)
47
+ @handle, @index = handle, index.to_i
48
+ end
49
+
50
+ public
51
+ # Displays data for the thermocouple sensor.
52
+ def inspect
53
+ "#<#{self.class} @index=#{index}, @sensitivity=#{sensitivity}, @temperature_min=#{temperature_min}, @temperature_max=#{temperature_max}, @type=#{type}>"
54
+ end
55
+
56
+ # @return [Integer] returns index of the thermocouple sensor, or raises an error.
57
+ def index
58
+ @index
59
+ end
60
+
61
+ # @return [Float] returns the potential of the thermocouple sensor, in mV, or raises an error.
62
+ def potential
63
+ ptr = ::FFI::MemoryPointer.new(:double)
64
+ Klass.getPotential(@handle, @index, ptr)
65
+ ptr.get_double(0)
66
+ end
67
+
68
+ # @return [Float] returns the smallest potential that the thermocouple sensor can return, in mV, or raises an error.
69
+ def potential_min
70
+ ptr = ::FFI::MemoryPointer.new(:double)
71
+ Klass.getPotentialMin(@handle, @index, ptr)
72
+ ptr.get_double(0)
73
+ end
74
+
75
+ # @return [Float] returns the largest potential that the thermocouple sensor can return, in mV, or raises an error.
76
+ def potential_max
77
+ ptr = ::FFI::MemoryPointer.new(:double)
78
+ Klass.getPotentialMax(@handle, @index, ptr)
79
+ ptr.get_double(0)
80
+ end
81
+
82
+ # @return [Float] returns the temperature change trigger sensitivity for the thermocouple sensor, or raises an error.
83
+ def sensitivity
84
+ ptr = ::FFI::MemoryPointer.new(:double)
85
+ Klass.getTemperatureChangeTrigger(@handle, @index, ptr)
86
+ ptr.get_double(0)
87
+ end
88
+
89
+ # Sets the temperature change trigger sensitivity of the thermocouple sensor, or raises an error.
90
+ # @param [Float] new_senstivity new sensitivity
91
+ # @return [Float] returns the temperature change trigger sensitivity of the thermocouple sensor, or raises an error.
92
+ def sensitivity=(new_sensitivity)
93
+ Klass.setTemperatureChangeTrigger(@handle, @index, new_sensitivity.to_f)
94
+ new_sensitivity
95
+ end
96
+
97
+ # @return [Float] returns the temperature of the thermocouple sensor, in degrees celcius, or raises an error.
98
+ def temperature
99
+ ptr = ::FFI::MemoryPointer.new(:double)
100
+ Klass.getTemperature(@handle, @index, ptr)
101
+ ptr.get_double(0)
102
+ end
103
+
104
+ # @return [Float] returns the minimum temperature value that the thermocouple sensor can return, in degrees celcius, or raises an error.
105
+ def temperature_min
106
+ ptr = ::FFI::MemoryPointer.new(:double)
107
+ Klass.getTemperatureMin(@handle, @index, ptr)
108
+ ptr.get_double(0)
109
+ end
110
+
111
+ # @return [Float] returns the maximum temperature value that the thermocouple sensor can return, in degrees celcius, or raises an error.
112
+ def temperature_max
113
+ ptr = ::FFI::MemoryPointer.new(:double)
114
+ Klass.getTemperatureMax(@handle, @index, ptr)
115
+ ptr.get_double(0)
116
+ end
117
+
118
+ # @return [Phidgets::FFI::TemperatureSensorThermocoupleTypes] returns the thermocouple type of the thermocouple sensor, or raises an error.
119
+ def type
120
+ ptr = ::FFI::MemoryPointer.new(:int)
121
+ Klass.getThermocoupleType(@handle, @index, ptr)
122
+ Phidgets::FFI::TemperatureSensorThermocoupleTypes[ptr.get_int(0)]
123
+ end
124
+
125
+ # Sets the thermocouple type of the thermocouple sensor, or raises an error.
126
+ # @param [Phidgets::FFI::TemperatureSensorThermocoupleTypes] new_type new type
127
+ # @return [Phidgets::FFI::TemperatureSensorThermocoupleTypes] returns the thermocouple type of the thermocouple sensor, or raises an error.
128
+ def type=(new_type)
129
+ ptr = ::FFI::MemoryPointer.new(:int)
130
+ Klass.setThermocoupleType(@handle, @index, Phidgets::FFI::TemperatureSensorThermocoupleTypes[new_type])
131
+ new_type
132
+ end
133
+
134
+ end #TemperatureSensorSensors
135
+
136
+ private
137
+
138
+ def load_device_attributes
139
+ load_thermocouples
140
+ end
141
+
142
+ def load_thermocouples
143
+ ptr = ::FFI::MemoryPointer.new(:int)
144
+ Klass.getTemperatureInputCount(@handle, ptr)
145
+
146
+ @thermocouples = []
147
+ ptr.get_int(0).times do |i|
148
+ @thermocouples << TemperatureSensorSensors.new(@handle, i)
149
+ end
150
+ end
151
+
152
+ def remove_specific_event_handlers
153
+ Klass.set_OnTemperatureChange_Handler(@handle, nil, nil)
154
+ end
155
+ end
156
+
157
+ end
@@ -0,0 +1,298 @@
1
+ module Phidgets
2
+
3
+ # This class represents a PhidgetTextLCD.
4
+ class TextLCD
5
+
6
+ Klass = Phidgets::FFI::CPhidgetTextLCD
7
+ include Phidgets::Common
8
+
9
+ # Collection of screens
10
+ # @return [TextLCDScreens]
11
+ attr_reader :screens
12
+
13
+ # Collection of custom characters that is defined for the PhidgetTextLCD
14
+ # @return [TextLCDCustomCharacter]
15
+ attr_reader :custom_characters
16
+
17
+ attr_reader :attributes
18
+
19
+ # The attributes of a PhidgetTextLCD
20
+ def attributes
21
+ super.merge({
22
+ :screens => screens.size
23
+ })
24
+ end
25
+
26
+ # This class represents a screen a PhidgetTextLCD. All the properties of a screen are stored and modified in this class.
27
+ class TextLCDScreens
28
+ Klass = Phidgets::FFI::CPhidgetTextLCD
29
+
30
+ private
31
+ def initialize(handle, index)
32
+ @handle, @index = handle, index.to_i
33
+ load_rows(0)
34
+ end
35
+
36
+ public
37
+ # Displays data for the servo motor.
38
+ def inspect
39
+ "#<#{self.class} @index=#{index}>"
40
+ end
41
+
42
+ # Collection of rows of text display in a PhidgetTextLCD
43
+ # @return [TextLCDScreensRows]
44
+ attr_reader :rows
45
+
46
+ # @return [Integer] returns index of the screen, or raises an error.
47
+ def index
48
+ @index
49
+ end
50
+
51
+ # @return [Boolean] returns the back light state of a screen, or raises an error.
52
+ def back_light
53
+ try_set_screen
54
+ ptr = ::FFI::MemoryPointer.new(:int)
55
+ Klass.getBacklight(@handle, ptr)
56
+ (ptr.get_int(0) == 0) ? false : true
57
+ end
58
+
59
+ # Sets the back light state of a screen, or raises an error.
60
+ # @param [Boolean] new_state new state
61
+ # @return [Boolean] returns the back light state of a screen, or raises an error.
62
+ def back_light=(new_state)
63
+ try_set_screen
64
+ tmp = new_state ? 1 : 0
65
+ Klass.setBacklight(@handle, tmp)
66
+ new_state
67
+ end
68
+
69
+ # @return [Integer] returns the brightness value of a screen, or raises an error. Not all PhidgetTextLCDs support brightness.
70
+ def brightness
71
+ try_set_screen
72
+ ptr = ::FFI::MemoryPointer.new(:int)
73
+ Klass.getBrightness(@handle, ptr)
74
+ ptr.get_int(0)
75
+ end
76
+
77
+ # Sets the brightness value of a screen, or raises an error. Not all PhidgetTextLCDs support brightness.
78
+ # @param [Integer] new_brightness new brightness(0-255)
79
+ # @return [Integer] returns the brightness value of a screen, or raises an error.
80
+ def brightness=(new_brightness)
81
+ try_set_screen
82
+ Klass.setBrightness(@handle, new_brightness.to_i)
83
+ new_brightness.to_i
84
+ end
85
+
86
+ # @return [Integer] returns the contrast of a screen, or raises an error.
87
+ def contrast
88
+ try_set_screen
89
+ ptr = ::FFI::MemoryPointer.new(:int)
90
+ Klass.getContrast(@handle, ptr)
91
+ ptr.get_int(0)
92
+ end
93
+
94
+ # Sets the contrast value of a screen, or raises an error.
95
+ # @param [Integer] new_contrast new contrast(0-255)
96
+ # @return [Integer] returns the contrast value of a screen, or raises an error.
97
+ def contrast=(new_contrast)
98
+ try_set_screen
99
+ Klass.setContrast(@handle, new_contrast.to_i)
100
+ new_contrast.to_i
101
+ end
102
+
103
+ # The cursor is an underscore which appears directly to the right of the last entered character on the display.
104
+ # @return [Boolean] returns the cursor state of a screen, or raises an error.
105
+ def cursor
106
+ try_set_screen
107
+ ptr = ::FFI::MemoryPointer.new(:int)
108
+ Klass.getCursonOn(@handle, ptr)
109
+ (ptr.get_int(0) == 0) ? false : true
110
+ end
111
+
112
+ # Sets the cursor state of a screen, or raises an error.
113
+ # @param [Boolean] new_state new state
114
+ # @return [Boolean] returns the cursor state of a screen, or raises an error.
115
+ def cursor=(new_state)
116
+ try_set_screen
117
+ tmp = new_state ? 1 : 0
118
+ Klass.setCursorOn(@handle, tmp)
119
+ new_state
120
+ end
121
+
122
+ # The cursor blink is a flashing box which appears directly to the right of the last entered character on the display, in the same apot as the cursor if it is enabled.
123
+ # @return [Boolean] returns the cursor blink state of a screen, or raises an error.
124
+ def cursor_blink
125
+ try_set_screen
126
+ ptr = ::FFI::MemoryPointer.new(:int)
127
+ Klass.getCursonBlink(@handle, ptr)
128
+ (ptr.get_int(0) == 0) ? false : true
129
+ end
130
+
131
+ # Sets the cursor blink state of a screen, or raises an error.
132
+ # @param [Boolean] new_state new state
133
+ # @return [Boolean] returns the cursor blink state of a screen, or raises an error.
134
+ def cursor_blink=(new_state)
135
+ try_set_screen
136
+ tmp = new_state ? 1 : 0
137
+ Klass.setCursorBlink(@handle, tmp)
138
+ new_state
139
+ end
140
+
141
+ # @return [Phidgets::FFI::TextLCDScreenSizes] returns the screen size of a screen, or raises an error. Not all PhidgetTextLCDs support screen_size.
142
+ def screen_size
143
+ try_set_screen
144
+ ptr = ::FFI::MemoryPointer.new(:int)
145
+ Klass.getScreenSize(@handle, ptr)
146
+ Phidgets::FFI::TextLCDScreenSizes[ptr.get_int(0)]
147
+ end
148
+
149
+ # Sets the screen size of a screen, or raises an error. Not all PhidgetTextLCDs support screen_size.
150
+ # @param [Phidgets::FFI::TextLCDScreenSizes] new_screen_size new screen size
151
+ # @return [Phidgets::FFI::TextLCDScreenSizes] returns the screen size of a screen, or raises an error.
152
+ def screen_size=(new_screen_size)
153
+ try_set_screen
154
+ Klass.setScreenSize(@handle, Phidgets::FFI::TextLCDScreenSizes[new_screen_size])
155
+
156
+ load_rows(@index) #readjust screen rows
157
+ new_screen_size
158
+ end
159
+
160
+ # Initializes a screen, or raises an error. Not all PhidgetTextLCDs support initialize
161
+ # @return [Boolean] returns true if the screen was successfully initialize, or raises an error.
162
+ def initialize_screen
163
+ try_set_screen
164
+ Klass.initialize(@handle)
165
+ true
166
+ end
167
+
168
+ # This class represents a row on a PhidgetTextLCD screen. All the properties of a row are stored and modified in this class.
169
+ class TextLCDScreensRows
170
+ Klass = Phidgets::FFI::CPhidgetTextLCD
171
+
172
+ private
173
+ def initialize(handle, row, screen_index)
174
+ @handle, @row, @screen_index = handle, row.to_i, screen_index.to_i
175
+ end
176
+
177
+ public
178
+
179
+ # Sets the display string of a row, or raises an error.
180
+ #
181
+ # example
182
+ # lcd.screens[0].rows[1].display_string = "\x48" #hex for H in ASCII
183
+ # lcd.screens[0].rows[2].display_string = "\147" #octal for g in ASCII
184
+ # @param [String] new_string string
185
+ # @return [String] returns the display string, or raises an error.
186
+ def display_string=(new_display_string)
187
+ if (Common.device_id(@handle).to_s == "textlcd_adapter")
188
+ Klass.setScreen(@handle, @screen_index)
189
+ end
190
+ Klass.setDisplayString(@handle, @row, new_display_string)
191
+ new_display_string
192
+ end
193
+
194
+ # Sets a single character for a specific column of a row, or raises an error.
195
+ #
196
+ # example
197
+ # lcd.screens[0].rows[1].display_character(4, 0x21) #hex for ! in ASCII
198
+ # lcd.screens[0].rows[1].display_character(5, 067) #octal for 7 in ASCII
199
+ # @param [Integer] column column number
200
+ # @param [Integer] new_character new character(in hexadecimal, octal, etc..)
201
+ # @return [Integer] returns the character, or raises an error.
202
+ def display_character(column, new_display_character)
203
+ if (Common.device_id(@handle).to_s == "textlcd_adapter")
204
+ Klass.setScreen(@handle, @screen_index)
205
+ end
206
+ Klass.setDisplayCharacter(@handle, @row, column, new_display_character)
207
+ new_display_character
208
+ end
209
+
210
+ # @return [Integer] returns the maximum length of a row, or raises an error.
211
+ def maximum_length
212
+ ptr = ::FFI::MemoryPointer.new(:int)
213
+ Klass.getColumnCount(@handle, ptr)
214
+ ptr.get_int(0)
215
+ end
216
+ end #TextLCDScreensRows
217
+
218
+ private
219
+ def load_rows(index)
220
+ ptr = ::FFI::MemoryPointer.new(:int)
221
+ Klass.getRowCount(@handle, ptr)
222
+ @rows = []
223
+ ptr.get_int(0).times do |i|
224
+ @rows << TextLCDScreensRows.new(@handle, i, index)
225
+ end
226
+ end
227
+
228
+ #Attemps to set the screen for a PhidgetTextLCD Adapter
229
+ def try_set_screen
230
+ if (Common.device_id(@handle).to_s == "textlcd_adapter")
231
+ Klass.setScreen(@handle, @index)
232
+ end
233
+ end
234
+
235
+ end #TextLCDScreens
236
+
237
+
238
+ # This class represents a custom character that are defined by a PhidgetTextLCD. All the properties of a custom character are stored and modified in this class.
239
+ class TextLCDCustomCharacter
240
+ Klass = Phidgets::FFI::CPhidgetTextLCD
241
+
242
+ private
243
+ def initialize(handle, index)
244
+ @handle, @index = handle, index.to_i
245
+ end
246
+
247
+ public
248
+
249
+ # Sets a custom character, or raises an error. The custom character is defined by two integers. See the product manual for more information.
250
+ # example
251
+ # lcd.custom_characters[0].set_custom_character(949247, 536)
252
+ # @param [Integer] first_value first value given by the customchar utility
253
+ # @param [Integer] second_value second value given by teh customchar utility
254
+ # @return [Array<Integer>[2]] returns the custom character, or raises an error.
255
+ def set_custom_character(var1, var2)
256
+ Klass.setCustomCharacter(@handle, @index + 8, var1, var2)
257
+ [var1, var2]
258
+ end
259
+
260
+ # Returns the character code associated with the custom character, or raises an error.
261
+ #
262
+ # example
263
+ # lcd.screens[0].rows[0].display_string = "#{lcd.custom_characters[0].string_code}"
264
+ # @return [String] returns the character code associated with the custom character, or raises an error.
265
+ def string_code
266
+ eval('"' + "\\1#{@index}" + '"') #ie, returns \10 - \17
267
+ end
268
+
269
+ end #TextLCDCustomCharacter
270
+
271
+ private
272
+
273
+ def load_device_attributes
274
+ load_screens
275
+ load_custom_characters
276
+ end
277
+
278
+ def load_screens
279
+ ptr = ::FFI::MemoryPointer.new(:int)
280
+ Klass.getScreenCount(@handle, ptr)
281
+ @screens = []
282
+ ptr.get_int(0).times do |i|
283
+ @screens << TextLCDScreens.new(@handle, i)
284
+ end
285
+ end
286
+
287
+ def load_custom_characters
288
+ @custom_characters = []
289
+ 8.times do |i| #8 custom variables
290
+ @custom_characters << TextLCDCustomCharacter.new(@handle, i)
291
+ end
292
+ end
293
+
294
+ def remove_specific_event_handlers
295
+ end
296
+ end
297
+
298
+ end