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
@@ -1,249 +1,268 @@
1
1
 
2
2
  #include "phidgets.h"
3
3
 
4
+ #define ENCODER_POSITION_CHANGE_CALLBACK 0
4
5
 
5
- VALUE ph_encoder_init(VALUE self);
6
- VALUE ph_encoder_get_input_count(VALUE self);
7
- VALUE ph_encoder_get_input_state(VALUE self, VALUE index);
8
- VALUE ph_encoder_get_encoder_count(VALUE self);
9
- VALUE ph_encoder_get_position(VALUE self, VALUE index);
10
- VALUE ph_encoder_set_position(VALUE self, VALUE index, VALUE position);
11
- VALUE ph_encoder_get_index_position(VALUE self, VALUE index);
12
- VALUE ph_encoder_get_enabled(VALUE self, VALUE index);
13
- VALUE ph_encoder_set_enabled(VALUE self, VALUE index, VALUE state);
14
-
15
- #ifdef PH_CALLBACK
16
- VALUE ph_encoder_set_on_input_change_handler(VALUE self, VALUE handler);
17
- VALUE ph_encoder_set_on_position_change_handler(VALUE self, VALUE handler);
18
- VALUE ph_encoder_set_on_index_handler(VALUE self, VALUE handler);
19
- int ph_encoder_on_input_change(CPhidgetEncoderHandle phid, void *userPtr, int index, int state);
20
- int ph_encoder_on_position_change(CPhidgetEncoderHandle phid, void *userPtr, int index, int time, int change);
21
- int ph_encoder_on_index(CPhidgetEncoderHandle phid, void *userPtr, int index, int index_pos);
22
- #endif
23
6
 
7
+ VALUE ph_encoder_init(VALUE self) {
8
+ ph_data_t *ph = get_ph_data(self);
9
+ ph_raise(PhidgetEncoder_create((PhidgetEncoderHandle *)(&(ph->handle))));
10
+ return self;
11
+ }
24
12
 
25
- void Init_encoder() {
26
- VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
27
- VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
28
- VALUE ph_encoder = rb_define_class_under(ph_module, "Encoder", ph_common);
29
-
30
- /* Document-method: new
31
- * call-seq: new
32
- *
33
- * Creates a Phidget Encoder object.
34
- */
35
- rb_define_method(ph_encoder, "initialize", ph_encoder_init, 0);
36
-
37
- /* Document-method: getInputCount
38
- * call-seq: getInputCount -> count
39
- *
40
- * Gets the number of digital inputs supported by this board.
41
- */
42
- rb_define_method(ph_encoder, "getInputCount", ph_encoder_get_input_count, 0);
43
-
44
- /* Document-method: getInputState
45
- * call-seq: getInputState(index) -> true or false
46
- *
47
- * Gets the state of a digital input.
48
- */
49
- rb_define_method(ph_encoder, "getInputState", ph_encoder_get_input_state, 1);
50
-
51
- /* Document-method: getEncoderCount
52
- * call-seq: getEncoderCount -> count
53
- *
54
- * Gets the number of encoder inputs supported by this board.
55
- */
56
- rb_define_method(ph_encoder, "getEncoderCount", ph_encoder_get_encoder_count, 0);
57
-
58
- /* Document-method: getPosition
59
- * call-seq: getPosition(index) -> position
60
- *
61
- * Gets the current position of an encoder.
62
- */
63
- rb_define_method(ph_encoder, "getPosition", ph_encoder_get_position, 1);
64
-
65
- /* Document-method: setPosition
66
- * call-seq: setPosition(index, position)
67
- *
68
- * Sets the current position of an encoder.
69
- */
70
- rb_define_method(ph_encoder, "setPosition", ph_encoder_set_position, 2);
71
-
72
- /* Document-method: getIndexPosition
73
- * call-seq: getIndexPosition(index) -> position
74
- *
75
- * Gets the position of the last index pulse, as referenced to getPosition. This will raise Phidgets::Error::UnknownVal
76
- * if there hasn't been an index event, or if the encoder doesn't support index.
77
- */
78
- rb_define_method(ph_encoder, "getIndexPosition", ph_encoder_get_index_position, 1);
79
-
80
- /* Document-method: getEnabled
81
- * call-seq: getEnabled(index) -> true or false
82
- *
83
- * Gets the enabled state of an encoder. This is whether the encoder is powered or not.
84
- */
85
- rb_define_method(ph_encoder, "getEnabled", ph_encoder_get_enabled, 1);
86
-
87
- /* Document-method: setEnabled
88
- * call-seq: setEnabled(index, state)
89
- *
90
- * Sets the enabled state of an encoder. This is whether the encoder is powered or not.
91
- */
92
- rb_define_method(ph_encoder, "setEnabled", ph_encoder_set_enabled, 2);
13
+ VALUE ph_encoder_get_enabled(VALUE self) {
14
+ return ph_get_bool(get_ph_handle(self), (phidget_get_bool_func)PhidgetEncoder_getEnabled);
15
+ }
93
16
 
94
- #ifdef PH_CALLBACK
95
- rb_define_private_method(ph_encoder, "ext_setOnInputChangeHandler", ph_encoder_set_on_input_change_handler, 1);
96
- rb_define_private_method(ph_encoder, "ext_setOnPositionChangeHandler", ph_encoder_set_on_position_change_handler, 1);
97
- rb_define_private_method(ph_encoder, "ext_setOnIndexHandler", ph_encoder_set_on_index_handler, 1);
98
- #endif
17
+ VALUE ph_encoder_set_enabled(VALUE self, VALUE state) {
18
+ ph_raise(PhidgetEncoder_setEnabled((PhidgetEncoderHandle)get_ph_handle(self), TYPE(state) == T_TRUE ? PTRUE : PFALSE));
19
+ return Qnil;
20
+ }
99
21
 
100
- rb_define_alias(ph_encoder, "input_count", "getInputCount");
101
- rb_define_alias(ph_encoder, "input_state", "getInputState");
102
- rb_define_alias(ph_encoder, "encoder_count", "getEncoderCount");
103
- rb_define_alias(ph_encoder, "position", "getPosition");
104
- rb_define_alias(ph_encoder, "set_position", "setPosition");
105
- rb_define_alias(ph_encoder, "index_position", "getIndexPosition");
106
- rb_define_alias(ph_encoder, "enabled?", "getEnabled");
107
- rb_define_alias(ph_encoder, "set_enabled", "setEnabled");
22
+ VALUE ph_encoder_get_data_interval(VALUE self) {
23
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetEncoder_getDataInterval);
108
24
  }
109
25
 
26
+ VALUE ph_encoder_set_data_interval(VALUE self, VALUE interval) {
27
+ ph_raise(PhidgetEncoder_setDataInterval((PhidgetEncoderHandle)get_ph_handle(self), NUM2UINT(interval)));
28
+ return Qnil;
29
+ }
110
30
 
31
+ VALUE ph_encoder_get_min_data_interval(VALUE self) {
32
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetEncoder_getMinDataInterval);
33
+ }
111
34
 
112
- VALUE ph_encoder_init(VALUE self) {
113
- ph_data_t *ph = get_ph_data(self);
114
- ph_raise(CPhidgetEncoder_create((CPhidgetEncoderHandle *)(&(ph->handle))));
115
- return self;
35
+ VALUE ph_encoder_get_max_data_interval(VALUE self) {
36
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetEncoder_getMaxDataInterval);
116
37
  }
117
38
 
118
- VALUE ph_encoder_get_input_count(VALUE self) {
119
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
120
- int count;
121
- ph_raise(CPhidgetEncoder_getInputCount(handle, &count));
122
- return INT2FIX(count);
39
+ VALUE ph_encoder_get_index_position(VALUE self) {
40
+ return ph_get_int64(get_ph_handle(self), (phidget_get_int64_func)PhidgetEncoder_getIndexPosition);
123
41
  }
124
42
 
125
- VALUE ph_encoder_get_input_state(VALUE self, VALUE index) {
126
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
127
- int state;
128
- ph_raise(CPhidgetEncoder_getInputState(handle, FIX2INT(index), &state));
129
- return state == PTRUE ? Qtrue : Qfalse;
43
+ VALUE ph_encoder_get_io_mode(VALUE self) {
44
+ return ph_get_int(get_ph_handle(self), (phidget_get_int_func)PhidgetEncoder_getIOMode);
130
45
  }
131
46
 
132
- VALUE ph_encoder_get_encoder_count(VALUE self) {
133
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
134
- int count;
135
- ph_raise(CPhidgetEncoder_getEncoderCount(handle, &count));
136
- return INT2FIX(count);
47
+ VALUE ph_encoder_set_io_mode(VALUE self, VALUE mode) {
48
+ ph_raise(PhidgetEncoder_setIOMode((PhidgetEncoderHandle)get_ph_handle(self), NUM2INT(mode)));
49
+ return Qnil;
137
50
  }
138
51
 
139
- VALUE ph_encoder_get_position(VALUE self, VALUE index) {
140
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
141
- int position;
142
- ph_raise(CPhidgetEncoder_getPosition(handle, FIX2INT(index), &position));
143
- return INT2FIX(position);
52
+ VALUE ph_encoder_get_position(VALUE self) {
53
+ return ph_get_int64(get_ph_handle(self), (phidget_get_int64_func)PhidgetEncoder_getPosition);
144
54
  }
145
55
 
146
- VALUE ph_encoder_set_position(VALUE self, VALUE index, VALUE position) {
147
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
148
- ph_raise(CPhidgetEncoder_setPosition(handle, FIX2INT(index), FIX2INT(position)));
56
+ VALUE ph_encoder_set_position(VALUE self, VALUE position) {
57
+ ph_raise(PhidgetEncoder_setPosition((PhidgetEncoderHandle)get_ph_handle(self), NUM2LL(position)));
149
58
  return Qnil;
150
59
  }
151
60
 
152
- VALUE ph_encoder_get_index_position(VALUE self, VALUE index) {
153
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
154
- int position;
155
- ph_raise(CPhidgetEncoder_getIndexPosition(handle, FIX2INT(index), &position));
156
- return INT2FIX(position);
61
+ VALUE ph_encoder_get_position_change_trigger(VALUE self) {
62
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetEncoder_getPositionChangeTrigger);
157
63
  }
158
64
 
159
- VALUE ph_encoder_get_enabled(VALUE self, VALUE index) {
160
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
161
- int state;
162
- ph_raise(CPhidgetEncoder_getEnabled(handle, FIX2INT(index), &state));
163
- return state == PTRUE ? Qtrue : Qfalse;
65
+ VALUE ph_encoder_set_position_change_trigger(VALUE self, VALUE trigger) {
66
+ ph_raise(PhidgetEncoder_setPositionChangeTrigger((PhidgetEncoderHandle)get_ph_handle(self), NUM2UINT(trigger)));
67
+ return Qnil;
164
68
  }
165
69
 
166
- VALUE ph_encoder_set_enabled(VALUE self, VALUE index, VALUE state) {
167
- CPhidgetEncoderHandle handle = (CPhidgetEncoderHandle)get_ph_handle(self);
168
- ph_raise(CPhidgetEncoder_setEnabled(handle, FIX2INT(index), TYPE(state) == T_TRUE ? PTRUE : PFALSE));
169
- return Qnil;
70
+ VALUE ph_encoder_get_min_position_change_trigger(VALUE self) {
71
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetEncoder_getMinPositionChangeTrigger);
170
72
  }
171
73
 
74
+ VALUE ph_encoder_get_max_position_change_trigger(VALUE self) {
75
+ return ph_get_uint(get_ph_handle(self), (phidget_get_uint_func)PhidgetEncoder_getMaxPositionChangeTrigger);
76
+ }
172
77
 
173
- #ifdef PH_CALLBACK
174
- VALUE ph_encoder_set_on_input_change_handler(VALUE self, VALUE handler) {
175
- ph_data_t *ph = get_ph_data(self);
176
- ph_callback_data_t *callback_data = &ph->dev_callback_1;
177
- if( TYPE(handler) == T_NIL ) {
178
- callback_data->exit = true;
179
- ph_raise(CPhidgetEncoder_set_OnInputChange_Handler((CPhidgetEncoderHandle)ph->handle, NULL, (void *)NULL));
180
- } else {
181
- callback_data->called = false;
182
- callback_data->exit = false;
183
- callback_data->phidget = self;
184
- callback_data->callback = handler;
185
- ph_raise(CPhidgetEncoder_set_OnInputChange_Handler((CPhidgetEncoderHandle)ph->handle, ph_encoder_on_input_change, (void *)callback_data));
186
- ph_callback_thread(callback_data);
187
- }
188
- return Qnil;
78
+
79
+ void CCONV ph_encoder_on_position_change(PhidgetEncoderHandle phid, void *userPtr, int position_change, double time_change, int index_triggered) {
80
+ ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
81
+ while(sem_wait(&callback_data->handler_ready)!=0) {};
82
+ callback_data->arg1 = INT2NUM(position_change);
83
+ callback_data->arg2 = DBL2NUM(time_change);
84
+ callback_data->arg3 = INT2NUM(index_triggered);
85
+ callback_data->arg4 = Qnil;
86
+ sem_post(&callback_data->callback_called);
189
87
  }
190
88
 
191
89
 
192
90
  VALUE ph_encoder_set_on_position_change_handler(VALUE self, VALUE handler) {
193
91
  ph_data_t *ph = get_ph_data(self);
194
- ph_callback_data_t *callback_data = &ph->dev_callback_2;
92
+ ph_callback_data_t *callback_data = &ph->dev_callbacks[ENCODER_POSITION_CHANGE_CALLBACK];
195
93
  if( TYPE(handler) == T_NIL ) {
94
+ callback_data->callback = T_NIL;
196
95
  callback_data->exit = true;
197
- ph_raise(CPhidgetEncoder_set_OnPositionChange_Handler((CPhidgetEncoderHandle)ph->handle, NULL, (void *)NULL));
96
+ ph_raise(PhidgetEncoder_setOnPositionChangeHandler((PhidgetEncoderHandle)ph->handle, NULL, (void *)NULL));
97
+ sem_post(&callback_data->callback_called);
198
98
  } else {
199
- callback_data->called = false;
200
99
  callback_data->exit = false;
201
100
  callback_data->phidget = self;
202
101
  callback_data->callback = handler;
203
- ph_raise(CPhidgetEncoder_set_OnPositionChange_Handler((CPhidgetEncoderHandle)ph->handle, ph_encoder_on_position_change, (void *)callback_data));
102
+ ph_raise(PhidgetEncoder_setOnPositionChangeHandler((PhidgetEncoderHandle)ph->handle, ph_encoder_on_position_change, (void *)callback_data));
204
103
  ph_callback_thread(callback_data);
205
104
  }
206
105
  return Qnil;
207
106
  }
208
107
 
209
108
 
210
- VALUE ph_encoder_set_on_index_handler(VALUE self, VALUE handler) {
211
- ph_data_t *ph = get_ph_data(self);
212
- ph_callback_data_t *callback_data = &ph->dev_callback_3;
213
- if( TYPE(handler) == T_NIL ) {
214
- callback_data->exit = true;
215
- ph_raise(CPhidgetEncoder_set_OnIndex_Handler((CPhidgetEncoderHandle)ph->handle, NULL, (void *)NULL));
216
- } else {
217
- callback_data->called = false;
218
- callback_data->exit = false;
219
- callback_data->phidget = self;
220
- callback_data->callback = handler;
221
- ph_raise(CPhidgetEncoder_set_OnIndex_Handler((CPhidgetEncoderHandle)ph->handle, ph_encoder_on_index, (void *)callback_data));
222
- ph_callback_thread(callback_data);
223
- }
224
- return Qnil;
225
- }
109
+ void Init_encoder() {
110
+ VALUE ph_module = rb_const_get(rb_cObject, rb_intern("Phidgets"));
111
+ VALUE ph_common = rb_const_get(ph_module, rb_intern("Common"));
112
+ VALUE ph_encoder = rb_define_class_under(ph_module, "Encoder", ph_common);
226
113
 
114
+ rb_define_const(ph_encoder, "IO_MODE_PUSH_PULL", INT2NUM(ENCODER_IO_MODE_PUSH_PULL));
115
+ rb_define_const(ph_encoder, "IO_MODE_LINE_DRIVER_2K2", INT2NUM(ENCODER_IO_MODE_LINE_DRIVER_2K2));
116
+ rb_define_const(ph_encoder, "IO_MODE_LINE_DRIVER_10K", INT2NUM(ENCODER_IO_MODE_LINE_DRIVER_10K));
117
+ rb_define_const(ph_encoder, "IO_MODE_OPEN_COLLECTOR_2K2", INT2NUM(ENCODER_IO_MODE_OPEN_COLLECTOR_2K2));
118
+ rb_define_const(ph_encoder, "IO_MODE_OPEN_COLLECTOR_10K", INT2NUM(ENCODER_IO_MODE_OPEN_COLLECTOR_10K));
227
119
 
228
- int ph_encoder_on_input_change(CPhidgetEncoderHandle phid, void *userPtr, int index, int state) {
229
- ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
230
- callback_data->called = true;
231
- return EPHIDGET_OK;
232
- }
233
120
 
121
+ /* Document-method: new
122
+ * call-seq: new
123
+ *
124
+ * Creates a Phidget Encoder object.
125
+ */
126
+ rb_define_method(ph_encoder, "initialize", ph_encoder_init, 0);
127
+
128
+ /* Document-method: getEnabled
129
+ * call-seq: getEnabled -> true or false
130
+ *
131
+ * The enabled state of the encoder.
132
+ */
133
+ rb_define_method(ph_encoder, "getEnabled", ph_encoder_get_enabled, 0);
134
+ rb_define_alias(ph_encoder, "enabled?", "getEnabled");
234
135
 
235
- int ph_encoder_on_position_change(CPhidgetEncoderHandle phid, void *userPtr, int index, int time, int change) {
236
- ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
237
- callback_data->called = true;
238
- return EPHIDGET_OK;
239
- }
136
+ /* Document-method: setEnabled
137
+ * call-seq: setEnabled(state)
138
+ *
139
+ * The enabled state of the encoder.
140
+ */
141
+ rb_define_method(ph_encoder, "setEnabled", ph_encoder_set_enabled, 1);
142
+ rb_define_alias(ph_encoder, "enabled=", "setEnabled");
240
143
 
144
+ /* Document-method: getDataInterval
145
+ * call-seq: getDataInterval -> interval
146
+ *
147
+ * The DataInterval is the time that must elapse before the channel will fire another PositionChange event.
148
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
149
+ * The timing between PositionChange events can also affected by the PositionChangeTrigger.
150
+ */
151
+ rb_define_method(ph_encoder, "getDataInterval", ph_encoder_get_data_interval, 0);
152
+ rb_define_alias(ph_encoder, "data_interval", "getDataInterval");
241
153
 
242
- int ph_encoder_on_index(CPhidgetEncoderHandle phid, void *userPtr, int index, int index_pos) {
243
- ph_callback_data_t *callback_data = ((ph_callback_data_t *)userPtr);
244
- callback_data->called = true;
245
- return EPHIDGET_OK;
246
- }
154
+ /* Document-method: setDataInterval
155
+ * call-seq: setDataInterval(interval)
156
+ *
157
+ * The DataInterval is the time that must elapse before the channel will fire another PositionChange event.
158
+ * The data interval is bounded by MinDataInterval and MaxDataInterval.
159
+ * The timing between PositionChange events can also affected by the PositionChangeTrigger.
160
+ */
161
+ rb_define_method(ph_encoder, "setDataInterval", ph_encoder_set_data_interval, 1);
162
+ rb_define_alias(ph_encoder, "data_interval=", "setDataInterval");
163
+
164
+ /* Document-method: getMinDataInterval
165
+ * call-seq: getMinDataInterval -> interval
166
+ *
167
+ * The minimum value that DataInterval can be set to.
168
+ */
169
+ rb_define_method(ph_encoder, "getMinDataInterval", ph_encoder_get_min_data_interval, 0);
170
+ rb_define_alias(ph_encoder, "min_data_interval", "getMinDataInterval");
171
+
172
+ /* Document-method: getMaxDataInterval
173
+ * call-seq: getMaxDataInterval -> interval
174
+ *
175
+ * The maximum value that DataInterval can be set to.
176
+ */
177
+ rb_define_method(ph_encoder, "getMaxDataInterval", ph_encoder_get_max_data_interval, 0);
178
+ rb_define_alias(ph_encoder, "max_data_interval", "getMaxDataInterval");
179
+
180
+ /* Document-method: getIndexPosition
181
+ * call-seq: getIndexPosition -> position
182
+ *
183
+ * The most recent position of the index channel calculated by the Phidgets library.
184
+ * The index channel will usually pulse once per rotation.
185
+ * Setting the encoder position will move the index position the same amount so their relative position stays the same.
186
+ * Index position is tracked locally as the last position at which the index was triggered.
187
+ * Setting position will only affect the local copy of the index position value. This means that index positions seen by multiple network applications may not agree.
188
+ */
189
+ rb_define_method(ph_encoder, "getIndexPosition", ph_encoder_get_index_position, 0);
190
+ rb_define_alias(ph_encoder, "index_position", "getIndexPosition");
191
+
192
+ /* Document-method: getIOMode
193
+ * call-seq: getIOMode -> mode
194
+ *
195
+ * The encoder interface mode. Match the mode to the type of encoder you have attached.
196
+ * It is recommended to only change this when the encoder disabled in order to avoid unexpected results.
197
+ */
198
+ rb_define_method(ph_encoder, "getIOMode", ph_encoder_get_io_mode, 0);
199
+ rb_define_alias(ph_encoder, "io_mode", "getIOMode");
200
+
201
+ /* Document-method: setIOMode
202
+ * call-seq: setIOMode(mode)
203
+ *
204
+ * The encoder interface mode. Match the mode to the type of encoder you have attached.
205
+ * It is recommended to only change this when the encoder disabled in order to avoid unexpected results.
206
+ */
207
+ rb_define_method(ph_encoder, "setIOMode", ph_encoder_set_io_mode, 1);
208
+ rb_define_alias(ph_encoder, "io_mode=", "setIOMode");
209
+
210
+ /* Document-method: getPosition
211
+ * call-seq: getPosition -> position
212
+ *
213
+ * The most recent position value calculated by the Phidgets library.
214
+ * Position counts quadrature edges within a quadrature cycle. This means there are four counts per full quadrature cycle.
215
+ * Position is tracked locally as the total position change from the time the channel is opened.
216
+ * Setting position will only affect the local copy of the position value. This means that positions seen by multiple network applications may not agree.
217
+ */
218
+ rb_define_method(ph_encoder, "getPosition", ph_encoder_get_position, 0);
219
+ rb_define_alias(ph_encoder, "position", "getPosition");
220
+
221
+ /* Document-method: setPosition
222
+ * call-seq: setPosition(position)
223
+ *
224
+ * The most recent position value calculated by the Phidgets library.
225
+ * Position counts quadrature edges within a quadrature cycle. This means there are four counts per full quadrature cycle.
226
+ * Position is tracked locally as the total position change from the time the channel is opened.
227
+ * Setting position will only affect the local copy of the position value. This means that positions seen by multiple network applications may not agree.
228
+ */
229
+ rb_define_method(ph_encoder, "setPosition", ph_encoder_set_position, 1);
230
+ rb_define_alias(ph_encoder, "position=", "setPosition");
247
231
 
248
- #endif
232
+ /* Document-method: getPositionChangeTrigger
233
+ * call-seq: getPositionChangeTrigger -> trigger
234
+ *
235
+ * The channel will not issue a PositionChange event until the position value has changed by the amount specified by the PositionChangeTrigger.
236
+ * Setting the PositionChangeTrigger to 0 will result in the channel firing events every DataInterval. This is useful for applications that implement their own data filtering.
237
+ */
238
+ rb_define_method(ph_encoder, "getPositionChangeTrigger", ph_encoder_get_position_change_trigger, 0);
239
+ rb_define_alias(ph_encoder, "position_change_trigger", "getPositionChangeTrigger");
240
+
241
+ /* Document-method: setPositionChangeTrigger
242
+ * call-seq: setPositionChangeTrigger(trigger)
243
+ *
244
+ * The channel will not issue a PositionChange event until the position value has changed by the amount specified by the PositionChangeTrigger.
245
+ * Setting the PositionChangeTrigger to 0 will result in the channel firing events every DataInterval. This is useful for applications that implement their own data filtering.
246
+ */
247
+ rb_define_method(ph_encoder, "setPositionChangeTrigger", ph_encoder_set_position_change_trigger, 1);
248
+ rb_define_alias(ph_encoder, "position_change_trigger=", "setPositionChangeTrigger");
249
+
250
+ /* Document-method: getMinPositionChangeTrigger
251
+ * call-seq: getMinPositionChangeTrigger -> trigger
252
+ *
253
+ * The minimum value that PositionChangeTrigger can be set to.
254
+ */
255
+ rb_define_method(ph_encoder, "getMinPositionChangeTrigger", ph_encoder_get_min_position_change_trigger, 0);
256
+ rb_define_alias(ph_encoder, "min_position_change_trigger", "getMinPositionChangeTrigger");
257
+
258
+ /* Document-method: getMaxPositionChangeTrigger
259
+ * call-seq: getMaxPositionChangeTrigger -> trigger
260
+ *
261
+ * The maximum value that PositionChangeTrigger can be set to.
262
+ */
263
+ rb_define_method(ph_encoder, "getMaxPositionChangeTrigger", ph_encoder_get_max_position_change_trigger, 0);
264
+ rb_define_alias(ph_encoder, "max_position_change_trigger", "getMaxPositionChangeTrigger");
265
+
266
+ rb_define_private_method(ph_encoder, "ext_setOnPositionChangeHandler", ph_encoder_set_on_position_change_handler, 1);
267
+ }
249
268