phidgets-ffi 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/LICENSE +9 -0
- data/README.rdoc +59 -0
- data/Rakefile +2 -0
- data/examples/ffi/dictionary.rb +59 -0
- data/examples/ffi/interface_kit.rb +44 -0
- data/examples/ffi/library_version.rb +11 -0
- data/examples/ffi/log.rb +11 -0
- data/examples/ffi/manager.rb +35 -0
- data/examples/ffi/servo.rb +67 -0
- data/examples/interface_kit.rb +20 -0
- data/examples/log.rb +9 -0
- data/examples/manager.rb +17 -0
- data/examples/servo.rb +36 -0
- data/lib/phidgets-ffi.rb +17 -0
- data/lib/phidgets-ffi/common.rb +222 -0
- data/lib/phidgets-ffi/error.rb +11 -0
- data/lib/phidgets-ffi/ffi/common.rb +50 -0
- data/lib/phidgets-ffi/ffi/constants.rb +109 -0
- data/lib/phidgets-ffi/ffi/core_ext.rb +17 -0
- data/lib/phidgets-ffi/ffi/dictionary.rb +44 -0
- data/lib/phidgets-ffi/ffi/interface_kit.rb +39 -0
- data/lib/phidgets-ffi/ffi/log.rb +36 -0
- data/lib/phidgets-ffi/ffi/manager.rb +36 -0
- data/lib/phidgets-ffi/ffi/servo.rb +50 -0
- data/lib/phidgets-ffi/interface_kit.rb +212 -0
- data/lib/phidgets-ffi/log.rb +28 -0
- data/lib/phidgets-ffi/manager.rb +109 -0
- data/lib/phidgets-ffi/phidgets-ffi.rb +6 -0
- data/lib/phidgets-ffi/servo.rb +112 -0
- data/lib/phidgets-ffi/version.rb +5 -0
- data/phidgets-ffi.gemspec +23 -0
- metadata +133 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Copyright (c) 2011, Kelley Reynolds
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
8
|
+
Neither the name of the Inside Systems, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
9
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
=Phidgets-FFI
|
2
|
+
|
3
|
+
This is a set of FFI bindings for the Phidgets21 library. Like every library, it only
|
4
|
+
supports the devices that I needed it to but it supports them completely. There is a
|
5
|
+
manager instance that lists attached devices, callbacks function perfectly, and there
|
6
|
+
are a set of idiomatic bindings that take the FFI and make it more accessible. Raw FFI
|
7
|
+
examples are in examples/ffi and the idiomatic versions are in examples/.
|
8
|
+
|
9
|
+
==Quick Start
|
10
|
+
|
11
|
+
To access the manager and see your attached devices:
|
12
|
+
|
13
|
+
Phidgets::Manager.new do |manager|
|
14
|
+
puts manager.devices.inspect
|
15
|
+
end
|
16
|
+
|
17
|
+
[{
|
18
|
+
:type=>"PhidgetServo",
|
19
|
+
:label=>"",
|
20
|
+
:device_id=>:servo_1motor,
|
21
|
+
:serial_number=>12345,
|
22
|
+
:name=>"Phidget Servo Controller 1-motor",
|
23
|
+
:device_class=>:servo,
|
24
|
+
:version=>313
|
25
|
+
}]
|
26
|
+
|
27
|
+
To access that servo controller and do something with the servo:
|
28
|
+
|
29
|
+
Phidgets::ServoController.new(12345) do |controller|
|
30
|
+
servo = controller.servos[0]
|
31
|
+
|
32
|
+
controller.on_change do |servo, position, data|
|
33
|
+
print "Moving servo #{servo.index} to #{position}\n"
|
34
|
+
end
|
35
|
+
|
36
|
+
max = servo.max
|
37
|
+
10.times do
|
38
|
+
servo.position = rand(max)
|
39
|
+
sleep 1
|
40
|
+
end
|
41
|
+
servo.engaged = false
|
42
|
+
end
|
43
|
+
|
44
|
+
==Logging
|
45
|
+
|
46
|
+
Logging can be accomplished by either accessing the C library directly or using the idiomatic method.
|
47
|
+
There are additional examples of each in the examples/ directory.
|
48
|
+
|
49
|
+
Phidgets::Log.enable(:verbose, nil)
|
50
|
+
Phidgets::Log.info('identifier', 'message')
|
51
|
+
Phidgets::Log.error('identifier', 'message')
|
52
|
+
Phidgets::Log.disable
|
53
|
+
|
54
|
+
==Notes
|
55
|
+
|
56
|
+
Again, note that not every device is here. I'm more than happy to add support for it
|
57
|
+
but I'll need a test device to make sure it works ;)
|
58
|
+
|
59
|
+
PS. I'm serious, contact me if you want my mailing address.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
include Phidgets::FFI
|
5
|
+
Phidgets::Log.enable(:verbose)
|
6
|
+
|
7
|
+
pattern = ".*"
|
8
|
+
key_listener = FFI::MemoryPointer.new(:pointer)
|
9
|
+
|
10
|
+
KeyChangedHandler = Proc.new { |dict, obj_ptr, key, value, reason|
|
11
|
+
print "Keychange { #{key} => #{value}, reason => '#{reason}'}\n"
|
12
|
+
}
|
13
|
+
ErrorHandler = Proc.new { |dict, obj_ptr, code, reason|
|
14
|
+
print "Error (#{code}): #{reason} \n"
|
15
|
+
}
|
16
|
+
ServerConnectedHandler = Proc.new { |dict, obj_ptr|
|
17
|
+
print "Server connected\n"
|
18
|
+
CPhidgetDictionary_set_OnKeyChange_Handler(dict, key_listener, pattern, KeyChangedHandler, nil);
|
19
|
+
}
|
20
|
+
ServerDisconnectedHandler = Proc.new { |dict, obj_ptr|
|
21
|
+
print "Server disconnected\n"
|
22
|
+
CPhidgetDictionary_remove_OnKeyChange_Handler(key_listener.get_pointer(0))
|
23
|
+
}
|
24
|
+
|
25
|
+
ptr = FFI::MemoryPointer.new(:pointer)
|
26
|
+
CPhidgetDictionary.create(ptr)
|
27
|
+
dictionary = ptr.get_pointer(0)
|
28
|
+
CPhidget_set_OnServerConnect_Handler(dictionary, ServerConnectedHandler, nil)
|
29
|
+
CPhidget_set_OnServerDisconnect_Handler(dictionary, ServerDisconnectedHandler, nil)
|
30
|
+
CPhidget_set_OnError_Handler(dictionary, ErrorHandler, nil)
|
31
|
+
|
32
|
+
server_status = FFI::MemoryPointer.new(:int)
|
33
|
+
address = FFI::MemoryPointer.new(:string)
|
34
|
+
port = FFI::MemoryPointer.new(:int)
|
35
|
+
result = CPhidgetDictionary_openRemoteIP(dictionary, "Mactop.local", 5001, nil);
|
36
|
+
while server_status.get_int(0) != 1
|
37
|
+
CPhidgetDictionary_getServerStatus(dictionary, server_status)
|
38
|
+
end
|
39
|
+
print "Attached to server\n"
|
40
|
+
|
41
|
+
CPhidgetDictionary_getServerAddress(dictionary, address, port)
|
42
|
+
printf "Address: %s -- Port: %i \n", address.get_pointer(0).read_string, port.get_int(0)
|
43
|
+
|
44
|
+
# Set some keys
|
45
|
+
sleep 1
|
46
|
+
CPhidgetDictionary_addKey(dictionary, "TEST1", "1234", 1)
|
47
|
+
CPhidgetDictionary_addKey(dictionary, "TEST2", "5678", 1);
|
48
|
+
CPhidgetDictionary_addKey(dictionary, "TEST3", "9012", 0);
|
49
|
+
CPhidgetDictionary_addKey(dictionary, "TEST4", "3456", 1);
|
50
|
+
sleep 2
|
51
|
+
puts "Delete the 4th key...."
|
52
|
+
sleep 1
|
53
|
+
CPhidgetDictionary_removeKey(dictionary, "TEST4")
|
54
|
+
sleep 2
|
55
|
+
puts "Press any key to end"
|
56
|
+
$stdin.getc
|
57
|
+
CPhidgetDictionary_close(dictionary);
|
58
|
+
CPhidgetDictionary_delete(dictionary);
|
59
|
+
sleep 10
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
def device_name_for(device)
|
5
|
+
ptr = FFI::MemoryPointer.new(:string, 1)
|
6
|
+
CPhidget_getDeviceName(device, ptr)
|
7
|
+
strPtr = ptr.get_pointer(0)
|
8
|
+
strPtr.null? ? nil : strPtr.read_string
|
9
|
+
end
|
10
|
+
|
11
|
+
def serial_number_for(device)
|
12
|
+
ptr = FFI::MemoryPointer.new(:int, 1)
|
13
|
+
CPhidget_getSerialNumber(device, ptr)
|
14
|
+
ptr.get_int(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
include Phidgets::FFI
|
18
|
+
|
19
|
+
ifkit = FFI::MemoryPointer.new(:pointer, 1)
|
20
|
+
if !(res = CPhidgetInterfaceKit_create(ifkit))
|
21
|
+
$stderr.puts "Unable to create interface kit"
|
22
|
+
exit!(res)
|
23
|
+
else
|
24
|
+
ifkit = ifkit.get_pointer(0)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Create an attachment and detachment handler
|
28
|
+
AttachHandler = Proc.new { |device, ptr|
|
29
|
+
print "Device attached: #{device_name_for(device)} (#{serial_number_for(device)})\n"
|
30
|
+
}
|
31
|
+
CPhidget_set_OnAttach_Handler(ifkit, AttachHandler, nil)
|
32
|
+
DetachHandler = Proc.new { |device, ptr|
|
33
|
+
print "Device detached: #{device_name_for(device)} (#{serial_number_for(device)})\n"
|
34
|
+
}
|
35
|
+
CPhidget_set_OnDetach_Handler(ifkit, DetachHandler, nil)
|
36
|
+
|
37
|
+
CPhidget_open(ifkit, -1)
|
38
|
+
CPhidget_waitForAttachment(ifkit, 10000)
|
39
|
+
|
40
|
+
print "You have 20 seconds to attach/detach the interface kit. It should activate the callback handlers\n"
|
41
|
+
sleep 20
|
42
|
+
|
43
|
+
CPhidget_close(ifkit)
|
44
|
+
CPhidget_delete(ifkit)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
ptr = FFI::MemoryPointer.new(:string, 1)
|
5
|
+
if !(res = Phidgets::FFI::CPhidget_getLibraryVersion(ptr))
|
6
|
+
$stderr.puts "Unable to get library version"
|
7
|
+
exit!(res)
|
8
|
+
else
|
9
|
+
puts ptr.get_pointer(0).read_string
|
10
|
+
end
|
11
|
+
|
data/examples/ffi/log.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
include Phidgets::FFI
|
5
|
+
|
6
|
+
# Will print a line or two to stdout
|
7
|
+
Log.enableLogging(:verbose, nil)
|
8
|
+
Log.log(:info, 'identifier', 'arbitrary message')
|
9
|
+
Log.log(:info, 'identifier', 'arbitrary with a string: %s', "foo")
|
10
|
+
Log.log(:info, 'identifier', 'arbitrary with an integer: %i', 123)
|
11
|
+
Log.log(:info, 'identifier', 'arbitrary with an integer: %10.2e', 123.123)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
include Phidgets::FFI
|
5
|
+
ptr = FFI::MemoryPointer.new(:pointer, 1)
|
6
|
+
CPhidgetManager.create(ptr)
|
7
|
+
manager = ptr.get_pointer(0)
|
8
|
+
CPhidgetManager.open(manager)
|
9
|
+
|
10
|
+
sleep(1)
|
11
|
+
devices_ptr, count = FFI::MemoryPointer.new(:pointer, 1), FFI::MemoryPointer.new(:int)
|
12
|
+
CPhidgetManager.getAttachedDevices(manager, devices_ptr, count)
|
13
|
+
devices = devices_ptr.get_array_of_pointer(0, count.get_int(0))
|
14
|
+
|
15
|
+
ptr1 = FFI::MemoryPointer.new(:string)
|
16
|
+
ptr2 = FFI::MemoryPointer.new(:int)
|
17
|
+
devices.each_with_index do |device, i|
|
18
|
+
device = device.get_pointer(0)
|
19
|
+
|
20
|
+
Common.getDeviceName(device, ptr1)
|
21
|
+
name = ptr1.get_pointer(0).read_string
|
22
|
+
|
23
|
+
Common.getSerialNumber(device, ptr2)
|
24
|
+
serial_number = ptr2.get_int(0)
|
25
|
+
|
26
|
+
Common.getDeviceVersion(device, ptr2)
|
27
|
+
version = ptr2.get_int(0)
|
28
|
+
|
29
|
+
print "Device #{i+1}: #{name} v#{version} - Serial Number #{serial_number}\n"
|
30
|
+
end
|
31
|
+
|
32
|
+
CPhidgetManager.freeAttachedDevicesArray(devices_ptr.get_pointer(0))
|
33
|
+
|
34
|
+
CPhidgetManager.close(manager)
|
35
|
+
CPhidgetManager.delete(manager)
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
include Phidgets::FFI
|
5
|
+
|
6
|
+
f = FFI::MemoryPointer.new(:pointer, 1)
|
7
|
+
if !(res = Phidgets::FFI::CPhidgetServo.create(f))
|
8
|
+
$stderr.puts "Unable to create servo"
|
9
|
+
exit!(res)
|
10
|
+
else
|
11
|
+
servo = f.get_pointer(0)
|
12
|
+
end
|
13
|
+
f = nil
|
14
|
+
def device_name_for(device)
|
15
|
+
ptr = FFI::MemoryPointer.new(:string, 1)
|
16
|
+
CPhidget_getDeviceName(device, ptr)
|
17
|
+
strPtr = ptr.get_pointer(0)
|
18
|
+
strPtr.null? ? nil : strPtr.read_string
|
19
|
+
end
|
20
|
+
|
21
|
+
def serial_number_for(device)
|
22
|
+
ptr = FFI::MemoryPointer.new(:int, 1)
|
23
|
+
CPhidget_getSerialNumber(device, ptr)
|
24
|
+
ptr.get_int(0)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Create an attachment and detachment handler
|
28
|
+
AttachHandler = Proc.new { |device, ptr|
|
29
|
+
print "Device attached: #{device_name_for(device)} (#{serial_number_for(device)})\n"
|
30
|
+
tmp = FFI::MemoryPointer.new(:double)
|
31
|
+
CPhidgetServo_getPositionMax(device, 0, tmp)
|
32
|
+
print "Max: #{tmp.get_double(0)}\n"
|
33
|
+
CPhidgetServo_getPositionMin(device, 0, tmp)
|
34
|
+
print "Min: #{tmp.get_double(0)}\n"
|
35
|
+
}
|
36
|
+
CPhidget_set_OnAttach_Handler(servo, AttachHandler, nil)
|
37
|
+
DetachHandler = Proc.new { |device, ptr|
|
38
|
+
print "Device detached: #{device_name_for(device)} (#{serial_number_for(device)})\n"
|
39
|
+
}
|
40
|
+
CPhidget_set_OnDetach_Handler(servo, DetachHandler, nil)
|
41
|
+
|
42
|
+
PositionChangeHandler = Proc.new { |device, ptr, index, position|
|
43
|
+
print "Moving motor #{index} to #{position}\n"
|
44
|
+
}
|
45
|
+
CPhidgetServo_set_OnPositionChange_Handler(servo, PositionChangeHandler, nil)
|
46
|
+
|
47
|
+
CPhidget_open(servo, -1)
|
48
|
+
CPhidget_waitForAttachment(servo, 10000)
|
49
|
+
|
50
|
+
ptr = FFI::MemoryPointer.new(:int)
|
51
|
+
exit! if CPhidgetServo_getEngaged(servo, 0, ptr) != 0
|
52
|
+
#CPhidgetServo_setEngaged(servo, 0, 1)
|
53
|
+
|
54
|
+
CPhidgetServo_getServoType(servo, 0, ptr)
|
55
|
+
print "Servo Type: #{ServoType[ptr.get_int(0)]}\n"
|
56
|
+
|
57
|
+
tmp = FFI::MemoryPointer.new(:double)
|
58
|
+
CPhidgetServo_getPositionMax(servo, 0, tmp)
|
59
|
+
max = tmp.get_double(0)
|
60
|
+
10.times do
|
61
|
+
CPhidgetServo_setPosition(servo, 0, rand(max))
|
62
|
+
sleep 1
|
63
|
+
end
|
64
|
+
|
65
|
+
CPhidgetServo_setEngaged(servo, 0, 0)
|
66
|
+
CPhidget_close(servo)
|
67
|
+
CPhidget_delete(servo)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
puts "Library Version: #{Phidgets::FFI.library_version}"
|
5
|
+
Phidgets::InterfaceKit.new(-1) do |ifkit|
|
6
|
+
puts "Device attributes: #{ifkit.attributes.inspect}"
|
7
|
+
|
8
|
+
ifkit.on_output_change do |output, state, obj|
|
9
|
+
print "Output #{output.index} has changed to #{state}\n"
|
10
|
+
end
|
11
|
+
|
12
|
+
ifkit.wait_for_attachment
|
13
|
+
ifkit.outputs[0].state = true
|
14
|
+
ifkit.outputs[0].state = false
|
15
|
+
|
16
|
+
puts ifkit.sensors[0].inspect
|
17
|
+
ifkit.sensors[0].rate = 64
|
18
|
+
ifkit.sensors[0].trigger = 20
|
19
|
+
puts ifkit.sensors[0].inspect
|
20
|
+
end
|
data/examples/log.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
# Will print a line or two to stdout
|
5
|
+
Phidgets::Log.enable(:verbose)
|
6
|
+
Phidgets::Log.info('identifier', 'arbitrary message')
|
7
|
+
Phidgets::Log.warning('identifier', 'arbitrary with a string: %s', "foo")
|
8
|
+
Phidgets::Log.error('identifier', 'arbitrary with an integer: %i', 123)
|
9
|
+
Phidgets::Log.verbose('identifier', 'arbitrary with an integer: %10.2e', 123.123)
|
data/examples/manager.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
Phidgets::Manager.new do |manager|
|
5
|
+
puts manager.devices.inspect
|
6
|
+
|
7
|
+
manager.on_attach do |device_ptr|
|
8
|
+
puts "Attaching #{device_ptr}"
|
9
|
+
end
|
10
|
+
|
11
|
+
manager.on_detach do |device_ptr|
|
12
|
+
puts "Detaching #{device_ptr}"
|
13
|
+
end
|
14
|
+
|
15
|
+
puts "You have 20 seconds to plug/unplug USB devices and watch the callbacks execute"
|
16
|
+
sleep 20
|
17
|
+
end
|
data/examples/servo.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
puts "Library Version: #{Phidgets::FFI.library_version}"
|
5
|
+
Phidgets::ServoController.new(-1) do |controller|
|
6
|
+
puts "Device Attributes: #{controller.attributes.inspect}"
|
7
|
+
servo = controller.servos[0]
|
8
|
+
puts "Current Type: #{servo.type}"
|
9
|
+
puts "Default Min/Max: #{servo.min}/#{servo.max}"
|
10
|
+
puts "Device Name: #{controller.name}"
|
11
|
+
puts "Device Type: #{controller.type}"
|
12
|
+
puts "Device SN: #{controller.serial_number}"
|
13
|
+
puts "Device Version: #{controller.version}"
|
14
|
+
|
15
|
+
puts "Device Label: #{controller.label}"
|
16
|
+
controller.on_change do |servo, position, obj|
|
17
|
+
print "Moving servo #{servo.index} to #{position}\n"
|
18
|
+
end
|
19
|
+
|
20
|
+
controller.on_detach do |obj, data|
|
21
|
+
print "Detached #{obj.type}\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
controller.on_attach do |obj, data|
|
25
|
+
print "Attached #{obj.type}\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
max = servo.max
|
29
|
+
10.times do
|
30
|
+
controller.wait_for_attachment
|
31
|
+
servo.position = rand(max)
|
32
|
+
sleep 0.5
|
33
|
+
end
|
34
|
+
servo.engaged = false
|
35
|
+
end
|
36
|
+
|
data/lib/phidgets-ffi.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
require 'phidgets-ffi/phidgets-ffi'
|
3
|
+
require 'phidgets-ffi/ffi/core_ext'
|
4
|
+
require 'phidgets-ffi/ffi/constants'
|
5
|
+
require 'phidgets-ffi/ffi/common'
|
6
|
+
require 'phidgets-ffi/ffi/dictionary'
|
7
|
+
require 'phidgets-ffi/ffi/manager'
|
8
|
+
require 'phidgets-ffi/ffi/log'
|
9
|
+
require 'phidgets-ffi/ffi/servo'
|
10
|
+
require 'phidgets-ffi/ffi/interface_kit'
|
11
|
+
|
12
|
+
require 'phidgets-ffi/common'
|
13
|
+
require 'phidgets-ffi/log'
|
14
|
+
require 'phidgets-ffi/error'
|
15
|
+
require 'phidgets-ffi/manager'
|
16
|
+
require 'phidgets-ffi/servo'
|
17
|
+
require 'phidgets-ffi/interface_kit'
|
@@ -0,0 +1,222 @@
|
|
1
|
+
module Phidgets
|
2
|
+
module FFI
|
3
|
+
def self.library_version
|
4
|
+
ptr = ::FFI::MemoryPointer.new(:string)
|
5
|
+
Phidgets::FFI::Common.getLibraryVersion(ptr)
|
6
|
+
strPtr = ptr.get_pointer(0)
|
7
|
+
strPtr.null? ? nil : strPtr.read_string
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_description(code)
|
11
|
+
ptr = ::FFI::MemoryPointer.new(:string)
|
12
|
+
Phidgets::FFI::Common.getErrorDescription(code, ptr)
|
13
|
+
strPtr = ptr.get_pointer(0)
|
14
|
+
strPtr.null? ? nil : strPtr.read_string
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module Utility
|
19
|
+
private
|
20
|
+
def object_for(obj_ptr)
|
21
|
+
(obj_ptr.null? ? nil : ObjectSpace._id2ref(obj_ptr.get_uint(0)))
|
22
|
+
end
|
23
|
+
|
24
|
+
def pointer_for(obj)
|
25
|
+
::FFI::MemoryPointer.new(:uint).write_uint(obj.object_id)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module Common
|
30
|
+
include Utility
|
31
|
+
|
32
|
+
def create
|
33
|
+
ptr = ::FFI::MemoryPointer.new(:pointer, 1)
|
34
|
+
self.class::Klass.create(ptr)
|
35
|
+
@handle = ptr.get_pointer(0)
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def open(serial_number=-1)
|
40
|
+
Phidgets::FFI::Common.open(@handle, serial_number)
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
def open_label(str)
|
45
|
+
Phidgets::FFI::Common.openLabel(@handle, str)
|
46
|
+
true
|
47
|
+
end
|
48
|
+
|
49
|
+
def close
|
50
|
+
Phidgets::FFI::Common.close(@handle)
|
51
|
+
true
|
52
|
+
end
|
53
|
+
|
54
|
+
def delete
|
55
|
+
Phidgets::FFI::Common.delete(@handle)
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def wait_for_attachment(milliseconds=10000)
|
60
|
+
Phidgets::FFI::Common.waitForAttachment(@handle, milliseconds)
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.name(handle)
|
65
|
+
ptr = ::FFI::MemoryPointer.new(:string)
|
66
|
+
Phidgets::FFI::Common.getDeviceName(handle, ptr)
|
67
|
+
strPtr = ptr.get_pointer(0)
|
68
|
+
strPtr.null? ? nil : strPtr.read_string
|
69
|
+
end
|
70
|
+
def name; Common.name(@handle); end
|
71
|
+
|
72
|
+
def self.serial_number(handle)
|
73
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
74
|
+
Phidgets::FFI::Common.getSerialNumber(handle, ptr)
|
75
|
+
ptr.get_int(0)
|
76
|
+
end
|
77
|
+
def serial_number; Common.serial_number(@handle); end
|
78
|
+
|
79
|
+
def self.version(handle)
|
80
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
81
|
+
Phidgets::FFI::Common.getDeviceVersion(handle, ptr)
|
82
|
+
ptr.get_int(0)
|
83
|
+
end
|
84
|
+
def version; Common.version(@handle); end
|
85
|
+
|
86
|
+
def attached?
|
87
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
88
|
+
Phidgets::FFI::Common.getDeviceStatus(@handle, ptr)
|
89
|
+
(ptr.get_int(0) == 0) ? false : true
|
90
|
+
end
|
91
|
+
|
92
|
+
def detached?
|
93
|
+
!attached?
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.type(handle)
|
97
|
+
ptr = ::FFI::MemoryPointer.new(:string)
|
98
|
+
Phidgets::FFI::Common.getDeviceType(handle, ptr)
|
99
|
+
strPtr = ptr.get_pointer(0)
|
100
|
+
strPtr.null? ? nil : strPtr.read_string
|
101
|
+
end
|
102
|
+
def type; Common.type(@handle); end
|
103
|
+
|
104
|
+
def self.label(handle)
|
105
|
+
ptr = ::FFI::MemoryPointer.new(:string)
|
106
|
+
Phidgets::FFI::Common.getDeviceLabel(handle, ptr)
|
107
|
+
strPtr = ptr.get_pointer(0)
|
108
|
+
strPtr.null? ? nil : strPtr.read_string
|
109
|
+
end
|
110
|
+
def label; Common.label(@handle); end
|
111
|
+
|
112
|
+
def label=(new_label)
|
113
|
+
Phidgets::FFI::Common.setDeviceLabel(@handle, new_label)
|
114
|
+
new_label
|
115
|
+
end
|
116
|
+
|
117
|
+
def server_id
|
118
|
+
ptr = ::FFI::MemoryPointer.new(:string)
|
119
|
+
Phidgets::FFI::Common.getServerID(@handle, ptr)
|
120
|
+
strPtr = ptr.get_pointer(0)
|
121
|
+
strPtr.null? ? nil : strPtr.read_string
|
122
|
+
end
|
123
|
+
|
124
|
+
def server_address
|
125
|
+
str_ptr, int_ptr = ::FFI::MemoryPointer.new(:string), ::FFI::MemoryPointer.new(:int)
|
126
|
+
Phidgets::FFI::Common.getServerID(@handle, str_ptr, int_ptr)
|
127
|
+
strPtr = str_ptr.get_pointer(0)
|
128
|
+
address = (strPtr.null? ? nil : strPtr.read_string)
|
129
|
+
port = int_ptr.get_int(0)
|
130
|
+
[address, port]
|
131
|
+
end
|
132
|
+
|
133
|
+
def server_status
|
134
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
135
|
+
Phidgets::FFI::Common.getServerStatus(@handle, ptr)
|
136
|
+
ptr.get_int(0)
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.device_id(handle)
|
140
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
141
|
+
Phidgets::FFI::Common.getDeviceID(handle, ptr)
|
142
|
+
Phidgets::FFI::DeviceID[ptr.get_int(0)]
|
143
|
+
end
|
144
|
+
def device_id; Common.device_id(@handle); end
|
145
|
+
|
146
|
+
def self.device_class(handle)
|
147
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
148
|
+
Phidgets::FFI::Common.getDeviceClass(handle, ptr)
|
149
|
+
Phidgets::FFI::DeviceClass[ptr.get_int(0)]
|
150
|
+
end
|
151
|
+
def device_class; Common.device_class(@handle); end
|
152
|
+
|
153
|
+
def on_attach(obj=nil, &block)
|
154
|
+
@on_attach_obj = obj
|
155
|
+
@on_attach = Proc.new { |handle, obj_ptr|
|
156
|
+
yield self, object_for(obj_ptr)
|
157
|
+
}
|
158
|
+
Phidgets::FFI::Common.set_OnAttach_Handler(@handle, @on_attach, pointer_for(obj))
|
159
|
+
end
|
160
|
+
|
161
|
+
def on_detach(obj=nil, &block)
|
162
|
+
@on_detach_obj = obj
|
163
|
+
@on_detach = Proc.new { |handle, obj_ptr|
|
164
|
+
yield self, object_for(obj_ptr)
|
165
|
+
}
|
166
|
+
Phidgets::FFI::Common.set_OnDetach_Handler(@handle, @on_detach, pointer_for(obj))
|
167
|
+
end
|
168
|
+
|
169
|
+
def on_server_connect(obj=nil, &block)
|
170
|
+
@on_server_connect_obj = obj
|
171
|
+
@on_server_connect = Proc.new { |handle, obj_ptr|
|
172
|
+
yield self, object_for(obj_ptr)
|
173
|
+
}
|
174
|
+
Phidgets::FFI::Common.set_OnServerConnect_Handler(@handle, @on_server_connect, pointer_for(obj))
|
175
|
+
end
|
176
|
+
|
177
|
+
def on_server_disconnect(obj=nil, &block)
|
178
|
+
@on_server_disconnect_obj = obj
|
179
|
+
@on_server_disconnect = Proc.new { |handle, obj_ptr|
|
180
|
+
yield self, object_for(obj_ptr)
|
181
|
+
}
|
182
|
+
Phidgets::FFI::Common.set_OnServerDisconnect_Handler(@handle, @on_server_disconnect, pointer_for(obj))
|
183
|
+
end
|
184
|
+
|
185
|
+
def on_error(obj=nil, &block)
|
186
|
+
@on_error_obj = obj
|
187
|
+
@on_error = Proc.new { |handle, obj_ptr, code, description|
|
188
|
+
yield self, object_for(obj_ptr), code, description
|
189
|
+
}
|
190
|
+
Phidgets::FFI::Common.set_OnError_Handler(@handle, @on_error, pointer_for(obj))
|
191
|
+
end
|
192
|
+
|
193
|
+
def on_sleep(obj=nil, &block)
|
194
|
+
@on_sleep_obj = obj
|
195
|
+
@on_sleep = Proc.new { |obj_ptr|
|
196
|
+
yield object_for(obj_ptr)
|
197
|
+
}
|
198
|
+
Phidgets::FFI::Common.set_OnWillSleep_Handler(@on_sleep, pointer_for(obj))
|
199
|
+
end
|
200
|
+
|
201
|
+
def on_wake(obj=nil, &block)
|
202
|
+
@on_wake_obj = obj
|
203
|
+
@on_wake = Proc.new { |obj_ptr|
|
204
|
+
yield object_for(obj_ptr)
|
205
|
+
}
|
206
|
+
Phidgets::FFI::Common.set_OnWakeup_Handler(@on_wake, pointer_for(obj))
|
207
|
+
end
|
208
|
+
|
209
|
+
def self.attributes(handle)
|
210
|
+
{
|
211
|
+
:type => type(handle),
|
212
|
+
:name => name(handle),
|
213
|
+
:serial_number => serial_number(handle),
|
214
|
+
:version => version(handle),
|
215
|
+
:label => label(handle),
|
216
|
+
:device_class => device_class(handle),
|
217
|
+
:device_id => device_id(handle),
|
218
|
+
}
|
219
|
+
end
|
220
|
+
def attributes; Common.attributes(@handle); end
|
221
|
+
end
|
222
|
+
end
|