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,178 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+
5
+ VALUE ph_led_init(VALUE self);
6
+ VALUE ph_led_get_led_count(VALUE self);
7
+ VALUE ph_led_get_current_limit(VALUE self);
8
+ VALUE ph_led_set_current_limit(VALUE self, VALUE limit);
9
+ VALUE ph_led_get_voltage(VALUE self);
10
+ VALUE ph_led_set_voltage(VALUE self, VALUE voltage);
11
+ VALUE ph_led_get_brightness(VALUE self, VALUE index);
12
+ VALUE ph_led_set_brightness(VALUE self, VALUE index, VALUE brightness);
13
+ VALUE ph_led_get_current_limit_indexed(VALUE self, VALUE index);
14
+ VALUE ph_led_set_current_limit_indexed(VALUE self, VALUE index, VALUE limit);
15
+
16
+
17
+ VALUE Init_phidgets_led(VALUE ph_module, VALUE ph_common) {
18
+ VALUE ph_led = rb_define_class_under(ph_module, "LED", ph_common);
19
+
20
+ rb_define_const(ph_led, "CURRENT_LIMIT_20mA", INT2FIX(PHIDGET_LED_CURRENT_LIMIT_20mA));
21
+ rb_define_const(ph_led, "CURRENT_LIMIT_40mA", INT2FIX(PHIDGET_LED_CURRENT_LIMIT_40mA));
22
+ rb_define_const(ph_led, "CURRENT_LIMIT_60mA", INT2FIX(PHIDGET_LED_CURRENT_LIMIT_60mA));
23
+ rb_define_const(ph_led, "CURRENT_LIMIT_80mA", INT2FIX(PHIDGET_LED_CURRENT_LIMIT_80mA));
24
+ rb_define_const(ph_led, "VOLTAGE_1_7V", INT2FIX(PHIDGET_LED_VOLTAGE_1_7V));
25
+ rb_define_const(ph_led, "VOLTAGE_2_75V", INT2FIX(PHIDGET_LED_VOLTAGE_2_75V));
26
+ rb_define_const(ph_led, "VOLTAGE_3_9V", INT2FIX(PHIDGET_LED_VOLTAGE_3_9V));
27
+ rb_define_const(ph_led, "VOLTAGE_5_0V", INT2FIX(PHIDGET_LED_VOLTAGE_5_0V));
28
+
29
+ /* Document-method: new
30
+ * call-seq: new
31
+ *
32
+ * Creates a Phidget LED object.
33
+ */
34
+ rb_define_method(ph_led, "initialize", ph_led_init, 0);
35
+
36
+ /* Document-method: getLEDCount
37
+ * call-seq: getLEDCount -> count
38
+ *
39
+ * Gets the number of LEDs supported by this board.
40
+ */
41
+ rb_define_method(ph_led, "getLEDCount", ph_led_get_led_count, 0);
42
+
43
+ /* Document-method: getCurrentLimit
44
+ * call-seq: getCurrentLimit -> limit
45
+ *
46
+ * Gets the current limit. This is for all ouputs.
47
+ */
48
+ rb_define_method(ph_led, "getCurrentLimit", ph_led_get_current_limit, 0);
49
+
50
+ /* Document-method: setCurrentLimit
51
+ * call-seq: setCurrentLimit(limit)
52
+ *
53
+ * Sets the current limit. This is for all ouputs.
54
+ */
55
+ rb_define_method(ph_led, "setCurrentLimit", ph_led_set_current_limit, 1);
56
+
57
+ /* Document-method: getVoltage
58
+ * call-seq: getVoltage -> voltage
59
+ *
60
+ * Gets the output voltage. This is for all ouputs.
61
+ */
62
+ rb_define_method(ph_led, "getVoltage", ph_led_get_voltage, 0);
63
+
64
+ /* Document-method: setVoltage
65
+ * call-seq: setVoltage(voltage)
66
+ *
67
+ * Sets the output voltage. This is for all ouputs.
68
+ */
69
+ rb_define_method(ph_led, "setVoltage", ph_led_set_voltage, 1);
70
+
71
+ /* Document-method: getBrightness
72
+ * call-seq: getBrightness(index) -> brightness
73
+ *
74
+ * Gets the brightness of an LED.
75
+ */
76
+ rb_define_method(ph_led, "getBrightness", ph_led_get_brightness, 1);
77
+
78
+ /* Document-method: setBrightness
79
+ * call-seq: setBrightness(index, brightness)
80
+ *
81
+ * Sets the brightness of an LED.
82
+ */
83
+ rb_define_method(ph_led, "setBrightness", ph_led_set_brightness, 2);
84
+
85
+ /* Document-method: getCurrentLimitIndexed
86
+ * call-seq: getCurrentLimitIndexed(index) -> limit
87
+ *
88
+ * Gets the current limit of an LED.
89
+ */
90
+ rb_define_method(ph_led, "getCurrentLimitIndexed", ph_led_get_current_limit_indexed, 1);
91
+
92
+ /* Document-method: setCurrentLimitIndexed
93
+ * call-seq: setCurrentLimitIndexed(index, limit)
94
+ *
95
+ * Sets the current limit of an LED.
96
+ */
97
+ rb_define_method(ph_led, "setCurrentLimitIndexed", ph_led_set_current_limit_indexed, 2);
98
+
99
+ rb_define_alias(ph_led, "led_count", "getLEDCount");
100
+ rb_define_alias(ph_led, "current_limit", "getCurrentLimit");
101
+ rb_define_alias(ph_led, "current_limit=", "setCurrentLimit");
102
+ rb_define_alias(ph_led, "voltage", "getVoltage");
103
+ rb_define_alias(ph_led, "voltage=", "setVoltage");
104
+ rb_define_alias(ph_led, "brightness", "getBrightness");
105
+ rb_define_alias(ph_led, "set_brightness", "setBrightness");
106
+ rb_define_alias(ph_led, "current_limit_indexed", "getCurrentLimitIndexed");
107
+ rb_define_alias(ph_led, "set_current_limit_indexed", "setCurrentLimitIndexed");
108
+
109
+ return ph_led;
110
+ }
111
+
112
+
113
+
114
+ VALUE ph_led_init(VALUE self) {
115
+ ph_data_t *ph = get_ph_data(self);
116
+ ph_raise(CPhidgetLED_create((CPhidgetLEDHandle *)(&(ph->handle))));
117
+ return self;
118
+ }
119
+
120
+ VALUE ph_led_get_led_count(VALUE self) {
121
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
122
+ int count;
123
+ ph_raise(CPhidgetLED_getLEDCount(handle, &count));
124
+ return INT2FIX(count);
125
+ }
126
+
127
+ VALUE ph_led_get_current_limit(VALUE self) {
128
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
129
+ CPhidgetLED_CurrentLimit limit;
130
+ ph_raise(CPhidgetLED_getCurrentLimit(handle, &limit));
131
+ return INT2FIX(limit);
132
+ }
133
+
134
+ VALUE ph_led_set_current_limit(VALUE self, VALUE limit) {
135
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
136
+ ph_raise(CPhidgetLED_setCurrentLimit(handle, FIX2INT(limit)));
137
+ return Qnil;
138
+ }
139
+
140
+ VALUE ph_led_get_voltage(VALUE self) {
141
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
142
+ CPhidgetLED_Voltage voltage;
143
+ ph_raise(CPhidgetLED_getVoltage(handle, &voltage));
144
+ return INT2FIX(voltage);
145
+ }
146
+
147
+ VALUE ph_led_set_voltage(VALUE self, VALUE voltage) {
148
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
149
+ ph_raise(CPhidgetLED_setVoltage(handle, FIX2INT(voltage)));
150
+ return Qnil;
151
+ }
152
+
153
+ VALUE ph_led_get_brightness(VALUE self, VALUE index) {
154
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
155
+ double brightness;
156
+ ph_raise(CPhidgetLED_getBrightness(handle, FIX2INT(index), &brightness));
157
+ return rb_float_new(brightness);
158
+ }
159
+
160
+ VALUE ph_led_set_brightness(VALUE self, VALUE index, VALUE brightness) {
161
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
162
+ ph_raise(CPhidgetLED_setBrightness(handle, FIX2INT(index), NUM2DBL(brightness)));
163
+ return Qnil;
164
+ }
165
+
166
+ VALUE ph_led_get_current_limit_indexed(VALUE self, VALUE index) {
167
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
168
+ double limit;
169
+ ph_raise(CPhidgetLED_getCurrentLimitIndexed(handle, FIX2INT(index), &limit));
170
+ return rb_float_new(limit);
171
+ }
172
+
173
+ VALUE ph_led_set_current_limit_indexed(VALUE self, VALUE index, VALUE limit) {
174
+ CPhidgetLEDHandle handle = (CPhidgetLEDHandle)get_ph_handle(self);
175
+ ph_raise(CPhidgetLED_setCurrentLimitIndexed(handle, FIX2INT(index), NUM2DBL(limit)));
176
+ return Qnil;
177
+ }
178
+
@@ -0,0 +1,366 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+
5
+ VALUE ph_manager_allocate(VALUE klass);
6
+ void ph_manager_free(ph_data_t *ph);
7
+ VALUE ph_manager_init(VALUE self);
8
+ VALUE ph_manager_open(VALUE self);
9
+ VALUE ph_manager_open_remote(VALUE self, VALUE server_id, VALUE password);
10
+ VALUE ph_manager_open_remote_ip(VALUE self, VALUE address, VALUE port, VALUE password);
11
+ VALUE ph_manager_close(VALUE self);
12
+ VALUE ph_manager_get_server_id(VALUE self);
13
+ VALUE ph_manager_get_server_address(VALUE self);
14
+ VALUE ph_manager_get_server_status(VALUE self);
15
+ VALUE ph_manager_get_attached_devices(VALUE self);
16
+ VALUE ph_manager_instance_from_class(CPhidget_DeviceClass dev_class);
17
+
18
+ #ifdef PH_CALLBACK
19
+ VALUE ph_manager_set_on_attach_handler(VALUE self, VALUE handler);
20
+ VALUE ph_manager_set_on_detach_handler(VALUE self, VALUE handler);
21
+ VALUE ph_manager_set_on_server_connect_handler(VALUE self, VALUE handler);
22
+ VALUE ph_manager_set_on_server_disconnect_handler(VALUE self, VALUE handler);
23
+ int ph_manager_on_attach(CPhidgetHandle phid, void *userPtr);
24
+ int ph_manager_on_detach(CPhidgetHandle phid, void *userPtr);
25
+ int ph_manager_on_server_connect(CPhidgetManagerHandle phid, void *userPtr);
26
+ int ph_manager_on_server_disconnect(CPhidgetManagerHandle phid, void *userPtr);
27
+ #endif
28
+
29
+
30
+ VALUE Init_phidgets_manager(VALUE ph_module) {
31
+ VALUE ph_manager = rb_define_class_under(ph_module, "Manager", rb_cObject);
32
+ rb_define_alloc_func(ph_manager, ph_manager_allocate);
33
+
34
+ /* Document-method: new
35
+ * call-seq: new
36
+ *
37
+ * Creates a Phidget Manager object.
38
+ */
39
+ rb_define_method(ph_manager, "initialize", ph_manager_init, 0);
40
+ rb_define_private_method(ph_manager, "ext_open", ph_manager_open, 0);
41
+
42
+ /* Document-method: openRemote
43
+ * call-seq: openRemote(server_id, password)
44
+ *
45
+ * Opens a Phidget manager remotely by ServerID. Note that this requires Bonjour
46
+ * (mDNS) to be running on both the host and the server.
47
+ */
48
+ rb_define_method(ph_manager, "openRemote", ph_manager_open_remote, 2);
49
+
50
+ /* Document-method: openRemoteIP
51
+ * call-seq: openRemoteIP(address, port, password)
52
+ *
53
+ * Opens a Phidget manager remotely by address and port.
54
+ */
55
+ rb_define_method(ph_manager, "openRemoteIP", ph_manager_open_remote_ip, 3);
56
+
57
+ /* Document-method: close
58
+ * call-seq: close
59
+ *
60
+ * Closes a Phidget Manager.
61
+ */
62
+ rb_define_method(ph_manager, "close", ph_manager_close, 0);
63
+
64
+ /* Document-method: getServerID
65
+ * call-seq: getServerID -> server_id
66
+ *
67
+ * Gets the server ID of a remotely opened Phidget Manager. This will fail if the manager was opened locally.
68
+ */
69
+ rb_define_method(ph_manager, "getServerID", ph_manager_get_server_id, 0);
70
+
71
+ /* Document-method: getServerAddress
72
+ * call-seq: getServerAddress -> [address, port]
73
+ *
74
+ * Gets the address and port of a remotely opened Phidget Manager. This will fail if the manager was opened locally.
75
+ */
76
+ rb_define_method(ph_manager, "getServerAddress", ph_manager_get_server_address, 0);
77
+
78
+ /* Document-method: getServerStatus
79
+ * call-seq: getServerStatus -> status
80
+ *
81
+ * Gets the connected to server status of a remotely opened Phidget Manager. This will fail if the manager was opened locally.
82
+ */
83
+ rb_define_method(ph_manager, "getServerStatus", ph_manager_get_server_status, 0);
84
+
85
+ /* Document-method: getAttachedDevices
86
+ * call-seq: getAttachedDevices -> [devices]
87
+ *
88
+ * Gets an array of all currently attached Phidgets.
89
+ */
90
+ rb_define_method(ph_manager, "getAttachedDevices", ph_manager_get_attached_devices, 0);
91
+
92
+ #ifdef PH_CALLBACK
93
+ rb_define_private_method(ph_manager, "ext_setOnAttachHandler", ph_manager_set_on_attach_handler, 1);
94
+ rb_define_private_method(ph_manager, "ext_setOnDetachHandler", ph_manager_set_on_detach_handler, 1);
95
+ rb_define_private_method(ph_manager, "ext_setOnServerConnectHandler", ph_manager_set_on_server_connect_handler, 1);
96
+ rb_define_private_method(ph_manager, "ext_setOnServerDisconnectHandler", ph_manager_set_on_server_disconnect_handler, 1);
97
+ #endif
98
+
99
+ rb_define_alias(ph_manager, "open_remote", "openRemote");
100
+ rb_define_alias(ph_manager, "open_remote_ip", "openRemoteIP");
101
+ rb_define_alias(ph_manager, "server_id", "getServerID");
102
+ rb_define_alias(ph_manager, "server_address", "getServerAddress");
103
+ rb_define_alias(ph_manager, "server_status", "getServerStatus");
104
+ rb_define_alias(ph_manager, "attached_devices", "getAttachedDevices");
105
+
106
+ return ph_manager;
107
+ }
108
+
109
+
110
+
111
+ VALUE ph_manager_allocate(VALUE klass) {
112
+ ph_data_t *ph;
113
+ VALUE self = Data_Make_Struct(klass, ph_data_t, 0, ph_manager_free, ph);
114
+ memset(ph, 0, sizeof(ph_data_t));
115
+ return self;
116
+ }
117
+
118
+ void ph_manager_free(ph_data_t *ph) {
119
+ if (ph && ph->handle) {
120
+ CPhidgetManager_close((CPhidgetManagerHandle)ph->handle);
121
+ CPhidgetManager_delete((CPhidgetManagerHandle)ph->handle);
122
+ ph->handle = NULL;
123
+ }
124
+ if(ph) xfree(ph);
125
+ }
126
+
127
+ VALUE ph_manager_init(VALUE self) {
128
+ ph_data_t *ph = get_ph_data(self);
129
+ ph_raise(CPhidgetManager_create((CPhidgetManagerHandle *)(&(ph->handle))));
130
+ return self;
131
+ }
132
+
133
+ VALUE ph_manager_open(VALUE self) {
134
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
135
+ ph_raise(CPhidgetManager_open(handle));
136
+ return Qnil;
137
+ }
138
+
139
+ VALUE ph_manager_open_remote(VALUE self, VALUE server_id, VALUE password) {
140
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
141
+ ph_raise(CPhidgetManager_openRemote(handle, StringValueCStr(server_id), StringValueCStr(password)));
142
+ return Qnil;
143
+ }
144
+
145
+ VALUE ph_manager_open_remote_ip(VALUE self, VALUE address, VALUE port, VALUE password) {
146
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
147
+ ph_raise(CPhidgetManager_openRemoteIP(handle, StringValueCStr(address), FIX2INT(port), StringValueCStr(password)));
148
+ return Qnil;
149
+ }
150
+
151
+ VALUE ph_manager_close(VALUE self) {
152
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
153
+ ph_raise(CPhidgetManager_close(handle));
154
+ return Qnil;
155
+ }
156
+
157
+ VALUE ph_manager_get_server_id(VALUE self) {
158
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
159
+ const char *server_id;
160
+ ph_raise(CPhidgetManager_getServerID(handle, &server_id));
161
+ return rb_str_new2(server_id);
162
+ }
163
+
164
+ VALUE ph_manager_get_server_address(VALUE self) {
165
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
166
+ const char *address;
167
+ int port;
168
+ ph_raise(CPhidgetManager_getServerAddress(handle, &address, &port));
169
+ return rb_ary_new3(2, rb_str_new2(address), INT2FIX(port));
170
+ }
171
+
172
+ VALUE ph_manager_get_server_status(VALUE self) {
173
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
174
+ int server_status;
175
+ ph_raise(CPhidgetManager_getServerStatus(handle, &server_status));
176
+ return INT2FIX(server_status);
177
+ }
178
+
179
+ VALUE ph_manager_get_attached_devices(VALUE self) {
180
+ CPhidgetManagerHandle handle = (CPhidgetManagerHandle)get_ph_handle(self);
181
+ CPhidgetHandle *devices;
182
+ int count, i;
183
+ VALUE rb_hash = rb_hash_new();
184
+ ph_raise(CPhidgetManager_getAttachedDevices(handle, &devices, &count));
185
+ for(i=0; i<count; i++) {
186
+ int serial;
187
+ CPhidget_DeviceClass dev_class;
188
+ ph_raise(CPhidget_getSerialNumber(devices[i], &serial));
189
+ ph_raise(CPhidget_getDeviceClass(devices[i], &dev_class));
190
+ rb_hash_aset(rb_hash, INT2FIX(serial), ph_manager_instance_from_class(dev_class));
191
+ }
192
+ ph_raise(CPhidgetManager_freeAttachedDevicesArray(devices));
193
+ return rb_hash;
194
+ }
195
+
196
+ VALUE ph_manager_instance_from_class(CPhidget_DeviceClass dev_class) {
197
+ VALUE ph_module = rb_const_get(rb_cModule, rb_intern("Phidgets"));
198
+ VALUE klass;
199
+ VALUE argv[0];
200
+
201
+ switch( dev_class ) {
202
+ case PHIDCLASS_ACCELEROMETER:
203
+ klass = rb_const_get(ph_module, rb_intern("Accelerometer"));
204
+ break;
205
+ case PHIDCLASS_ADVANCEDSERVO:
206
+ klass = rb_const_get(ph_module, rb_intern("AdvancedServo"));
207
+ break;
208
+ case PHIDCLASS_ANALOG:
209
+ klass = rb_const_get(ph_module, rb_intern("Analog"));
210
+ break;
211
+ case PHIDCLASS_BRIDGE:
212
+ klass = rb_const_get(ph_module, rb_intern("Bridge"));
213
+ break;
214
+ case PHIDCLASS_ENCODER:
215
+ klass = rb_const_get(ph_module, rb_intern("Encoder"));
216
+ break;
217
+ case PHIDCLASS_FREQUENCYCOUNTER:
218
+ klass = rb_const_get(ph_module, rb_intern("FrequencyCounter"));
219
+ break;
220
+ case PHIDCLASS_GPS:
221
+ klass = rb_const_get(ph_module, rb_intern("GPS"));
222
+ break;
223
+ case PHIDCLASS_INTERFACEKIT:
224
+ klass = rb_const_get(ph_module, rb_intern("InterfaceKit"));
225
+ break;
226
+ case PHIDCLASS_IR:
227
+ klass = rb_const_get(ph_module, rb_intern("IR"));
228
+ break;
229
+ case PHIDCLASS_LED:
230
+ klass = rb_const_get(ph_module, rb_intern("LED"));
231
+ break;
232
+ case PHIDCLASS_MOTORCONTROL:
233
+ klass = rb_const_get(ph_module, rb_intern("MotorControl"));
234
+ break;
235
+ case PHIDCLASS_PHSENSOR:
236
+ klass = rb_const_get(ph_module, rb_intern("PhSensor"));
237
+ break;
238
+ case PHIDCLASS_RFID:
239
+ klass = rb_const_get(ph_module, rb_intern("RFID"));
240
+ break;
241
+ case PHIDCLASS_SERVO:
242
+ klass = rb_const_get(ph_module, rb_intern("Servo"));
243
+ break;
244
+ case PHIDCLASS_SPATIAL:
245
+ klass = rb_const_get(ph_module, rb_intern("Spatial"));
246
+ break;
247
+ case PHIDCLASS_STEPPER:
248
+ klass = rb_const_get(ph_module, rb_intern("Stepper"));
249
+ break;
250
+ case PHIDCLASS_TEMPERATURESENSOR:
251
+ klass = rb_const_get(ph_module, rb_intern("TemperatureSensor"));
252
+ break;
253
+ case PHIDCLASS_TEXTLCD:
254
+ klass = rb_const_get(ph_module, rb_intern("TextLCD"));
255
+ break;
256
+ case PHIDCLASS_TEXTLED:
257
+ klass = rb_const_get(ph_module, rb_intern("TextLED"));
258
+ break;
259
+ case PHIDCLASS_WEIGHTSENSOR:
260
+ klass = rb_const_get(ph_module, rb_intern("WeightSensor"));
261
+ break;
262
+ default:
263
+ klass = Qnil;
264
+ }
265
+
266
+ return rb_class_new_instance(0, argv, klass);
267
+ }
268
+
269
+ #ifdef PH_CALLBACK
270
+ VALUE ph_manager_set_on_attach_handler(VALUE self, VALUE handler) {
271
+ ph_data_t *ph = get_ph_data(self);
272
+ ph_callback_data_t *callback_data = &ph->attach_callback;
273
+ if( TYPE(handler) == T_NIL ) {
274
+ callback_data->exit = true;
275
+ ph_raise(CPhidgetManager_set_OnAttach_Handler((CPhidgetManagerHandle)ph->handle, NULL, (void *)NULL));
276
+ } else {
277
+ callback_data->called = false;
278
+ callback_data->exit = false;
279
+ callback_data->phidget = self;
280
+ callback_data->callback = handler;
281
+ ph_raise(CPhidgetManager_set_OnAttach_Handler((CPhidgetManagerHandle)ph->handle, ph_manager_on_attach, (void *)callback_data));
282
+ ph_callback_thread(callback_data);
283
+ }
284
+ return Qnil;
285
+ }
286
+
287
+ VALUE ph_manager_set_on_detach_handler(VALUE self, VALUE handler) {
288
+ ph_data_t *ph = get_ph_data(self);
289
+ ph_callback_data_t *callback_data = &ph->detach_callback;
290
+ if( TYPE(handler) == T_NIL ) {
291
+ callback_data->exit = true;
292
+ ph_raise(CPhidgetManager_set_OnDetach_Handler((CPhidgetManagerHandle)ph->handle, NULL, (void *)NULL));
293
+ } else {
294
+ callback_data->called = false;
295
+ callback_data->exit = false;
296
+ callback_data->phidget = self;
297
+ callback_data->callback = handler;
298
+ ph_raise(CPhidgetManager_set_OnDetach_Handler((CPhidgetManagerHandle)ph->handle, ph_manager_on_detach, (void *)callback_data));
299
+ ph_callback_thread(callback_data);
300
+ }
301
+ return Qnil;
302
+ }
303
+
304
+ VALUE ph_manager_set_on_server_connect_handler(VALUE self, VALUE handler) {
305
+ ph_data_t *ph = get_ph_data(self);
306
+ ph_callback_data_t *callback_data = &ph->server_connect_callback;
307
+ if( TYPE(handler) == T_NIL ) {
308
+ callback_data->exit = true;
309
+ ph_raise(CPhidgetManager_set_OnServerConnect_Handler((CPhidgetManagerHandle)ph->handle, NULL, (void *)NULL));
310
+ } else {
311
+ callback_data->called = false;
312
+ callback_data->exit = false;
313
+ callback_data->phidget = self;
314
+ callback_data->callback = handler;
315
+ ph_raise(CPhidgetManager_set_OnServerConnect_Handler((CPhidgetManagerHandle)ph->handle, ph_manager_on_server_connect, (void *)callback_data));
316
+ ph_callback_thread(callback_data);
317
+ }
318
+ return Qnil;
319
+ }
320
+
321
+ VALUE ph_manager_set_on_server_disconnect_handler(VALUE self, VALUE handler) {
322
+ ph_data_t *ph = get_ph_data(self);
323
+ ph_callback_data_t *callback_data = &ph->server_disconnect_callback;
324
+ if( TYPE(handler) == T_NIL ) {
325
+ callback_data->exit = true;
326
+ ph_raise(CPhidgetManager_set_OnServerDisconnect_Handler((CPhidgetManagerHandle)ph->handle, NULL, (void *)NULL));
327
+ } else {
328
+ callback_data->called = false;
329
+ callback_data->exit = false;
330
+ callback_data->phidget = self;
331
+ callback_data->callback = handler;
332
+ ph_raise(CPhidgetManager_set_OnServerDisconnect_Handler((CPhidgetManagerHandle)ph->handle, ph_manager_on_server_disconnect, (void *)callback_data));
333
+ ph_callback_thread(callback_data);
334
+ }
335
+ return Qnil;
336
+ }
337
+
338
+
339
+ int ph_manager_on_attach(CPhidgetHandle phid, void *userPtr) {
340
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
341
+ callback_data->called = true;
342
+ return EPHIDGET_OK;
343
+ }
344
+
345
+
346
+ int ph_manager_on_detach(CPhidgetHandle phid, void *userPtr) {
347
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
348
+ callback_data->called = true;
349
+ return EPHIDGET_OK;
350
+ }
351
+
352
+
353
+ int ph_manager_on_server_connect(CPhidgetManagerHandle phid, void *userPtr) {
354
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
355
+ callback_data->called = true;
356
+ return EPHIDGET_OK;
357
+ }
358
+
359
+
360
+ int ph_manager_on_server_disconnect(CPhidgetManagerHandle phid, void *userPtr) {
361
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
362
+ callback_data->called = true;
363
+ return EPHIDGET_OK;
364
+ }
365
+ #endif
366
+