phidgets 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,288 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+ #define DIGITAL_OUTPUT_DUTY_CYCLE_ASYNC_CALLBACK 0
5
+ #define DIGITAL_OUTPUT_LED_CURRENT_LIMIT_ASYNC_CALLBACK 1
6
+ #define DIGITAL_OUTPUT_STATE_ASYNC_CALLBACK 2
7
+
8
+
9
+ VALUE ph_digital_output_init(VALUE self) {
10
+ ph_data_t *ph = get_ph_data(self);
11
+ ph_raise(PhidgetDigitalOutput_create((PhidgetDigitalOutputHandle *)(&(ph->handle))));
12
+ return self;
13
+ }
14
+
15
+ VALUE ph_digital_output_get_duty_cycle(VALUE self) {
16
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetDigitalOutput_getDutyCycle);
17
+ }
18
+
19
+ VALUE ph_digital_output_set_duty_cycle(VALUE self, VALUE duty_cycle) {
20
+ ph_raise(PhidgetDigitalOutput_setDutyCycle((PhidgetDigitalOutputHandle)get_ph_handle(self), NUM2DBL(duty_cycle)));
21
+ return Qnil;
22
+ }
23
+
24
+ VALUE ph_digital_output_get_min_duty_cycle(VALUE self) {
25
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetDigitalOutput_getMinDutyCycle);
26
+ }
27
+
28
+ VALUE ph_digital_output_get_max_duty_cycle(VALUE self) {
29
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetDigitalOutput_getMaxDutyCycle);
30
+ }
31
+
32
+ VALUE ph_digital_output_get_led_current_limit(VALUE self) {
33
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetDigitalOutput_getLEDCurrentLimit);
34
+ }
35
+
36
+ VALUE ph_digital_output_set_led_current_limit(VALUE self, VALUE current_limit) {
37
+ ph_raise(PhidgetDigitalOutput_setLEDCurrentLimit((PhidgetDigitalOutputHandle)get_ph_handle(self), NUM2DBL(current_limit)));
38
+ return Qnil;
39
+ }
40
+
41
+ VALUE ph_digital_output_get_min_led_current_limit(VALUE self) {
42
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetDigitalOutput_getMinLEDCurrentLimit);
43
+ }
44
+
45
+ VALUE ph_digital_output_get_max_led_current_limit(VALUE self) {
46
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetDigitalOutput_getMaxLEDCurrentLimit);
47
+ }
48
+
49
+ VALUE ph_digital_output_get_led_forward_voltage(VALUE self) {
50
+ return ph_get_int(get_ph_handle(self), (phidget_get_int_func)PhidgetDigitalOutput_getLEDForwardVoltage);
51
+ }
52
+
53
+ VALUE ph_digital_output_set_led_forward_voltage(VALUE self, VALUE forward_voltage) {
54
+ ph_raise(PhidgetDigitalOutput_setLEDForwardVoltage((PhidgetDigitalOutputHandle)get_ph_handle(self), NUM2INT(forward_voltage)));
55
+ return Qnil;
56
+ }
57
+
58
+ VALUE ph_digital_output_get_state(VALUE self) {
59
+ return ph_get_bool(get_ph_handle(self), (phidget_get_bool_func)PhidgetDigitalOutput_getState);
60
+ }
61
+
62
+ VALUE ph_digital_output_set_state(VALUE self, VALUE state) {
63
+ ph_raise(PhidgetDigitalOutput_setState((PhidgetDigitalOutputHandle)get_ph_handle(self), TYPE(state) == T_TRUE ? PTRUE : PFALSE));
64
+ return Qnil;
65
+ }
66
+
67
+
68
+
69
+ void CCONV ph_digital_output_duty_cycle_async(PhidgetHandle phid, void *userPtr, PhidgetReturnCode res) {
70
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
71
+ callback_data->exit = true;
72
+ callback_data->arg1 = INT2NUM(res);
73
+ callback_data->arg2 = Qnil;
74
+ callback_data->arg3 = Qnil;
75
+ callback_data->arg4 = Qnil;
76
+ sem_post(&callback_data->callback_called);
77
+ }
78
+
79
+ VALUE ph_digital_output_set_duty_cycle_async(VALUE self, VALUE duty_cycle, VALUE handler) {
80
+ ph_data_t *ph = get_ph_data(self);
81
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DIGITAL_OUTPUT_DUTY_CYCLE_ASYNC_CALLBACK];
82
+ if( TYPE(handler) == T_NIL ) {
83
+ PhidgetDigitalOutput_setDutyCycle_async((PhidgetDigitalOutputHandle)ph->handle, NUM2DBL(duty_cycle), NULL, (void *)NULL);
84
+ } else {
85
+ callback_data->exit = false;
86
+ callback_data->phidget = self;
87
+ callback_data->callback = handler;
88
+ PhidgetDigitalOutput_setDutyCycle_async((PhidgetDigitalOutputHandle)ph->handle, NUM2DBL(duty_cycle), ph_digital_output_duty_cycle_async, (void *)callback_data);
89
+ ph_callback_thread(callback_data);
90
+ }
91
+ return Qnil;
92
+ }
93
+
94
+
95
+ void CCONV ph_digital_output_led_current_limit_async(PhidgetHandle phid, void *userPtr, PhidgetReturnCode res) {
96
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
97
+ callback_data->exit = true;
98
+ callback_data->arg1 = INT2NUM(res);
99
+ callback_data->arg2 = Qnil;
100
+ callback_data->arg3 = Qnil;
101
+ callback_data->arg4 = Qnil;
102
+ sem_post(&callback_data->callback_called);
103
+ }
104
+
105
+ VALUE ph_digital_output_set_led_current_limit_async(VALUE self, VALUE current_limit, VALUE handler) {
106
+ ph_data_t *ph = get_ph_data(self);
107
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DIGITAL_OUTPUT_LED_CURRENT_LIMIT_ASYNC_CALLBACK];
108
+ if( TYPE(handler) == T_NIL ) {
109
+ PhidgetDigitalOutput_setLEDCurrentLimit_async((PhidgetDigitalOutputHandle)ph->handle, NUM2DBL(current_limit), NULL, (void *)NULL);
110
+ } else {
111
+ callback_data->exit = false;
112
+ callback_data->phidget = self;
113
+ callback_data->callback = handler;
114
+ PhidgetDigitalOutput_setLEDCurrentLimit_async((PhidgetDigitalOutputHandle)ph->handle, NUM2DBL(current_limit), ph_digital_output_led_current_limit_async, (void *)callback_data);
115
+ ph_callback_thread(callback_data);
116
+ }
117
+ return Qnil;
118
+ }
119
+
120
+
121
+ void CCONV ph_digital_output_state_async(PhidgetHandle phid, void *userPtr, PhidgetReturnCode res) {
122
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
123
+ callback_data->exit = true;
124
+ callback_data->arg1 = INT2NUM(res);
125
+ callback_data->arg2 = Qnil;
126
+ callback_data->arg3 = Qnil;
127
+ callback_data->arg4 = Qnil;
128
+ sem_post(&callback_data->callback_called);
129
+ }
130
+
131
+ VALUE ph_digital_output_set_state_async(VALUE self, VALUE state, VALUE handler) {
132
+ ph_data_t *ph = get_ph_data(self);
133
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DIGITAL_OUTPUT_STATE_ASYNC_CALLBACK];
134
+ if( TYPE(handler) == T_NIL ) {
135
+ PhidgetDigitalOutput_setState_async((PhidgetDigitalOutputHandle)ph->handle, TYPE(state) == T_TRUE ? PTRUE : PFALSE, NULL, (void *)NULL);
136
+ } else {
137
+ callback_data->exit = false;
138
+ callback_data->phidget = self;
139
+ callback_data->callback = handler;
140
+ PhidgetDigitalOutput_setState_async((PhidgetDigitalOutputHandle)ph->handle, TYPE(state) == T_TRUE ? PTRUE : PFALSE, ph_digital_output_state_async, (void *)callback_data);
141
+ ph_callback_thread(callback_data);
142
+ }
143
+ return Qnil;
144
+ }
145
+
146
+
147
+ void Init_digital_output() {
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_digital_output = rb_define_class_under(ph_module, "DigitalOutput", ph_common);
151
+
152
+
153
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_1_7V", INT2NUM(LED_FORWARD_VOLTAGE_1_7V));
154
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_2_75V", INT2NUM(LED_FORWARD_VOLTAGE_2_75V));
155
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_3_2V", INT2NUM(LED_FORWARD_VOLTAGE_3_2V));
156
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_3_9V", INT2NUM(LED_FORWARD_VOLTAGE_3_9V));
157
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_4_0V", INT2NUM(LED_FORWARD_VOLTAGE_4_0V));
158
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_4_8V", INT2NUM(LED_FORWARD_VOLTAGE_4_8V));
159
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_5_0V", INT2NUM(LED_FORWARD_VOLTAGE_5_0V));
160
+ rb_define_const(ph_digital_output, "LED_FORWARD_VOLTAGE_5_6V", INT2NUM(LED_FORWARD_VOLTAGE_5_6V));
161
+
162
+
163
+ /* Document-method: new
164
+ * call-seq: new
165
+ *
166
+ * Creates a Phidget DigitalOutput object.
167
+ */
168
+ rb_define_method(ph_digital_output, "initialize", ph_digital_output_init, 0);
169
+
170
+ /* Document-method: getDutyCycle
171
+ * call-seq: getDutyCycle -> duty_cycle
172
+ *
173
+ * The DutyCycle represents the fraction of time the output is on (high).
174
+ * A DutyCycle of 1.0 translates to a high output, a DutyCycle of 0 translates to a low output.
175
+ * A DutyCycle of 0.5 translates to an output that is high half the time, which results in an average output voltage of (output voltage x 0.5)
176
+ * You can use the DutyCycle to create a dimming effect on LEDs.
177
+ */
178
+ rb_define_method(ph_digital_output, "getDutyCycle", ph_digital_output_get_duty_cycle, 0);
179
+ rb_define_alias(ph_digital_output, "duty_cycle", "getDutyCycle");
180
+
181
+ /* Document-method: setDutyCycle
182
+ * call-seq: setDutyCycle(duty_cycle)
183
+ *
184
+ * The DutyCycle represents the fraction of time the output is on (high).
185
+ * This will override the State setting on the channel.
186
+ * A DutyCycle of 1.0 translates to a high output, a DutyCycle of 0 translates to a low output.
187
+ * This is equivalent to setting a State of TRUE and FALSE respectively.
188
+ * A DutyCycle of 0.5 translates to an output that is high half the time, which results in an average output voltage of (output voltage x 0.5)
189
+ * You can use the DutyCycle to create a dimming effect on LEDs.
190
+ * If the DigitalOutput channel you are using does not support PWM, then this value may only be set to 1.0 or 0.0.
191
+ */
192
+ rb_define_method(ph_digital_output, "setDutyCycle", ph_digital_output_set_duty_cycle, 1);
193
+ rb_define_alias(ph_digital_output, "duty_cycle=", "setDutyCycle");
194
+
195
+ /* Document-method: getMinDutyCycle
196
+ * call-seq: getMinDutyCycle -> min_duty_cycle
197
+ *
198
+ * The minimum value that DutyCycle can be set to.
199
+ */
200
+ rb_define_method(ph_digital_output, "getMinDutyCycle", ph_digital_output_get_min_duty_cycle, 0);
201
+ rb_define_alias(ph_digital_output, "min_duty_cycle", "getMinDutyCycle");
202
+
203
+ /* Document-method: getMaxDutyCycle
204
+ * call-seq: getMaxDutyCycle -> max_duty_cycle
205
+ *
206
+ * The maximum value that DutyCycle can be set to.
207
+ */
208
+ rb_define_method(ph_digital_output, "getMaxDutyCycle", ph_digital_output_get_max_duty_cycle, 0);
209
+ rb_define_alias(ph_digital_output, "max_duty_cycle", "getMaxDutyCycle");
210
+
211
+ /* Document-method: getLEDCurrentLimit
212
+ * call-seq: getLEDCurrentLimit -> current_limit
213
+ *
214
+ * The LEDCurrentLimit is the maximum amount of current that the controller will provide to the output.
215
+ * Reference the data sheet of the LED you are using before setting this value.
216
+ */
217
+ rb_define_method(ph_digital_output, "getLEDCurrentLimit", ph_digital_output_get_led_current_limit, 0);
218
+ rb_define_alias(ph_digital_output, "led_current_limit", "getLEDCurrentLimit");
219
+
220
+ /* Document-method: setLEDCurrentLimit
221
+ * call-seq: setLEDCurrentLimit(current_limit)
222
+ *
223
+ * The LEDCurrentLimit is the maximum amount of current that the controller will provide to the output.
224
+ * Reference the data sheet of the LED you are using before setting this value.
225
+ */
226
+ rb_define_method(ph_digital_output, "setLEDCurrentLimit", ph_digital_output_set_led_current_limit, 1);
227
+ rb_define_alias(ph_digital_output, "led_current_limit=", "setLEDCurrentLimit");
228
+
229
+ /* Document-method: getMinLEDCurrentLimit
230
+ * call-seq: getMinLEDCurrentLimit -> min_current_limit
231
+ *
232
+ * The minimum value that LEDCurrentLimit can be set to.
233
+ */
234
+ rb_define_method(ph_digital_output, "getMinLEDCurrentLimit", ph_digital_output_get_min_led_current_limit, 0);
235
+ rb_define_alias(ph_digital_output, "min_led_current_limit", "getMinLEDCurrentLimit");
236
+
237
+ /* Document-method: getMaxLEDCurrentLimit
238
+ * call-seq: getMaxLEDCurrentLimit -> max_current_limit
239
+ *
240
+ * The maximum value that LEDCurrentLimit can be set to.
241
+ */
242
+ rb_define_method(ph_digital_output, "getMaxLEDCurrentLimit", ph_digital_output_get_max_led_current_limit, 0);
243
+ rb_define_alias(ph_digital_output, "max_led_current_limit", "getMaxLEDCurrentLimit");
244
+
245
+ /* Document-method: getLEDForwardVoltage
246
+ * call-seq: getLEDForwardVoltage -> forward_voltage
247
+ *
248
+ * The LEDForwardVoltage is the voltage that will be available to your LED.
249
+ * Reference the data sheet of the LED you are using before setting this value. Choose the LEDForwardVoltage that is closest to the forward voltage specified in the data sheet.
250
+ * This forward voltage is shared for all channels on this device. Setting the LEDForwardVoltage on any channel will set the LEDForwardVoltage for all channels on the device.
251
+ */
252
+ rb_define_method(ph_digital_output, "getLEDForwardVoltage", ph_digital_output_get_led_forward_voltage, 0);
253
+ rb_define_alias(ph_digital_output, "led_forward_voltage", "getLEDForwardVoltage");
254
+
255
+ /* Document-method: setLEDForwardVoltage
256
+ * call-seq: setLEDForwardVoltage(forward_voltage)
257
+ *
258
+ * The LEDForwardVoltage is the voltage that will be available to your LED.
259
+ * Reference the data sheet of the LED you are using before setting this value. Choose the LEDForwardVoltage that is closest to the forward voltage specified in the data sheet.
260
+ * This forward voltage is shared for all channels on this device. Setting the LEDForwardVoltage on any channel will set the LEDForwardVoltage for all channels on the device.
261
+ */
262
+ rb_define_method(ph_digital_output, "setLEDForwardVoltage", ph_digital_output_set_led_forward_voltage, 1);
263
+ rb_define_alias(ph_digital_output, "led_forward_voltage=", "setLEDForwardVoltage");
264
+
265
+ /* Document-method: getState
266
+ * call-seq: getState -> state
267
+ *
268
+ * The State will indicate whether the output is high (true) or low (false).
269
+ * If a DutyCycle has been set, the state will return as true if the DutyCycle is above 0.5, or false otherwise.
270
+ */
271
+ rb_define_method(ph_digital_output, "getState", ph_digital_output_get_state, 0);
272
+ rb_define_alias(ph_digital_output, "state", "getState");
273
+
274
+ /* Document-method: setState
275
+ * call-seq: setState(state)
276
+ *
277
+ * The State will dictate whether the output is constantly high (TRUE) or low (FALSE).
278
+ * This will override any DutyCycle that may have been set on the channel.
279
+ * Setting the State to TRUE is the same as setting DutyCycle to 1.0, and setting the State to FALSE is the same as setting a DutyCycle of 0.0.
280
+ */
281
+ rb_define_method(ph_digital_output, "setState", ph_digital_output_set_state, 1);
282
+ rb_define_alias(ph_digital_output, "state=", "setState");
283
+
284
+ rb_define_private_method(ph_digital_output, "ext_setDutyCycle_async", ph_digital_output_set_duty_cycle_async, 2);
285
+ rb_define_private_method(ph_digital_output, "ext_setLEDCurrentLimit_async", ph_digital_output_set_led_current_limit_async, 2);
286
+ rb_define_private_method(ph_digital_output, "ext_setState_async", ph_digital_output_set_state_async, 2);
287
+ }
288
+
@@ -0,0 +1,295 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+ #define DISTANCE_SENSOR_DISTANCE_CHANGE_CALLBACK 0
5
+ #define DISTANCE_SENSOR_SONAR_REFLECTIONS_UPDATE_CALLBACK 1
6
+
7
+
8
+ VALUE ph_distance_init(VALUE self) {
9
+ ph_data_t *ph = get_ph_data(self);
10
+ ph_raise(PhidgetDistanceSensor_create((PhidgetDistanceSensorHandle *)(&(ph->handle))));
11
+ return self;
12
+ }
13
+
14
+ VALUE ph_distance_get_data_interval(VALUE self) {
15
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getDataInterval);
16
+ }
17
+
18
+ VALUE ph_distance_set_data_interval(VALUE self, VALUE interval) {
19
+ ph_raise(PhidgetDistanceSensor_setDataInterval((PhidgetDistanceSensorHandle)get_ph_handle(self), NUM2UINT(interval)));
20
+ return Qnil;
21
+ }
22
+
23
+ VALUE ph_distance_get_min_data_interval(VALUE self) {
24
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getMinDataInterval);
25
+ }
26
+
27
+ VALUE ph_distance_get_max_data_interval(VALUE self) {
28
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getMaxDataInterval);
29
+ }
30
+
31
+ VALUE ph_distance_get_distance(VALUE self) {
32
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getDistance);
33
+ }
34
+
35
+ VALUE ph_distance_get_min_distance(VALUE self) {
36
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getMinDistance);
37
+ }
38
+
39
+ VALUE ph_distance_get_max_distance(VALUE self) {
40
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getMaxDistance);
41
+ }
42
+
43
+ VALUE ph_distance_get_distance_change_trigger(VALUE self) {
44
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getDistanceChangeTrigger);
45
+ }
46
+
47
+ VALUE ph_distance_set_distance_change_trigger(VALUE self, VALUE trigger) {
48
+ ph_raise(PhidgetDistanceSensor_setDistanceChangeTrigger((PhidgetDistanceSensorHandle)get_ph_handle(self), NUM2UINT(trigger)));
49
+ return Qnil;
50
+ }
51
+
52
+ VALUE ph_distance_get_min_distance_change_trigger(VALUE self) {
53
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getMinDistanceChangeTrigger);
54
+ }
55
+
56
+ VALUE ph_distance_get_max_distance_change_trigger(VALUE self) {
57
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetDistanceSensor_getMaxDistanceChangeTrigger);
58
+ }
59
+
60
+ VALUE ph_distance_get_sonar_quiet_mode(VALUE self) {
61
+ return ph_get_bool(get_ph_handle(self), (phidget_get_bool_func)PhidgetDistanceSensor_getSonarQuietMode);
62
+ }
63
+
64
+ VALUE ph_distance_set_sonar_quiet_mode(VALUE self, VALUE mode) {
65
+ ph_raise(PhidgetDistanceSensor_setSonarQuietMode((PhidgetDistanceSensorHandle)get_ph_handle(self), TYPE(mode) == T_TRUE ? PTRUE : PFALSE));
66
+ return Qnil;
67
+ }
68
+
69
+ VALUE ph_distance_get_sonar_reflections(VALUE self) {
70
+ PhidgetDistanceSensorHandle handle = (PhidgetDistanceSensorHandle)get_ph_handle(self);
71
+ uint32_t distances[8];
72
+ uint32_t amplitudes[8];
73
+ uint32_t count, i;
74
+ VALUE rb_reflections = rb_hash_new();
75
+ VALUE rb_distances = rb_ary_new();
76
+ VALUE rb_amplitudes = rb_ary_new();
77
+ ph_raise(PhidgetDistanceSensor_getSonarReflections(handle, &distances, &amplitudes, &count));
78
+ for(i=0; i<count; i++) {
79
+ rb_ary_push(rb_distances, UINT2NUM(distances[i]));
80
+ rb_ary_push(rb_amplitudes, UINT2NUM(amplitudes[i]));
81
+ }
82
+ rb_hash_aset(rb_reflections, rb_str_new2("distances"), rb_distances);
83
+ rb_hash_aset(rb_reflections, rb_str_new2("amplitudes"), rb_amplitudes);
84
+ return rb_reflections;
85
+ }
86
+
87
+
88
+ void CCONV ph_distance_on_distance_change(PhidgetDistanceSensorHandle phid, void *userPtr, uint32_t distance) {
89
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
90
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
91
+ callback_data->arg1 = UINT2NUM(distance);
92
+ callback_data->arg2 = Qnil;
93
+ callback_data->arg3 = Qnil;
94
+ callback_data->arg4 = Qnil;
95
+ sem_post(&callback_data->callback_called);
96
+ }
97
+
98
+
99
+ VALUE ph_distance_set_on_distance_change_handler(VALUE self, VALUE handler) {
100
+ ph_data_t *ph = get_ph_data(self);
101
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DISTANCE_SENSOR_DISTANCE_CHANGE_CALLBACK];
102
+ if( TYPE(handler) == T_NIL ) {
103
+ callback_data->callback = T_NIL;
104
+ callback_data->exit = true;
105
+ ph_raise(PhidgetDistanceSensor_setOnDistanceChangeHandler((PhidgetDistanceSensorHandle)ph->handle, NULL, (void *)NULL));
106
+ sem_post(&callback_data->callback_called);
107
+ } else {
108
+ callback_data->exit = false;
109
+ callback_data->phidget = self;
110
+ callback_data->callback = handler;
111
+ ph_raise(PhidgetDistanceSensor_setOnDistanceChangeHandler((PhidgetDistanceSensorHandle)ph->handle, ph_distance_on_distance_change, (void *)callback_data));
112
+ ph_callback_thread(callback_data);
113
+ }
114
+ return Qnil;
115
+ }
116
+
117
+
118
+ void CCONV ph_distance_on_sonar_reflections_update(PhidgetDistanceSensorHandle phid, void *userPtr, const uint32_t distances[8], const uint32_t amplitudes[8], uint32_t count) {
119
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
120
+ uint32_t i;
121
+ VALUE rb_distances = rb_ary_new();
122
+ VALUE rb_amplitudes = rb_ary_new();
123
+ for(i=0; i<count; i++) {
124
+ rb_ary_push(rb_distances, UINT2NUM(distances[i]));
125
+ rb_ary_push(rb_amplitudes, UINT2NUM(amplitudes[i]));
126
+ }
127
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
128
+ callback_data->arg1 = rb_distances;
129
+ callback_data->arg2 = rb_amplitudes;
130
+ callback_data->arg3 = Qnil;
131
+ callback_data->arg4 = Qnil;
132
+ sem_post(&callback_data->callback_called);
133
+ }
134
+
135
+
136
+ VALUE ph_distance_set_on_sonar_reflections_update_handler(VALUE self, VALUE handler) {
137
+ ph_data_t *ph = get_ph_data(self);
138
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[DISTANCE_SENSOR_SONAR_REFLECTIONS_UPDATE_CALLBACK];
139
+ if( TYPE(handler) == T_NIL ) {
140
+ callback_data->callback = T_NIL;
141
+ callback_data->exit = true;
142
+ ph_raise(PhidgetDistanceSensor_setOnSonarReflectionsUpdateHandler((PhidgetDistanceSensorHandle)ph->handle, NULL, (void *)NULL));
143
+ sem_post(&callback_data->callback_called);
144
+ } else {
145
+ callback_data->exit = false;
146
+ callback_data->phidget = self;
147
+ callback_data->callback = handler;
148
+ ph_raise(PhidgetDistanceSensor_setOnSonarReflectionsUpdateHandler((PhidgetDistanceSensorHandle)ph->handle, ph_distance_on_sonar_reflections_update, (void *)callback_data));
149
+ ph_callback_thread(callback_data);
150
+ }
151
+ return Qnil;
152
+ }
153
+
154
+
155
+ void Init_distance() {
156
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
157
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
158
+ VALUE ph_distance = rb_define_class_under(ph_module, "DistanceSensor", ph_common);
159
+
160
+ /* Document-method: new
161
+ * call-seq: new
162
+ *
163
+ * Creates a Phidget DistanceSensor object.
164
+ */
165
+ rb_define_method(ph_distance, "initialize", ph_distance_init, 0);
166
+
167
+ /* Document-method: getDataInterval
168
+ * call-seq: getDataInterval -> interval
169
+ *
170
+ * The DataInterval is the time that must elapse before the channel will fire another event.
171
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
172
+ * The timing between events can also affected by the change trigger.
173
+ */
174
+ rb_define_method(ph_distance, "getDataInterval", ph_distance_get_data_interval, 0);
175
+ rb_define_alias(ph_distance, "data_interval", "getDataInterval");
176
+
177
+ /* Document-method: setDataInterval
178
+ * call-seq: setDataInterval(interval)
179
+ *
180
+ * The DataInterval is the time that must elapse before the channel will fire another event.
181
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
182
+ * The timing between events can also affected by the change trigger.
183
+ */
184
+ rb_define_method(ph_distance, "setDataInterval", ph_distance_set_data_interval, 1);
185
+ rb_define_alias(ph_distance, "data_interval=", "setDataInterval");
186
+
187
+ /* Document-method: getMinDataInterval
188
+ * call-seq: getMinDataInterval -> interval
189
+ *
190
+ * The minimum value that DataInterval can be set to.
191
+ */
192
+ rb_define_method(ph_distance, "getMinDataInterval", ph_distance_get_min_data_interval, 0);
193
+ rb_define_alias(ph_distance, "min_data_interval", "getMinDataInterval");
194
+
195
+ /* Document-method: getMaxDataInterval
196
+ * call-seq: getMaxDataInterval -> interval
197
+ *
198
+ * The maximum value that DataInterval can be set to.
199
+ */
200
+ rb_define_method(ph_distance, "getMaxDataInterval", ph_distance_get_max_data_interval, 0);
201
+ rb_define_alias(ph_distance, "max_data_interval", "getMaxDataInterval");
202
+
203
+ /* Document-method: getDistance
204
+ * call-seq: getDistance -> distance
205
+ *
206
+ * The most recent distance value that the channel has reported.
207
+ * This value will always be between MinDistance and MaxDistance.
208
+ */
209
+ rb_define_method(ph_distance, "getDistance", ph_distance_get_distance, 0);
210
+ rb_define_alias(ph_distance, "distance", "getDistance");
211
+
212
+ /* Document-method: getMinDistance
213
+ * call-seq: getMinDistance -> distance
214
+ *
215
+ * The minimum distance that a event will report.
216
+ */
217
+ rb_define_method(ph_distance, "getMinDistance", ph_distance_get_min_distance, 0);
218
+ rb_define_alias(ph_distance, "min_distance", "getMinDistance");
219
+
220
+ /* Document-method: getMaxDistance
221
+ * call-seq: getMaxDistance -> distance
222
+ *
223
+ * The maximum distance that a event will report.
224
+ */
225
+ rb_define_method(ph_distance, "getMaxDistance", ph_distance_get_max_distance, 0);
226
+ rb_define_alias(ph_distance, "max_distance", "getMaxDistance");
227
+
228
+ /* Document-method: getDistanceChangeTrigger
229
+ * call-seq: getDistanceChangeTrigger -> change_trigger
230
+ *
231
+ * The channel will not issue an event until the distance value has changed by the amount specified by the DistanceChangeTrigger.
232
+ * Setting the DistanceChangeTrigger to 0 will result in the channel firing events every DataInterval. This is useful for applications that implement their own data filtering.
233
+ */
234
+ rb_define_method(ph_distance, "getDistanceChangeTrigger", ph_distance_get_distance_change_trigger, 0);
235
+ rb_define_alias(ph_distance, "distance_change_trigger", "getDistanceChangeTrigger");
236
+
237
+ /* Document-method: setDistanceChangeTrigger
238
+ * call-seq: setDistanceChangeTrigger(change_trigger)
239
+ *
240
+ * The channel will not issue an event until the distance value has changed by the amount specified by the DistanceChangeTrigger.
241
+ * Setting the DistanceChangeTrigger to 0 will result in the channel firing events every DataInterval. This is useful for applications that implement their own data filtering.
242
+ */
243
+ rb_define_method(ph_distance, "setDistanceChangeTrigger", ph_distance_set_distance_change_trigger, 1);
244
+ rb_define_alias(ph_distance, "distance_change_trigger=", "setDistanceChangeTrigger");
245
+
246
+ /* Document-method: getMinDistanceChangeTrigger
247
+ * call-seq: getMinDistanceChangeTrigger -> change_trigger
248
+ *
249
+ * The minimum value that DistanceChangeTrigger can be set to.
250
+ */
251
+ rb_define_method(ph_distance, "getMinDistanceChangeTrigger", ph_distance_get_min_distance_change_trigger, 0);
252
+ rb_define_alias(ph_distance, "min_distance_change_trigger", "getMinDistanceChangeTrigger");
253
+
254
+ /* Document-method: getMaxDistanceChangeTrigger
255
+ * call-seq: getMaxDistanceChangeTrigger -> change_trigger
256
+ *
257
+ * The maximum value that DistanceChangeTrigger can be set to.
258
+ */
259
+ rb_define_method(ph_distance, "getMaxDistanceChangeTrigger", ph_distance_get_max_distance_change_trigger, 0);
260
+ rb_define_alias(ph_distance, "max_distance_change_trigger", "getMaxDistanceChangeTrigger");
261
+
262
+ /* Document-method: getSonarQuietMode
263
+ * call-seq: getSonarQuietMode -> true or false
264
+ *
265
+ * When set to true, the device will operate more quietly.
266
+ * The measurable range is reduced when operating in quiet mode.
267
+ */
268
+ rb_define_method(ph_distance, "getSonarQuietMode", ph_distance_get_sonar_quiet_mode, 0);
269
+ rb_define_alias(ph_distance, "sonar_quiet_mode", "getSonarQuietMode");
270
+
271
+ /* Document-method: setSonarQuietMode
272
+ * call-seq: setSonarQuietMode(quiet_mode)
273
+ *
274
+ * When set to true, the device will operate more quietly.
275
+ * The measurable range is reduced when operating in quiet mode.
276
+ */
277
+ rb_define_method(ph_distance, "setSonarQuietMode", ph_distance_set_sonar_quiet_mode, 1);
278
+ rb_define_alias(ph_distance, "sonar_quiet_mode=", "setSonarQuietMode");
279
+
280
+ /* Document-method: getSonarReflections
281
+ * call-seq: getSonarReflections -> reflections
282
+ *
283
+ * The most recent reflection values that the channel has reported.
284
+ * The distance values will always be between MinDistance and MaxDistance.
285
+ * The closest reflection will be placed at index 0 of the distances array, and the furthest reflection at index 7
286
+ * The amplitude values are relative amplitudes of the reflections that are normalized to an arbitrary scale.
287
+ */
288
+ rb_define_method(ph_distance, "getSonarReflections", ph_distance_get_sonar_reflections, 0);
289
+ rb_define_alias(ph_distance, "sonar_reflections", "getSonarReflections");
290
+
291
+
292
+ rb_define_private_method(ph_distance, "ext_setOnDistanceChangeHandler", ph_distance_set_on_distance_change_handler, 1);
293
+ rb_define_private_method(ph_distance, "ext_setOnSonarReflectionsUpdateHandler", ph_distance_set_on_sonar_reflections_update_handler, 1);
294
+ }
295
+