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,340 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+
5
+ VALUE ph_ifkit_init(VALUE self);
6
+ VALUE ph_ifkit_get_input_count(VALUE self);
7
+ VALUE ph_ifkit_get_input_state(VALUE self, VALUE index);
8
+ VALUE ph_ifkit_get_output_count(VALUE self);
9
+ VALUE ph_ifkit_get_output_state(VALUE self, VALUE index);
10
+ VALUE ph_ifkit_set_output_state(VALUE self, VALUE index, VALUE state);
11
+ VALUE ph_ifkit_get_sensor_count(VALUE self);
12
+ VALUE ph_ifkit_get_sensor_value(VALUE self, VALUE index);
13
+ VALUE ph_ifkit_get_sensor_raw_value(VALUE self, VALUE index);
14
+ VALUE ph_ifkit_get_ratiometric(VALUE self);
15
+ VALUE ph_ifkit_set_ratiometric(VALUE self, VALUE ratiometric);
16
+ VALUE ph_ifkit_get_data_rate(VALUE self, VALUE index);
17
+ VALUE ph_ifkit_get_data_rate_min(VALUE self, VALUE index);
18
+ VALUE ph_ifkit_get_data_rate_max(VALUE self, VALUE index);
19
+ VALUE ph_ifkit_set_data_rate(VALUE self, VALUE index, VALUE rate);
20
+
21
+ #ifdef PH_CALLBACK
22
+ VALUE ph_ifkit_set_on_input_change_handler(VALUE self, VALUE handler);
23
+ VALUE ph_ifkit_set_on_output_change_handler(VALUE self, VALUE handler);
24
+ VALUE ph_ifkit_set_on_sensor_change_handler(VALUE self, VALUE handler);
25
+ int ph_ifkit_on_input_change(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int state);
26
+ int ph_ifkit_on_output_change(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int state);
27
+ int ph_ifkit_on_sensor_change(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int value);
28
+ #endif
29
+
30
+
31
+ VALUE Init_phidgets_ifkit(VALUE ph_module, VALUE ph_common) {
32
+ VALUE ph_ifkit = rb_define_class_under(ph_module, "InterfaceKit", ph_common);
33
+
34
+ /* Document-method: new
35
+ * call-seq: new
36
+ *
37
+ * Creates a Phidget InterfaceKit object.
38
+ */
39
+ rb_define_method(ph_ifkit, "initialize", ph_ifkit_init, 0);
40
+
41
+ /* Document-method: getInputCount
42
+ * call-seq: getInputCount -> count
43
+ *
44
+ * Gets the number of digital inputs supported by this board.
45
+ */
46
+ rb_define_method(ph_ifkit, "getInputCount", ph_ifkit_get_input_count, 0);
47
+
48
+ /* Document-method: getInputState
49
+ * call-seq: getInputState(index) -> true or false
50
+ *
51
+ * Gets the state of a digital input.
52
+ */
53
+ rb_define_method(ph_ifkit, "getInputState", ph_ifkit_get_input_state, 1);
54
+
55
+ /* Document-method: getOutputCount
56
+ * call-seq: getOutputCount -> count
57
+ *
58
+ * Gets the number of digital outputs supported by this board.
59
+ */
60
+ rb_define_method(ph_ifkit, "getOutputCount", ph_ifkit_get_output_count, 0);
61
+
62
+ /* Document-method: getOutputState
63
+ * call-seq: getOutputState(index) -> true or false
64
+ *
65
+ * Gets the state of a digital output.
66
+ */
67
+ rb_define_method(ph_ifkit, "getOutputState", ph_ifkit_get_output_state, 1);
68
+
69
+ /* Document-method: setOutputState
70
+ * call-seq: setOutputState(index, state)
71
+ *
72
+ * Sets the state of a digital output.
73
+ */
74
+ rb_define_method(ph_ifkit, "setOutputState", ph_ifkit_set_output_state, 2);
75
+
76
+ /* Document-method: getSensorCount
77
+ * call-seq: getSensorCount -> count
78
+ *
79
+ * Gets the number of sensor (analog) inputs supported by this board.
80
+ */
81
+ rb_define_method(ph_ifkit, "getSensorCount", ph_ifkit_get_sensor_count, 0);
82
+
83
+ /* Document-method: getSensorValue
84
+ * call-seq: getSensorValue(index) -> value
85
+ *
86
+ * Gets a sensor value (0-1000).
87
+ */
88
+ rb_define_method(ph_ifkit, "getSensorValue", ph_ifkit_get_sensor_value, 1);
89
+
90
+ /* Document-method: getSensorRawValue
91
+ * call-seq: getSensorRawValue(index) -> value
92
+ *
93
+ * Gets a sensor raw value (12-bit).
94
+ */
95
+ rb_define_method(ph_ifkit, "getSensorRawValue", ph_ifkit_get_sensor_raw_value, 1);
96
+
97
+ /* Document-method: getRatiometric
98
+ * call-seq: getRatiometric -> true or false
99
+ *
100
+ * Gets the ratiometric state for this board.
101
+ */
102
+ rb_define_method(ph_ifkit, "getRatiometric", ph_ifkit_get_ratiometric, 0);
103
+
104
+ /* Document-method: setRatiometric
105
+ * call-seq: setRatiometric(state)
106
+ *
107
+ * Sets the ratiometric state for this board.
108
+ */
109
+ rb_define_method(ph_ifkit, "setRatiometric", ph_ifkit_set_ratiometric, 1);
110
+
111
+ /* Document-method: getDataRate
112
+ * call-seq: getDataRate(index) -> milliseconds
113
+ *
114
+ * Gets the Data Rate for an analog input.
115
+ */
116
+ rb_define_method(ph_ifkit, "getDataRate", ph_ifkit_get_data_rate, 1);
117
+
118
+ /* Document-method: getDataRateMin
119
+ * call-seq: getDataRateMin(index) -> milliseconds
120
+ *
121
+ * Gets the minimum supported data rate for an analog input
122
+ */
123
+ rb_define_method(ph_ifkit, "getDataRateMin", ph_ifkit_get_data_rate_min, 1);
124
+
125
+ /* Document-method: getDataRateMax
126
+ * call-seq: getDataRateMax(index) -> milliseconds
127
+ *
128
+ * Gets the maximum supported data rate for an analog input
129
+ */
130
+ rb_define_method(ph_ifkit, "getDataRateMax", ph_ifkit_get_data_rate_max, 1);
131
+
132
+ /* Document-method: setDataRate
133
+ * call-seq: setDataRate(index, milliseconds)
134
+ *
135
+ * Gets the maximum supported data rate for an analog input
136
+ */
137
+ rb_define_method(ph_ifkit, "setDataRate", ph_ifkit_set_data_rate, 2);
138
+
139
+ #ifdef PH_CALLBACK
140
+ rb_define_private_method(ph_ifkit, "ext_setOnInputChangeHandler", ph_ifkit_set_on_input_change_handler, 1);
141
+ rb_define_private_method(ph_ifkit, "ext_setOnOutputChangeHandler", ph_ifkit_set_on_output_change_handler, 1);
142
+ rb_define_private_method(ph_ifkit, "ext_setOnSensorChangeHandler", ph_ifkit_set_on_sensor_change_handler, 1);
143
+ #endif
144
+
145
+ rb_define_alias(ph_ifkit, "input_count", "getInputCount");
146
+ rb_define_alias(ph_ifkit, "input_state", "getInputState");
147
+ rb_define_alias(ph_ifkit, "output_count", "getOutputCount");
148
+ rb_define_alias(ph_ifkit, "output_state", "getOutputState");
149
+ rb_define_alias(ph_ifkit, "set_output_state", "setOutputState");
150
+ rb_define_alias(ph_ifkit, "sensor_count", "getSensorCount");
151
+ rb_define_alias(ph_ifkit, "sensor_value", "getSensorValue");
152
+ rb_define_alias(ph_ifkit, "sensor_raw_value", "getSensorRawValue");
153
+ rb_define_alias(ph_ifkit, "ratiometric?", "getRatiometric");
154
+ rb_define_alias(ph_ifkit, "ratiometric=", "setRatiometric");
155
+ rb_define_alias(ph_ifkit, "data_rate", "getDataRate");
156
+ rb_define_alias(ph_ifkit, "data_rate_min", "getDataRateMin");
157
+ rb_define_alias(ph_ifkit, "data_rate_max", "getDataRateMax");
158
+ rb_define_alias(ph_ifkit, "set_data_rate", "setDataRate");
159
+
160
+ return ph_ifkit;
161
+ }
162
+
163
+
164
+
165
+ VALUE ph_ifkit_init(VALUE self) {
166
+ ph_data_t *ph = get_ph_data(self);
167
+ ph_raise(CPhidgetInterfaceKit_create((CPhidgetInterfaceKitHandle *)(&(ph->handle))));
168
+ return self;
169
+ }
170
+
171
+ VALUE ph_ifkit_get_input_count(VALUE self) {
172
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
173
+ int count;
174
+ ph_raise(CPhidgetInterfaceKit_getInputCount(handle, &count));
175
+ return INT2FIX(count);
176
+ }
177
+
178
+ VALUE ph_ifkit_get_input_state(VALUE self, VALUE index) {
179
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
180
+ int state;
181
+ ph_raise(CPhidgetInterfaceKit_getInputState(handle, FIX2INT(index), &state));
182
+ return state == PTRUE ? Qtrue : Qfalse;
183
+ }
184
+
185
+ VALUE ph_ifkit_get_output_count(VALUE self) {
186
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
187
+ int count;
188
+ ph_raise(CPhidgetInterfaceKit_getOutputCount(handle, &count));
189
+ return INT2FIX(count);
190
+ }
191
+
192
+ VALUE ph_ifkit_get_output_state(VALUE self, VALUE index) {
193
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
194
+ int state;
195
+ ph_raise(CPhidgetInterfaceKit_getOutputState(handle, FIX2INT(index), &state));
196
+ return state == PTRUE ? Qtrue : Qfalse;
197
+ }
198
+
199
+ VALUE ph_ifkit_set_output_state(VALUE self, VALUE index, VALUE state) {
200
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
201
+ ph_raise(CPhidgetInterfaceKit_setOutputState(handle, FIX2INT(index), TYPE(state) == T_TRUE ? PTRUE : PFALSE));
202
+ return Qnil;
203
+ }
204
+
205
+ VALUE ph_ifkit_get_sensor_count(VALUE self) {
206
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
207
+ int count;
208
+ ph_raise(CPhidgetInterfaceKit_getSensorCount(handle, &count));
209
+ return INT2FIX(count);
210
+ }
211
+
212
+ VALUE ph_ifkit_get_sensor_value(VALUE self, VALUE index) {
213
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
214
+ int value;
215
+ ph_raise(CPhidgetInterfaceKit_getSensorValue(handle, FIX2INT(index), &value));
216
+ return INT2FIX(value);
217
+ }
218
+
219
+ VALUE ph_ifkit_get_sensor_raw_value(VALUE self, VALUE index) {
220
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
221
+ int value;
222
+ ph_raise(CPhidgetInterfaceKit_getSensorRawValue(handle, FIX2INT(index), &value));
223
+ return INT2FIX(value);
224
+ }
225
+
226
+ VALUE ph_ifkit_get_ratiometric(VALUE self) {
227
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
228
+ int ratiometric;
229
+ ph_raise(CPhidgetInterfaceKit_getRatiometric(handle, &ratiometric));
230
+ return ratiometric == PTRUE ? Qtrue : Qfalse;
231
+ }
232
+
233
+ VALUE ph_ifkit_set_ratiometric(VALUE self, VALUE ratiometric) {
234
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
235
+ ph_raise(CPhidgetInterfaceKit_setRatiometric(handle, TYPE(ratiometric) == T_TRUE ? PTRUE : PFALSE));
236
+ return Qnil;
237
+ }
238
+
239
+ VALUE ph_ifkit_get_data_rate(VALUE self, VALUE index) {
240
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
241
+ int rate;
242
+ ph_raise(CPhidgetInterfaceKit_getDataRate(handle, FIX2INT(index), &rate));
243
+ return INT2FIX(rate);
244
+ }
245
+
246
+ VALUE ph_ifkit_get_data_rate_min(VALUE self, VALUE index) {
247
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
248
+ int rate;
249
+ ph_raise(CPhidgetInterfaceKit_getDataRateMin(handle, FIX2INT(index), &rate));
250
+ return INT2FIX(rate);
251
+ }
252
+
253
+ VALUE ph_ifkit_get_data_rate_max(VALUE self, VALUE index) {
254
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
255
+ int rate;
256
+ ph_raise(CPhidgetInterfaceKit_getDataRateMax(handle, FIX2INT(index), &rate));
257
+ return INT2FIX(rate);
258
+ }
259
+
260
+ VALUE ph_ifkit_set_data_rate(VALUE self, VALUE index, VALUE rate) {
261
+ CPhidgetInterfaceKitHandle handle = (CPhidgetInterfaceKitHandle)get_ph_handle(self);
262
+ ph_raise(CPhidgetInterfaceKit_setDataRate(handle, FIX2INT(index), FIX2INT(rate)));
263
+ return Qnil;
264
+ }
265
+
266
+ #ifdef PH_CALLBACK
267
+ VALUE ph_ifkit_set_on_input_change_handler(VALUE self, VALUE handler) {
268
+ ph_data_t *ph = get_ph_data(self);
269
+ ph_callback_data_t *callback_data = &ph->dev_callback_1;
270
+ if( TYPE(handler) == T_NIL ) {
271
+ callback_data->exit = true;
272
+ ph_raise(CPhidgetInterfaceKit_set_OnInputChange_Handler((CPhidgetInterfaceKitHandle)ph->handle, NULL, (void *)NULL));
273
+ } else {
274
+ callback_data->called = false;
275
+ callback_data->exit = false;
276
+ callback_data->phidget = self;
277
+ callback_data->callback = handler;
278
+ ph_raise(CPhidgetInterfaceKit_set_OnInputChange_Handler((CPhidgetInterfaceKitHandle)ph->handle, ph_ifkit_on_input_change, (void *)callback_data));
279
+ ph_callback_thread(callback_data);
280
+ }
281
+ return Qnil;
282
+ }
283
+
284
+ VALUE ph_ifkit_set_on_output_change_handler(VALUE self, VALUE handler) {
285
+ ph_data_t *ph = get_ph_data(self);
286
+ ph_callback_data_t *callback_data = &ph->dev_callback_2;
287
+ if( TYPE(handler) == T_NIL ) {
288
+ callback_data->exit = true;
289
+ ph_raise(CPhidgetInterfaceKit_set_OnOutputChange_Handler((CPhidgetInterfaceKitHandle)ph->handle, NULL, (void *)NULL));
290
+ } else {
291
+ callback_data->called = false;
292
+ callback_data->exit = false;
293
+ callback_data->phidget = self;
294
+ callback_data->callback = handler;
295
+ ph_raise(CPhidgetInterfaceKit_set_OnOutputChange_Handler((CPhidgetInterfaceKitHandle)ph->handle, ph_ifkit_on_output_change, (void *)callback_data));
296
+ ph_callback_thread(callback_data);
297
+ }
298
+ return Qnil;
299
+ }
300
+
301
+ VALUE ph_ifkit_set_on_sensor_change_handler(VALUE self, VALUE handler) {
302
+ ph_data_t *ph = get_ph_data(self);
303
+ ph_callback_data_t *callback_data = &ph->dev_callback_3;
304
+ if( TYPE(handler) == T_NIL ) {
305
+ callback_data->exit = true;
306
+ ph_raise(CPhidgetInterfaceKit_set_OnSensorChange_Handler((CPhidgetInterfaceKitHandle)ph->handle, NULL, (void *)NULL));
307
+ } else {
308
+ callback_data->called = false;
309
+ callback_data->exit = false;
310
+ callback_data->phidget = self;
311
+ callback_data->callback = handler;
312
+ ph_raise(CPhidgetInterfaceKit_set_OnSensorChange_Handler((CPhidgetInterfaceKitHandle)ph->handle, ph_ifkit_on_sensor_change, (void *)callback_data));
313
+ ph_callback_thread(callback_data);
314
+ }
315
+ return Qnil;
316
+ }
317
+
318
+
319
+ int ph_ifkit_on_input_change(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int state) {
320
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
321
+ callback_data->called = true;
322
+ return EPHIDGET_OK;
323
+ }
324
+
325
+
326
+ int ph_ifkit_on_output_change(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int state) {
327
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
328
+ callback_data->called = true;
329
+ return EPHIDGET_OK;
330
+ }
331
+
332
+
333
+ int ph_ifkit_on_sensor_change(CPhidgetInterfaceKitHandle phid, void *userPtr, int index, int value) {
334
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
335
+ callback_data->called = true;
336
+ return EPHIDGET_OK;
337
+ }
338
+ #endif
339
+
340
+
@@ -0,0 +1,251 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+
5
+ VALUE ph_ir_init(VALUE self);
6
+ VALUE ph_ir_transmit(VALUE self, VALUE data, VALUE code_info);
7
+ VALUE ph_ir_transmit_repeat(VALUE self);
8
+ VALUE ph_ir_transmit_raw(VALUE self, VALUE data, VALUE carrier_freq, VALUE duty_cycle, VALUE gap);
9
+ VALUE ph_ir_get_raw_data(VALUE self, VALUE length);
10
+ VALUE ph_ir_get_last_code(VALUE self);
11
+ VALUE ph_ir_get_last_learned_code(VALUE self);
12
+
13
+ #ifdef PH_CALLBACK
14
+ VALUE ph_ir_set_on_code_handler(VALUE self, VALUE handler);
15
+ VALUE ph_ir_set_on_learn_handler(VALUE self, VALUE handler);
16
+ VALUE ph_ir_set_on_raw_data_handler(VALUE self, VALUE handler);
17
+ int ph_ir_on_code(CPhidgetIRHandle phid, void *userPtr, unsigned char *data, int dataLength, int bitCount, int repeat);
18
+ int ph_ir_on_learn(CPhidgetIRHandle phid, void *userPtr, unsigned char *data, int dataLength, CPhidgetIR_CodeInfoHandle codeInfo);
19
+ int ph_ir_on_raw_data(CPhidgetIRHandle phid, void *userPtr, int *data, int dataLength);
20
+ #endif
21
+
22
+
23
+ VALUE Init_phidgets_ir(VALUE ph_module, VALUE ph_common) {
24
+ VALUE ph_ir = rb_define_class_under(ph_module, "IR", ph_common);
25
+
26
+ /* Unknown - the default value */
27
+ rb_define_const(ph_ir, "ENCODING_UNKNOWN", INT2FIX(PHIDGET_IR_ENCODING_UNKNOWN));
28
+ /* Space encoding, or Pulse Distance Modulation */
29
+ rb_define_const(ph_ir, "ENCODING_SPACE", INT2FIX(PHIDGET_IR_ENCODING_SPACE));
30
+ /* Pulse encoding, or Pulse Width Modulation */
31
+ rb_define_const(ph_ir, "ENCODING_PULSE", INT2FIX(PHIDGET_IR_ENCODING_PULSE));
32
+ /* Bi-Phase, or Manchester encoding */
33
+ rb_define_const(ph_ir, "ENCODING_BIPHASE", INT2FIX(PHIDGET_IR_ENCODING_BIPHASE));
34
+ /* RC5 - a type of Bi-Phase encoding */
35
+ rb_define_const(ph_ir, "ENCODING_RC5", INT2FIX(PHIDGET_IR_ENCODING_RC5));
36
+ /* RC6 - a type of Bi-Phase encoding */
37
+ rb_define_const(ph_ir, "ENCODING_RC6", INT2FIX(PHIDGET_IR_ENCODING_RC6));
38
+
39
+ /* Unknown - the default value */
40
+ rb_define_const(ph_ir, "LENGTH_UNKNOWN", INT2FIX(PHIDGET_IR_LENGTH_UNKNOWN));
41
+ /* Constant - the bitstream + gap length is constant */
42
+ rb_define_const(ph_ir, "LENGTH_CONSTANT", INT2FIX(PHIDGET_IR_LENGTH_CONSTANT));
43
+ /* Variable - the bitstream has a variable length with a constant gap */
44
+ rb_define_const(ph_ir, "LENGTH_VARIABLE", INT2FIX(PHIDGET_IR_LENGTH_VARIABLE));
45
+
46
+ /* Document-method: new
47
+ * call-seq: new
48
+ *
49
+ * Creates a Phidget IR object.
50
+ */
51
+ rb_define_method(ph_ir, "initialize", ph_ir_init, 0);
52
+
53
+ /* Document-method: transmit
54
+ * call-seq: transmit(data, code_info)
55
+ *
56
+ * ** NOT IMPLEMENTED **
57
+ */
58
+ rb_define_method(ph_ir, "transmit", ph_ir_transmit, 2);
59
+
60
+ /* Document-method: transmitRepeat
61
+ * call-seq: transmitRepeat
62
+ *
63
+ * Transmits a repeat of the last transmited code. Depending of the CodeInfo structure,
64
+ * this may be a retransmission of the code itself, or there may be a special repeat code.
65
+ */
66
+ rb_define_method(ph_ir, "transmitRepeat", ph_ir_transmit_repeat, 0);
67
+
68
+ /* Document-method: transmitRaw
69
+ * call-seq: transmitRaw(data, carrier_frequency, duty_cycle, gap)
70
+ *
71
+ * Transmits RAW data as a series of pulses and spaces.
72
+ */
73
+ rb_define_method(ph_ir, "transmitRaw", ph_ir_transmit_raw, 4);
74
+
75
+ /* Document-method: getRawData
76
+ * call-seq: getRawData(max_length) -> raw_data
77
+ *
78
+ * Read any available raw data. This should be polled continuously (every 20ms) to avoid missing data.
79
+ * Read data always starts with a space and ends with a pulse.
80
+ */
81
+ rb_define_method(ph_ir, "getRawData", ph_ir_get_raw_data, 1);
82
+
83
+ /* Document-method: getLastCode
84
+ * call-seq: getLastCode -> code
85
+ *
86
+ * ** NOT IMPLEMENTED **
87
+ */
88
+ rb_define_method(ph_ir, "getLastCode", ph_ir_get_last_code, 0);
89
+
90
+ /* Document-method: getLastLearnedCode
91
+ * call-seq: getLastLearnedCode -> code
92
+ *
93
+ * ** NOT IMPLEMENTED **
94
+ */
95
+ rb_define_method(ph_ir, "getLastLearnedCode", ph_ir_get_last_learned_code, 0);
96
+
97
+ #ifdef PH_CALLBACK
98
+ rb_define_private_method(ph_ir, "ext_setOnCodeHandler", ph_ir_set_on_code_handler, 1);
99
+ rb_define_private_method(ph_ir, "ext_setOnLearnHandler", ph_ir_set_on_learn_handler, 1);
100
+ rb_define_private_method(ph_ir, "ext_setOnRawDataHandler", ph_ir_set_on_raw_data_handler, 1);
101
+ #endif
102
+
103
+ rb_define_alias(ph_ir, "transmit_repeat", "transmitRepeat");
104
+ rb_define_alias(ph_ir, "transmit_raw", "transmitRaw");
105
+ rb_define_alias(ph_ir, "raw_data", "getRawData");
106
+ rb_define_alias(ph_ir, "last_code", "getLastCode");
107
+ rb_define_alias(ph_ir, "last_learned_code", "getLastLearnedCode");
108
+
109
+ return ph_ir;
110
+ }
111
+
112
+
113
+
114
+ VALUE ph_ir_init(VALUE self) {
115
+ ph_data_t *ph = get_ph_data(self);
116
+ ph_raise(CPhidgetIR_create((CPhidgetIRHandle *)(&(ph->handle))));
117
+ return self;
118
+ }
119
+
120
+ VALUE ph_ir_transmit(VALUE self, VALUE data, VALUE code_info) {
121
+ ph_raise(EPHIDGET_NOTIMPLEMENTED);
122
+ return Qnil;
123
+ }
124
+
125
+ VALUE ph_ir_transmit_repeat(VALUE self) {
126
+ CPhidgetIRHandle handle = (CPhidgetIRHandle)get_ph_handle(self);
127
+ ph_raise(CPhidgetIR_TransmitRepeat(handle));
128
+ return Qnil;
129
+ }
130
+
131
+ VALUE ph_ir_transmit_raw(VALUE self, VALUE data, VALUE carrier_freq, VALUE duty_cycle, VALUE gap) {
132
+ CPhidgetIRHandle handle = (CPhidgetIRHandle)get_ph_handle(self);
133
+ int data_length = RARRAY_LEN(data);
134
+ int *int_data = (int *)malloc(data_length * sizeof(int));
135
+ int i;
136
+
137
+ for(i=0; i<data_length; i++) {
138
+ int_data[i] = NUM2INT(rb_ary_entry(data, i));
139
+ }
140
+
141
+ ph_raise(CPhidgetIR_TransmitRaw(handle, int_data, data_length, FIX2INT(carrier_freq), FIX2INT(duty_cycle), FIX2INT(gap)));
142
+
143
+ free(int_data);
144
+ return Qnil;
145
+ }
146
+
147
+ VALUE ph_ir_get_raw_data(VALUE self, VALUE length) {
148
+ CPhidgetIRHandle handle = (CPhidgetIRHandle)get_ph_handle(self);
149
+ int data_length = FIX2INT(length);
150
+ int *raw_data = (int *)malloc(data_length * sizeof(int));
151
+ VALUE rb_data;
152
+ int i;
153
+
154
+ ph_raise(CPhidgetIR_getRawData(handle, raw_data, &data_length));
155
+
156
+ rb_data = rb_ary_new();
157
+ for(i=0; i<data_length; i++) {
158
+ rb_ary_push(rb_data, INT2NUM(raw_data[i]));
159
+ }
160
+ free(raw_data);
161
+ return Qnil;
162
+ }
163
+
164
+ VALUE ph_ir_get_last_code(VALUE self) {
165
+ ph_raise(EPHIDGET_NOTIMPLEMENTED);
166
+ return Qnil;
167
+ }
168
+
169
+ VALUE ph_ir_get_last_learned_code(VALUE self) {
170
+ ph_raise(EPHIDGET_NOTIMPLEMENTED);
171
+ return Qnil;
172
+ }
173
+
174
+
175
+ #ifdef PH_CALLBACK
176
+ VALUE ph_ir_set_on_code_handler(VALUE self, VALUE handler) {
177
+ ph_data_t *ph = get_ph_data(self);
178
+ ph_callback_data_t *callback_data = &ph->dev_callback_1;
179
+ if( TYPE(handler) == T_NIL ) {
180
+ callback_data->exit = true;
181
+ ph_raise(CPhidgetIR_set_OnCode_Handler((CPhidgetIRHandle)ph->handle, NULL, (void *)NULL));
182
+ } else {
183
+ callback_data->called = false;
184
+ callback_data->exit = false;
185
+ callback_data->phidget = self;
186
+ callback_data->callback = handler;
187
+ ph_raise(CPhidgetIR_set_OnCode_Handler((CPhidgetIRHandle)ph->handle, ph_ir_on_code, (void *)callback_data));
188
+ ph_callback_thread(callback_data);
189
+ }
190
+ return Qnil;
191
+ }
192
+
193
+
194
+ VALUE ph_ir_set_on_learn_handler(VALUE self, VALUE handler) {
195
+ ph_data_t *ph = get_ph_data(self);
196
+ ph_callback_data_t *callback_data = &ph->dev_callback_2;
197
+ if( TYPE(handler) == T_NIL ) {
198
+ callback_data->exit = true;
199
+ ph_raise(CPhidgetIR_set_OnLearn_Handler((CPhidgetIRHandle)ph->handle, NULL, (void *)NULL));
200
+ } else {
201
+ callback_data->called = false;
202
+ callback_data->exit = false;
203
+ callback_data->phidget = self;
204
+ callback_data->callback = handler;
205
+ ph_raise(CPhidgetIR_set_OnLearn_Handler((CPhidgetIRHandle)ph->handle, ph_ir_on_learn, (void *)callback_data));
206
+ ph_callback_thread(callback_data);
207
+ }
208
+ return Qnil;
209
+ }
210
+
211
+
212
+ VALUE ph_ir_set_on_raw_data_handler(VALUE self, VALUE handler) {
213
+ ph_data_t *ph = get_ph_data(self);
214
+ ph_callback_data_t *callback_data = &ph->dev_callback_3;
215
+ if( TYPE(handler) == T_NIL ) {
216
+ callback_data->exit = true;
217
+ ph_raise(CPhidgetIR_set_OnRawData_Handler((CPhidgetIRHandle)ph->handle, NULL, (void *)NULL));
218
+ } else {
219
+ callback_data->called = false;
220
+ callback_data->exit = false;
221
+ callback_data->phidget = self;
222
+ callback_data->callback = handler;
223
+ ph_raise(CPhidgetIR_set_OnRawData_Handler((CPhidgetIRHandle)ph->handle, ph_ir_on_raw_data, (void *)callback_data));
224
+ ph_callback_thread(callback_data);
225
+ }
226
+ return Qnil;
227
+ }
228
+
229
+
230
+ int ph_ir_on_code(CPhidgetIRHandle phid, void *userPtr, unsigned char *data, int dataLength, int bitCount, int repeat) {
231
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
232
+ callback_data->called = true;
233
+ return EPHIDGET_OK;
234
+ }
235
+
236
+
237
+ int ph_ir_on_learn(CPhidgetIRHandle phid, void *userPtr, unsigned char *data, int dataLength, CPhidgetIR_CodeInfoHandle codeInfo) {
238
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
239
+ callback_data->called = true;
240
+ return EPHIDGET_OK;
241
+ }
242
+
243
+
244
+ int ph_ir_on_raw_data(CPhidgetIRHandle phid, void *userPtr, int *data, int dataLength) {
245
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
246
+ callback_data->called = true;
247
+ return EPHIDGET_OK;
248
+ }
249
+
250
+ #endif
251
+