phidgets 0.1.3 → 1.0.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 (146) hide show
  1. checksums.yaml +4 -4
  2. data/History.txt +3 -0
  3. data/README.rdoc +32 -43
  4. data/Rakefile +4 -2
  5. data/bin/phidget +18 -72
  6. data/ext/phidgets/extconf.rb +5 -8
  7. data/ext/phidgets/phidgets.c +708 -173
  8. data/ext/phidgets/phidgets.h +54 -35
  9. data/ext/phidgets/phidgets_accelerometer.c +193 -109
  10. data/ext/phidgets/phidgets_bldc_motor.c +529 -0
  11. data/ext/phidgets/phidgets_capacitive_touch.c +302 -0
  12. data/ext/phidgets/phidgets_common.c +570 -315
  13. data/ext/phidgets/phidgets_current_input.c +229 -0
  14. data/ext/phidgets/phidgets_dc_motor.c +562 -0
  15. data/ext/phidgets/phidgets_dictionary.c +154 -213
  16. data/ext/phidgets/phidgets_digital_input.c +127 -0
  17. data/ext/phidgets/phidgets_digital_output.c +288 -0
  18. data/ext/phidgets/phidgets_distance_sensor.c +295 -0
  19. data/ext/phidgets/phidgets_encoder.c +211 -192
  20. data/ext/phidgets/phidgets_frequency_counter.c +310 -177
  21. data/ext/phidgets/phidgets_gps.c +226 -164
  22. data/ext/phidgets/phidgets_gyroscope.c +195 -0
  23. data/ext/phidgets/phidgets_hub.c +39 -0
  24. data/ext/phidgets/phidgets_humidity_sensor.c +200 -0
  25. data/ext/phidgets/phidgets_ir.c +211 -171
  26. data/ext/phidgets/phidgets_lcd.c +512 -0
  27. data/ext/phidgets/phidgets_light_sensor.c +200 -0
  28. data/ext/phidgets/phidgets_log.c +263 -0
  29. data/ext/phidgets/phidgets_magnetometer.c +279 -0
  30. data/ext/phidgets/phidgets_manager.c +86 -297
  31. data/ext/phidgets/phidgets_motor_position_controller.c +787 -0
  32. data/ext/phidgets/phidgets_phsensor.c +200 -152
  33. data/ext/phidgets/phidgets_power_guard.c +144 -0
  34. data/ext/phidgets/phidgets_pressure_sensor.c +200 -0
  35. data/ext/phidgets/phidgets_rc_servo.c +672 -0
  36. data/ext/phidgets/phidgets_resistance_input.c +227 -0
  37. data/ext/phidgets/phidgets_rfid.c +107 -221
  38. data/ext/phidgets/phidgets_sound_sensor.c +284 -0
  39. data/ext/phidgets/phidgets_spatial.c +124 -318
  40. data/ext/phidgets/phidgets_stepper.c +457 -430
  41. data/ext/phidgets/phidgets_temp_sensor.c +223 -228
  42. data/ext/phidgets/phidgets_voltage_input.c +428 -0
  43. data/ext/phidgets/phidgets_voltage_output.c +167 -0
  44. data/ext/phidgets/phidgets_voltage_ratio_input.c +435 -0
  45. data/lib/phidgets.rb +21 -14
  46. data/lib/phidgets/accelerometer.rb +11 -15
  47. data/lib/phidgets/bldc_motor.rb +45 -0
  48. data/lib/phidgets/capacitive_touch.rb +33 -0
  49. data/lib/phidgets/common.rb +40 -69
  50. data/lib/phidgets/current_input.rb +21 -0
  51. data/lib/phidgets/dc_motor.rb +45 -0
  52. data/lib/phidgets/dictionary.rb +30 -39
  53. data/lib/phidgets/digital_input.rb +21 -0
  54. data/lib/phidgets/digital_output.rb +56 -0
  55. data/lib/phidgets/distance_sensor.rb +33 -0
  56. data/lib/phidgets/encoder.rb +1 -29
  57. data/lib/phidgets/frequency_counter.rb +23 -14
  58. data/lib/phidgets/gps.rb +34 -26
  59. data/lib/phidgets/gyroscope.rb +21 -0
  60. data/lib/phidgets/humidity_sensor.rb +21 -0
  61. data/lib/phidgets/ir.rb +34 -39
  62. data/lib/phidgets/light_sensor.rb +21 -0
  63. data/lib/phidgets/magnetometer.rb +21 -0
  64. data/lib/phidgets/manager.rb +18 -66
  65. data/lib/phidgets/motor_position_controller.rb +45 -0
  66. data/lib/phidgets/ph_sensor.rb +2 -6
  67. data/lib/phidgets/pressure_sensor.rb +21 -0
  68. data/lib/phidgets/rc_servo.rb +58 -0
  69. data/lib/phidgets/resistance_input.rb +21 -0
  70. data/lib/phidgets/rfid.rb +22 -38
  71. data/lib/phidgets/sound_sensor.rb +21 -0
  72. data/lib/phidgets/spatial.rb +11 -15
  73. data/lib/phidgets/stepper.rb +48 -50
  74. data/lib/phidgets/temperature_sensor.rb +11 -15
  75. data/lib/phidgets/version.rb +5 -0
  76. data/lib/phidgets/voltage_input.rb +34 -0
  77. data/lib/phidgets/voltage_output.rb +23 -0
  78. data/lib/phidgets/voltage_ratio_input.rb +34 -0
  79. data/phidgets.gemspec +3 -22
  80. data/test/test_accelerometer.rb +42 -23
  81. data/test/test_bldc_motor.rb +134 -0
  82. data/test/test_capacitive_touch.rb +82 -0
  83. data/test/test_common.rb +125 -108
  84. data/test/test_current_input.rb +62 -0
  85. data/test/test_dc_motor.rb +146 -0
  86. data/test/test_dictionary.rb +22 -54
  87. data/test/test_digital_input.rb +30 -0
  88. data/test/test_digital_output.rb +70 -0
  89. data/test/test_distance_sensor.rb +76 -0
  90. data/test/test_encoder.rb +45 -38
  91. data/test/test_frequency_counter.rb +71 -36
  92. data/test/test_gps.rb +29 -38
  93. data/test/test_gyroscope.rb +54 -0
  94. data/test/test_helper.rb +0 -1
  95. data/test/test_hub.rb +14 -0
  96. data/test/test_humidity_sensor.rb +58 -0
  97. data/test/test_ir.rb +34 -34
  98. data/test/test_lcd.rb +146 -0
  99. data/test/test_light_sensor.rb +58 -0
  100. data/test/test_magnetometer.rb +78 -0
  101. data/test/test_manager.rb +10 -79
  102. data/test/test_motor_control.rb +146 -108
  103. data/test/test_phidgets.rb +2 -14
  104. data/test/test_phsensor.rb +46 -34
  105. data/test/test_power_guard.rb +42 -0
  106. data/test/test_pressure_sensor.rb +58 -0
  107. data/test/test_rc_servo.rb +174 -0
  108. data/test/test_resistance_input.rb +66 -0
  109. data/test/test_rfid.rb +15 -54
  110. data/test/test_sound_sensor.rb +78 -0
  111. data/test/test_spatial.rb +19 -85
  112. data/test/test_stepper.rb +89 -98
  113. data/test/test_temp_sensor.rb +42 -47
  114. data/test/test_voltage_input.rb +102 -0
  115. data/test/test_voltage_output.rb +46 -0
  116. data/test/test_voltage_ratio_input.rb +102 -0
  117. metadata +72 -89
  118. data/ext/phidgets/phidgets_advanced_servo.c +0 -567
  119. data/ext/phidgets/phidgets_analog.c +0 -139
  120. data/ext/phidgets/phidgets_bridge.c +0 -263
  121. data/ext/phidgets/phidgets_interface_kit.c +0 -340
  122. data/ext/phidgets/phidgets_led.c +0 -178
  123. data/ext/phidgets/phidgets_motor_control.c +0 -642
  124. data/ext/phidgets/phidgets_servo.c +0 -276
  125. data/ext/phidgets/phidgets_text_lcd.c +0 -381
  126. data/ext/phidgets/phidgets_text_led.c +0 -107
  127. data/ext/phidgets/phidgets_weight_sensor.c +0 -113
  128. data/lib/phidgets/advanced_servo.rb +0 -49
  129. data/lib/phidgets/analog.rb +0 -8
  130. data/lib/phidgets/bridge.rb +0 -25
  131. data/lib/phidgets/interfacekit.rb +0 -49
  132. data/lib/phidgets/led.rb +0 -8
  133. data/lib/phidgets/motor_control.rb +0 -110
  134. data/lib/phidgets/servo.rb +0 -23
  135. data/lib/phidgets/text_lcd.rb +0 -8
  136. data/lib/phidgets/text_led.rb +0 -8
  137. data/lib/phidgets/weight_sensor.rb +0 -25
  138. data/test/test_advanced_servo.rb +0 -152
  139. data/test/test_analog.rb +0 -45
  140. data/test/test_bridge.rb +0 -77
  141. data/test/test_interfacekit.rb +0 -97
  142. data/test/test_led.rb +0 -55
  143. data/test/test_servo.rb +0 -67
  144. data/test/test_text_lcd.rb +0 -115
  145. data/test/test_text_led.rb +0 -35
  146. data/test/test_weight_sensor.rb +0 -32
@@ -2,277 +2,218 @@
2
2
  #include "phidgets.h"
3
3
 
4
4
 
5
- VALUE ph_dictionary_allocate(VALUE klass);
6
- void ph_dictionary_free(ph_data_t *ph);
7
- VALUE ph_dictionary_init(VALUE self);
8
- VALUE ph_dictionary_open_remote(VALUE self, VALUE server_id, VALUE password);
9
- VALUE ph_dictionary_open_remote_ip(VALUE self, VALUE address, VALUE port, VALUE password);
10
- VALUE ph_dictionary_close(VALUE self);
11
- VALUE ph_dictionary_get_key(VALUE self, VALUE key);
12
- VALUE ph_dictionary_add_key(VALUE self, VALUE key, VALUE value, VALUE persist);
13
- VALUE ph_dictionary_remove_key(VALUE self, VALUE pattern);
14
- VALUE ph_dictionary_get_server_id(VALUE self);
15
- VALUE ph_dictionary_get_server_address(VALUE self);
16
- VALUE ph_dictionary_get_server_status(VALUE self);
17
-
18
- #ifdef PH_CALLBACK
19
- VALUE ph_dictionary_set_on_key_change_handler(VALUE self, VALUE handler);
20
- VALUE ph_dictionary_remove_on_key_change_handler(VALUE self, VALUE listener);
21
- VALUE ph_dictionary_set_on_server_connect_handler(VALUE self, VALUE handler);
22
- VALUE ph_dictionary_set_on_server_disconnect_handler(VALUE self, VALUE handler);
23
- int ph_dictionary_on_server_connect(CPhidgetDictionaryHandle phid, void *userPtr);
24
- int ph_dictionary_on_server_disconnect(CPhidgetDictionaryHandle phid, void *userPtr);
25
- #endif
5
+ #define DICTIONARY_VALUE_LEN 512
6
+ #define DICTIONARY_ADD_CALLBACK 0
7
+ #define DICTIONARY_REMOVE_CALLBACK 0
8
+ #define DICTIONARY_UPDATE_CALLBACK 0
26
9
 
27
10
 
28
- void Init_dictionary() {
29
- VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
30
- VALUE ph_dictionary = rb_define_class_under(ph_module, "Dictionary", rb_cObject);
31
- rb_define_alloc_func(ph_dictionary, ph_dictionary_allocate);
32
-
33
- /* Document-method: new
34
- * call-seq: new
35
- *
36
- * Creates a Phidget Dictionary object.
37
- */
38
- rb_define_method(ph_dictionary, "initialize", ph_dictionary_init, 0);
39
-
40
- /* Document-method: openRemote
41
- * call-seq: openRemote(server_id, password)
42
- *
43
- * Opens a Phidget dictionary by ServerID. Note that this requires Bonjour (mDNS) to be running on both the host and the server.
44
- */
45
- rb_define_method(ph_dictionary, "openRemote", ph_dictionary_open_remote, 2);
46
-
47
- /* Document-method: openRemoteIP
48
- * call-seq: openRemoteIP(address, port, password)
49
- *
50
- * Opens a Phidget dictionary by address and port.
51
- */
52
- rb_define_method(ph_dictionary, "openRemoteIP", ph_dictionary_open_remote_ip, 3);
53
-
54
- /* Document-method: close
55
- * call-seq: close
56
- *
57
- * Closes the connection to a Phidget Dictionary.
58
- */
59
- rb_define_method(ph_dictionary, "close", ph_dictionary_close, 0);
60
-
61
- /* Document-method: getKey
62
- * call-seq: getKey(key) -> value
63
- *
64
- * Gets a key value. If more then one key matches, only the first value is returned.
65
- */
66
- rb_define_method(ph_dictionary, "getKey", ph_dictionary_get_key, 1);
67
-
68
- /* Document-method: addKey
69
- * call-seq: addKey(key, value, persistent)
70
- *
71
- * Adds a key/value pair to the dictionary. Or, changes an existing key's value.
72
- */
73
- rb_define_method(ph_dictionary, "addKey", ph_dictionary_add_key, 3);
74
-
75
- /* Document-method: removeKey
76
- * call-seq: removeKey(pattern)
77
- *
78
- * Removes a set of keys from the dictionary.
79
- */
80
- rb_define_method(ph_dictionary, "removeKey", ph_dictionary_remove_key, 1);
81
-
82
- /* Document-method: getServerID
83
- * call-seq: getServerID -> server_id
84
- *
85
- * Gets the server ID.
86
- */
87
- rb_define_method(ph_dictionary, "getServerID", ph_dictionary_get_server_id, 0);
88
-
89
- /* Document-method: getServerAddress
90
- * call-seq: getServerAddress -> [address, port]
91
- *
92
- * Gets the address and port.
93
- */
94
- rb_define_method(ph_dictionary, "getServerAddress", ph_dictionary_get_server_address, 0);
95
-
96
- /* Document-method: getServerStatus
97
- * call-seq: getServerStatus -> status
98
- *
99
- * Gets the connected to server status.
100
- */
101
- rb_define_method(ph_dictionary, "getServerStatus", ph_dictionary_get_server_status, 0);
102
-
103
- #ifdef PH_CALLBACK
104
-
105
- /* Document-method: setOnKeyChangeHandler
106
- * call-seq: setOnKeyChangeHandler(proc=nil, &block)
107
- *
108
- * ** NOT IMPLEMENTED **
109
- */
110
- rb_define_method(ph_dictionary, "setOnKeyChangeHandler", ph_dictionary_set_on_key_change_handler, 1);
111
-
112
- /* Document-method: removeOnKeyChangeHandler
113
- * call-seq: removeOnKeyChangeHandler(listener)
114
- *
115
- * ** NOT IMPLEMENTED **
116
- */
117
- rb_define_method(ph_dictionary, "removeOnKeyChangeHandler", ph_dictionary_remove_on_key_change_handler, 1);
118
- rb_define_private_method(ph_dictionary, "ext_setOnServerConnectHandler", ph_dictionary_set_on_server_connect_handler, 1);
119
- rb_define_private_method(ph_dictionary, "ext_setOnServerDisconnectHandler", ph_dictionary_set_on_server_disconnect_handler, 1);
120
- rb_define_alias(ph_dictionary, "on_key_change", "setOnKeyChangeHandler");
121
- rb_define_alias(ph_dictionary, "remove_on_key_change", "removeOnKeyChangeHandler");
122
- #endif
123
-
124
- rb_define_alias(ph_dictionary, "open_remote", "openRemote");
125
- rb_define_alias(ph_dictionary, "open_remote_ip", "openRemoteIP");
126
- rb_define_alias(ph_dictionary, "server_id", "getServerID");
127
- rb_define_alias(ph_dictionary, "get_key", "getKey");
128
- rb_define_alias(ph_dictionary, "add_key", "addKey");
129
- rb_define_alias(ph_dictionary, "remove_key", "removeKey");
130
- rb_define_alias(ph_dictionary, "server_address", "getServerAddress");
131
- rb_define_alias(ph_dictionary, "server_status", "getServerStatus");
132
- }
133
-
134
-
135
-
136
- VALUE ph_dictionary_allocate(VALUE klass) {
137
- ph_data_t *ph;
138
- VALUE self = Data_Make_Struct(klass, ph_data_t, 0, ph_dictionary_free, ph);
139
- memset(ph, 0, sizeof(ph_data_t));
140
- return self;
141
- }
142
-
143
- void ph_dictionary_free(ph_data_t *ph) {
144
- if (ph && ph->handle) {
145
- CPhidgetDictionary_close((CPhidgetDictionaryHandle)ph->handle);
146
- CPhidgetDictionary_delete((CPhidgetDictionaryHandle)ph->handle);
147
- ph->handle = NULL;
148
- }
149
- if(ph) xfree(ph);
150
- }
151
-
152
11
  VALUE ph_dictionary_init(VALUE self) {
153
12
  ph_data_t *ph = get_ph_data(self);
154
- ph_raise(CPhidgetDictionary_create((CPhidgetDictionaryHandle *)(&(ph->handle))));
13
+ ph_raise(PhidgetDictionary_create((PhidgetDictionaryHandle *)(&(ph->handle))));
155
14
  return self;
156
15
  }
157
16
 
158
- VALUE ph_dictionary_open_remote(VALUE self, VALUE server_id, VALUE password) {
159
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
160
- ph_raise(CPhidgetDictionary_openRemote(handle, StringValueCStr(server_id), StringValueCStr(password)));
17
+ VALUE ph_dictionary_add(VALUE self, VALUE key, VALUE value) {
18
+ ph_raise(PhidgetDictionary_add((PhidgetDictionaryHandle)get_ph_handle(self), StringValueCStr(key), StringValueCStr(value)));
161
19
  return Qnil;
162
20
  }
163
21
 
164
- VALUE ph_dictionary_open_remote_ip(VALUE self, VALUE address, VALUE port, VALUE password) {
165
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
166
- ph_raise(CPhidgetDictionary_openRemoteIP(handle, StringValueCStr(address), FIX2INT(port), StringValueCStr(password)));
167
- return Qnil;
22
+ VALUE ph_dictionary_get(VALUE self, VALUE key) {
23
+ char value[DICTIONARY_VALUE_LEN];
24
+ ph_raise(PhidgetDictionary_get((PhidgetDictionaryHandle)get_ph_handle(self), StringValueCStr(key), value, DICTIONARY_VALUE_LEN));
25
+ return rb_str_new2(value);
168
26
  }
169
27
 
170
- VALUE ph_dictionary_close(VALUE self) {
171
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
172
- ph_raise(CPhidgetDictionary_close(handle));
28
+ VALUE ph_dictionary_remove(VALUE self, VALUE key) {
29
+ ph_raise(PhidgetDictionary_remove((PhidgetDictionaryHandle)get_ph_handle(self), StringValueCStr(key)));
173
30
  return Qnil;
174
31
  }
175
32
 
176
- VALUE ph_dictionary_get_key(VALUE self, VALUE key) {
177
- #define VALUE_LEN 512
178
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
179
- char value[VALUE_LEN];
180
- ph_raise(CPhidgetDictionary_getKey(handle, StringValueCStr(key), value, VALUE_LEN));
181
- return rb_str_new2(value);
33
+ VALUE ph_dictionary_remove_all(VALUE self) {
34
+ ph_raise(PhidgetDictionary_removeAll((PhidgetDictionaryHandle)get_ph_handle(self)));
35
+ return Qnil;
182
36
  }
183
37
 
184
- VALUE ph_dictionary_add_key(VALUE self, VALUE key, VALUE value, VALUE persist) {
185
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
186
- ph_raise(CPhidgetDictionary_addKey(handle, StringValueCStr(key), StringValueCStr(value), TYPE(persist) == T_TRUE ? PTRUE : PFALSE));
187
- return Qnil;
38
+ VALUE ph_dictionary_scan(VALUE self, VALUE start_key) {
39
+ char list[DICTIONARY_VALUE_LEN * 10];
40
+ ph_raise(PhidgetDictionary_scan((PhidgetDictionaryHandle)get_ph_handle(self), StringValueCStr(start_key), list, DICTIONARY_VALUE_LEN * 10));
41
+ return rb_str_new2(list);
188
42
  }
189
43
 
190
- VALUE ph_dictionary_remove_key(VALUE self, VALUE pattern) {
191
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
192
- ph_raise(CPhidgetDictionary_removeKey(handle, StringValueCStr(pattern)));
44
+ VALUE ph_dictionary_set(VALUE self, VALUE key, VALUE value) {
45
+ ph_raise(PhidgetDictionary_set((PhidgetDictionaryHandle)get_ph_handle(self), StringValueCStr(key), StringValueCStr(value)));
193
46
  return Qnil;
194
47
  }
195
48
 
196
- VALUE ph_dictionary_get_server_id(VALUE self) {
197
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
198
- const char *server_id;
199
- ph_raise(CPhidgetDictionary_getServerID(handle, &server_id));
200
- return rb_str_new2(server_id);
49
+ VALUE ph_dictionary_update(VALUE self, VALUE key, VALUE value) {
50
+ ph_raise(PhidgetDictionary_update((PhidgetDictionaryHandle)get_ph_handle(self), StringValueCStr(key), StringValueCStr(value)));
51
+ return Qnil;
201
52
  }
202
53
 
203
- VALUE ph_dictionary_get_server_address(VALUE self) {
204
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
205
- const char *address;
206
- int port;
207
- ph_raise(CPhidgetDictionary_getServerAddress(handle, &address, &port));
208
- return rb_ary_new3(2, rb_str_new2(address), INT2FIX(port));
209
- }
210
54
 
211
- VALUE ph_dictionary_get_server_status(VALUE self) {
212
- CPhidgetDictionaryHandle handle = (CPhidgetDictionaryHandle)get_ph_handle(self);
213
- int server_status;
214
- ph_raise(CPhidgetDictionary_getServerStatus(handle, &server_status));
215
- return INT2FIX(server_status);
55
+ void CCONV ph_dictionary_on_add(PhidgetDictionaryHandle phid, void *userPtr, const char *key, const char *value) {
56
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
57
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
58
+ callback_data->arg1 = rb_str_new2(key);
59
+ callback_data->arg2 = rb_str_new2(value);
60
+ callback_data->arg3 = Qnil;
61
+ callback_data->arg4 = Qnil;
62
+ sem_post(&callback_data->callback_called);
216
63
  }
217
64
 
218
65
 
219
- #ifdef PH_CALLBACK
220
- VALUE ph_dictionary_set_on_key_change_handler(VALUE self, VALUE handler) {
221
- ph_raise(EPHIDGET_NOTIMPLEMENTED);
66
+ VALUE ph_dictionary_set_on_add_handler(VALUE self, VALUE handler) {
67
+ ph_data_t *ph = get_ph_data(self);
68
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DICTIONARY_ADD_CALLBACK];
69
+ if( TYPE(handler) == T_NIL ) {
70
+ callback_data->callback = T_NIL;
71
+ callback_data->exit = true;
72
+ ph_raise(PhidgetDictionary_setOnAddHandler((PhidgetDictionaryHandle)ph->handle, NULL, (void *)NULL));
73
+ sem_post(&callback_data->callback_called);
74
+ } else {
75
+ callback_data->exit = false;
76
+ callback_data->phidget = self;
77
+ callback_data->callback = handler;
78
+ ph_raise(PhidgetDictionary_setOnAddHandler((PhidgetDictionaryHandle)ph->handle, ph_dictionary_on_add, (void *)callback_data));
79
+ ph_callback_thread(callback_data);
80
+ }
222
81
  return Qnil;
223
82
  }
224
83
 
225
- VALUE ph_dictionary_remove_on_key_change_handler(VALUE self, VALUE listener) {
226
- ph_raise(EPHIDGET_NOTIMPLEMENTED);
227
- return Qnil;
84
+
85
+ void CCONV ph_dictionary_on_remove(PhidgetDictionaryHandle phid, void *userPtr, const char *key) {
86
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
87
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
88
+ callback_data->arg1 = rb_str_new2(key);
89
+ callback_data->arg2 = Qnil;
90
+ callback_data->arg3 = Qnil;
91
+ callback_data->arg4 = Qnil;
92
+ sem_post(&callback_data->callback_called);
228
93
  }
229
94
 
230
- VALUE ph_dictionary_set_on_server_connect_handler(VALUE self, VALUE handler) {
95
+
96
+ VALUE ph_dictionary_set_on_remove_handler(VALUE self, VALUE handler) {
231
97
  ph_data_t *ph = get_ph_data(self);
232
- ph_callback_data_t *callback_data = &ph->server_connect_callback;
98
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DICTIONARY_REMOVE_CALLBACK];
233
99
  if( TYPE(handler) == T_NIL ) {
100
+ callback_data->callback = T_NIL;
234
101
  callback_data->exit = true;
235
- ph_raise(CPhidgetDictionary_set_OnServerConnect_Handler((CPhidgetDictionaryHandle)ph->handle, NULL, (void *)NULL));
102
+ ph_raise(PhidgetDictionary_setOnRemoveHandler((PhidgetDictionaryHandle)ph->handle, NULL, (void *)NULL));
103
+ sem_post(&callback_data->callback_called);
236
104
  } else {
237
- callback_data->called = false;
238
105
  callback_data->exit = false;
239
106
  callback_data->phidget = self;
240
107
  callback_data->callback = handler;
241
- ph_raise(CPhidgetDictionary_set_OnServerConnect_Handler((CPhidgetDictionaryHandle)ph->handle, ph_dictionary_on_server_connect, (void *)callback_data));
108
+ ph_raise(PhidgetDictionary_setOnRemoveHandler((PhidgetDictionaryHandle)ph->handle, ph_dictionary_on_remove, (void *)callback_data));
242
109
  ph_callback_thread(callback_data);
243
110
  }
244
111
  return Qnil;
245
112
  }
246
113
 
247
- VALUE ph_dictionary_set_on_server_disconnect_handler(VALUE self, VALUE handler) {
114
+
115
+ void CCONV ph_dictionary_on_update(PhidgetDictionaryHandle phid, void *userPtr, const char *key, const char *value) {
116
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
117
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
118
+ callback_data->arg1 = rb_str_new2(key);
119
+ callback_data->arg2 = rb_str_new2(value);
120
+ callback_data->arg3 = Qnil;
121
+ callback_data->arg4 = Qnil;
122
+ sem_post(&callback_data->callback_called);
123
+ }
124
+
125
+
126
+ VALUE ph_dictionary_set_on_update_handler(VALUE self, VALUE handler) {
248
127
  ph_data_t *ph = get_ph_data(self);
249
- ph_callback_data_t *callback_data = &ph->server_disconnect_callback;
128
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DICTIONARY_UPDATE_CALLBACK];
250
129
  if( TYPE(handler) == T_NIL ) {
130
+ callback_data->callback = T_NIL;
251
131
  callback_data->exit = true;
252
- ph_raise(CPhidgetDictionary_set_OnServerDisconnect_Handler((CPhidgetDictionaryHandle)ph->handle, NULL, (void *)NULL));
132
+ ph_raise(PhidgetDictionary_setOnUpdateHandler((PhidgetDictionaryHandle)ph->handle, NULL, (void *)NULL));
133
+ sem_post(&callback_data->callback_called);
253
134
  } else {
254
- callback_data->called = false;
255
135
  callback_data->exit = false;
256
136
  callback_data->phidget = self;
257
137
  callback_data->callback = handler;
258
- ph_raise(CPhidgetDictionary_set_OnServerDisconnect_Handler((CPhidgetDictionaryHandle)ph->handle, ph_dictionary_on_server_disconnect, (void *)callback_data));
138
+ ph_raise(PhidgetDictionary_setOnUpdateHandler((PhidgetDictionaryHandle)ph->handle, ph_dictionary_on_update, (void *)callback_data));
259
139
  ph_callback_thread(callback_data);
260
140
  }
261
141
  return Qnil;
262
142
  }
263
143
 
264
144
 
265
- int ph_dictionary_on_server_connect(CPhidgetDictionaryHandle phid, void *userPtr) {
266
- ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
267
- callback_data->called = true;
268
- return EPHIDGET_OK;
269
- }
270
145
 
271
146
 
272
- int ph_dictionary_on_server_disconnect(CPhidgetDictionaryHandle phid, void *userPtr) {
273
- ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
274
- callback_data->called = true;
275
- return EPHIDGET_OK;
276
- }
277
- #endif
147
+ void Init_dictionary() {
148
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
149
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
150
+ VALUE ph_dictionary = rb_define_class_under(ph_module, "Dictionary", ph_common);
151
+
152
+ /* Document-method: new
153
+ * call-seq: new
154
+ *
155
+ * Creates a Phidget Dictionary object.
156
+ */
157
+ rb_define_method(ph_dictionary, "initialize", ph_dictionary_init, 0);
158
+
159
+ /* Document-method: add
160
+ * call-seq: add(key, value)
161
+ *
162
+ * Adds a new key value pair to the dictionary. It is an error if the key already exits.
163
+ */
164
+ rb_define_method(ph_dictionary, "add", ph_dictionary_add, 2);
165
+
166
+ /* Document-method: get
167
+ * call-seq: get(key) -> value
168
+ *
169
+ * Gets a key value. If more then one key matches, only the first value is returned.
170
+ */
171
+ rb_define_method(ph_dictionary, "get", ph_dictionary_get, 1);
172
+
173
+ /* Document-method: remove
174
+ * call-seq: remove(key)
175
+ *
176
+ * Removes the key from the dictionary.
177
+ */
178
+ rb_define_method(ph_dictionary, "remove", ph_dictionary_remove, 1);
278
179
 
180
+ /* Document-method: removeAll
181
+ * call-seq: removeAll
182
+ *
183
+ * Removes every key from the dictionary.
184
+ */
185
+ rb_define_method(ph_dictionary, "removeAll", ph_dictionary_remove_all, 0);
186
+ rb_define_alias(ph_dictionary, "remove_all", "removeAll");
187
+
188
+ /* Document-method: scan
189
+ * call-seq: scan(start_key)
190
+ *
191
+ * Scans the keys in the dictionary, indexed by start or the first key in the dictionary if start is NULL or an empty String.
192
+ * The result is formated as a newline seperated list of keys
193
+ * The list begins at the key following the start key
194
+ * The list might not contain all of the keys in the dictionary
195
+ * To continue scanning, call the method again with the last entry from the previous result
196
+ * When all of the keys have been scanned, a zero length string is returned
197
+ * Keys added during the scan may be missed, and keys deleted during the scan may be included.
198
+ */
199
+ rb_define_method(ph_dictionary, "scan", ph_dictionary_scan, 1);
200
+
201
+ /* Document-method: set
202
+ * call-seq: set(key, value)
203
+ *
204
+ * Sets the value of a key, or creates the key value pair if the key does not already exist.
205
+ */
206
+ rb_define_method(ph_dictionary, "set", ph_dictionary_set, 2);
207
+
208
+ /* Document-method: update
209
+ * call-seq: update(key, value)
210
+ *
211
+ * Updates a key value pair in the dictionary. It is an error if the key does not exist.
212
+ */
213
+ rb_define_method(ph_dictionary, "update", ph_dictionary_update, 2);
214
+
215
+
216
+ rb_define_private_method(ph_dictionary, "ext_setOnAddHandler", ph_dictionary_set_on_add_handler, 1);
217
+ rb_define_private_method(ph_dictionary, "ext_setOnRemoveHandler", ph_dictionary_set_on_remove_handler, 1);
218
+ rb_define_private_method(ph_dictionary, "ext_setOnUpdateHandler", ph_dictionary_set_on_update_handler, 1);
219
+ }
@@ -0,0 +1,127 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+ #define DIGITAL_INPUT_STATE_CHANGE_CALLBACK 0
5
+
6
+
7
+ VALUE ph_digital_input_init(VALUE self) {
8
+ ph_data_t *ph = get_ph_data(self);
9
+ ph_raise(PhidgetDigitalInput_create((PhidgetDigitalInputHandle *)(&(ph->handle))));
10
+ return self;
11
+ }
12
+
13
+ VALUE ph_digital_input_get_input_mode(VALUE self) {
14
+ return ph_get_int(get_ph_handle(self), (phidget_get_int_func)PhidgetDigitalInput_getInputMode);
15
+ }
16
+
17
+ VALUE ph_digital_input_set_input_mode(VALUE self, VALUE mode) {
18
+ ph_raise(PhidgetDigitalInput_setInputMode((PhidgetDigitalInputHandle)get_ph_handle(self), NUM2INT(mode)));
19
+ return Qnil;
20
+ }
21
+
22
+ VALUE ph_digital_input_get_power_supply(VALUE self) {
23
+ return ph_get_int(get_ph_handle(self), (phidget_get_int_func)PhidgetDigitalInput_getPowerSupply);
24
+ }
25
+
26
+ VALUE ph_digital_input_set_power_supply(VALUE self, VALUE power_supply) {
27
+ ph_raise(PhidgetDigitalInput_setPowerSupply((PhidgetDigitalInputHandle)get_ph_handle(self), NUM2INT(power_supply)));
28
+ return Qnil;
29
+ }
30
+
31
+ VALUE ph_digital_input_get_state(VALUE self) {
32
+ return ph_get_bool(get_ph_handle(self), (phidget_get_bool_func)PhidgetDigitalInput_getState);
33
+ }
34
+
35
+
36
+ void CCONV ph_digital_input_on_state_change(PhidgetDigitalInputHandle phid, void *userPtr, int state) {
37
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
38
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
39
+ callback_data->arg1 = state == PTRUE ? Qtrue : Qfalse;
40
+ callback_data->arg2 = Qnil;
41
+ callback_data->arg3 = Qnil;
42
+ callback_data->arg4 = Qnil;
43
+ sem_post(&callback_data->callback_called);
44
+ }
45
+
46
+ VALUE ph_digital_input_set_on_state_change_handler(VALUE self, VALUE handler) {
47
+ ph_data_t *ph = get_ph_data(self);
48
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DIGITAL_INPUT_STATE_CHANGE_CALLBACK];
49
+ if( TYPE(handler) == T_NIL ) {
50
+ callback_data->callback = T_NIL;
51
+ callback_data->exit = true;
52
+ ph_raise(PhidgetDigitalInput_setOnStateChangeHandler((PhidgetDigitalInputHandle)ph->handle, NULL, (void *)NULL));
53
+ sem_post(&callback_data->callback_called);
54
+ } else {
55
+ callback_data->exit = false;
56
+ callback_data->phidget = self;
57
+ callback_data->callback = handler;
58
+ ph_raise(PhidgetDigitalInput_setOnStateChangeHandler((PhidgetDigitalInputHandle)ph->handle, ph_digital_input_on_state_change, (void *)callback_data));
59
+ ph_callback_thread(callback_data);
60
+ }
61
+ return Qnil;
62
+ }
63
+
64
+
65
+ void Init_digital_input() {
66
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
67
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
68
+ VALUE ph_digital_input = rb_define_class_under(ph_module, "DigitalInput", ph_common);
69
+
70
+
71
+ /* Document-method: new
72
+ * call-seq: new
73
+ *
74
+ * Creates a Phidget DigitalInput object.
75
+ */
76
+ rb_define_method(ph_digital_input, "initialize", ph_digital_input_init, 0);
77
+
78
+ /* Document-method: getInputMode
79
+ * call-seq: getInputMode -> mode
80
+ *
81
+ * The input polarity mode for your channel.
82
+ * See your device's User Guide for more information about what value to chooose for the InputMode.
83
+ */
84
+ rb_define_method(ph_digital_input, "getInputMode", ph_digital_input_get_input_mode, 0);
85
+ rb_define_alias(ph_digital_input, "input_mode", "getInputMode");
86
+
87
+ /* Document-method: setInputMode
88
+ * call-seq: setInputMode(mode)
89
+ *
90
+ * The input polarity mode for your channel.
91
+ * See your device's User Guide for more information about what value to chooose for the InputMode.
92
+ */
93
+ rb_define_method(ph_digital_input, "setInputMode", ph_digital_input_set_input_mode, 1);
94
+ rb_define_alias(ph_digital_input, "input_mode=", "setInputMode");
95
+
96
+ /* Document-method: getPowerSupply
97
+ * call-seq: getPowerSupply -> power_supply
98
+ *
99
+ * Choose the power supply voltage.
100
+ * Set this to the voltage specified in the attached sensor's data sheet to power it.
101
+ * Set to POWER_SUPPLY_OFF to turn off the supply to save power.
102
+ */
103
+ rb_define_method(ph_digital_input, "getPowerSupply", ph_digital_input_get_power_supply, 0);
104
+ rb_define_alias(ph_digital_input, "power_supply", "getPowerSupply");
105
+
106
+ /* Document-method: setPowerSupply
107
+ * call-seq: setPowerSupply(power_supply)
108
+ *
109
+ * Choose the power supply voltage.
110
+ * Set this to the voltage specified in the attached sensor's data sheet to power it.
111
+ * Set to POWER_SUPPLY_OFF to turn off the supply to save power.
112
+ */
113
+ rb_define_method(ph_digital_input, "setPowerSupply", ph_digital_input_set_power_supply, 1);
114
+ rb_define_alias(ph_digital_input, "power_supply=", "setPowerSupply");
115
+
116
+ /* Document-method: getState
117
+ * call-seq: getState -> state
118
+ *
119
+ * The most recent state value that the channel has reported.
120
+ */
121
+ rb_define_method(ph_digital_input, "getState", ph_digital_input_get_state, 0);
122
+ rb_define_alias(ph_digital_input, "state", "getState");
123
+
124
+
125
+ rb_define_private_method(ph_digital_input, "ext_setOnStateChangeHandler", ph_digital_input_set_on_state_change_handler, 1);
126
+ }
127
+