phidgets-ffi 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -3
- data/examples/HelloWorld.rb +36 -0
- data/examples/led.rb +18 -5
- data/examples/rfid.rb +5 -1
- data/lib/phidgets-ffi/ffi/led.rb +4 -2
- data/lib/phidgets-ffi/ffi/rfid.rb +14 -7
- data/lib/phidgets-ffi/led.rb +23 -8
- data/lib/phidgets-ffi/rfid.rb +35 -25
- data/lib/phidgets-ffi/version.rb +1 -1
- metadata +18 -7
data/README.rdoc
CHANGED
@@ -13,12 +13,12 @@ Idiomatic examples are in examples/ and the Raw FFI examples are in examples/raw
|
|
13
13
|
First, we need to set up the proper environment and get the necessary files off the Phidgets website. Visit the drivers section at www.phidgets.com and get the latest Phidget driver for your system. You will also need to install the phidgets-ffi gem from RubyGems. More information will be provided in the "Installing the phidgets-ffi gem" section of this document.
|
14
14
|
|
15
15
|
We also recommend that you download the following reference materials:
|
16
|
-
* {Programming
|
17
|
-
* The {
|
16
|
+
* {General Phidget Programming}[http://www.phidgets.com/docs/General_Phidget_Programming]
|
17
|
+
* The {User Guide}[http://www.phidgets.com/docs/Category:UserGuide] for your device
|
18
18
|
* {RubyDoc API Documentation}[http://rubydoc.info/gems/phidgets-ffi/frames]
|
19
19
|
* {Ruby code samples}[https://github.com/kreynolds/phidgets-ffi/tree/master/examples]
|
20
20
|
|
21
|
-
The RubyDoc API manual contains calls and events for every type of Phidget and can be used as a reference. You can find a high level discussion about programming with Phidgets in general
|
21
|
+
The RubyDoc API manual contains calls and events for every type of Phidget and can be used as a reference. You can find a high level discussion about programming with Phidgets in general on the General Phidget Programming page. The user guide for your device also contains an API section that describes limitations, defaults, and implementation details specific to your Phidget. You may want to have these pages open while working through these instructions.
|
22
22
|
|
23
23
|
==Installing the phidgets-ffi gem
|
24
24
|
To install:
|
@@ -0,0 +1,36 @@
|
|
1
|
+
=begin
|
2
|
+
Phidget Hello World Program for all devices
|
3
|
+
(c) Phidgets 2012
|
4
|
+
=end
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'phidgets-ffi'
|
8
|
+
|
9
|
+
manager = Phidgets::Manager.new
|
10
|
+
|
11
|
+
#Determine if there are devices already attached to the computer
|
12
|
+
manager.devices.size.times do |i|
|
13
|
+
puts "Hello Device #{Phidgets::Common.name(manager.devices[i])}, Serial Number: #{Phidgets::Common.serial_number(manager.devices[i])}"
|
14
|
+
end
|
15
|
+
|
16
|
+
# ========== Event Handling Functions ==========
|
17
|
+
#The attach event will detect any devices attached AFTER the program is run.
|
18
|
+
manager.on_attach do |device_ptr, obj|
|
19
|
+
puts "Hello Device #{Phidgets::Common.name(device_ptr)}, Serial Number: #{Phidgets::Common.serial_number(device_ptr)}"
|
20
|
+
end
|
21
|
+
|
22
|
+
manager.on_detach do |device_ptr, obj|
|
23
|
+
puts "Goodbye Device #{Phidgets::Common.name(device_ptr)}, Serial Number: #{Phidgets::Common.serial_number(device_ptr)}"
|
24
|
+
end
|
25
|
+
|
26
|
+
manager.on_error do |device, obj, code, description|
|
27
|
+
puts "Error - code #{code}, description #{description}"
|
28
|
+
end
|
29
|
+
|
30
|
+
puts 'Phidget Simple Playground (plug and unplug devices)'
|
31
|
+
puts 'Please Enter to end anytime...'
|
32
|
+
|
33
|
+
gets.chomp
|
34
|
+
|
35
|
+
puts 'Closing...'
|
36
|
+
manager.close
|
data/examples/led.rb
CHANGED
@@ -17,10 +17,8 @@ led.on_attach do |device, obj|
|
|
17
17
|
puts "Version: #{device.version}"
|
18
18
|
puts "# LEDs: #{device.leds.size}"
|
19
19
|
|
20
|
-
device.
|
21
|
-
|
22
|
-
end
|
23
|
-
|
20
|
+
device.current_limit = Phidgets::FFI::LEDCurrentLimit[:current_limit_20mA]
|
21
|
+
device.voltage = Phidgets::FFI::LEDVoltage[:voltage_3_9V]
|
24
22
|
end
|
25
23
|
|
26
24
|
led.on_detach do |device, obj|
|
@@ -31,6 +29,21 @@ led.on_error do |device, obj, code, description|
|
|
31
29
|
puts "Error #{code}: #{description}"
|
32
30
|
end
|
33
31
|
|
34
|
-
sleep
|
32
|
+
sleep 1
|
33
|
+
|
34
|
+
if(led.attached?)
|
35
|
+
led.leds.each do |i|
|
36
|
+
i.current_limit = 20
|
37
|
+
i.brightness = 50
|
38
|
+
end
|
39
|
+
sleep 1
|
40
|
+
led.leds.each do |i|
|
41
|
+
i.brightness = 100
|
42
|
+
end
|
43
|
+
sleep 1
|
44
|
+
led.leds.each do |i|
|
45
|
+
i.current_limit = 2
|
46
|
+
end
|
47
|
+
end
|
35
48
|
|
36
49
|
led.close
|
data/examples/rfid.rb
CHANGED
@@ -22,7 +22,6 @@ rfid.on_attach do |device, obj|
|
|
22
22
|
rfid.antenna = true
|
23
23
|
rfid.led = true
|
24
24
|
sleep 1
|
25
|
-
|
26
25
|
end
|
27
26
|
|
28
27
|
rfid.on_detach do |device, obj|
|
@@ -55,6 +54,11 @@ if(rfid.attached?)
|
|
55
54
|
begin
|
56
55
|
puts "Tag present: #{rfid.tag_present}"
|
57
56
|
puts "Last tag: #{rfid.last_tag}"
|
57
|
+
puts "Last tag proto: #{rfid.last_tag_protocol}"
|
58
|
+
|
59
|
+
# Example for writing to a tag:
|
60
|
+
#rfid.write("Some tag..", Phidgets::FFI::RFIDTagProtocol[:PhidgetTAG])
|
61
|
+
|
58
62
|
rescue Phidgets::Error::UnknownVal => e
|
59
63
|
puts "Exception caught: #{e.message}"
|
60
64
|
end
|
data/lib/phidgets-ffi/ffi/led.rb
CHANGED
@@ -18,8 +18,10 @@ module Phidgets
|
|
18
18
|
|
19
19
|
attach_function :CPhidgetLED_create, [:phid], :int
|
20
20
|
attach_function :CPhidgetLED_getLEDCount, [:phid, :pointer], :int
|
21
|
-
attach_function :
|
22
|
-
attach_function :
|
21
|
+
attach_function :CPhidgetLED_getBrightness, [:phid, :int, :pointer], :int
|
22
|
+
attach_function :CPhidgetLED_setBrightness, [:phid, :int, :double], :int
|
23
|
+
attach_function :CPhidgetLED_getCurrentLimitIndexed, [:phid, :int, :pointer], :int
|
24
|
+
attach_function :CPhidgetLED_setCurrentLimitIndexed, [:phid, :int, :double], :int
|
23
25
|
attach_function :CPhidgetLED_getCurrentLimit, [:phid, :pointer], :int
|
24
26
|
attach_function :CPhidgetLED_setCurrentLimit, [:phid, LEDCurrentLimit], :int
|
25
27
|
attach_function :CPhidgetLED_getVoltage, [:phid, :pointer], :int
|
@@ -1,6 +1,12 @@
|
|
1
1
|
module Phidgets
|
2
2
|
module FFI
|
3
|
-
|
3
|
+
|
4
|
+
RFIDTagProtocol = enum(
|
5
|
+
:EM4100, 1,
|
6
|
+
:ISO11785_FDX_B,
|
7
|
+
:PhidgetTAG
|
8
|
+
)
|
9
|
+
|
4
10
|
attach_function :CPhidgetRFID_create, [:phid], :int
|
5
11
|
attach_function :CPhidgetRFID_getOutputCount, [:phid, :pointer], :int
|
6
12
|
attach_function :CPhidgetRFID_getOutputState, [:phid, :int, :pointer], :int
|
@@ -9,17 +15,18 @@ module Phidgets
|
|
9
15
|
attach_function :CPhidgetRFID_setAntennaOn, [:phid, :int], :int
|
10
16
|
attach_function :CPhidgetRFID_getLEDOn, [:phid, :pointer], :int
|
11
17
|
attach_function :CPhidgetRFID_setLEDOn, [:phid, :int], :int
|
12
|
-
attach_function :
|
13
|
-
attach_function :CPhidgetRFID_getTagStatus, [:phid, :pointer], :int
|
18
|
+
attach_function :CPhidgetRFID_getLastTag2, [:phid, :pointer, :pointer], :int
|
19
|
+
attach_function :CPhidgetRFID_getTagStatus, [:phid, :pointer], :int
|
20
|
+
attach_function :CPhidgetRFID_write, [:phid, :pointer, RFIDTagProtocol, :int], :int
|
14
21
|
|
15
22
|
callback :CPhidgetRFID_set_OnOutputChange_Callback, [:phid, :user_ptr, :int, :int], :int
|
16
23
|
attach_function :CPhidgetRFID_set_OnOutputChange_Handler, [:phid, :CPhidgetRFID_set_OnOutputChange_Callback, :user_ptr], :int
|
17
24
|
|
18
|
-
callback :
|
19
|
-
attach_function :
|
25
|
+
callback :CPhidgetRFID_set_OnTag2_Callback, [:phid, :user_ptr, :pointer, RFIDTagProtocol], :int
|
26
|
+
attach_function :CPhidgetRFID_set_OnTag2_Handler, [:phid, :CPhidgetRFID_set_OnTag2_Callback, :user_ptr], :int
|
20
27
|
|
21
|
-
callback :
|
22
|
-
attach_function :
|
28
|
+
callback :CPhidgetRFID_set_OnTagLost2_Callback, [:phid, :user_ptr, :pointer, RFIDTagProtocol], :int
|
29
|
+
attach_function :CPhidgetRFID_set_OnTagLost2_Handler, [:phid, :CPhidgetRFID_set_OnTagLost2_Callback, :user_ptr], :int
|
23
30
|
|
24
31
|
module CPhidgetRFID
|
25
32
|
def self.method_missing(method, *args, &block)
|
data/lib/phidgets-ffi/led.rb
CHANGED
@@ -66,24 +66,39 @@ module Phidgets
|
|
66
66
|
"#<#{self.class} @index=#{index}, @brightness=#{brightness}>"
|
67
67
|
end
|
68
68
|
|
69
|
-
# @return [Integer] returns the index of the
|
69
|
+
# @return [Integer] returns the index of the led, or raises an error.
|
70
70
|
def index
|
71
71
|
@index
|
72
72
|
end
|
73
73
|
|
74
|
-
# @return [
|
74
|
+
# @return [Float] returns the brightness level of an LED, or raises an error.
|
75
75
|
def brightness
|
76
|
-
ptr = ::FFI::MemoryPointer.new(:
|
77
|
-
Klass.
|
76
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
77
|
+
Klass.getBrightness(@handle, @index, ptr)
|
78
78
|
ptr.get_int(0)
|
79
79
|
end
|
80
80
|
|
81
81
|
# Sets the brightness level of an LED, or raises an error. Brightness levels range from 0-100
|
82
|
-
# @param [
|
83
|
-
# @return [
|
82
|
+
# @param [Float] new_brightness new brightness
|
83
|
+
# @return [Float] returns the brightness of an LED, or raises an error.
|
84
84
|
def brightness=(new_brightness)
|
85
|
-
Klass.
|
86
|
-
new_brightness.
|
85
|
+
Klass.setBrightness(@handle, @index, new_brightness.to_f)
|
86
|
+
new_brightness.to_f
|
87
|
+
end
|
88
|
+
|
89
|
+
# @return [Float] returns the current limit of an LED, or raises an error.
|
90
|
+
def current_limit
|
91
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
92
|
+
Klass.getCurrentLimitIndexed(@handle, @index, ptr)
|
93
|
+
ptr.get_int(0)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Sets the current limit of an LED, or raises an error. Current Limit levels range from 0-80 mA
|
97
|
+
# @param [Float] new_current_limit new current limit
|
98
|
+
# @return [Float] returns the current limit of an LED, or raises an error.
|
99
|
+
def current_limit=(new_current_limit)
|
100
|
+
Klass.setCurrentLimitIndexed(@handle, @index, new_current_limit.to_f)
|
101
|
+
new_current_limit.to_f
|
87
102
|
end
|
88
103
|
|
89
104
|
end #LEDOutputs
|
data/lib/phidgets-ffi/rfid.rb
CHANGED
@@ -51,10 +51,10 @@ module Phidgets
|
|
51
51
|
# @return [Boolean] returns true or raises an error
|
52
52
|
def on_tag(obj=nil, &block)
|
53
53
|
@on_tag_obj = obj
|
54
|
-
@on_tag = Proc.new { |device, obj_ptr, tag|
|
55
|
-
yield self,
|
54
|
+
@on_tag = Proc.new { |device, obj_ptr, tag, proto|
|
55
|
+
yield self, tag.read_string, object_for(obj_ptr)
|
56
56
|
}
|
57
|
-
Klass.
|
57
|
+
Klass.set_OnTag2_Handler(@handle, @on_tag, pointer_for(obj))
|
58
58
|
end
|
59
59
|
|
60
60
|
# Sets a tag lost handler callback function. This is called when a tag is removed from the reader
|
@@ -69,10 +69,10 @@ module Phidgets
|
|
69
69
|
# @return [Boolean] returns true or raises an error
|
70
70
|
def on_tag_lost(obj=nil, &block)
|
71
71
|
@on_tag_lost_obj = obj
|
72
|
-
@on_tag_lost = Proc.new { |device, obj_ptr, tag|
|
73
|
-
yield self,
|
72
|
+
@on_tag_lost = Proc.new { |device, obj_ptr, tag, proto|
|
73
|
+
yield self, tag.read_string, object_for(obj_ptr)
|
74
74
|
}
|
75
|
-
Klass.
|
75
|
+
Klass.set_OnTagLost2_Handler(@handle, @on_tag_lost, pointer_for(obj))
|
76
76
|
end
|
77
77
|
|
78
78
|
# Returns the antenna state of the Phidget.
|
@@ -115,11 +115,21 @@ module Phidgets
|
|
115
115
|
#
|
116
116
|
# @return [String] returns the last tag or raises an error
|
117
117
|
def last_tag
|
118
|
-
|
119
|
-
|
120
|
-
Klass.
|
121
|
-
|
122
|
-
|
118
|
+
tag = ::FFI::MemoryPointer.new(:string)
|
119
|
+
proto = ::FFI::MemoryPointer.new(:int)
|
120
|
+
Klass.getLastTag2(@handle, tag, proto)
|
121
|
+
strPtr = tag.get_pointer(0)
|
122
|
+
strPtr.null? ? nil : strPtr.read_string
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns the protocol of the last tag read by the reader. This may or may not still be on the reader - use {Phidgets::RFID#tag_present} to find out.
|
126
|
+
#
|
127
|
+
# @return [Phidgets::FFI::RFIDTagProtocol] returns the last tag protocol or raises an error
|
128
|
+
def last_tag_protocol
|
129
|
+
tag = ::FFI::MemoryPointer.new(:string)
|
130
|
+
proto = ::FFI::MemoryPointer.new(:int)
|
131
|
+
Klass.getLastTag2(@handle, tag, proto)
|
132
|
+
Phidgets::FFI::RFIDTagProtocol[proto.get_int(0)]
|
123
133
|
end
|
124
134
|
|
125
135
|
# Returns the value indicating whether or not a tag is on the reader.
|
@@ -131,6 +141,18 @@ module Phidgets
|
|
131
141
|
(ptr.get_int(0) == 0) ? false : true
|
132
142
|
end
|
133
143
|
|
144
|
+
# Writes to a tag.
|
145
|
+
#
|
146
|
+
# @param [String] tag Tag data to write. See product manual for formatting.
|
147
|
+
# @param [Phidgets::FFI::RFIDTagProtocol] protocol Tag Protocol to use.
|
148
|
+
# @param [Boolean] lock Lock the tag from further writes
|
149
|
+
# @return [Boolean] returns true or raises an error
|
150
|
+
def write(tag, protocol, lock=false)
|
151
|
+
tmp = lock ? 1 : 0
|
152
|
+
Klass.write(@handle, tag, Phidgets::FFI::RFIDTagProtocol[protocol], tmp)
|
153
|
+
true
|
154
|
+
end
|
155
|
+
|
134
156
|
# This class represents an digital output for a PhidgetRFID All the properties of an digital output are stored and modified in this class.
|
135
157
|
class RFIDOutputs
|
136
158
|
Klass = Phidgets::FFI::CPhidgetRFID
|
@@ -197,22 +219,10 @@ module Phidgets
|
|
197
219
|
end
|
198
220
|
end
|
199
221
|
|
200
|
-
def convert_int_to_hex(int_tag)
|
201
|
-
tag_string = []
|
202
|
-
5.times { |i|
|
203
|
-
if int_tag[i].get_uchar(0).to_s(16).size == 1 #ruby removes leading 0, so we will put the 0 back in.
|
204
|
-
tag_string[i] = "0#{int_tag[i].get_uchar(0).to_s(16)}"
|
205
|
-
else
|
206
|
-
tag_string[i] = int_tag[i].get_uchar(0).to_s(16)
|
207
|
-
end
|
208
|
-
}
|
209
|
-
tag_string
|
210
|
-
end
|
211
|
-
|
212
222
|
def remove_specific_event_handlers
|
213
223
|
Klass.set_OnOutputChange_Handler(@handle, nil, nil)
|
214
|
-
Klass.
|
215
|
-
Klass.
|
224
|
+
Klass.set_OnTag2_Handler(@handle, nil, nil)
|
225
|
+
Klass.set_OnTagLost2_Handler(@handle, nil, nil)
|
216
226
|
end
|
217
227
|
|
218
228
|
end
|
data/lib/phidgets-ffi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phidgets-ffi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: 1.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.0
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: ffi
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,7 +37,12 @@ dependencies:
|
|
32
37
|
version: 1.0.9
|
33
38
|
type: :runtime
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.9
|
36
46
|
description: FFI Bindings for the Phidget Library
|
37
47
|
email:
|
38
48
|
- support@phidgets.com
|
@@ -45,6 +55,7 @@ files:
|
|
45
55
|
- LICENSE
|
46
56
|
- README.rdoc
|
47
57
|
- Rakefile
|
58
|
+
- examples/HelloWorld.rb
|
48
59
|
- examples/accelerometer.rb
|
49
60
|
- examples/advanced_servo.rb
|
50
61
|
- examples/analog.rb
|
@@ -140,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
151
|
version: 1.3.6
|
141
152
|
requirements: []
|
142
153
|
rubyforge_project: phidgets-ffi
|
143
|
-
rubygems_version: 1.8.
|
154
|
+
rubygems_version: 1.8.23
|
144
155
|
signing_key:
|
145
156
|
specification_version: 3
|
146
157
|
summary: FFI Bindings for the Phidget Library
|