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
@@ -0,0 +1,195 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+ #define GYROSCOPE_ANGULAR_RATE_UPDATE_CALLBACK 0
5
+
6
+
7
+ VALUE ph_gyroscope_init(VALUE self) {
8
+ ph_data_t *ph = get_ph_data(self);
9
+ ph_raise(PhidgetGyroscope_create((PhidgetGyroscopeHandle *)(&(ph->handle))));
10
+ return self;
11
+ }
12
+
13
+ VALUE ph_gyroscope_get_angular_rate(VALUE self) {
14
+ double angular_rate[3];
15
+ ph_raise(PhidgetGyroscope_getAngularRate((PhidgetGyroscopeHandle)get_ph_handle(self), &angular_rate));
16
+ return rb_ary_new3(3, DBL2NUM(angular_rate[0]), DBL2NUM(angular_rate[1]), DBL2NUM(angular_rate[2]));
17
+ }
18
+
19
+ VALUE ph_gyroscope_get_min_angular_rate(VALUE self) {
20
+ double angular_rate[3];
21
+ ph_raise(PhidgetGyroscope_getMinAngularRate((PhidgetGyroscopeHandle)get_ph_handle(self), &angular_rate));
22
+ return rb_ary_new3(3, DBL2NUM(angular_rate[0]), DBL2NUM(angular_rate[1]), DBL2NUM(angular_rate[2]));
23
+ }
24
+
25
+ VALUE ph_gyroscope_get_max_angular_rate(VALUE self) {
26
+ double angular_rate[3];
27
+ ph_raise(PhidgetGyroscope_getMaxAngularRate((PhidgetGyroscopeHandle)get_ph_handle(self), &angular_rate));
28
+ return rb_ary_new3(3, DBL2NUM(angular_rate[0]), DBL2NUM(angular_rate[1]), DBL2NUM(angular_rate[2]));
29
+ }
30
+
31
+ VALUE ph_gyroscope_get_axis_count(VALUE self) {
32
+ return ph_get_int(get_ph_handle(self), (phidget_get_int_func)PhidgetGyroscope_getAxisCount);
33
+ }
34
+
35
+ VALUE ph_gyroscope_get_data_interval(VALUE self) {
36
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetGyroscope_getDataInterval);
37
+ }
38
+
39
+ VALUE ph_gyroscope_set_data_interval(VALUE self, VALUE interval) {
40
+ ph_raise(PhidgetGyroscope_setDataInterval((PhidgetGyroscopeHandle)get_ph_handle(self), NUM2UINT(interval)));
41
+ return Qnil;
42
+ }
43
+
44
+ VALUE ph_gyroscope_get_min_data_interval(VALUE self) {
45
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetGyroscope_getMinDataInterval);
46
+ }
47
+
48
+ VALUE ph_gyroscope_get_max_data_interval(VALUE self) {
49
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetGyroscope_getMaxDataInterval);
50
+ }
51
+
52
+ VALUE ph_gyroscope_get_timestamp(VALUE self) {
53
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetGyroscope_getTimestamp);
54
+ }
55
+
56
+ VALUE ph_gyroscope_zero(VALUE self) {
57
+ ph_raise(PhidgetGyroscope_zero((PhidgetGyroscopeHandle)get_ph_handle(self)));
58
+ return Qnil;
59
+ }
60
+
61
+
62
+ void CCONV ph_gyroscope_on_angular_rate_update(PhidgetGyroscopeHandle phid, void *userPtr, const double angularRate[3], double timestamp) {
63
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
64
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
65
+ callback_data->arg1 = rb_ary_new3(3, DBL2NUM(angularRate[0]), DBL2NUM(angularRate[1]), DBL2NUM(angularRate[2]));
66
+ callback_data->arg2 = DBL2NUM(timestamp);
67
+ callback_data->arg3 = Qnil;
68
+ callback_data->arg4 = Qnil;
69
+ sem_post(&callback_data->callback_called);
70
+ }
71
+
72
+
73
+ VALUE ph_gyroscope_set_on_angular_rate_update_handler(VALUE self, VALUE handler) {
74
+ ph_data_t *ph = get_ph_data(self);
75
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[GYROSCOPE_ANGULAR_RATE_UPDATE_CALLBACK];
76
+ if( TYPE(handler) == T_NIL ) {
77
+ callback_data->callback = T_NIL;
78
+ callback_data->exit = true;
79
+ ph_raise(PhidgetGyroscope_setOnAngularRateUpdateHandler((PhidgetGyroscopeHandle)ph->handle, NULL, (void *)NULL));
80
+ sem_post(&callback_data->callback_called);
81
+ } else {
82
+ callback_data->exit = false;
83
+ callback_data->phidget = self;
84
+ callback_data->callback = handler;
85
+ ph_raise(PhidgetGyroscope_setOnAngularRateUpdateHandler((PhidgetGyroscopeHandle)ph->handle, ph_gyroscope_on_angular_rate_update, (void *)callback_data));
86
+ ph_callback_thread(callback_data);
87
+ }
88
+ return Qnil;
89
+ }
90
+
91
+
92
+ void Init_gyroscope() {
93
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
94
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
95
+ VALUE ph_gyroscope = rb_define_class_under(ph_module, "Gyroscope", ph_common);
96
+
97
+ /* Document-method: new
98
+ * call-seq: new
99
+ *
100
+ * Creates a Phidget Gyroscope object.
101
+ */
102
+ rb_define_method(ph_gyroscope, "initialize", ph_gyroscope_init, 0);
103
+
104
+ /* Document-method: getAngularRate
105
+ * call-seq: getAngularRate -> angular_rate
106
+ *
107
+ * The most recent angular rate value that the channel has reported.
108
+ * This value will always be between MinAngularRate and MaxAngularRate.
109
+ */
110
+ rb_define_method(ph_gyroscope, "getAngularRate", ph_gyroscope_get_angular_rate, 0);
111
+ rb_define_alias(ph_gyroscope, "angular_rate", "getAngularRate");
112
+
113
+ /* Document-method: getMinAngularRate
114
+ * call-seq: getMinAngularRate -> angular_rate
115
+ *
116
+ * The minimum value the AngularRateUpdate event will report.
117
+ */
118
+ rb_define_method(ph_gyroscope, "getMinAngularRate", ph_gyroscope_get_min_angular_rate, 0);
119
+ rb_define_alias(ph_gyroscope, "min_angular_rate", "getMinAngularRate");
120
+
121
+ /* Document-method: getMaxAngularRate
122
+ * call-seq: getMaxAngularRate -> angular_rate
123
+ *
124
+ * The maximum value the AngularRateUpdate event will report.
125
+ */
126
+ rb_define_method(ph_gyroscope, "getMaxAngularRate", ph_gyroscope_get_max_angular_rate, 0);
127
+ rb_define_alias(ph_gyroscope, "max_angular_rate", "getMaxAngularRate");
128
+
129
+ /* Document-method: getAxisCount
130
+ * call-seq: getAxisCount -> axis_count
131
+ *
132
+ * The number of axes the channel can measure angular rate on.
133
+ * See your device's User Guide for more information about the number of axes and their orientation.
134
+ */
135
+ rb_define_method(ph_gyroscope, "getAxisCount", ph_gyroscope_get_axis_count, 0);
136
+ rb_define_alias(ph_gyroscope, "axis_count", "getAxisCount");
137
+
138
+ /* Document-method: getDataInterval
139
+ * call-seq: getDataInterval -> interval
140
+ *
141
+ * The DataInterval is the time that must elapse before the channel will fire another AngularRateUpdate event.
142
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
143
+ */
144
+ rb_define_method(ph_gyroscope, "getDataInterval", ph_gyroscope_get_data_interval, 0);
145
+ rb_define_alias(ph_gyroscope, "data_interval", "getDataInterval");
146
+
147
+ /* Document-method: setDataInterval
148
+ * call-seq: setDataInterval(interval)
149
+ *
150
+ * The DataInterval is the time that must elapse before the channel will fire another AngularRateUpdate event.
151
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
152
+ */
153
+ rb_define_method(ph_gyroscope, "setDataInterval", ph_gyroscope_set_data_interval, 1);
154
+ rb_define_alias(ph_gyroscope, "data_interval=", "setDataInterval");
155
+
156
+ /* Document-method: getMinDataInterval
157
+ * call-seq: getMinDataInterval -> interval
158
+ *
159
+ * The minimum value that DataInterval can be set to.
160
+ */
161
+ rb_define_method(ph_gyroscope, "getMinDataInterval", ph_gyroscope_get_min_data_interval, 0);
162
+ rb_define_alias(ph_gyroscope, "min_data_interval", "getMinDataInterval");
163
+
164
+ /* Document-method: getMaxDataInterval
165
+ * call-seq: getMaxDataInterval -> interval
166
+ *
167
+ * The maximum value that DataInterval can be set to.
168
+ */
169
+ rb_define_method(ph_gyroscope, "getMaxDataInterval", ph_gyroscope_get_max_data_interval, 0);
170
+ rb_define_alias(ph_gyroscope, "max_data_interval", "getMaxDataInterval");
171
+
172
+ /* Document-method: getTimestamp
173
+ * call-seq: getTimestamp -> timestamp
174
+ *
175
+ * The most recent timestamp value that the channel has reported. This is an extremely accurate time measurement streamed from the device.
176
+ * If your application requires a time measurement, you should use this value over a local software timestamp.
177
+ */
178
+ rb_define_method(ph_gyroscope, "getTimestamp", ph_gyroscope_get_timestamp, 0);
179
+ rb_define_alias(ph_gyroscope, "timestamp", "getTimestamp");
180
+
181
+ /* Document-method: zero
182
+ * call-seq: zero
183
+ *
184
+ * Re-zeros the gyroscope in 1-2 seconds.
185
+ * The device must be stationary when zeroing.
186
+ * The angular rate will be reported as 0.0°/s while zeroing.
187
+ * Zeroing the gyroscope is a method of compensating for the drift that is inherent to all gyroscopes. See your device's User Guide for more information on dealing with drift.
188
+ */
189
+ rb_define_method(ph_gyroscope, "zero", ph_gyroscope_zero, 0);
190
+ rb_define_alias(ph_gyroscope, "zero", "zero");
191
+
192
+
193
+ rb_define_private_method(ph_gyroscope, "ext_setOnAngularRateUpdateHandler", ph_gyroscope_set_on_angular_rate_update_handler, 1);
194
+ }
195
+
@@ -0,0 +1,39 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+
5
+ VALUE ph_hub_init(VALUE self) {
6
+ ph_data_t *ph = get_ph_data(self);
7
+ ph_raise(PhidgetHub_create((PhidgetHubHandle *)(&(ph->handle))));
8
+ return self;
9
+ }
10
+
11
+
12
+ VALUE ph_hub_set_port_power(VALUE self, VALUE port, VALUE state) {
13
+ ph_raise(PhidgetHub_setPortPower((PhidgetHubHandle)get_ph_handle(self), NUM2INT(port), TYPE(state) == T_TRUE ? PTRUE : PFALSE));
14
+ return Qnil;
15
+ }
16
+
17
+
18
+
19
+ void Init_hub() {
20
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
21
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
22
+ VALUE ph_hub = rb_define_class_under(ph_module, "Hub", ph_common);
23
+
24
+ /* Document-method: new
25
+ * call-seq: new
26
+ *
27
+ * Creates a Phidget Hub object.
28
+ */
29
+ rb_define_method(ph_hub, "initialize", ph_hub_init, 0);
30
+
31
+ /* Document-method: setPortPower
32
+ * call-seq: setPortPower(port, state)
33
+ *
34
+ * Controls power to the VINT Hub Ports.
35
+ */
36
+ rb_define_method(ph_hub, "setPortPower", ph_hub_set_port_power, 2);
37
+ rb_define_alias(ph_hub, "set_port_power", "setPortPower");
38
+ }
39
+
@@ -0,0 +1,200 @@
1
+
2
+ #include "phidgets.h"
3
+
4
+ #define HUMIDITY_SENSOR_HUMIDITY_CHANGE_CALLBACK 0
5
+
6
+
7
+ VALUE ph_humidity_init(VALUE self) {
8
+ ph_data_t *ph = get_ph_data(self);
9
+ ph_raise(PhidgetHumiditySensor_create((PhidgetHumiditySensorHandle *)(&(ph->handle))));
10
+ return self;
11
+ }
12
+
13
+ VALUE ph_humidity_get_data_interval(VALUE self) {
14
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetHumiditySensor_getDataInterval);
15
+ }
16
+
17
+ VALUE ph_humidity_set_data_interval(VALUE self, VALUE interval) {
18
+ ph_raise(PhidgetHumiditySensor_setDataInterval((PhidgetHumiditySensorHandle)get_ph_handle(self), NUM2UINT(interval)));
19
+ return Qnil;
20
+ }
21
+
22
+ VALUE ph_humidity_get_min_data_interval(VALUE self) {
23
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetHumiditySensor_getMinDataInterval);
24
+ }
25
+
26
+ VALUE ph_humidity_get_max_data_interval(VALUE self) {
27
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetHumiditySensor_getMaxDataInterval);
28
+ }
29
+
30
+ VALUE ph_humidity_get_humidity(VALUE self) {
31
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetHumiditySensor_getHumidity);
32
+ }
33
+
34
+ VALUE ph_humidity_get_min_humidity(VALUE self) {
35
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetHumiditySensor_getMinHumidity);
36
+ }
37
+
38
+ VALUE ph_humidity_get_max_humidity(VALUE self) {
39
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetHumiditySensor_getMaxHumidity);
40
+ }
41
+
42
+ VALUE ph_humidity_get_humidity_change_trigger(VALUE self) {
43
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetHumiditySensor_getHumidityChangeTrigger);
44
+ }
45
+
46
+ VALUE ph_humidity_set_humidity_change_trigger(VALUE self, VALUE change_trigger) {
47
+ ph_raise(PhidgetHumiditySensor_setHumidityChangeTrigger((PhidgetHumiditySensorHandle)get_ph_handle(self), NUM2DBL(change_trigger)));
48
+ return Qnil;
49
+ }
50
+
51
+ VALUE ph_humidity_get_min_humidity_change_trigger(VALUE self) {
52
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetHumiditySensor_getMinHumidityChangeTrigger);
53
+ }
54
+
55
+ VALUE ph_humidity_get_max_humidity_change_trigger(VALUE self) {
56
+ return ph_get_double(get_ph_handle(self), (phidget_get_double_func)PhidgetHumiditySensor_getMaxHumidityChangeTrigger);
57
+ }
58
+
59
+
60
+ void CCONV ph_humidity_on_humidity_change(PhidgetHumiditySensorHandle phid, void *userPtr, double humidity) {
61
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
62
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
63
+ callback_data->arg1 = DBL2NUM(humidity);
64
+ callback_data->arg2 = Qnil;
65
+ callback_data->arg3 = Qnil;
66
+ callback_data->arg4 = Qnil;
67
+ sem_post(&callback_data->callback_called);
68
+ }
69
+
70
+
71
+ VALUE ph_humidity_set_on_humidity_change_handler(VALUE self, VALUE handler) {
72
+ ph_data_t *ph = get_ph_data(self);
73
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[HUMIDITY_SENSOR_HUMIDITY_CHANGE_CALLBACK];
74
+ if( TYPE(handler) == T_NIL ) {
75
+ callback_data->callback = T_NIL;
76
+ callback_data->exit = true;
77
+ ph_raise(PhidgetHumiditySensor_setOnHumidityChangeHandler((PhidgetHumiditySensorHandle)ph->handle, NULL, (void *)NULL));
78
+ sem_post(&callback_data->callback_called);
79
+ } else {
80
+ callback_data->exit = false;
81
+ callback_data->phidget = self;
82
+ callback_data->callback = handler;
83
+ ph_raise(PhidgetHumiditySensor_setOnHumidityChangeHandler((PhidgetHumiditySensorHandle)ph->handle, ph_humidity_on_humidity_change, (void *)callback_data));
84
+ ph_callback_thread(callback_data);
85
+ }
86
+ return Qnil;
87
+ }
88
+
89
+
90
+ void Init_humidity() {
91
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
92
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
93
+ VALUE ph_humidity = rb_define_class_under(ph_module, "HumiditySensor", ph_common);
94
+
95
+ /* Document-method: new
96
+ * call-seq: new
97
+ *
98
+ * Creates a Phidget HumiditySensor object.
99
+ */
100
+ rb_define_method(ph_humidity, "initialize", ph_humidity_init, 0);
101
+
102
+ /* Document-method: getDataInterval
103
+ * call-seq: getDataInterval -> interval
104
+ *
105
+ * The DataInterval is the time that must elapse before the channel will fire another HumidityChange event.
106
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
107
+ * The timing between HumidityChange events can also affected by the HumidityChangeTrigger.
108
+ */
109
+ rb_define_method(ph_humidity, "getDataInterval", ph_humidity_get_data_interval, 0);
110
+ rb_define_alias(ph_humidity, "data_interval", "getDataInterval");
111
+
112
+ /* Document-method: setDataInterval
113
+ * call-seq: setDataInterval(interval)
114
+ *
115
+ * The DataInterval is the time that must elapse before the channel will fire another HumidityChange event.
116
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
117
+ * The timing between HumidityChange events can also affected by the HumidityChangeTrigger.
118
+ */
119
+ rb_define_method(ph_humidity, "setDataInterval", ph_humidity_set_data_interval, 1);
120
+ rb_define_alias(ph_humidity, "data_interval=", "setDataInterval");
121
+
122
+ /* Document-method: getMinDataInterval
123
+ * call-seq: getMinDataInterval -> interval
124
+ *
125
+ * The minimum value that DataInterval can be set to.
126
+ */
127
+ rb_define_method(ph_humidity, "getMinDataInterval", ph_humidity_get_min_data_interval, 0);
128
+ rb_define_alias(ph_humidity, "min_data_interval", "getMinDataInterval");
129
+
130
+ /* Document-method: getMaxDataInterval
131
+ * call-seq: getMaxDataInterval -> interval
132
+ *
133
+ * The maximum value that DataInterval can be set to.
134
+ */
135
+ rb_define_method(ph_humidity, "getMaxDataInterval", ph_humidity_get_max_data_interval, 0);
136
+ rb_define_alias(ph_humidity, "max_data_interval", "getMaxDataInterval");
137
+
138
+ /* Document-method: getHumidity
139
+ * call-seq: getHumidity -> humidity
140
+ *
141
+ * The most recent humidity value that the channel has reported.
142
+ * This value will always be between MinHumidity and MaxHumidity.
143
+ */
144
+ rb_define_method(ph_humidity, "getHumidity", ph_humidity_get_humidity, 0);
145
+ rb_define_alias(ph_humidity, "humidity", "getHumidity");
146
+
147
+ /* Document-method: getMinHumidity
148
+ * call-seq: getMinHumidity -> humidity
149
+ *
150
+ * The minimum value that the HumidityChange event will report.
151
+ */
152
+ rb_define_method(ph_humidity, "getMinHumidity", ph_humidity_get_min_humidity, 0);
153
+ rb_define_alias(ph_humidity, "min_humidity", "getMinHumidity");
154
+
155
+ /* Document-method: getMaxHumidity
156
+ * call-seq: getMaxHumidity -> humidity
157
+ *
158
+ * The maximum value that the HumidityChange event will report.
159
+ */
160
+ rb_define_method(ph_humidity, "getMaxHumidity", ph_humidity_get_max_humidity, 0);
161
+ rb_define_alias(ph_humidity, "max_humidity", "getMaxHumidity");
162
+
163
+ /* Document-method: getHumidityChangeTrigger
164
+ * call-seq: getHumidityChangeTrigger -> change_trigger
165
+ *
166
+ * The channel will not issue a HumidityChange event until the humidity value has changed by the amount specified by the HumidityChangeTrigger.
167
+ * Setting the HumidityChangeTrigger to 0 will result in the channel firing events every DataInterval. This is useful for applications that implement their own data filtering.
168
+ */
169
+ rb_define_method(ph_humidity, "getHumidityChangeTrigger", ph_humidity_get_humidity_change_trigger, 0);
170
+ rb_define_alias(ph_humidity, "humidity_change_trigger", "getHumidityChangeTrigger");
171
+
172
+ /* Document-method: setHumidityChangeTrigger
173
+ * call-seq: setHumidityChangeTrigger(change_trigger)
174
+ *
175
+ * The channel will not issue a HumidityChange event until the humidity value has changed by the amount specified by the HumidityChangeTrigger.
176
+ * Setting the HumidityChangeTrigger to 0 will result in the channel firing events every DataInterval. This is useful for applications that implement their own data filtering.
177
+ */
178
+ rb_define_method(ph_humidity, "setHumidityChangeTrigger", ph_humidity_set_humidity_change_trigger, 1);
179
+ rb_define_alias(ph_humidity, "humidity_change_trigger=", "setHumidityChangeTrigger");
180
+
181
+ /* Document-method: getMinHumidityChangeTrigger
182
+ * call-seq: getMinHumidityChangeTrigger -> change_trigger
183
+ *
184
+ * The minimum value that HumidityChangeTrigger can be set to.
185
+ */
186
+ rb_define_method(ph_humidity, "getMinHumidityChangeTrigger", ph_humidity_get_min_humidity_change_trigger, 0);
187
+ rb_define_alias(ph_humidity, "min_humidity_change_trigger", "getMinHumidityChangeTrigger");
188
+
189
+ /* Document-method: getMaxHumidityChangeTrigger
190
+ * call-seq: getMaxHumidityChangeTrigger -> change_trigger
191
+ *
192
+ * The maximum value that HumidityChangeTrigger can be set to.
193
+ */
194
+ rb_define_method(ph_humidity, "getMaxHumidityChangeTrigger", ph_humidity_get_max_humidity_change_trigger, 0);
195
+ rb_define_alias(ph_humidity, "max_humidity_change_trigger", "getMaxHumidityChangeTrigger");
196
+
197
+
198
+ rb_define_private_method(ph_humidity, "ext_setOnHumidityChangeHandler", ph_humidity_set_on_humidity_change_handler, 1);
199
+ }
200
+
@@ -1,251 +1,291 @@
1
1
 
2
2
  #include "phidgets.h"
3
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
- void Init_ir() {
24
- VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
25
- VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
26
- VALUE ph_ir = rb_define_class_under(ph_module, "IR", ph_common);
27
-
28
- /* Unknown - the default value */
29
- rb_define_const(ph_ir, "ENCODING_UNKNOWN", INT2FIX(PHIDGET_IR_ENCODING_UNKNOWN));
30
- /* Space encoding, or Pulse Distance Modulation */
31
- rb_define_const(ph_ir, "ENCODING_SPACE", INT2FIX(PHIDGET_IR_ENCODING_SPACE));
32
- /* Pulse encoding, or Pulse Width Modulation */
33
- rb_define_const(ph_ir, "ENCODING_PULSE", INT2FIX(PHIDGET_IR_ENCODING_PULSE));
34
- /* Bi-Phase, or Manchester encoding */
35
- rb_define_const(ph_ir, "ENCODING_BIPHASE", INT2FIX(PHIDGET_IR_ENCODING_BIPHASE));
36
- /* RC5 - a type of Bi-Phase encoding */
37
- rb_define_const(ph_ir, "ENCODING_RC5", INT2FIX(PHIDGET_IR_ENCODING_RC5));
38
- /* RC6 - a type of Bi-Phase encoding */
39
- rb_define_const(ph_ir, "ENCODING_RC6", INT2FIX(PHIDGET_IR_ENCODING_RC6));
40
-
41
- /* Unknown - the default value */
42
- rb_define_const(ph_ir, "LENGTH_UNKNOWN", INT2FIX(PHIDGET_IR_LENGTH_UNKNOWN));
43
- /* Constant - the bitstream + gap length is constant */
44
- rb_define_const(ph_ir, "LENGTH_CONSTANT", INT2FIX(PHIDGET_IR_LENGTH_CONSTANT));
45
- /* Variable - the bitstream has a variable length with a constant gap */
46
- rb_define_const(ph_ir, "LENGTH_VARIABLE", INT2FIX(PHIDGET_IR_LENGTH_VARIABLE));
47
-
48
- /* Document-method: new
49
- * call-seq: new
50
- *
51
- * Creates a Phidget IR object.
52
- */
53
- rb_define_method(ph_ir, "initialize", ph_ir_init, 0);
54
-
55
- /* Document-method: transmit
56
- * call-seq: transmit(data, code_info)
57
- *
58
- * ** NOT IMPLEMENTED **
59
- */
60
- rb_define_method(ph_ir, "transmit", ph_ir_transmit, 2);
61
-
62
- /* Document-method: transmitRepeat
63
- * call-seq: transmitRepeat
64
- *
65
- * Transmits a repeat of the last transmited code. Depending of the CodeInfo structure,
66
- * this may be a retransmission of the code itself, or there may be a special repeat code.
67
- */
68
- rb_define_method(ph_ir, "transmitRepeat", ph_ir_transmit_repeat, 0);
69
-
70
- /* Document-method: transmitRaw
71
- * call-seq: transmitRaw(data, carrier_frequency, duty_cycle, gap)
72
- *
73
- * Transmits RAW data as a series of pulses and spaces.
74
- */
75
- rb_define_method(ph_ir, "transmitRaw", ph_ir_transmit_raw, 4);
76
-
77
- /* Document-method: getRawData
78
- * call-seq: getRawData(max_length) -> raw_data
79
- *
80
- * Read any available raw data. This should be polled continuously (every 20ms) to avoid missing data.
81
- * Read data always starts with a space and ends with a pulse.
82
- */
83
- rb_define_method(ph_ir, "getRawData", ph_ir_get_raw_data, 1);
84
-
85
- /* Document-method: getLastCode
86
- * call-seq: getLastCode -> code
87
- *
88
- * ** NOT IMPLEMENTED **
89
- */
90
- rb_define_method(ph_ir, "getLastCode", ph_ir_get_last_code, 0);
91
-
92
- /* Document-method: getLastLearnedCode
93
- * call-seq: getLastLearnedCode -> code
94
- *
95
- * ** NOT IMPLEMENTED **
96
- */
97
- rb_define_method(ph_ir, "getLastLearnedCode", ph_ir_get_last_learned_code, 0);
98
-
99
- #ifdef PH_CALLBACK
100
- rb_define_private_method(ph_ir, "ext_setOnCodeHandler", ph_ir_set_on_code_handler, 1);
101
- rb_define_private_method(ph_ir, "ext_setOnLearnHandler", ph_ir_set_on_learn_handler, 1);
102
- rb_define_private_method(ph_ir, "ext_setOnRawDataHandler", ph_ir_set_on_raw_data_handler, 1);
103
- #endif
104
-
105
- rb_define_alias(ph_ir, "transmit_repeat", "transmitRepeat");
106
- rb_define_alias(ph_ir, "transmit_raw", "transmitRaw");
107
- rb_define_alias(ph_ir, "raw_data", "getRawData");
108
- rb_define_alias(ph_ir, "last_code", "getLastCode");
109
- rb_define_alias(ph_ir, "last_learned_code", "getLastLearnedCode");
110
- }
4
+ #define IR_CODE_CALLBACK 0
5
+ #define IR_LEARN_CALLBACK 1
6
+ #define IR_RAW_DATA_CALLBACK 2
111
7
 
112
8
 
113
9
 
114
10
  VALUE ph_ir_init(VALUE self) {
115
11
  ph_data_t *ph = get_ph_data(self);
116
- ph_raise(CPhidgetIR_create((CPhidgetIRHandle *)(&(ph->handle))));
12
+ ph_raise(PhidgetIR_create((PhidgetIRHandle *)(&(ph->handle))));
117
13
  return self;
118
14
  }
119
15
 
120
- VALUE ph_ir_transmit(VALUE self, VALUE data, VALUE code_info) {
121
- ph_raise(EPHIDGET_NOTIMPLEMENTED);
16
+
17
+ VALUE ph_ir_transmit(VALUE self, VALUE code, VALUE code_info) {
18
+ PhidgetIRHandle handle = (PhidgetIRHandle)get_ph_handle(self);
19
+ PhidgetIR_CodeInfo info;
20
+ int i;
21
+
22
+ if(! RB_TYPE_P(code, T_STRING)) ph_raise(EPHIDGET_INVALIDARG);
23
+ if(! RB_TYPE_P(code_info, T_HASH)) ph_raise(EPHIDGET_INVALIDARG);
24
+
25
+ info.bitCount = NUM2UINT(rb_hash_aref(code_info, rb_str_new2("bitCount")));
26
+ info.encoding = NUM2INT(rb_hash_aref(code_info, rb_str_new2("encoding")));
27
+ info.length = NUM2INT(rb_hash_aref(code_info, rb_str_new2("length")));
28
+ info.gap = NUM2UINT(rb_hash_aref(code_info, rb_str_new2("gap")));
29
+ info.trail = NUM2UINT(rb_hash_aref(code_info, rb_str_new2("trail")));
30
+ for(i=0; i<2; i++) { info.header[i] = NUM2UINT(rb_ary_entry(rb_hash_aref(code_info, rb_str_new2("header")), i)); }
31
+ for(i=0; i<2; i++) { info.one[i] = NUM2UINT(rb_ary_entry(rb_hash_aref(code_info, rb_str_new2("one")), i)); }
32
+ for(i=0; i<2; i++) { info.zero[i] = NUM2UINT(rb_ary_entry(rb_hash_aref(code_info, rb_str_new2("zero")), i)); }
33
+ for(i=0; i<26; i++) { info.repeat[i] = NUM2UINT(rb_ary_entry(rb_hash_aref(code_info, rb_str_new2("repeat")), i)); }
34
+ info.minRepeat = NUM2UINT(rb_hash_aref(code_info, rb_str_new2("minRepeat")));
35
+ info.dutyCycle = NUM2DBL(rb_hash_aref(code_info, rb_str_new2("dutyCycle")));
36
+ info.carrierFrequency = NUM2UINT(rb_hash_aref(code_info, rb_str_new2("carrierFrequency")));
37
+ for(i=0; i<33; i++) { info.toggleMask[i] = NUM2CHR(rb_ary_entry(rb_hash_aref(code_info, rb_str_new2("toggleMask")), i)); }
38
+
39
+ ph_raise(PhidgetIR_transmit(handle, StringValuePtr(code), &info));
122
40
  return Qnil;
123
41
  }
124
42
 
43
+
125
44
  VALUE ph_ir_transmit_repeat(VALUE self) {
126
- CPhidgetIRHandle handle = (CPhidgetIRHandle)get_ph_handle(self);
127
- ph_raise(CPhidgetIR_TransmitRepeat(handle));
45
+ ph_raise(PhidgetIR_transmitRepeat((PhidgetIRHandle)get_ph_handle(self)));
128
46
  return Qnil;
129
47
  }
130
48
 
49
+
131
50
  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;
51
+ PhidgetIRHandle handle = (PhidgetIRHandle)get_ph_handle(self);
52
+ size_t data_length = RARRAY_LEN(data);
53
+ uint32_t raw_data[200];
54
+ size_t i;
136
55
 
137
- for(i=0; i<data_length; i++) {
138
- int_data[i] = NUM2INT(rb_ary_entry(data, i));
139
- }
56
+ for(i=0; i<data_length; i++) { raw_data[i] = NUM2UINT(rb_ary_entry(data, i)); }
57
+ ph_raise(PhidgetIR_transmitRaw(handle, raw_data, data_length, NUM2UINT(carrier_freq), NUM2DBL(duty_cycle), NUM2UINT(gap)));
58
+ return Qnil;
59
+ }
140
60
 
141
- ph_raise(CPhidgetIR_TransmitRaw(handle, int_data, data_length, FIX2INT(carrier_freq), FIX2INT(duty_cycle), FIX2INT(gap)));
142
61
 
143
- free(int_data);
144
- return Qnil;
62
+ VALUE ph_ir_get_last_code(VALUE self) {
63
+ PhidgetIRHandle handle = (PhidgetIRHandle)get_ph_handle(self);
64
+ char code[IR_MAX_CODE_STR_LENGTH];
65
+ uint32_t bitCount;
66
+ VALUE rb_code_data = rb_hash_new();
67
+
68
+ ph_raise(PhidgetIR_getLastCode(handle, code, IR_MAX_CODE_STR_LENGTH, &bitCount));
69
+ rb_hash_aset(rb_code_data, rb_str_new2("code"), rb_str_new2(code));
70
+ rb_hash_aset(rb_code_data, rb_str_new2("bitCount"), UINT2NUM(bitCount));
71
+ return rb_code_data;
145
72
  }
146
73
 
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
74
 
154
- ph_raise(CPhidgetIR_getRawData(handle, raw_data, &data_length));
75
+ VALUE ph_ir_get_last_learned_code(VALUE self) {
76
+ PhidgetIRHandle handle = (PhidgetIRHandle)get_ph_handle(self);
77
+ char code[IR_MAX_CODE_STR_LENGTH];
78
+ PhidgetIR_CodeInfo info;
79
+ VALUE rb_code_data = rb_hash_new();
80
+ VALUE rb_code_info = rb_hash_new();
81
+ VALUE rb_info_header = rb_ary_new();
82
+ VALUE rb_info_one = rb_ary_new();
83
+ VALUE rb_info_zero = rb_ary_new();
84
+ VALUE rb_info_repeat = rb_ary_new();
85
+ VALUE rb_info_toggle_mask = rb_ary_new();
86
+ int i;
155
87
 
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;
88
+ ph_raise(PhidgetIR_getLastLearnedCode(handle, code, IR_MAX_CODE_STR_LENGTH, &info));
89
+
90
+ rb_hash_aset(rb_code_info, rb_str_new2("bitCount"), UINT2NUM(info.bitCount));
91
+ rb_hash_aset(rb_code_info, rb_str_new2("encoding"), INT2NUM(info.encoding));
92
+ rb_hash_aset(rb_code_info, rb_str_new2("length"), INT2NUM(info.length));
93
+ rb_hash_aset(rb_code_info, rb_str_new2("gap"), UINT2NUM(info.gap));
94
+ rb_hash_aset(rb_code_info, rb_str_new2("trail"), UINT2NUM(info.trail));
95
+ for(i=0; i<2; i++) { rb_ary_push(rb_info_header, UINT2NUM(info.header[i])); }
96
+ rb_hash_aset(rb_code_info, rb_str_new2("header"), rb_info_header);
97
+ for(i=0; i<2; i++) { rb_ary_push(rb_info_one, UINT2NUM(info.one[i])); }
98
+ rb_hash_aset(rb_code_info, rb_str_new2("one"), rb_info_one);
99
+ for(i=0; i<2; i++) { rb_ary_push(rb_info_zero, UINT2NUM(info.zero[i])); }
100
+ rb_hash_aset(rb_code_info, rb_str_new2("zero"), rb_info_zero);
101
+ for(i=0; i<26; i++) { rb_ary_push(rb_info_repeat, UINT2NUM(info.repeat[i])); }
102
+ rb_hash_aset(rb_code_info, rb_str_new2("repeat"), rb_info_repeat);
103
+ rb_hash_aset(rb_code_info, rb_str_new2("minRepeat"), UINT2NUM(info.minRepeat));
104
+ rb_hash_aset(rb_code_info, rb_str_new2("dutyCycle"), DBL2NUM(info.dutyCycle));
105
+ rb_hash_aset(rb_code_info, rb_str_new2("carrierFrequency"), UINT2NUM(info.carrierFrequency));
106
+ for(i=0; i<33; i++) { rb_ary_push(rb_info_toggle_mask, INT2NUM(info.toggleMask[i])); }
107
+ rb_hash_aset(rb_code_info, rb_str_new2("toggleMask"), rb_info_toggle_mask);
108
+
109
+ rb_hash_aset(rb_code_data, rb_str_new2("code"), rb_str_new2(code));
110
+ rb_hash_aset(rb_code_data, rb_str_new2("codeInfo"), rb_code_info);
111
+ return rb_code_data;
162
112
  }
163
113
 
164
- VALUE ph_ir_get_last_code(VALUE self) {
165
- ph_raise(EPHIDGET_NOTIMPLEMENTED);
166
- return Qnil;
167
- }
168
114
 
169
- VALUE ph_ir_get_last_learned_code(VALUE self) {
170
- ph_raise(EPHIDGET_NOTIMPLEMENTED);
171
- return Qnil;
115
+ void CCONV ph_ir_on_code(PhidgetIRHandle phid, void *userPtr, const char *code, uint32_t bitCount, int repeat) {
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(code);
119
+ callback_data->arg2 = UINT2NUM(bitCount);
120
+ callback_data->arg3 = INT2NUM(repeat);
121
+ callback_data->arg4 = Qnil;
122
+ sem_post(&callback_data->callback_called);
172
123
  }
173
124
 
174
125
 
175
- #ifdef PH_CALLBACK
176
126
  VALUE ph_ir_set_on_code_handler(VALUE self, VALUE handler) {
177
127
  ph_data_t *ph = get_ph_data(self);
178
- ph_callback_data_t *callback_data = &ph->dev_callback_1;
128
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[IR_CODE_CALLBACK];
179
129
  if( TYPE(handler) == T_NIL ) {
130
+ callback_data->callback = T_NIL;
180
131
  callback_data->exit = true;
181
- ph_raise(CPhidgetIR_set_OnCode_Handler((CPhidgetIRHandle)ph->handle, NULL, (void *)NULL));
132
+ ph_raise(PhidgetIR_setOnCodeHandler((PhidgetIRHandle)ph->handle, NULL, (void *)NULL));
133
+ sem_post(&callback_data->callback_called);
182
134
  } else {
183
- callback_data->called = false;
184
135
  callback_data->exit = false;
185
136
  callback_data->phidget = self;
186
137
  callback_data->callback = handler;
187
- ph_raise(CPhidgetIR_set_OnCode_Handler((CPhidgetIRHandle)ph->handle, ph_ir_on_code, (void *)callback_data));
138
+ ph_raise(PhidgetIR_setOnCodeHandler((PhidgetIRHandle)ph->handle, ph_ir_on_code, (void *)callback_data));
188
139
  ph_callback_thread(callback_data);
189
140
  }
190
141
  return Qnil;
191
142
  }
192
143
 
193
144
 
145
+ void CCONV ph_ir_on_learn(PhidgetIRHandle phid, void *userPtr, const char *code, PhidgetIR_CodeInfo *codeInfo) {
146
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
147
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
148
+ callback_data->arg1 = Qnil;
149
+ callback_data->arg2 = Qnil;
150
+ callback_data->arg3 = Qnil;
151
+ callback_data->arg4 = Qnil;
152
+ sem_post(&callback_data->callback_called);
153
+ }
154
+
155
+
194
156
  VALUE ph_ir_set_on_learn_handler(VALUE self, VALUE handler) {
195
157
  ph_data_t *ph = get_ph_data(self);
196
- ph_callback_data_t *callback_data = &ph->dev_callback_2;
158
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[IR_LEARN_CALLBACK];
197
159
  if( TYPE(handler) == T_NIL ) {
160
+ callback_data->callback = T_NIL;
198
161
  callback_data->exit = true;
199
- ph_raise(CPhidgetIR_set_OnLearn_Handler((CPhidgetIRHandle)ph->handle, NULL, (void *)NULL));
162
+ ph_raise(PhidgetIR_setOnLearnHandler((PhidgetIRHandle)ph->handle, NULL, (void *)NULL));
163
+ sem_post(&callback_data->callback_called);
200
164
  } else {
201
- callback_data->called = false;
202
165
  callback_data->exit = false;
203
166
  callback_data->phidget = self;
204
167
  callback_data->callback = handler;
205
- ph_raise(CPhidgetIR_set_OnLearn_Handler((CPhidgetIRHandle)ph->handle, ph_ir_on_learn, (void *)callback_data));
168
+ ph_raise(PhidgetIR_setOnLearnHandler((PhidgetIRHandle)ph->handle, ph_ir_on_learn, (void *)callback_data));
206
169
  ph_callback_thread(callback_data);
207
170
  }
208
171
  return Qnil;
209
172
  }
210
173
 
211
174
 
175
+ void CCONV ph_ir_on_raw_data(PhidgetIRHandle phid, void *userPtr, const uint32_t *data, size_t dataLen) {
176
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
177
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
178
+ callback_data->arg1 = Qnil;
179
+ callback_data->arg2 = Qnil;
180
+ callback_data->arg3 = Qnil;
181
+ callback_data->arg4 = Qnil;
182
+ sem_post(&callback_data->callback_called);
183
+ }
184
+
185
+
212
186
  VALUE ph_ir_set_on_raw_data_handler(VALUE self, VALUE handler) {
213
187
  ph_data_t *ph = get_ph_data(self);
214
- ph_callback_data_t *callback_data = &ph->dev_callback_3;
188
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[IR_RAW_DATA_CALLBACK];
215
189
  if( TYPE(handler) == T_NIL ) {
190
+ callback_data->callback = T_NIL;
216
191
  callback_data->exit = true;
217
- ph_raise(CPhidgetIR_set_OnRawData_Handler((CPhidgetIRHandle)ph->handle, NULL, (void *)NULL));
192
+ ph_raise(PhidgetIR_setOnRawDataHandler((PhidgetIRHandle)ph->handle, NULL, (void *)NULL));
193
+ sem_post(&callback_data->callback_called);
218
194
  } else {
219
- callback_data->called = false;
220
195
  callback_data->exit = false;
221
196
  callback_data->phidget = self;
222
197
  callback_data->callback = handler;
223
- ph_raise(CPhidgetIR_set_OnRawData_Handler((CPhidgetIRHandle)ph->handle, ph_ir_on_raw_data, (void *)callback_data));
198
+ ph_raise(PhidgetIR_setOnRawDataHandler((PhidgetIRHandle)ph->handle, ph_ir_on_raw_data, (void *)callback_data));
224
199
  ph_callback_thread(callback_data);
225
200
  }
226
201
  return Qnil;
227
202
  }
228
203
 
229
204
 
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
- }
205
+ void Init_ir() {
206
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
207
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
208
+ VALUE ph_ir = rb_define_class_under(ph_module, "IR", ph_common);
235
209
 
210
+ rb_define_const(ph_ir, "ENCODING_UNKNOWN", INT2NUM(IR_ENCODING_UNKNOWN));
211
+ rb_define_const(ph_ir, "ENCODING_SPACE", INT2NUM(IR_ENCODING_SPACE));
212
+ rb_define_const(ph_ir, "ENCODING_PULSE", INT2NUM(IR_ENCODING_PULSE));
213
+ rb_define_const(ph_ir, "ENCODING_BIPHASE", INT2NUM(IR_ENCODING_BIPHASE));
214
+ rb_define_const(ph_ir, "ENCODING_RC5", INT2NUM(IR_ENCODING_RC5));
215
+ rb_define_const(ph_ir, "ENCODING_RC6", INT2NUM(IR_ENCODING_RC6));
236
216
 
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
- }
217
+ rb_define_const(ph_ir, "LENGTH_UNKNOWN", INT2NUM(IR_LENGTH_UNKNOWN));
218
+ rb_define_const(ph_ir, "LENGTH_CONSTANT", INT2NUM(IR_LENGTH_CONSTANT));
219
+ rb_define_const(ph_ir, "LENGTH_VARIABLE", INT2NUM(IR_LENGTH_VARIABLE));
242
220
 
243
221
 
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
- }
222
+ /* Document-method: new
223
+ * call-seq: new
224
+ *
225
+ * Creates a Phidget IR object.
226
+ */
227
+ rb_define_method(ph_ir, "initialize", ph_ir_init, 0);
249
228
 
250
- #endif
229
+ /* Document-method: transmit
230
+ * call-seq: transmit(code, code_info)
231
+ *
232
+ * Transmits a code
233
+ * code data is transmitted MSBit first.
234
+ * MSByte is in array index 0 of code
235
+ * LSBit is right justified, therefore, MSBit may be in bit position 0-7 (of array index 0) depending on the bit count
236
+ */
237
+ rb_define_method(ph_ir, "transmit", ph_ir_transmit, 2);
238
+
239
+ /* Document-method: transmitRepeat
240
+ * call-seq: transmitRepeat
241
+ *
242
+ * Transmits a repeat of the last transmited code.
243
+ * Depending on the CodeInfo structure, this may be a retransmission of the code itself, or there may be a special repeat code.
244
+ */
245
+ rb_define_method(ph_ir, "transmitRepeat", ph_ir_transmit_repeat, 0);
246
+ rb_define_alias(ph_ir, "transmit_repeat", "transmitRepeat");
247
+
248
+ /* Document-method: transmitRaw
249
+ * call-seq: transmitRaw(data, carrier_frequency, duty_cycle, gap)
250
+ *
251
+ * Transmits raw data as a series of pulses and spaces.
252
+ * data must start and end with a pulse.
253
+ * Each element is a positive time in μs
254
+ * dataLength has a maximum length of 200, however, streams should be kept must shorter than this (less than 100ms between gaps).
255
+ * dataLength must be an odd number
256
+ * Leave carrierFrequency as 0 for default.
257
+ * carrierFrequency has a range of 10kHz - 1MHz
258
+ * Leave dutyCycle as 0 for default
259
+ * dutyCycle can have a value between 0.1 and 0.5
260
+ * Specifying a gap will guarantee a gap time (no transmitting) after data is sent.
261
+ * gap time is in μs
262
+ * gap time can be set to 0
263
+ */
264
+ rb_define_method(ph_ir, "transmitRaw", ph_ir_transmit_raw, 4);
265
+ rb_define_alias(ph_ir, "transmit_raw", "transmitRaw");
266
+
267
+ /* Document-method: getLastCode
268
+ * call-seq: getLastCode -> code
269
+ *
270
+ * The last code the channel has received.
271
+ * The code is represented by a hexadecimal string (array of bytes)
272
+ */
273
+ rb_define_method(ph_ir, "getLastCode", ph_ir_get_last_code, 0);
274
+ rb_define_alias(ph_ir, "last_code", "getLastCode");
275
+
276
+ /* Document-method: getLastLearnedCode
277
+ * call-seq: getLastLearnedCode -> code
278
+ *
279
+ * The last code the channel has learned.
280
+ * The code is represented by a hexadecimal string (array of bytes).
281
+ * The codeInfo structure holds data that describes the learned code
282
+ */
283
+ rb_define_method(ph_ir, "getLastLearnedCode", ph_ir_get_last_learned_code, 0);
284
+ rb_define_alias(ph_ir, "last_learned_code", "getLastLearnedCode");
285
+
286
+
287
+ rb_define_private_method(ph_ir, "ext_setOnCodeHandler", ph_ir_set_on_code_handler, 1);
288
+ rb_define_private_method(ph_ir, "ext_setOnLearnHandler", ph_ir_set_on_learn_handler, 1);
289
+ rb_define_private_method(ph_ir, "ext_setOnRawDataHandler", ph_ir_set_on_raw_data_handler, 1);
290
+ }
251
291