phidgets 0.0.5 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. data/History.txt +10 -2
  2. data/README.rdoc +41 -18
  3. data/Rakefile +31 -20
  4. data/bin/phidget +29 -44
  5. data/ext/phidgets/extconf.rb +14 -0
  6. data/ext/phidgets/phidgets.c +272 -0
  7. data/ext/phidgets/phidgets.h +82 -0
  8. data/ext/phidgets/phidgets_accelerometer.c +165 -0
  9. data/ext/phidgets/phidgets_advanced_servo.c +567 -0
  10. data/ext/phidgets/phidgets_analog.c +139 -0
  11. data/ext/phidgets/phidgets_bridge.c +263 -0
  12. data/ext/phidgets/phidgets_common.c +454 -0
  13. data/ext/phidgets/phidgets_dictionary.c +279 -0
  14. data/ext/phidgets/phidgets_encoder.c +249 -0
  15. data/ext/phidgets/phidgets_frequency_counter.c +241 -0
  16. data/ext/phidgets/phidgets_gps.c +235 -0
  17. data/ext/phidgets/phidgets_interface_kit.c +340 -0
  18. data/ext/phidgets/phidgets_ir.c +251 -0
  19. data/ext/phidgets/phidgets_led.c +178 -0
  20. data/ext/phidgets/phidgets_manager.c +366 -0
  21. data/ext/phidgets/phidgets_motor_control.c +642 -0
  22. data/ext/phidgets/phidgets_phsensor.c +208 -0
  23. data/ext/phidgets/phidgets_rfid.c +281 -0
  24. data/ext/phidgets/phidgets_servo.c +276 -0
  25. data/ext/phidgets/phidgets_spatial.c +369 -0
  26. data/ext/phidgets/phidgets_stepper.c +560 -0
  27. data/ext/phidgets/phidgets_temp_sensor.c +295 -0
  28. data/ext/phidgets/phidgets_text_lcd.c +381 -0
  29. data/ext/phidgets/phidgets_text_led.c +107 -0
  30. data/ext/phidgets/phidgets_weight_sensor.c +113 -0
  31. data/lib/phidgets/accelerometer.rb +25 -0
  32. data/lib/phidgets/advanced_servo.rb +49 -0
  33. data/lib/phidgets/analog.rb +8 -0
  34. data/lib/phidgets/bridge.rb +25 -0
  35. data/lib/phidgets/common.rb +75 -190
  36. data/lib/phidgets/dictionary.rb +53 -0
  37. data/lib/phidgets/encoder.rb +49 -0
  38. data/lib/phidgets/frequency_counter.rb +25 -0
  39. data/lib/phidgets/gps.rb +37 -0
  40. data/lib/phidgets/interfacekit.rb +38 -128
  41. data/lib/phidgets/ir.rb +50 -0
  42. data/lib/phidgets/led.rb +8 -0
  43. data/lib/phidgets/manager.rb +67 -119
  44. data/lib/phidgets/motor_control.rb +110 -0
  45. data/lib/phidgets/ph_sensor.rb +25 -0
  46. data/lib/phidgets/rfid.rb +38 -111
  47. data/lib/phidgets/servo.rb +12 -95
  48. data/lib/phidgets/spatial.rb +25 -0
  49. data/lib/phidgets/stepper.rb +61 -0
  50. data/lib/phidgets/temperature_sensor.rb +25 -0
  51. data/lib/phidgets/text_lcd.rb +8 -0
  52. data/lib/phidgets/text_led.rb +8 -0
  53. data/lib/phidgets/weight_sensor.rb +25 -0
  54. data/lib/phidgets.rb +22 -3
  55. data/phidgets.gemspec +42 -0
  56. data/test/test_accelerometer.rb +47 -0
  57. data/test/test_advanced_servo.rb +152 -0
  58. data/test/test_analog.rb +45 -0
  59. data/test/test_bridge.rb +77 -0
  60. data/test/test_common.rb +167 -0
  61. data/test/test_dictionary.rb +82 -0
  62. data/test/test_encoder.rb +67 -0
  63. data/test/test_frequency_counter.rb +67 -0
  64. data/test/test_gps.rb +67 -0
  65. data/test/test_helper.rb +1 -0
  66. data/test/test_interfacekit.rb +86 -182
  67. data/test/test_ir.rb +57 -0
  68. data/test/test_led.rb +55 -0
  69. data/test/test_manager.rb +94 -0
  70. data/test/test_motor_control.rb +172 -0
  71. data/test/test_phidgets.rb +14 -6
  72. data/test/test_phsensor.rb +62 -0
  73. data/test/test_rfid.rb +77 -0
  74. data/test/test_servo.rb +67 -0
  75. data/test/test_spatial.rb +112 -0
  76. data/test/test_stepper.rb +163 -0
  77. data/test/test_temp_sensor.rb +87 -0
  78. data/test/test_text_lcd.rb +115 -0
  79. data/test/test_text_led.rb +35 -0
  80. data/test/test_weight_sensor.rb +32 -0
  81. metadata +165 -75
  82. data/Manifest.txt +0 -21
  83. data/PostInstall.txt +0 -3
  84. data/README.txt +0 -87
  85. data/lib/phidgets/phidgets.rb +0 -225
  86. data/script/console +0 -10
  87. data/script/destroy +0 -14
  88. data/script/generate +0 -14
@@ -0,0 +1,107 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+
5
+ VALUE ph_textled_init(VALUE self);
6
+ VALUE ph_textled_get_row_count(VALUE self);
7
+ VALUE ph_textled_get_column_count(VALUE self);
8
+ VALUE ph_textled_get_brightness(VALUE self);
9
+ VALUE ph_textled_set_brightness(VALUE self, VALUE brightness);
10
+ VALUE ph_textled_set_display_string(VALUE self, VALUE index, VALUE string);
11
+
12
+
13
+ VALUE Init_phidgets_textled(VALUE ph_module, VALUE ph_common) {
14
+ VALUE ph_textled = rb_define_class_under(ph_module, "TextLED", ph_common);
15
+
16
+ /* Document-method: new
17
+ * call-seq: new
18
+ *
19
+ * Creates a Phidget TextLED object.
20
+ */
21
+ rb_define_method(ph_textled, "initialize", ph_textled_init, 0);
22
+
23
+ /* Document-method: getRowCount
24
+ * call-seq: getRowCount -> count
25
+ *
26
+ * Gets the number of rows supported by this display.
27
+ */
28
+ rb_define_method(ph_textled, "getRowCount", ph_textled_get_row_count, 0);
29
+
30
+ /* Document-method: getColumnCount
31
+ * call-seq: getColumnCount -> count
32
+ *
33
+ * Gets the number of columns per supported by this display.
34
+ */
35
+ rb_define_method(ph_textled, "getColumnCount", ph_textled_get_column_count, 0);
36
+
37
+ /* Document-method: getBrightness
38
+ * call-seq: getBrightness -> brightness
39
+ *
40
+ * Gets the last set brightness value.
41
+ */
42
+ rb_define_method(ph_textled, "getBrightness", ph_textled_get_brightness, 0);
43
+
44
+ /* Document-method: setBrightness
45
+ * call-seq: setBrightness(brightness)
46
+ *
47
+ * Sets the last set brightness value.
48
+ */
49
+ rb_define_method(ph_textled, "setBrightness", ph_textled_set_brightness, 1);
50
+
51
+ /* Document-method: setDisplayString
52
+ * call-seq: setDisplayString(index, string)
53
+ *
54
+ * Sets a row on the display.
55
+ */
56
+ rb_define_method(ph_textled, "setDisplayString", ph_textled_set_display_string, 2);
57
+
58
+ rb_define_alias(ph_textled, "row_count", "getRowCount");
59
+ rb_define_alias(ph_textled, "column_count", "getColumnCount");
60
+ rb_define_alias(ph_textled, "brightness", "getBrightness");
61
+ rb_define_alias(ph_textled, "brightness=", "setBrightness");
62
+ rb_define_alias(ph_textled, "display_string", "setDisplayString");
63
+
64
+ return ph_textled;
65
+ }
66
+
67
+
68
+
69
+ VALUE ph_textled_init(VALUE self) {
70
+ ph_data_t *ph = get_ph_data(self);
71
+ ph_raise(CPhidgetTextLED_create((CPhidgetTextLEDHandle *)(&(ph->handle))));
72
+ return self;
73
+ }
74
+
75
+ VALUE ph_textled_get_row_count(VALUE self) {
76
+ CPhidgetTextLEDHandle handle = (CPhidgetTextLEDHandle)get_ph_handle(self);
77
+ int count;
78
+ ph_raise(CPhidgetTextLED_getRowCount(handle, &count));
79
+ return INT2FIX(count);
80
+ }
81
+
82
+ VALUE ph_textled_get_column_count(VALUE self) {
83
+ CPhidgetTextLEDHandle handle = (CPhidgetTextLEDHandle)get_ph_handle(self);
84
+ int count;
85
+ ph_raise(CPhidgetTextLED_getColumnCount(handle, &count));
86
+ return INT2FIX(count);
87
+ }
88
+
89
+ VALUE ph_textled_get_brightness(VALUE self) {
90
+ CPhidgetTextLEDHandle handle = (CPhidgetTextLEDHandle)get_ph_handle(self);
91
+ int brightness;
92
+ ph_raise(CPhidgetTextLED_getBrightness(handle, &brightness));
93
+ return INT2FIX(brightness);
94
+ }
95
+
96
+ VALUE ph_textled_set_brightness(VALUE self, VALUE brightness) {
97
+ CPhidgetTextLEDHandle handle = (CPhidgetTextLEDHandle)get_ph_handle(self);
98
+ ph_raise(CPhidgetTextLED_setBrightness(handle, FIX2INT(brightness)));
99
+ return Qnil;
100
+ }
101
+
102
+ VALUE ph_textled_set_display_string(VALUE self, VALUE index, VALUE string) {
103
+ CPhidgetTextLEDHandle handle = (CPhidgetTextLEDHandle)get_ph_handle(self);
104
+ ph_raise(CPhidgetTextLED_setDisplayString(handle, FIX2INT(index), StringValueCStr(string)));
105
+ return Qnil;
106
+ }
107
+
@@ -0,0 +1,113 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+
5
+ VALUE ph_weight_init(VALUE self);
6
+ VALUE ph_weight_get_weight(VALUE self);
7
+ VALUE ph_weight_get_weight_change_trigger(VALUE self);
8
+ VALUE ph_weight_set_weight_change_trigger(VALUE self, VALUE trigger);
9
+
10
+ #ifdef PH_CALLBACK
11
+ VALUE ph_weight_set_on_weight_change_handler(VALUE self, VALUE handler);
12
+ int ph_weight_on_weight_change(CPhidgetWeightSensorHandle phid, void *userPtr, double weight);
13
+ #endif
14
+
15
+
16
+ VALUE Init_phidgets_weight(VALUE ph_module, VALUE ph_common) {
17
+ VALUE ph_weight = rb_define_class_under(ph_module, "WeightSensor", ph_common);
18
+
19
+ /* Document-method: new
20
+ * call-seq: new
21
+ *
22
+ * Creates a Phidget WeightSensor object.
23
+ */
24
+ rb_define_method(ph_weight, "initialize", ph_weight_init, 0);
25
+
26
+ /* Document-method: getWeight
27
+ * call-seq: getWeight -> weight
28
+ *
29
+ * Gets the sensed weight.
30
+ */
31
+ rb_define_method(ph_weight, "getWeight", ph_weight_get_weight, 0);
32
+
33
+ /* Document-method: getWeightChangeTrigger
34
+ * call-seq: getWeightChangeTrigger -> trigger
35
+ *
36
+ * Gets the weight change trigger.
37
+ */
38
+ rb_define_method(ph_weight, "getWeightChangeTrigger", ph_weight_get_weight_change_trigger, 0);
39
+
40
+ /* Document-method: setWeightChangeTrigger
41
+ * call-seq: setWeightChangeTrigger(trigger)
42
+ *
43
+ * Sets the weight change trigger.
44
+ */
45
+ rb_define_method(ph_weight, "setWeightChangeTrigger", ph_weight_set_weight_change_trigger, 1);
46
+
47
+ #ifdef PH_CALLBACK
48
+ rb_define_private_method(ph_weight, "ext_setOnWeightChangeHandler", ph_weight_set_on_weight_change_handler, 1);
49
+ #endif
50
+
51
+ rb_define_alias(ph_weight, "weight", "getWeight");
52
+ rb_define_alias(ph_weight, "weight_change_trigger", "getWeightChangeTrigger");
53
+ rb_define_alias(ph_weight, "weight_change_trigger=", "setWeightChangeTrigger");
54
+
55
+ return ph_weight;
56
+ }
57
+
58
+
59
+
60
+ VALUE ph_weight_init(VALUE self) {
61
+ ph_data_t *ph = get_ph_data(self);
62
+ ph_raise(CPhidgetWeightSensor_create((CPhidgetWeightSensorHandle *)(&(ph->handle))));
63
+ return self;
64
+ }
65
+
66
+ VALUE ph_weight_get_weight(VALUE self) {
67
+ CPhidgetWeightSensorHandle handle = (CPhidgetWeightSensorHandle)get_ph_handle(self);
68
+ double weight;
69
+ ph_raise(CPhidgetWeightSensor_getWeight(handle, &weight));
70
+ return rb_float_new(weight);
71
+ }
72
+
73
+ VALUE ph_weight_get_weight_change_trigger(VALUE self) {
74
+ CPhidgetWeightSensorHandle handle = (CPhidgetWeightSensorHandle)get_ph_handle(self);
75
+ double trigger;
76
+ ph_raise(CPhidgetWeightSensor_getWeightChangeTrigger(handle, &trigger));
77
+ return rb_float_new(trigger);
78
+ }
79
+
80
+ VALUE ph_weight_set_weight_change_trigger(VALUE self, VALUE trigger) {
81
+ CPhidgetWeightSensorHandle handle = (CPhidgetWeightSensorHandle)get_ph_handle(self);
82
+ ph_raise(CPhidgetWeightSensor_setWeightChangeTrigger(handle, NUM2DBL(trigger)));
83
+ return Qnil;
84
+ }
85
+
86
+
87
+ #ifdef PH_CALLBACK
88
+ VALUE ph_weight_set_on_weight_change_handler(VALUE self, VALUE handler) {
89
+ ph_data_t *ph = get_ph_data(self);
90
+ ph_callback_data_t *callback_data = &ph->dev_callback_1;
91
+ if( TYPE(handler) == T_NIL ) {
92
+ callback_data->exit = true;
93
+ ph_raise(CPhidgetWeightSensor_set_OnWeightChange_Handler((CPhidgetWeightSensorHandle)ph->handle, NULL, (void *)NULL));
94
+ } else {
95
+ callback_data->called = false;
96
+ callback_data->exit = false;
97
+ callback_data->phidget = self;
98
+ callback_data->callback = handler;
99
+ ph_raise(CPhidgetWeightSensor_set_OnWeightChange_Handler((CPhidgetWeightSensorHandle)ph->handle, ph_weight_on_weight_change, (void *)callback_data));
100
+ ph_callback_thread(callback_data);
101
+ }
102
+ return Qnil;
103
+ }
104
+
105
+
106
+ int ph_weight_on_weight_change(CPhidgetWeightSensorHandle phid, void *userPtr, double weight) {
107
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
108
+ callback_data->called = true;
109
+ return EPHIDGET_OK;
110
+ }
111
+
112
+ #endif
113
+
@@ -0,0 +1,25 @@
1
+
2
+
3
+ module Phidgets
4
+ class Accelerometer < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnAccelerationChangeHandler(proc=nil, &block)
10
+ #
11
+ # Sets an acceleration change event handler. This is called when the acceleration changes by more then the change trigger.
12
+ #
13
+ def setOnAccelerationChangeHandler(cb_proc = nil, &cb_block)
14
+ @on_acceleration_change_thread.kill if defined? @on_acceleration_change_thread
15
+ callback = cb_proc || cb_block
16
+ @on_acceleration_change_thread = Thread.new {ext_setOnAccelerationChangeHandler(callback)}
17
+ end
18
+
19
+ alias :on_acceleration_change :setOnAccelerationChangeHandler
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+
@@ -0,0 +1,49 @@
1
+
2
+
3
+ module Phidgets
4
+ class AdvancedServo < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnVelocityChangeHandler(proc=nil, &block)
10
+ #
11
+ # Sets a velocity change event handler. This is called when the velocity changes.
12
+ #
13
+ def setOnVelocityChangeHandler(cb_proc = nil, &cb_block)
14
+ @on_velocity_change_thread.kill if defined? @on_velocity_change_thread
15
+ callback = cb_proc || cb_block
16
+ @on_velocity_change_thread = Thread.new {ext_setOnVelocityChangeHandler(callback)}
17
+ end
18
+
19
+ # call-seq:
20
+ # setOnPositionChangeHandler(proc=nil, &block)
21
+ #
22
+ # Sets a position change event handler. This is called when the position changes.
23
+ #
24
+ def setOnPositionChangeHandler(cb_proc = nil, &cb_block)
25
+ @on_position_change_thread.kill if defined? @on_position_change_thread
26
+ callback = cb_proc || cb_block
27
+ @on_position_change_thread = Thread.new {ext_setOnPositionChangeHandler(callback)}
28
+ end
29
+
30
+ # call-seq:
31
+ # setOnCurrentChangeHandler(proc=nil, &block)
32
+ #
33
+ # Sets a current change event handler. This is called when the current draw changes.
34
+ #
35
+ def setOnCurrentChangeHandler(cb_proc = nil, &cb_block)
36
+ @on_current_change_thread.kill if defined? @on_current_change_thread
37
+ callback = cb_proc || cb_block
38
+ @on_current_change_thread = Thread.new {ext_setOnCurrentChangeHandler(callback)}
39
+ end
40
+
41
+ alias :on_velocity_change :setOnVelocityChangeHandler
42
+ alias :on_position_change :setOnPositionChangeHandler
43
+ alias :on_current_change :setOnCurrentChangeHandler
44
+
45
+ end
46
+
47
+ end
48
+ end
49
+
@@ -0,0 +1,8 @@
1
+
2
+
3
+ module Phidgets
4
+ class Analog < Common
5
+
6
+ end
7
+ end
8
+
@@ -0,0 +1,25 @@
1
+
2
+
3
+ module Phidgets
4
+ class Bridge < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnBridgeDataHandler(proc=nil, &block)
10
+ #
11
+ # Sets a bridge data event handler. This is called at a set rate as defined by data rate.
12
+ #
13
+ def setOnBridgeDataHandler(cb_proc = nil, &cb_block)
14
+ @on_bridge_data_thread.kill if defined? @on_bridge_data_thread
15
+ callback = cb_proc || cb_block
16
+ @on_bridge_data_thread = Thread.new {ext_setOnBridgeDataHandler(callback)}
17
+ end
18
+
19
+ alias :on_bridge_data :setOnBridgeDataHandler
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+
@@ -1,200 +1,85 @@
1
1
 
2
2
  module Phidgets
3
-
4
- extern "int CPhidget_open(void *, int)"
5
- extern "int CPhidget_openRemote(void *, int, void *, void *)"
6
- extern "int CPhidget_openRemoteIP(void *, int, void *, int, void *)"
7
- extern "int CPhidget_close(void *)"
8
- extern "int CPhidget_delete(void *)"
9
- extern "int CPhidget_getDeviceName(void *, void *)"
10
- extern "int CPhidget_getSerialNumber(void *, int *)"
11
- extern "int CPhidget_getDeviceVersion(void *, int *)"
12
- extern "int CPhidget_getDeviceStatus(void *, int *)"
13
- extern "int CPhidget_getDeviceType(void *, void *)"
14
- extern "int CPhidget_getDeviceLabel(void *, void *)"
15
- extern "int CPhidget_setDeviceLabel(void *, void *)"
16
- extern "int CPhidget_waitForAttachment(void *, int)"
17
- extern "int CPhidget_getServerID(void *, void *)"
18
- extern "int CPhidget_getServerAddress(void *, void *, int *)"
19
- extern "int CPhidget_getServerStatus(void *, int *)"
20
- extern "int CPhidget_getDeviceID(void *, int *)"
21
- extern "int CPhidget_getDeviceClass(void *, int *)"
22
-
23
3
  class Common
24
4
 
25
- # Opens a Phidget.
26
- # === Parameters
27
- # * _serial_number_ = Serial number. Specify -1 to open any.
28
- # * _timeout_ = Time to wait for attachment. Specify 0 to not wait.
29
- def open(serial_number=-1, timeout=0)
30
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_open', @handle, serial_number.to_i)
31
- raise Phidgets::Exception.new(r) if r != 0
32
- waitForAttachment(timeout) if timeout > 0
33
- end
34
-
35
- # Opens a Phidget remotely by ServerID. Note that this requires Bonjour (mDNS) to be running on both the host and the server.
36
- # === Parameters
37
- # * _serial_number_ = Serial number. Specify -1 to open any.
38
- # * _server_ = Server ID. Specify nil to open any.
39
- # * _password_ = Password. Can be nil if the server is running unsecured.
40
- # * _timeout_ = Time to wait for attachment. Specify 0 to not wait.
41
- def openRemote(serial_number=-1, server=nil, password=nil, timeout=0)
42
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_openRemote', @handle, serial_number.to_i, server, password)
43
- raise Phidgets::Exception.new(r) if r != 0
44
- waitForAttachment(timeout) if timeout > 0
45
- end
46
-
47
- # Opens a Phidget remotely by address and port.
48
- # === Parameters
49
- # * _serial_number_ = Serial number. Specify -1 to open any.
50
- # * _address_ = Address. This can be a hostname or IP address.
51
- # * _port_ = Port number. Default is 5001.
52
- # * _password_ = Password. Can be nil if the server is running unsecured.
53
- # * _timeout_ = Time to wait for attachment. Specify 0 to not wait.
54
- def openRemoteIP(serial_number, address, port=5001, password=nil, timeout=0)
55
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_openRemoteIP', @handle, serial_number.to_i, address, port.to_i, password)
56
- raise Phidgets::Exception.new(r) if r != 0
57
- waitForAttachment(timeout) if timeout > 0
58
- end
59
-
60
- # Closes a Phidget.
61
- def close
62
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_close', @handle)
63
- raise Phidgets::Exception.new(r) if r != 0
64
- end
65
-
66
- # Frees a Phidget handle.
67
- def delete
68
- @handle.free = nil
69
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_delete', @handle)
70
- raise Phidgets::Exception.new(r) if r != 0
71
- end
72
-
73
- # Gets the specific name of a Phidget.
74
- def getDeviceName
75
- ptr = Phidgets.malloc(SIZEOF_VOIDP)
76
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceName', @handle, ptr.ref)
77
- raise Phidgets::Exception.new(r) if r != 0
78
- ptr.free = nil
79
- ptr.to_s
80
- end
81
-
82
- # Gets the serial number of a Phidget.
83
- def getSerialNumber
84
- sn = Phidgets.malloc(SIZEOF_INT)
85
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getSerialNumber', @handle, sn.ref)
86
- raise Phidgets::Exception.new(r) if r != 0
87
- sn.free = nil
88
- sn.to_i
89
- end
90
-
91
- # Gets the firmware version of a Phidget.
92
- def getDeviceVersion
93
- ver = Phidgets.malloc(SIZEOF_INT)
94
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceVersion', @handle, ver.ref)
95
- raise Phidgets::Exception.new(r) if r != 0
96
- ver.free = nil
97
- ver.to_i
98
- end
99
-
100
- # Gets the attached status of a Phidget.
101
- def getDeviceStatus
102
- stat = Phidgets.malloc(SIZEOF_INT)
103
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceStatus', @handle, stat.ref)
104
- raise Phidgets::Exception.new(r) if r != 0
105
- stat.free = nil
106
- stat.to_i
107
- end
108
-
109
- # Gets the type (class) of a Phidget.
110
- def getDeviceType
111
- ptr = Phidgets.malloc(SIZEOF_VOIDP)
112
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceType', @handle, ptr.ref)
113
- raise Phidgets::Exception.new(r) if r != 0
114
- ptr.free = nil
115
- ptr.to_s
116
- end
117
-
118
- # Gets the label of a Phidget.
119
- def getDeviceLabel
120
- ptr = Phidgets.malloc(SIZEOF_VOIDP)
121
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceLabel', @handle, ptr.ref)
122
- raise Phidgets::Exception.new(r) if r != 0
123
- ptr.free = nil
124
- ptr.to_s
125
- end
126
-
127
- # Sets the label of a Phidget. Note that this is not supported on very old Phidgets, and not yet supported in Windows.
128
- # === Parameters
129
- # * _label_ = A string containing the label to be set.
130
- def setDeviceLabel(label)
131
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_setDeviceLabel', @handle, label)
132
- raise Phidgets::Exception.new(r) if r != 0
133
- end
134
-
135
- # Waits for attachment to happen. This can be called right after calling open, as an alternative to using the attach handler.
136
- # === Parameters
137
- # * _timeout_ = Time to wait for the attachment. Specify 0 to wait forever.
138
- def waitForAttachment(timeout)
139
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_waitForAttachment', @handle, timeout.to_i)
140
- raise Phidgets::Exception.new(r) if r != 0
141
- end
142
-
143
- # Gets the server ID of a remotely opened Phidget. This will fail if the Phidget was opened locally.
144
- def getServerID
145
- ptr = Phidgets.malloc(SIZEOF_VOIDP)
146
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getServerID', @handle, ptr.ref)
147
- raise Phidgets::Exception.new(r) if r != 0
148
- ptr.free = nil
149
- ptr.to_s
150
- end
151
-
152
- # Gets the address and port of a remotely opened Phidget. This will fail if the Phidget was opened locally.
153
- def getServerAddress
154
- ptr = Phidgets.malloc(SIZEOF_VOIDP)
155
- port = Phidgets.malloc(SIZEOF_INT)
156
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getServerAddress', @handle, ptr.ref, port.ref)
157
- raise Phidgets::Exception.new(r) if r != 0
158
- ptr.free = nil
159
- port.free = nil
160
- [ptr.to_s, port.to_i]
161
- end
162
-
163
- # Gets the connected to server status of a remotely opened Phidget. This will fail if the Phidget was opened locally.
164
- def getServerStatus
165
- stat = Phidgets.malloc(SIZEOF_INT)
166
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getServerStatus', @handle, stat.ref)
167
- raise Phidgets::Exception.new(r) if r != 0
168
- stat.free = nil
169
- stat.to_i
170
- end
171
-
172
- # Gets the device ID of a Phidget.
173
- def getDeviceID
174
- dev = Phidgets.malloc(SIZEOF_INT)
175
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceID', @handle, dev.ref)
176
- raise Phidgets::Exception.new(r) if r != 0
177
- dev.free = nil
178
- dev.to_i
179
- end
5
+ # call-seq:
6
+ # open(args)
7
+ #
8
+ # Opens a Phidget. Will attempt to make the correct open call (openLabel, openRemote, openRemoteIP, etc)
9
+ # based on the arguments passed in. Args can either be a serial number or a hash of arguments. If it is a
10
+ # hash, the following keys may be used: :serial_number, :label, :server_id, :address, :port, :password.
11
+ #
12
+ def open(args)
13
+ args = {:serial_number => args.to_i} unless args.is_a? Hash
14
+ if args.key? :server_id
15
+ return open_remote(args[:serial_number], args[:server_id], args[:password]) if args.key? :serial_number
16
+ return open_label_remote(args[:label], args[:server_id], args[:password]) if args.key? :label
17
+ elsif args.key? :address
18
+ return open_remote_ip(args[:serial_number], args[:address], args[:port], args[:password]) if args.key? :serial_number
19
+ return open_label_remote_ip(args[:label], args[:address], args[:port], args[:password]) if args.key? :label
20
+ else
21
+ return ext_open(args[:serial_number]) if args.key? :serial_number
22
+ return open_label(args[:label]) if args.key? :label
23
+ end
24
+ raise Phidgets::Error::InvalidArg.new
25
+ end
26
+
27
+
28
+ unless RUBY_VERSION < '1.9.0'
29
+
30
+ # call-seq:
31
+ # setOnAttachHandler(proc=nil, &block)
32
+ #
33
+ # Sets an attach handler callback function. This is called when this Phidget is plugged into the system, and is ready for use.
34
+ #
35
+ def setOnAttachHandler(cb_proc = nil, &cb_block)
36
+ @on_attach_thread.kill if defined? @on_attach_thread
37
+ callback = cb_proc || cb_block
38
+ @on_attach_thread = Thread.new {ext_setOnAttachHandler(callback)}
39
+ end
40
+
41
+ # call-seq:
42
+ # setOnDetachHandler(proc=nil, &block)
43
+ #
44
+ # Sets a detach handler callback function. This is called when this Phidget is unplugged from the system.
45
+ #
46
+ def setOnDetachHandler(cb_proc = nil, &cb_block)
47
+ @on_detach_thread.kill if defined? @on_detach_thread
48
+ callback = cb_proc || cb_block
49
+ @on_detach_thread = Thread.new {ext_setOnDetachHandler(callback)}
50
+ end
51
+
52
+ # call-seq:
53
+ # setOnServerConnectHandler(proc=nil, &block)
54
+ #
55
+ # Sets a server connect handler callback function. This is used for opening Phidgets remotely,
56
+ # and is called when a connection to the sever has been made.
57
+ #
58
+ def setOnServerConnectHandler(cb_proc = nil, &cb_block)
59
+ @on_server_connect_thread.kill if defined? @on_server_connect_thread
60
+ callback = cb_proc || cb_block
61
+ @on_server_connect_thread = Thread.new {ext_setOnServerConnectHandler(callback)}
62
+ end
63
+
64
+ # call-seq:
65
+ # setOnServerDisconnectHandler(proc=nil, &block)
66
+ #
67
+ # Sets a server disconnect handler callback function. This is used for opening Phidgets remotely,
68
+ # and is called when a connection to the server has been lost.
69
+ #
70
+ def setOnServerDisconnectHandler(cb_proc = nil, &cb_block)
71
+ @on_server_disconnect_thread.kill if defined? @on_server_disconnect_thread
72
+ callback = cb_proc || cb_block
73
+ @on_server_disconnect_thread = Thread.new {ext_setOnServerDisconnectHandler(callback)}
74
+ end
75
+
76
+ alias :on_attach :setOnAttachHandler
77
+ alias :on_detach :setOnDetachHandler
78
+ alias :on_server_connect :setOnServerConnectHandler
79
+ alias :on_server_disconnect :setOnServerDisconnectHandler
180
80
 
181
- # Gets the class of a Phidget.
182
- def getDeviceClass
183
- dev = Phidgets.malloc(SIZEOF_INT)
184
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceClass', @handle, dev.ref)
185
- raise Phidgets::Exception.new(r) if r != 0
186
- dev.free = nil
187
- dev.to_i
188
- end
189
-
190
-
191
- private
192
-
193
- def initialize
194
- @handle = Phidgets.malloc(SIZEOF_VOIDP)
195
81
  end
196
82
 
197
83
  end
198
-
199
84
  end
200
85
 
@@ -0,0 +1,53 @@
1
+
2
+ module Phidgets
3
+ class Dictionary
4
+
5
+ # call-seq:
6
+ # open(args)
7
+ #
8
+ # Opens a Dictionary. Will attempt to make the correct open call (openRemote, openRemoteIP) based
9
+ # on the arguments passed in. Args is a hash and the following keys may be used:
10
+ # :server_id, :address, :port, :password.
11
+ #
12
+ def open(args)
13
+ if args.key? :server_id
14
+ return open_remote(args[:server_id], args[:password])
15
+ elsif args.key? :address
16
+ return open_remote_ip(args[:address], args[:port], args[:password])
17
+ end
18
+ raise Phidgets::Error::InvalidArg.new
19
+ end
20
+
21
+
22
+ unless RUBY_VERSION < '1.9.0'
23
+
24
+ # call-seq:
25
+ # setOnServerConnectHandler(proc=nil, &block)
26
+ #
27
+ # Sets a server connect handler callback function. This is called when a connection to the sever has been made.
28
+ #
29
+ def setOnServerConnectHandler(cb_proc = nil, &cb_block)
30
+ @on_server_connect_thread.kill if defined? @on_server_connect_thread
31
+ callback = cb_proc || cb_block
32
+ @on_server_connect_thread = Thread.new {ext_setOnServerConnectHandler(callback)}
33
+ end
34
+
35
+ # call-seq:
36
+ # setOnServerDisconnectHandler(proc=nil, &block)
37
+ #
38
+ # Sets a server disconnect handler callback function. This is called when a connection to the server has been lost.
39
+ #
40
+ def setOnServerDisconnectHandler(cb_proc = nil, &cb_block)
41
+ @on_server_disconnect_thread.kill if defined? @on_server_disconnect_thread
42
+ callback = cb_proc || cb_block
43
+ @on_server_disconnect_thread = Thread.new {ext_setOnServerDisconnectHandler(callback)}
44
+ end
45
+
46
+ alias :on_server_connect :setOnServerConnectHandler
47
+ alias :on_server_disconnect :setOnServerDisconnectHandler
48
+
49
+ end
50
+
51
+ end
52
+ end
53
+