phidgets 0.0.1

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.
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require File.dirname(__FILE__) + '/phidgets/common.rb'
5
+ require File.dirname(__FILE__) + '/phidgets/interfacekit.rb'
6
+ require File.dirname(__FILE__) + '/phidgets/rfid.rb'
7
+
8
+ module Phidgets
9
+ VERSION = '0.0.1'
10
+ end
@@ -0,0 +1,395 @@
1
+
2
+ require 'dl'
3
+ require 'rbconfig'
4
+
5
+ module Phidgets
6
+
7
+ NOTATTACHED = 0
8
+ ATTACHED = 1
9
+
10
+ CLASS_NOTHING = 1
11
+ CLASS_ACCELEROMETER = 2
12
+ CLASS_ADVANCEDSERVO = 3
13
+ CLASS_ENCODER = 4
14
+ CLASS_GPS = 5
15
+ CLASS_GYROSCOPE = 6
16
+ CLASS_INTERFACEKIT = 7
17
+ CLASS_LED = 8
18
+ CLASS_MOTORCONTROL = 9
19
+ CLASS_PHSENSOR = 10
20
+ CLASS_RFID = 11
21
+ CLASS_SERVO = 12
22
+ CLASS_STEPPER = 13
23
+ CLASS_TEMPERATURESENSOR = 14
24
+ CLASS_TEXTLCD = 15
25
+ CLASS_TEXTLED = 16
26
+ CLASS_WEIGHTSENSOR = 17
27
+
28
+ ID_ACCELEROMETER_2AXIS = 0x071
29
+ ID_ACCELEROMETER_3AXIS = 0x07E
30
+ ID_ADVANCEDSERVO_8MOTOR = 0x03A
31
+ ID_BIPOLAR_STEPPER_1MOTOR = 0x07B
32
+ ID_ENCODER_1ENCODER_1INPUT = 0x04B
33
+ ID_ENCODER_HS_1ENCODER = 0x080
34
+ ID_INTERFACEKIT_0_0_4 = 0x040
35
+ ID_INTERFACEKIT_0_0_8 = 0x081
36
+ ID_INTERFACEKIT_0_16_16 = 0x044
37
+ ID_INTERFACEKIT_8_8_8 = 0x045
38
+ ID_INTERFACEKIT_8_8_8_w_LCD = 0x07D
39
+ ID_LED_64 = 0x04A
40
+ ID_LINEAR_TOUCH = 0x076
41
+ ID_MOTORCONTROL_HC_2MOTOR = 0x059
42
+ ID_MOTORCONTROL_LV_2MOTOR_4INPUT = 0x058
43
+ ID_PHSENSOR = 0x074
44
+ ID_RFID_2OUTPUT = 0x031
45
+ ID_ROTARY_TOUCH = 0x077
46
+ ID_SERVO_1MOTOR = 0x039
47
+ ID_TEMPERATURESENSOR = 0x070
48
+ ID_TEXTLCD_2x20_w_8_8_8 = 0x17D
49
+ ID_UNIPOLAR_STEPPER_4MOTOR = 0x07A
50
+ ID_INTERFACEKIT_0_8_8_w_LCD = 0x053
51
+ ID_INTERFACEKIT_4_8_8 = 0x004
52
+ ID_RFID = 0x030
53
+ ID_SERVO_1MOTOR_OLD = 0x002
54
+ ID_SERVO_4MOTOR = 0x038
55
+ ID_SERVO_4MOTOR_OLD = 0x003
56
+ ID_TEXTLCD_2x20 = 0x052
57
+ ID_TEXTLCD_2x20_w_0_8_8 = 0x153
58
+ ID_TEXTLED_1x8 = 0x049
59
+ ID_TEXTLED_4x8 = 0x048
60
+ ID_WEIGHTSENSOR = 0x072
61
+
62
+ def Phidgets.setLibName(name)
63
+ Common.setLibName(name)
64
+ end
65
+
66
+ class Exception < RuntimeError
67
+ attr_reader :code
68
+
69
+ EPHIDGET_NOTFOUND = 1
70
+ EPHIDGET_NOMEMORY = 2
71
+ EPHIDGET_UNEXPECTED = 3
72
+ EPHIDGET_INVALIDARG = 4
73
+ EPHIDGET_NOTATTACHED = 5
74
+ EPHIDGET_INTERRUPTED = 6
75
+ EPHIDGET_INVALID = 7
76
+ EPHIDGET_NETWORK = 8
77
+ EPHIDGET_UNKNOWNVAL = 9
78
+ EPHIDGET_BADPASSWORD = 10
79
+ EPHIDGET_UNSUPPORTED = 11
80
+ EPHIDGET_DUPLICATE = 12
81
+ EPHIDGET_TIMEOUT = 13
82
+ EPHIDGET_OUTOFBOUNDS = 14
83
+ EPHIDGET_EVENT = 15
84
+ EPHIDGET_NETWORK_NOTCONNECTED = 16
85
+ EPHIDGET_WRONGDEVICE = 17
86
+ EPHIDGET_CLOSED = 18
87
+ EPHIDGET_BADVERSION = 19
88
+
89
+ def initialize(code)
90
+ @code = code
91
+ case code
92
+ when -2
93
+ super('Failed to load Phidgets Library')
94
+ when -1
95
+ super('Unable to determine Phidgets Library name.')
96
+ when EPHIDGET_NOTFOUND
97
+ super('A Phidget matching the type and or serial number could not be found.')
98
+ when EPHIDGET_NOMEMORY
99
+ super('Memory could not be allocated.')
100
+ when EPHIDGET_UNEXPECTED
101
+ super('Unexpected Error. Contact Phidgets Inc. for support.')
102
+ when EPHIDGET_INVALIDARG
103
+ super('Invalid argument passed to function.')
104
+ when EPHIDGET_NOTATTACHED
105
+ super('Phidget not physically attached.')
106
+ when EPHIDGET_INTERRUPTED
107
+ super('Read/Write operation was interrupted.')
108
+ when EPHIDGET_INVALID
109
+ super('The Error Code is not defined.')
110
+ when EPHIDGET_NETWORK
111
+ super('Network Error.')
112
+ when EPHIDGET_UNKNOWNVAL
113
+ super('Value is Unknown (State not yet received from device, or not yet set by user).')
114
+ when EPHIDGET_BADPASSWORD
115
+ super('Authorization Failed.')
116
+ when EPHIDGET_UNSUPPORTED
117
+ super('Not Supported.')
118
+ when EPHIDGET_DUPLICATE
119
+ super('Duplicated request.')
120
+ when EPHIDGET_TIMEOUT
121
+ super('Given timeout has been exceeded.')
122
+ when EPHIDGET_OUTOFBOUNDS
123
+ super('Index out of Bounds.')
124
+ when EPHIDGET_EVENT
125
+ super('A non-null error code was returned from an event handler.')
126
+ when EPHIDGET_NETWORK_NOTCONNECTED
127
+ super('A connection to the server does not exist.')
128
+ when EPHIDGET_WRONGDEVICE
129
+ super('Function is not applicable for this device.')
130
+ when EPHIDGET_CLOSED
131
+ super('Phidget handle was closed.')
132
+ when EPHIDGET_BADVERSION
133
+ super('Webservice and Client protocol versions don’t match. Update to newest release.')
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ class Common
140
+ @@libname = nil
141
+ @@lib = nil
142
+ @@open_remote = nil
143
+ @@open_remote_ip = nil
144
+ @@open = nil
145
+ @@close = nil
146
+ @@delete = nil
147
+ @@detach_handler = nil
148
+ @@attach_handler = nil
149
+ @@connect_handler = nil
150
+ @@disconnect_handler = nil
151
+ @@error_handler = nil
152
+ @@name = nil
153
+ @@serial = nil
154
+ @@version = nil
155
+ @@status = nil
156
+ @@lib_version = nil
157
+ @@type = nil
158
+ @@get_label = nil
159
+ @@set_label = nil
160
+ @@error_description = nil
161
+ @@wait = nil
162
+ @@server_id = nil
163
+ @@server_addr = nil
164
+ @@server_status = nil
165
+ @@device_id = nil
166
+ @@device_class = nil
167
+
168
+ def initialize
169
+ if @@libname == nil
170
+ case Config::CONFIG['target_os']
171
+ when 'linux'
172
+ @@libname = 'libphidget21.so'
173
+ when 'mswin32'
174
+ @@libname = 'phidget21.dll'
175
+ else
176
+ raise Phidgets::Exception.new(-1)
177
+ end
178
+ end
179
+
180
+ begin
181
+ @@lib = DL.dlopen(@@libname)
182
+ rescue
183
+ raise Phidgets::Exception.new(-2)
184
+ end
185
+
186
+ @handle = DL.malloc(DL.sizeof('P'))
187
+
188
+ end
189
+
190
+ private
191
+
192
+ def sym(name, interface)
193
+ @@lib[name, interface]
194
+ end
195
+
196
+ def call_IX(sym_name, func_name, format, arg1)
197
+ sym_name = sym(func_name, format) if sym_name == nil
198
+ r,rs = sym_name.call(arg1)
199
+ raise Phidgets::Exception.new(r) if r != 0
200
+ end
201
+
202
+ def call_IXX(sym_name, func_name, format, arg1, arg2)
203
+ sym_name = sym(func_name, format) if sym_name == nil
204
+ r,rs = sym_name.call(arg1, arg2)
205
+ raise Phidgets::Exception.new(r) if r != 0
206
+ end
207
+
208
+ def call_IXXX(sym_name, func_name, format, arg1, arg2, arg3)
209
+ sym_name = sym(func_name, format) if sym_name == nil
210
+ r,rs = sym_name.call(arg1, arg2, arg3)
211
+ raise Phidgets::Exception.new(r) if r != 0
212
+ end
213
+
214
+ def call_IXXXX(sym_name, func_name, format, arg1, arg2, arg3, arg4)
215
+ sym_name = sym(func_name, format) if sym_name == nil
216
+ r,rs = sym_name.call(arg1, arg2, arg3, arg4)
217
+ raise Phidgets::Exception.new(r) if r != 0
218
+ end
219
+
220
+ def call_IXXXXX(sym_name, func_name, format, arg1, arg2, arg3, arg4, arg5)
221
+ sym_name = sym(func_name, format) if sym_name == nil
222
+ r,rs = sym_name.call(arg1, arg2, arg3, arg4, arg5)
223
+ raise Phidgets::Exception.new(r) if r != 0
224
+ end
225
+
226
+ def call_IPi(sym_name, func_name, arg1)
227
+ sym_name = sym(func_name, 'IPi') if sym_name == nil
228
+ return_arg = 0
229
+ r,rs = sym_name.call(arg1, return_arg)
230
+ raise Phidgets::Exception.new(r) if r != 0
231
+ rs[1]
232
+ end
233
+
234
+ def call_IPIi(sym_name, func_name, arg1, arg2)
235
+ sym_name = sym(func_name, 'IPIi') if sym_name == nil
236
+ return_arg = 0
237
+ r,rs = sym_name.call(arg1, arg2, return_arg)
238
+ raise Phidgets::Exception.new(r) if r != 0
239
+ rs[2]
240
+ end
241
+
242
+ def call_IPs(sym_name, func_name, arg1)
243
+ sym_name = sym(func_name, 'IPp') if sym_name == nil
244
+ arg_ptr = DL.malloc(DL.sizeof('P'))
245
+ r,rs = sym_name.call(arg1, arg_ptr.ref)
246
+ raise Phidgets::Exception.new(r) if r != 0
247
+ arg_ptr.free = nil
248
+ arg_ptr.to_s
249
+ end
250
+
251
+ def createCallback(callback)
252
+ DL.callback('IPP') {|handle,data|
253
+ data = ObjectSpace._id2ref(data.to_i)
254
+ eval("#{callback}(data)")
255
+ }
256
+ end
257
+
258
+ def createErrorCallback(callback)
259
+ DL.callback('IPPIS') {|handle,data,error_code,error_string|
260
+ data = ObjectSpace._id2ref(data.to_i)
261
+ eval("#{callback}(data,error_code,error_string)")
262
+ }
263
+ end
264
+
265
+ public
266
+
267
+ def Common.setLibName(name)
268
+ @@libname = name
269
+ end
270
+
271
+ def openRemote(serial_number=-1, server=nil, password=nil, timeout=0)
272
+ call_IXXXX(@@open_remote, 'CPhidget_openRemote', 'IPISS', @handle, serial_number, server, password)
273
+ waitForAttachment(timeout) if timeout > 0
274
+ end
275
+
276
+ def openRemoteIP(serial_number, address, port=5001, password=nil, timeout=0)
277
+ call_IXXXXX(@@open_remote_ip, 'CPhidget_openRemoteIP', 'IPISIS', @handle, serial_number, address, port, password)
278
+ waitForAttachment(timeout) if timeout > 0
279
+ end
280
+
281
+ def open(serial_number=-1, timeout=0)
282
+ call_IXX(@@open, 'CPhidget_open', 'IPI', @handle, serial_number)
283
+ waitForAttachment(timeout) if timeout > 0
284
+ end
285
+
286
+ def close
287
+ call_IX(@@close, 'CPhidget_close', 'IP', @handle)
288
+ end
289
+
290
+ def delete
291
+ @handle.free = nil
292
+ call_IX(@@delete, 'CPhidget_delete', 'IP', @handle)
293
+ end
294
+
295
+ def setOnDetachHandler(callback, data)
296
+ call_IXXX(@@detach_handler, 'CPhidget_set_OnDetach_Handler', 'IPPP', @handle, createCallback(callback), DL::PtrData.new(data.object_id))
297
+ end
298
+
299
+ def setOnAttachHandler(callback, data)
300
+ call_IXXX(@@attach_handler, 'CPhidget_set_OnAttach_Handler', 'IPPP', @handle, createCallback(callback), DL::PtrData.new(data.object_id))
301
+ end
302
+
303
+ def setOnConnectHandler(callback, data)
304
+ call_IXXX(@@connect_handler, 'CPhidget_set_OnServerConnect_Handler', 'IPPP', @handle, createCallback(callback), DL::PtrData.new(data.object_id))
305
+ end
306
+
307
+ def setOnDisconnectHandler(callback, data)
308
+ call_IXXX(@@disconnect_handler, 'CPhidget_set_OnServerDisconnect_Handler', 'IPPP', @handle, createCallback(callback), DL::PtrData.new(data.object_id))
309
+ end
310
+
311
+ def setOnErrorHandler(callback, data)
312
+ call_IXXX(@@error_handler, 'CPhidget_set_OnError_Handler', 'IPPP', @handle, createErrorCallback(callback), DL::PtrData.new(data.object_id))
313
+ end
314
+
315
+ def getDeviceName
316
+ call_IPs(@@name, 'CPhidget_getDeviceName', @handle)
317
+ end
318
+
319
+ def getSerialNumber
320
+ call_IPi(@@serial, 'CPhidget_getSerialNumber', @handle)
321
+ end
322
+
323
+ def getDeviceVersion
324
+ call_IPi(@@version, 'CPhidget_getDeviceVersion', @handle)
325
+ end
326
+
327
+ def getDeviceStatus
328
+ call_IPi(@@status, 'CPhidget_getDeviceStatus', @handle)
329
+ end
330
+
331
+ def getLibraryVersion
332
+ @@lib_version = sym('CPhidget_getLibraryVersion', 'Ip') if @@lib_version == nil
333
+ lib_version_ptr = DL.malloc(DL.sizeof('P'))
334
+ r,rs = @@lib_version.call(lib_version_ptr.ref)
335
+ raise Phidgets::Exception.new(r) if r != 0
336
+ lib_version_ptr.free = nil
337
+ lib_version_ptr.to_s
338
+ end
339
+
340
+ def getDeviceType
341
+ call_IPs(@@type, 'CPhidget_getDeviceType', @handle)
342
+ end
343
+
344
+ def getDeviceLabel
345
+ call_IPs(@@get_label, 'CPhidget_getDeviceLabel', @handle)
346
+ end
347
+
348
+ def setDeviceLabel(label)
349
+ call_IXX(@@set_label, 'CPhidget_setDeviceLabel', 'IPS', @handle, label)
350
+ end
351
+
352
+ def getErrorDescription(error_code)
353
+ @@error_description = sym('CPhidget_getErrorDescription', 'IIp') if @@error_description == nil
354
+ error_description_ptr = DL.malloc(DL.sizeof('P'))
355
+ r,rs = @@error_description.call(error_code, error_description_ptr.ref)
356
+ raise Phidgets::Exception.new(r) if r != 0
357
+ error_description_ptr.free = nil
358
+ error_description_ptr.to_s
359
+ end
360
+
361
+ def waitForAttachment(timeout)
362
+ call_IXX(@@wait, 'CPhidget_waitForAttachment', 'IPI', @handle, timeout)
363
+ end
364
+
365
+ def getServerID
366
+ call_IPs(@@server_id, 'CPhidget_getServerID', @handle)
367
+ end
368
+
369
+ def getServerAddress
370
+ @@server_addr = sym('CPhidget_getServerAddress', 'IPpi') if @@server_addr == nil
371
+ server_addr_ptr = DL.malloc(DL.sizeof('P'))
372
+ server_port = 0
373
+ r,rs = @@server_addr.call(@handle, server_addr_ptr.ref, server_port)
374
+ raise Phidgets::Exception.new(r) if r != 0
375
+ server_addr_ptr.free = nil
376
+ a = [server_addr_ptr.to_s, rs[2]]
377
+ a
378
+ end
379
+
380
+ def getServerStatus
381
+ call_IPi(@@server_status, 'CPhidget_getServerStatus', @handle)
382
+ end
383
+
384
+ def getDeviceID
385
+ call_IPi(@@device_id, 'CPhidget_getDeviceID', @handle)
386
+ end
387
+
388
+ def getDeviceClass
389
+ call_IPi(@@device_class, 'CPhidget_getDeviceClass', @handle)
390
+ end
391
+
392
+ end
393
+
394
+ end
395
+
@@ -0,0 +1,78 @@
1
+
2
+ #require 'phidgets/common'
3
+
4
+ module Phidgets
5
+
6
+ class InterfaceKit < Common
7
+ @@create = nil
8
+ @@input_count = nil
9
+ @@input_state = nil
10
+ @@output_count = nil
11
+ @@get_output_state = nil
12
+ @@set_output_state = nil
13
+ @@sensor_count = nil
14
+ @@sensor_value = nil
15
+ @@sensor_raw_value = nil
16
+ @@get_ratiometric = nil
17
+ @@set_ratiometric = nil
18
+
19
+ def initialize(serial_number=-1, timeout=0)
20
+ super()
21
+ create
22
+ open(serial_number, timeout) if timeout > 0
23
+ end
24
+
25
+ private
26
+
27
+ def create
28
+ @@create = sym('CPhidgetInterfaceKit_create', 'Ip') if @@create == nil
29
+ r,rs = @@create.call(@handle.ref)
30
+ raise Phidgets::Exception.new(r) if r != 0
31
+ end
32
+
33
+ public
34
+
35
+ def getInputCount
36
+ call_IPi(@@input_count, 'CPhidgetInterfaceKit_getInputCount', @handle)
37
+ end
38
+
39
+ def getInputState(index)
40
+ call_IPIi(@@input_state, 'CPhidgetInterfaceKit_getInputState', @handle, index)
41
+ end
42
+
43
+ def getOutputCount
44
+ call_IPi(@@output_count, 'CPhidgetInterfaceKit_getOutputCount', @handle)
45
+ end
46
+
47
+ def getOutputState(index)
48
+ call_IPIi(@@get_output_state, 'CPhidgetInterfaceKit_getOutputState', @handle, index)
49
+ end
50
+
51
+ def setOutputState(index, state)
52
+ call_IXXX(@@set_output_state, 'CPhidgetInterfaceKit_setOutputState', 'IPII', @handle, index, state)
53
+ end
54
+
55
+ def getSensorCount
56
+ call_IPi(@@sensor_count, 'CPhidgetInterfaceKit_getSensorCount', @handle)
57
+ end
58
+
59
+ def getSensorValue(index)
60
+ call_IPIi(@@sensor_value, 'CPhidgetInterfaceKit_getSensorValue', @handle, index)
61
+ end
62
+
63
+ def getSensorRawValue(index)
64
+ call_IPIi(@@sensor_raw_value, 'CPhidgetInterfaceKit_getSensorRawValue', @handle, index)
65
+ end
66
+
67
+ def getRatiometric
68
+ call_IPi(@@get_ratiometric, 'CPhidgetInterfaceKit_getRatiometric', @handle)
69
+ end
70
+
71
+ def setRatiometric(ratiometric)
72
+ call_IXX(@@set_ratiometric, 'CPhidgetInterfaceKit_setRatiometric', 'IPI', @handle, ratiometric)
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+