phidgets_native 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/Gemfile +3 -0
  2. data/Gemfile.lock +12 -0
  3. data/README.rdoc +176 -0
  4. data/Rakefile +31 -0
  5. data/examples/gps.rb +25 -0
  6. data/examples/interface_kit.rb +61 -0
  7. data/examples/lib/common.rb +51 -0
  8. data/examples/lib/console_table.rb +38 -0
  9. data/examples/list_all.rb +16 -0
  10. data/examples/spatial.rb +53 -0
  11. data/ext/phidgets_native/accelerometer_ruby.c +36 -0
  12. data/ext/phidgets_native/advancedservo_ruby.c +33 -0
  13. data/ext/phidgets_native/analog_ruby.c +34 -0
  14. data/ext/phidgets_native/bridge_ruby.c +32 -0
  15. data/ext/phidgets_native/device.c +68 -0
  16. data/ext/phidgets_native/device_ruby.c +404 -0
  17. data/ext/phidgets_native/encoder_ruby.c +34 -0
  18. data/ext/phidgets_native/extconf.rb +18 -0
  19. data/ext/phidgets_native/frequencycounter_ruby.c +32 -0
  20. data/ext/phidgets_native/gps.c +102 -0
  21. data/ext/phidgets_native/gps_ruby.c +212 -0
  22. data/ext/phidgets_native/interfacekit.c +203 -0
  23. data/ext/phidgets_native/interfacekit_ruby.c +507 -0
  24. data/ext/phidgets_native/ir_ruby.c +33 -0
  25. data/ext/phidgets_native/led_ruby.c +33 -0
  26. data/ext/phidgets_native/motorcontrol_ruby.c +32 -0
  27. data/ext/phidgets_native/phidgets_native.c +197 -0
  28. data/ext/phidgets_native/phidgets_native.h +320 -0
  29. data/ext/phidgets_native/phidgets_native_ruby.c +468 -0
  30. data/ext/phidgets_native/phsensor_ruby.c +32 -0
  31. data/ext/phidgets_native/rfid_ruby.c +31 -0
  32. data/ext/phidgets_native/servo_ruby.c +31 -0
  33. data/ext/phidgets_native/spatial.c +168 -0
  34. data/ext/phidgets_native/spatial_ruby.c +533 -0
  35. data/ext/phidgets_native/stepper_ruby.c +32 -0
  36. data/ext/phidgets_native/temperaturesensor_ruby.c +31 -0
  37. data/ext/phidgets_native/textlcd_ruby.c +31 -0
  38. data/ext/phidgets_native/textled_ruby.c +31 -0
  39. data/ext/phidgets_native/weightsensor_ruby.c +32 -0
  40. data/phidgets_native.gemspec +21 -0
  41. metadata +96 -0
@@ -0,0 +1,404 @@
1
+ #include "phidgets_native.h"
2
+
3
+ const char MSG_SERIAL_MUST_BE_FIX[] = "serial number must be a fixnum";
4
+ const char MSG_TIMEOUT_MUST_BE_FIX[] = "timeout must be a fixnum";
5
+ const char MSG_UNSUPPORTED_INITIALIZATION[] = "device cannot be directly instantiated, please instantiate an inherited class";
6
+
7
+ /*
8
+ * Document-class: PhidgetsNative::Device
9
+ *
10
+ * This class is the base class from which all phidget devices will inherit.
11
+ * Basic functionality related to the management of any device can be found
12
+ * in here. These methods are all available to all phidgets.
13
+ */
14
+
15
+ void Init_phidgets_native_device(VALUE m_Phidget) {
16
+ VALUE c_Device = rb_define_class_under(m_Phidget,"Device",rb_cObject);
17
+
18
+ rb_define_alloc_func(c_Device, device_allocate);
19
+
20
+ /*
21
+ * Document-method: new
22
+ * call-seq:
23
+ * new(serial_number)
24
+ *
25
+ * All phidget objects are created from the device serial number. Serial numbers
26
+ * are required to be Fixnums (aka "unsigned integers").
27
+ */
28
+ rb_define_method(c_Device, "initialize", device_initialize, 1);
29
+
30
+ /*
31
+ * Document-method: close
32
+ * call-seq:
33
+ * close -> nil
34
+ *
35
+ * This method will unregister the phidget event handlers, and free up all
36
+ * API resources associated with the phidget. This is an optional, but useful
37
+ * way to remove the object's overhead before the GC kicks in and actually
38
+ * frees the resource.
39
+ */
40
+ rb_define_method(c_Device, "close", device_close, 0);
41
+
42
+ /*
43
+ * Document-method: wait_for_attachment
44
+ * call-seq:
45
+ * wait_for_attachment(timeout) -> FixNum
46
+ *
47
+ * Call CPhidget_waitForAttachment[http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html]
48
+ * to execute wait_for_attachment. Given a timeout, this method will pause
49
+ * execution until this device is attached, or timeout milliseconds expires.
50
+ * The method returns the provided timeout parameter.
51
+ */
52
+ rb_define_method(c_Device, "wait_for_attachment", device_wait_for_attachment, 1);
53
+
54
+ /*
55
+ * Document-method: is_attached?
56
+ * call-seq:
57
+ * is_attached? -> boolean
58
+ *
59
+ * Returns true if the device is connected, false if otherwise.
60
+ */
61
+ rb_define_method(c_Device, "is_attached?", device_is_attached, 0);
62
+
63
+ /*
64
+ * Document-method: device_class
65
+ * call-seq:
66
+ * device_class -> String
67
+ *
68
+ * Returns a string indicating the "class" of the device. This string comes
69
+ * directly from the CPhidget_getDeviceClass[http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html]
70
+ * function.
71
+ */
72
+ rb_define_method(c_Device, "device_class", device_device_class, 0);
73
+
74
+ /*
75
+ * Document-method: device_id
76
+ * call-seq:
77
+ * device_id -> String
78
+ *
79
+ * Returns a string indicating the "identifier" of the device. Seemingly, this
80
+ * is the product name and part number. This string comes directly from the
81
+ * CPhidget_getDeviceID[http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html]
82
+ * function.
83
+ */
84
+ rb_define_method(c_Device, "device_id", device_device_id, 0);
85
+
86
+ /*
87
+ * Document-method: type
88
+ * call-seq:
89
+ * type -> String
90
+ *
91
+ * Returns a string indicating the "type" of the device. This doesn't appear
92
+ * to differ much from the device_closs. This string comes directly from the
93
+ * CPhidget_getDeviceType[http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html]
94
+ * function.
95
+ */
96
+ rb_define_method(c_Device, "type", device_type, 0);
97
+
98
+ /*
99
+ * Document-method: name
100
+ * call-seq:
101
+ * name -> String
102
+ *
103
+ * Returns a string indicating the "name" of the device. This seems to be just
104
+ * the product name. This string comes directly from the
105
+ * CPhidget_getDeviceName[http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html]
106
+ * function.
107
+ */
108
+ rb_define_method(c_Device, "name", device_name, 0);
109
+
110
+ /*
111
+ * Document-method: label
112
+ * call-seq:
113
+ * label -> String
114
+ *
115
+ * Returns a string which contains the device label. This string comes directly
116
+ * from the CPhidget_getDeviceLabel[http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html]
117
+ * function.
118
+ */
119
+ rb_define_method(c_Device, "label", device_label, 0);
120
+
121
+ /*
122
+ * Document-method: serial_number
123
+ * call-seq:
124
+ * serial_number -> FixNum
125
+ *
126
+ * Returns a string which contains the device's serial number. This string comes
127
+ * from what was provided in the object initializer
128
+ */
129
+ rb_define_method(c_Device, "serial_number", device_serial_number, 0);
130
+
131
+ /*
132
+ * Document-method: version
133
+ * call-seq:
134
+ * version -> FixNum
135
+ *
136
+ * Returns an FixNum containing the device firmware version. This number comes
137
+ * from the CPhidget_getDeviceVersion[http://www.phidgets.com/documentation/web/cdoc/group__phidcommon.html]
138
+ * function.
139
+ */
140
+ rb_define_method(c_Device, "version", device_version, 0);
141
+ }
142
+
143
+
144
+ VALUE device_allocate(VALUE class) {
145
+
146
+ // We'll need this all over the place later:
147
+ PhidgetInfo *info;
148
+ VALUE self = Data_Make_Struct(class, PhidgetInfo, 0, device_free, info);
149
+ memset(info, 0, sizeof(PhidgetInfo));
150
+ info->is_attached = false;
151
+
152
+ return self;
153
+ }
154
+
155
+ VALUE device_initialize(VALUE self, VALUE serial) {
156
+ PhidgetInfo *info = device_info(self);
157
+
158
+ if (TYPE(serial) != T_FIXNUM) {
159
+ rb_raise(rb_eTypeError, MSG_SERIAL_MUST_BE_FIX);
160
+ return Qnil;
161
+ }
162
+
163
+ info->serial = FIX2INT(serial);
164
+
165
+ // If we didn't get called via an inherited class, then we setup a generic pointer
166
+ // approach to doing this...
167
+ if ( info->handle == NULL )
168
+ rb_raise(rb_eTypeError, MSG_UNSUPPORTED_INITIALIZATION);
169
+
170
+ // Register the event handlers:
171
+ ensure(CPhidget_set_OnAttach_Handler((CPhidgetHandle)info->handle, device_on_attach, info));
172
+ ensure(CPhidget_set_OnDetach_Handler((CPhidgetHandle)info->handle, device_on_detach, info));
173
+ ensure(CPhidget_set_OnError_Handler((CPhidgetHandle)info->handle, device_on_error, info));
174
+
175
+ ensure(CPhidget_open((CPhidgetHandle)info->handle, FIX2INT(serial)));
176
+
177
+ return self;
178
+ }
179
+
180
+ VALUE device_close(VALUE self) {
181
+ PhidgetInfo *info = device_info(self);
182
+
183
+ ensure(CPhidget_set_OnAttach_Handler((CPhidgetHandle)info->handle, NULL, NULL));
184
+ ensure(CPhidget_set_OnDetach_Handler((CPhidgetHandle)info->handle, NULL, NULL));
185
+ ensure(CPhidget_set_OnError_Handler((CPhidgetHandle)info->handle, NULL, NULL));
186
+
187
+ return Qnil;
188
+ }
189
+
190
+ VALUE device_wait_for_attachment(VALUE self, VALUE timeout) {
191
+ PhidgetInfo *info = device_info(self);
192
+
193
+ if (TYPE(timeout) != T_FIXNUM) {
194
+ rb_raise(rb_eTypeError, MSG_TIMEOUT_MUST_BE_FIX);
195
+ return Qnil;
196
+ }
197
+
198
+ ensure(CPhidget_waitForAttachment((CPhidgetHandle)info->handle, FIX2UINT(timeout)));
199
+
200
+ // We need to give the attach handler time to fire. Maybe there's a better way
201
+ // to do this. Also, there's no need to wait any further if the device wasn't
202
+ // found...
203
+ usleep(10000);
204
+
205
+ return timeout;
206
+ }
207
+
208
+ VALUE device_is_attached(VALUE self) {
209
+ PhidgetInfo *info = device_info(self);
210
+
211
+ return (info->is_attached) ? Qtrue : Qfalse;
212
+ }
213
+
214
+ VALUE device_device_class(VALUE self) {
215
+ PhidgetInfo *info = device_info(self);
216
+
217
+ if (info->device_class == 0) return Qnil;
218
+
219
+ switch (info->device_class) {
220
+ case PHIDCLASS_ACCELEROMETER:
221
+ return rb_str_new2("Phidget Accelerometer");
222
+ case PHIDCLASS_ADVANCEDSERVO:
223
+ return rb_str_new2("Phidget Advanced Servo");
224
+ case PHIDCLASS_ANALOG:
225
+ return rb_str_new2("Phidget Analog");
226
+ case PHIDCLASS_BRIDGE:
227
+ return rb_str_new2("Phidget Bridge");
228
+ case PHIDCLASS_ENCODER:
229
+ return rb_str_new2("Phidget Encoder");
230
+ case PHIDCLASS_FREQUENCYCOUNTER:
231
+ return rb_str_new2("Phidget Frequency Counter");
232
+ case PHIDCLASS_GPS:
233
+ return rb_str_new2("Phidget GPS");
234
+ case PHIDCLASS_INTERFACEKIT:
235
+ return rb_str_new2("Phidget Interface Kit");
236
+ case PHIDCLASS_IR:
237
+ return rb_str_new2("Phidget IR");
238
+ case PHIDCLASS_LED:
239
+ return rb_str_new2("Phidget LED");
240
+ case PHIDCLASS_MOTORCONTROL:
241
+ return rb_str_new2("Phidget Motor Control");
242
+ case PHIDCLASS_PHSENSOR:
243
+ return rb_str_new2("Phidget PH Sensor");
244
+ case PHIDCLASS_RFID:
245
+ return rb_str_new2("Phidget RFID");
246
+ case PHIDCLASS_SERVO:
247
+ return rb_str_new2("Phidget Servo");
248
+ case PHIDCLASS_SPATIAL:
249
+ return rb_str_new2("Phidget Spatial");
250
+ case PHIDCLASS_STEPPER:
251
+ return rb_str_new2("Phidget Stepper");
252
+ case PHIDCLASS_TEMPERATURESENSOR:
253
+ return rb_str_new2("Phidget Temperature Sensor");
254
+ case PHIDCLASS_TEXTLCD:
255
+ return rb_str_new2("Phidget TextLCD");
256
+ case PHIDCLASS_TEXTLED:
257
+ return rb_str_new2("Phidget TextLED");
258
+ case PHIDCLASS_WEIGHTSENSOR:
259
+ return rb_str_new2("Phidget Weight Sensor");
260
+ default:
261
+ return rb_str_new2("Unknown Phidget");
262
+ break;
263
+ }
264
+ }
265
+
266
+ VALUE device_device_id(VALUE self) {
267
+ PhidgetInfo *info = device_info(self);
268
+
269
+ if (info->device_class == 0) return Qnil;
270
+
271
+ switch (info->device_id) {
272
+ case PHIDID_ACCELEROMETER_3AXIS:
273
+ return rb_str_new2("Phidget 3-axis Accelerometer (1059)");
274
+ case PHIDID_ADVANCEDSERVO_1MOTOR:
275
+ return rb_str_new2("Phidget 1 Motor Advanced Servo (1066)");
276
+ case PHIDID_ADVANCEDSERVO_8MOTOR:
277
+ return rb_str_new2("Phidget 8 Motor Advanced Servo (1061)");
278
+ case PHIDID_ANALOG_4OUTPUT:
279
+ return rb_str_new2("Phidget Analog 4-output (1002)");
280
+ case PHIDID_BIPOLAR_STEPPER_1MOTOR:
281
+ return rb_str_new2("Phidget 1 Motor Bipolar Stepper Controller with 4 Digital Inputs (1063)");
282
+ case PHIDID_BRIDGE_4INPUT:
283
+ return rb_str_new2("Phidget Bridge 4-input (1046)");
284
+ case PHIDID_ENCODER_1ENCODER_1INPUT:
285
+ return rb_str_new2("Phidget Encoder - Mechanical (1052)");
286
+ case PHIDID_ENCODER_HS_1ENCODER:
287
+ return rb_str_new2("Phidget High Speed Encoder (1057)");
288
+ case PHIDID_ENCODER_HS_4ENCODER_4INPUT:
289
+ return rb_str_new2("Phidget High Speed Encoder - 4 Encoder (1047)");
290
+ case PHIDID_FREQUENCYCOUNTER_2INPUT:
291
+ return rb_str_new2("Phidget Frequency Counter 2-input (1054)");
292
+ case PHIDID_GPS:
293
+ return rb_str_new2("Phidget GPS (1040)");
294
+ case PHIDID_INTERFACEKIT_0_0_4:
295
+ return rb_str_new2("Phidget Interface Kit 0/0/4 (1014)");
296
+ case PHIDID_INTERFACEKIT_0_0_8:
297
+ return rb_str_new2("Phidget Interface Kit 0/0/8 (1017)");
298
+ case PHIDID_INTERFACEKIT_0_16_16:
299
+ return rb_str_new2("Phidget Interface Kit 0/16/16 (1012)");
300
+ case PHIDID_INTERFACEKIT_2_2_2:
301
+ return rb_str_new2("Phidget Interface Kit 2/2/2 (1011)");
302
+ case PHIDID_INTERFACEKIT_8_8_8:
303
+ return rb_str_new2("Phidget Interface Kit 8/8/8 (1013, 1018, 1019)");
304
+ case PHIDID_INTERFACEKIT_8_8_8_w_LCD:
305
+ return rb_str_new2("Phidget Interface Kit 8/8/8 with TextLCD (1201, 1202, 1203)");
306
+ case PHIDID_IR:
307
+ return rb_str_new2("Phidget IR Receiver Transmitter (1055)");
308
+ case PHIDID_LED_64_ADV:
309
+ return rb_str_new2("Phidget LED 64 Advanced (1031)");
310
+ case PHIDID_LINEAR_TOUCH:
311
+ return rb_str_new2("Phidget Linear Touch (1015)");
312
+ case PHIDID_MOTORCONTROL_1MOTOR:
313
+ return rb_str_new2("Phidget 1 Motor Motor Controller (1065)");
314
+ case PHIDID_MOTORCONTROL_HC_2MOTOR:
315
+ return rb_str_new2("Phidget 2 Motor High Current Motor Controller (1064)");
316
+ case PHIDID_RFID_2OUTPUT:
317
+ return rb_str_new2("Phidget RFID with Digital Outputs and Onboard LED (1023)");
318
+ case PHIDID_RFID_2OUTPUT_READ_WRITE:
319
+ return rb_str_new2("Phidget RFID with R/W support (1024)");
320
+ case PHIDID_ROTARY_TOUCH:
321
+ return rb_str_new2("Phidget Rotary Touch (1016)");
322
+ case PHIDID_SPATIAL_ACCEL_3AXIS:
323
+ return rb_str_new2("Phidget Spatial 3-axis accel (1049, 1041, 1043)");
324
+ case PHIDID_SPATIAL_ACCEL_GYRO_COMPASS:
325
+ return rb_str_new2("Phidget Spatial 3/3/3 (1056, 1042, 1044)");
326
+ case PHIDID_TEMPERATURESENSOR:
327
+ return rb_str_new2("Phidget Temperature Sensor (1051)");
328
+ case PHIDID_TEMPERATURESENSOR_4:
329
+ return rb_str_new2("Phidget Temperature Sensor 4-input (1048)");
330
+ case PHIDID_TEMPERATURESENSOR_IR:
331
+ return rb_str_new2("Phidget Temperature Sensor IR (1045)");
332
+ case PHIDID_TEXTLCD_2x20_w_8_8_8:
333
+ return rb_str_new2("Phidget TextLCD with Interface Kit 8/8/8 (1201, 1202, 1203)");
334
+ case PHIDID_TEXTLCD_ADAPTER:
335
+ return rb_str_new2("Phidget TextLCD Adapter (1204)");
336
+ case PHIDID_UNIPOLAR_STEPPER_4MOTOR:
337
+ return rb_str_new2("Phidget 4 Motor Unipolar Stepper Controller (1062)");
338
+ case PHIDID_ACCELEROMETER_2AXIS:
339
+ return rb_str_new2("Phidget 2-axis Accelerometer (1053, 1054)");
340
+ case PHIDID_INTERFACEKIT_0_8_8_w_LCD:
341
+ return rb_str_new2("Phidget Interface Kit 0/8/8 with TextLCD (1219, 1220, 1221)");
342
+ case PHIDID_INTERFACEKIT_4_8_8:
343
+ return rb_str_new2("Phidget Interface Kit 4/8/8");
344
+ case PHIDID_LED_64:
345
+ return rb_str_new2("Phidget LED 64 (1030)");
346
+ case PHIDID_MOTORCONTROL_LV_2MOTOR_4INPUT:
347
+ return rb_str_new2("Phidget 2 Motor Low Voltage Motor Controller with 4 Digital Inputs (1060)");
348
+ case PHIDID_PHSENSOR:
349
+ return rb_str_new2("Phidget PH Sensor (1058)");
350
+ case PHIDID_RFID:
351
+ return rb_str_new2("Phidget RFID without Digital Outputs");
352
+ case PHIDID_SERVO_1MOTOR:
353
+ return rb_str_new2("Phidget 1 Motor Servo Controller (1000)");
354
+ case PHIDID_SERVO_1MOTOR_OLD:
355
+ return rb_str_new2("Phidget 1 Motor Servo Controller - Old Version");
356
+ case PHIDID_SERVO_4MOTOR:
357
+ return rb_str_new2("Phidget 4 Motor Servo Controller (1001)");
358
+ case PHIDID_SERVO_4MOTOR_OLD:
359
+ return rb_str_new2("Phidget 4 Motor Servo Controller - Old Version");
360
+ case PHIDID_TEXTLCD_2x20:
361
+ return rb_str_new2("Phidget TextLCD without Interface Kit (1210)");
362
+ case PHIDID_TEXTLCD_2x20_w_0_8_8:
363
+ return rb_str_new2("Phidget TextLCD with Interface Kit 0/8/8 (1219, 1220, 1221)");
364
+ case PHIDID_TEXTLED_1x8:
365
+ return rb_str_new2("Phidget TextLED 1x8");
366
+ case PHIDID_TEXTLED_4x8:
367
+ return rb_str_new2("Phidget TextLED 4x8 (1040)");
368
+ case PHIDID_WEIGHTSENSOR:
369
+ return rb_str_new2("Phidget Weight Sensor (1050)");
370
+ default:
371
+ return rb_str_new2("Unknown Phidget");
372
+ }
373
+ }
374
+
375
+ VALUE device_type(VALUE self) {
376
+ PhidgetInfo *info = device_info(self);
377
+
378
+ return (info->type == NULL) ? Qnil : rb_str_new2(info->type);
379
+ }
380
+
381
+ VALUE device_name(VALUE self) {
382
+ PhidgetInfo *info = device_info(self);
383
+
384
+ return (info->name == NULL) ? Qnil : rb_str_new2(info->name);
385
+ }
386
+
387
+ VALUE device_label(VALUE self) {
388
+ PhidgetInfo *info = device_info(self);
389
+
390
+ return (info->label == NULL) ? Qnil : rb_str_new2(info->label);
391
+ }
392
+
393
+ VALUE device_serial_number(VALUE self) {
394
+ PhidgetInfo *info = device_info(self);
395
+
396
+ return (info->serial == 0) ? Qnil : INT2FIX(info->serial);
397
+ }
398
+
399
+ VALUE device_version(VALUE self) {
400
+ PhidgetInfo *info = device_info(self);
401
+
402
+ return (info->version == 0) ? Qnil : INT2FIX(info->version);
403
+ }
404
+
@@ -0,0 +1,34 @@
1
+ #include "phidgets_native.h"
2
+
3
+ /*
4
+ * Document-class: PhidgetsNative::Encoder < PhidgetsNative::Device
5
+ *
6
+ * This class is a stub, and is currently in need of an actual implementation.
7
+ * Nonetheless, all of the methods from its parent class PhidgetsNative::Device are
8
+ * available.
9
+ */
10
+
11
+ void Init_phidgets_native_encoder(VALUE m_Phidget) {
12
+ VALUE c_Device = rb_const_get(m_Phidget, rb_intern("Device"));
13
+
14
+ VALUE c_Encoder = rb_define_class_under(m_Phidget, "Encoder", c_Device);
15
+
16
+ /*
17
+ * Document-method: new
18
+ * call-seq:
19
+ * new(serial_number)
20
+ *
21
+ * All phidget objects are created from the device serial number. Serial numbers
22
+ * are required to be Fixnums (aka "unsigned integers").
23
+ */
24
+ rb_define_method(c_Encoder, "initialize", encoder_initialize, 1);
25
+
26
+ }
27
+
28
+ VALUE encoder_initialize(VALUE self, VALUE serial) {
29
+ PhidgetInfo *info = device_info(self);
30
+ CPhidgetEncoderHandle encoder = 0;
31
+ ensure(CPhidgetEncoder_create(&encoder));
32
+ info->handle = (CPhidgetHandle)encoder;
33
+ return rb_call_super(1, &serial);
34
+ }
@@ -0,0 +1,18 @@
1
+ # File: extconf.rb
2
+ require 'mkmf'
3
+
4
+ extension_name = 'phidgets_native'
5
+
6
+ HEADER_DIRS = [ '/Library/Frameworks/Phidget21.framework/Headers' ]
7
+
8
+ find_header 'phidget21.h', *HEADER_DIRS
9
+ find_library 'libphidget21', 'CPhidget_getLibraryVersion', '/usr/lib/'
10
+
11
+ have_framework 'Phidget21'
12
+ have_library('phidget21')
13
+
14
+ dir_config extension_name
15
+
16
+ $CFLAGS << ' -Wno-unused-variable -std=gnu99'
17
+
18
+ create_makefile extension_name
@@ -0,0 +1,32 @@
1
+ #include "phidgets_native.h"
2
+
3
+ /*
4
+ * Document-class: PhidgetsNative::FrequencyCounter < PhidgetsNative::Device
5
+ *
6
+ * This class is a stub, and is currently in need of an actual implementation.
7
+ * Nonetheless, all of the methods from its parent class PhidgetsNative::Device are
8
+ * available.
9
+ */
10
+ void Init_phidgets_native_frequencycounter(VALUE m_Phidget) {
11
+ VALUE c_Device = rb_const_get(m_Phidget, rb_intern("Device"));
12
+
13
+ VALUE c_FrequencyCounter = rb_define_class_under(m_Phidget, "FrequencyCounter", c_Device);
14
+
15
+ /*
16
+ * Document-method: new
17
+ * call-seq:
18
+ * new(serial_number)
19
+ *
20
+ * All phidget objects are created from the device serial number. Serial numbers
21
+ * are required to be Fixnums (aka "unsigned integers").
22
+ */
23
+ rb_define_method(c_FrequencyCounter, "initialize", frequencycounter_initialize, 1);
24
+ }
25
+
26
+ VALUE frequencycounter_initialize(VALUE self, VALUE serial) {
27
+ PhidgetInfo *info = device_info(self);
28
+ CPhidgetFrequencyCounterHandle frequencycounter = 0;
29
+ ensure(CPhidgetFrequencyCounter_create(&frequencycounter));
30
+ info->handle = (CPhidgetHandle)frequencycounter;
31
+ return rb_call_super(1, &serial);
32
+ }
@@ -0,0 +1,102 @@
1
+ #include "phidgets_native.h"
2
+
3
+ int CCONV gps_on_position_change(CPhidgetGPSHandle gps, void *userptr, double latitude, double longitude, double altitude) {
4
+ PhidgetInfo *info = userptr;
5
+ GpsInfo *gps_info = info->type_info;
6
+
7
+ // Calculate the sample time:
8
+ sample_tick(gps_info->sample_rate, NULL);
9
+
10
+ // Get the GPS time:
11
+ GPSDate date;
12
+ GPSTime time;
13
+
14
+ if ( (CPhidgetGPS_getDate(gps, &date) == EPHIDGET_OK) &&
15
+ (CPhidgetGPS_getTime(gps, &time) == EPHIDGET_OK) ) {
16
+ gps_info->now_at_utc.tm_sec = time.tm_sec;
17
+ gps_info->now_at_utc.tm_min = time.tm_min;
18
+ gps_info->now_at_utc.tm_hour = time.tm_hour;
19
+ gps_info->now_at_utc.tm_mday = date.tm_mday;
20
+ gps_info->now_at_utc.tm_mon = date.tm_mon;
21
+ gps_info->now_at_utc.tm_year = date.tm_year;
22
+ gps_info->now_at_utc_ms = (time.tm_ms >= 1000) ? 0 : time.tm_ms;
23
+ gps_info->is_now_at_utc_known = true;
24
+ } else
25
+ gps_info->is_now_at_utc_known = false;
26
+
27
+ // Get Position values:
28
+ if (gps_info->latitude != PUNK_DBL) {
29
+ gps_info->is_latitude_known = true;
30
+ gps_info->latitude = latitude;
31
+ } else
32
+ gps_info->is_latitude_known = false;
33
+
34
+ if (gps_info->longitude != PUNK_DBL) {
35
+ gps_info->is_longitude_known = true;
36
+ gps_info->longitude = longitude;
37
+ } else
38
+ gps_info->is_longitude_known = false;
39
+
40
+ if (gps_info->altitude != PUNK_DBL) {
41
+ gps_info->is_altitude_known = true;
42
+ gps_info->altitude = altitude;
43
+ } else
44
+ gps_info->is_altitude_known = false;
45
+
46
+ double heading, velocity;
47
+ if (CPhidgetGPS_getHeading(gps, &heading) == EPHIDGET_OK) {
48
+ gps_info->heading = heading;
49
+ gps_info->is_heading_known = true;
50
+ } else
51
+ gps_info->is_heading_known = false;
52
+
53
+ if (CPhidgetGPS_getVelocity(gps, &velocity) == EPHIDGET_OK) {
54
+ gps_info->velocity = velocity;
55
+ gps_info->is_velocity_known = true;
56
+ } else
57
+ gps_info->is_velocity_known = false;
58
+
59
+ return 0;
60
+ }
61
+
62
+ int CCONV gps_on_fix_change(CPhidgetGPSHandle gps, void *userptr, int status) {
63
+ PhidgetInfo *info = userptr;
64
+ GpsInfo *gps_info = info->type_info;
65
+
66
+ // Calculate the sample time:
67
+ sample_tick(gps_info->sample_rate, NULL);
68
+
69
+ // I'm fairly certain that status is always either 1 or 0
70
+ gps_info->is_fixed = (status) ? true : false;
71
+
72
+ return 0;
73
+ }
74
+
75
+ int CCONV gps_on_detach(CPhidgetHandle phidget, void *userptr) {
76
+ PhidgetInfo *info = userptr;
77
+ GpsInfo *gps_info = info->type_info;
78
+
79
+ gps_info->is_fixed = false;
80
+ gps_info->is_latitude_known = false;
81
+ gps_info->is_longitude_known = false;
82
+ gps_info->is_altitude_known = false;
83
+ gps_info->is_heading_known = false;
84
+ gps_info->is_velocity_known = false;
85
+ gps_info->is_now_at_utc_known = false;
86
+
87
+ sample_zero(gps_info->sample_rate);
88
+
89
+ return 0;
90
+ }
91
+
92
+ void gps_on_free(void *type_info) {
93
+ GpsInfo *gps_info = type_info;
94
+
95
+ if (gps_info->sample_rate)
96
+ sample_free(gps_info->sample_rate);
97
+ if (gps_info)
98
+ xfree(gps_info);
99
+
100
+ return;
101
+ }
102
+