phidgets-ffi 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
- =begin
1
+ =begin
2
2
  Phidget Hello World Program for all devices
3
3
  (c) Phidgets 2012
4
4
  =end
5
-
5
+
6
6
  require 'rubygems'
7
7
  require 'phidgets-ffi'
8
8
 
@@ -31,6 +31,6 @@ puts 'Phidget Simple Playground (plug and unplug devices)'
31
31
  puts 'Please Enter to end anytime...'
32
32
 
33
33
  gets.chomp
34
-
34
+
35
35
  puts 'Closing...'
36
36
  manager.close
@@ -3,15 +3,14 @@ require 'phidgets-ffi'
3
3
 
4
4
  puts "Library Version: #{Phidgets::FFI.library_version}"
5
5
 
6
- accel = Phidgets::Accelerometer.new
6
+ accel = Phidgets::Accelerometer.new
7
7
 
8
8
  puts "Wait for PhidgetAccelerometer to attached..."
9
-
9
+
10
10
  #The following method runs when the PhidgetAccelerometer is attached to the system
11
11
  accel.on_attach do |device, obj|
12
-
13
- puts "Device attributes: #{device.attributes} attached"
14
- puts "Class: #{device.device_class}"
12
+ puts "Device attributes: #{device.attributes} attached"
13
+ puts "Class: #{device.device_class}"
15
14
  puts "Id: #{device.id}"
16
15
  puts "Serial number: #{device.serial_number}"
17
16
  puts "Version: #{device.version}"
@@ -22,7 +21,7 @@ accel.on_attach do |device, obj|
22
21
  puts "Acceleration min[0]: #{device.axes[0].acceleration_min}"
23
22
  puts "Acceleration max[0]: #{device.axes[0].acceleration_max}"
24
23
  end
25
-
24
+
26
25
  accel.on_detach do |device, obj|
27
26
  puts "#{device.attributes.inspect} detached"
28
27
  end
@@ -33,7 +32,7 @@ end
33
32
 
34
33
  accel.on_acceleration_change do |device, axis, value|
35
34
  puts "Axis #{axis.index}'s acceleration has changed to #{value}"
36
- end
35
+ end
37
36
 
38
37
  sleep 10
39
38
  accel.close
@@ -4,14 +4,13 @@ require 'phidgets-ffi'
4
4
  puts "Library Version: #{Phidgets::FFI.library_version}"
5
5
 
6
6
  adv = Phidgets::AdvancedServo.new
7
-
7
+
8
8
  puts "Wait for PhidgetAdvancedServo to attached..."
9
9
 
10
10
  #The following method runs when the PhidgetAdvancedServo is attached to the system
11
11
  adv.on_attach do |device, obj|
12
-
13
- puts "Device attributes: #{device.attributes} attached"
14
- puts "Class: #{device.device_class}"
12
+ puts "Device attributes: #{device.attributes} attached"
13
+ puts "Class: #{device.device_class}"
15
14
  puts "Id: #{device.id}"
16
15
  puts "Serial number: #{device.serial_number}"
17
16
  puts "Version: #{device.version}"
@@ -19,9 +18,8 @@ adv.on_attach do |device, obj|
19
18
 
20
19
  device.advanced_servos[0].engaged = true
21
20
  sleep 1 #allow time for engaged to be set before event ends
22
-
23
21
  end
24
-
22
+
25
23
  adv.on_detach do |device, obj|
26
24
  puts "#{device.attributes.inspect} detached"
27
25
  end
@@ -45,7 +43,6 @@ end
45
43
  sleep 3
46
44
 
47
45
  if(adv.attached?)
48
-
49
46
  max = adv.advanced_servos[0].position_max
50
47
  3.times do
51
48
  adv.advanced_servos[0].position = rand(max)
@@ -53,7 +50,7 @@ if(adv.attached?)
53
50
  end
54
51
 
55
52
  puts "Setting servo parameters: #{adv.advanced_servos[0].set_servo_parameters(600, 2000, 120, 1500)}"
56
-
53
+
57
54
  puts "Acceleration: #{adv.advanced_servos[0].acceleration}"
58
55
  puts "Acceleration min: #{adv.advanced_servos[0].acceleration_min}"
59
56
  puts "Acceleration max: #{adv.advanced_servos[0].acceleration_max}"
@@ -77,7 +74,7 @@ if(adv.attached?)
77
74
  adv.advanced_servos[0].type = Phidgets::FFI::AdvancedServoType[:default]
78
75
  puts "Type: #{adv.advanced_servos[0].type}"
79
76
 
80
- begin
77
+ begin
81
78
  puts "Position: #{adv.advanced_servos[0].position}" #An error is raised when the position is unknown
82
79
  rescue Phidgets::Error::UnknownVal => e
83
80
  puts "Exception caught: #{e.message}"
@@ -85,7 +82,6 @@ if(adv.attached?)
85
82
 
86
83
  puts "Position max: #{adv.advanced_servos[0].position_max}"
87
84
  puts "Position min: #{adv.advanced_servos[0].position_min}"
88
-
89
85
  end
90
86
 
91
87
  sleep 2
@@ -4,14 +4,13 @@ require 'phidgets-ffi'
4
4
  puts "Library Version: #{Phidgets::FFI.library_version}"
5
5
 
6
6
  an = Phidgets::Analog.new
7
-
7
+
8
8
  puts "Wait for PhidgetAnalog to attached..."
9
-
9
+
10
10
  #The following method runs when the PhidgetAnalog is attached to the system
11
11
  an.on_attach do |device, obj|
12
-
13
- puts "Device attributes: #{device.attributes} attached"
14
- puts "Class: #{device.device_class}"
12
+ puts "Device attributes: #{device.attributes} attached"
13
+ puts "Class: #{device.device_class}"
15
14
  puts "Id: #{device.id}"
16
15
  puts "Serial number: #{device.serial_number}"
17
16
  puts "Version: #{device.version}"
@@ -19,17 +18,17 @@ an.on_attach do |device, obj|
19
18
 
20
19
  puts "Voltage min: #{device.outputs[0].voltage_min}"
21
20
  puts "Voltage max: #{device.outputs[0].voltage_max}"
22
-
21
+
23
22
  device.outputs[0].enabled = true
24
23
  sleep 1
25
24
  device.outputs[0].voltage = 2.2
26
-
25
+
27
26
  sleep 3
28
27
 
29
- device.outputs[0].enabled = false
30
- sleep 1
28
+ device.outputs[0].enabled = false
29
+ sleep 1
31
30
  end
32
-
31
+
33
32
  an.on_detach do |device, obj|
34
33
  puts "#{device.attributes.inspect} detached"
35
34
  end
@@ -6,12 +6,11 @@ puts "Library Version: #{Phidgets::FFI.library_version}"
6
6
  br = Phidgets::Bridge.new
7
7
 
8
8
  puts "Wait for PhidgetBridge to attached..."
9
-
9
+
10
10
  #The following method runs when the PhidgetBridge is attached to the system
11
11
  br.on_attach do |device, obj|
12
-
13
- puts "Device attributes: #{device.attributes} attached"
14
- puts "Class: #{device.device_class}"
12
+ puts "Device attributes: #{device.attributes} attached"
13
+ puts "Class: #{device.device_class}"
15
14
  puts "Id: #{device.id}"
16
15
  puts "Serial number: #{device.serial_number}"
17
16
  puts "Version: #{device.version}"
@@ -22,12 +21,11 @@ br.on_attach do |device, obj|
22
21
  device.data_rate = 64
23
22
 
24
23
  sleep 1
25
-
24
+
26
25
  puts "Enabled: #{device.inputs[0].enabled}"
27
26
  puts "Gain: #{device.inputs[0].gain}"
28
-
29
27
  end
30
-
28
+
31
29
  br.on_detach do |device, obj|
32
30
  puts "#{device.attributes.inspect} detached"
33
31
  end
@@ -38,20 +36,20 @@ end
38
36
 
39
37
  br.on_bridge_data do |device, inputs, value|
40
38
  puts "Bridge #{inputs.index}'s value changed to #{value}"
41
- end
39
+ end
42
40
 
43
41
  sleep 5
44
42
 
45
43
  if(br.attached?)
46
44
  5.times do
47
- begin
48
- puts "Bridge Value[0]: #{br.inputs[0].bridge_value}"
45
+ begin
46
+ puts "Bridge Value[0]: #{br.inputs[0].bridge_value}"
49
47
  rescue Phidgets::Error::UnknownVal => e
50
48
  puts "Exception caught: #{e.message}"
51
49
  end
52
- sleep 0.5
50
+ sleep 0.5
53
51
  end
54
52
  end
55
53
 
56
54
  sleep 5
57
- br.close
55
+ br.close
@@ -8,34 +8,37 @@ puts "Library Version: #{Phidgets::FFI.library_version}"
8
8
  dict = Phidgets::Dictionary.new
9
9
 
10
10
  dict.on_error do |obj, code, reason|
11
- puts "Error (#{code}): #{reason}"
12
- end
13
-
14
- dict.on_connect do |obj|
15
- puts "Connected!"
16
- #Listening for every pattern
17
- dict.on_change do |obj, key, val, reason|
18
- puts "Every key: #{key} => #{val}-- #{reason}"
19
- end
20
-
21
- dict.on_change('snoop') do |obj, key, val, reason|
22
- puts "Keys matching snoop: #{key} => #{val}-- #{reason}"
23
- end
24
-
25
- #Patterns an also be of ruby type: Regexp
26
- dict.on_change(/test.*/) do |obj, key, val, reason|
27
- puts "Keys matching /test.*/: #{key} => #{val}-- #{reason}"
28
- end
11
+ puts "Error (#{code}): #{reason}"
12
+ end
13
+
14
+ dict.on_connect do |obj|
15
+ puts "Connected!"
16
+
17
+ #Listening for every pattern
18
+ dict.on_change do |obj, key, val, reason|
19
+ puts "Every key: #{key} => #{val}-- #{reason}"
20
+ end
21
+
22
+ dict.on_change('snoop') do |obj, key, val, reason|
23
+ puts "Keys matching snoop: #{key} => #{val}-- #{reason}"
24
+ end
25
+
26
+ #Patterns an also be of ruby type: Regexp
27
+ dict.on_change(/test.*/) do |obj, key, val, reason|
28
+ puts "Keys matching /test.*/: #{key} => #{val}-- #{reason}"
29
29
  end
30
+ end
31
+
32
+ dict.on_disconnect do |obj|
33
+ puts "Disconnected!"
34
+ end
30
35
 
31
- dict.on_disconnect do |obj|
32
- puts "Disconnected!"
33
- end
34
-
35
36
  options = {:address => 'localhost', :port => 5001, :password => nil}
37
+
36
38
  dict.open(options)
37
-
39
+
38
40
  sleep 5
41
+
39
42
  if dict.status.to_s == 'connected'
40
43
  puts "Listening to: #{dict.listeners.inspect}"
41
44
 
@@ -47,13 +50,13 @@ if dict.status.to_s == 'connected'
47
50
  dict["fnord"] = {:value => "This is a fnordic key", :persistent => true}
48
51
  dict["test"] = {:value => "This is a key that is called test", :persistent => false}
49
52
  dict["test"] = nil
50
-
53
+
51
54
  20.times do |i|
52
55
  print "#{i}\n"
53
56
  begin
54
57
  dict["snoop"] = {:value => "value: #{i}"}
55
58
  dict["testerson"] = {:value => "This is another test matching key"}
56
-
59
+
57
60
  rescue Phidgets::Error::NetworkNotConnected
58
61
  end
59
62
  sleep 1
@@ -4,39 +4,38 @@ require 'phidgets-ffi'
4
4
  puts "Library Version: #{Phidgets::FFI.library_version}"
5
5
 
6
6
  en = Phidgets::Encoder.new
7
-
7
+
8
8
  puts "Wait for PhidgetEncoder to attached..."
9
9
 
10
10
  #The following method runs when the PhidgetEncoder is attached to the system
11
11
  en.on_attach do |device, obj|
12
-
13
- puts "Device attributes: #{device.attributes} attached"
14
- puts "Class: #{device.device_class}"
12
+ puts "Device attributes: #{device.attributes} attached"
13
+ puts "Class: #{device.device_class}"
15
14
  puts "Id: #{device.id}"
16
15
  puts "Serial number: #{device.serial_number}"
17
16
  puts "Version: #{device.version}"
18
- puts "# Digital Inputs: #{en.inputs.size}"
17
+ puts "# Digital Inputs: #{en.inputs.size}"
19
18
  puts "# Encoders: #{device.encoders.size}"
20
19
 
21
- begin
20
+ begin
22
21
  en.encoders[0].enabled = true #some encoders do not support the enable feature
23
22
  rescue Phidgets::Error::Unsupported => e
24
23
  puts "Exception caught: #{e.message}"
25
24
  end
26
-
25
+
27
26
  sleep 1
28
27
 
29
28
  en.encoders[0].position = 200
30
-
29
+
31
30
  begin
32
31
  puts "Index Position: #{en.encoders[0].index_position}"
33
32
  rescue Phidgets::Error::UnknownVal => e
34
33
  puts "Exception caught: #{e.message}"
35
34
  end
36
-
35
+
37
36
  puts "Position: #{en.encoders[0].position}"
38
37
  end
39
-
38
+
40
39
  en.on_detach do |device, obj|
41
40
  puts "#{device.attributes.inspect} detached"
42
41
  end
@@ -46,12 +45,16 @@ en.on_error do |device, obj, code, description|
46
45
  end
47
46
 
48
47
  en.on_input_change do |device, input, state, obj|
49
- puts "Input #{input.index}'s state has changed to #{state}"
48
+ puts "Input #{input.index}'s state has changed to #{state}"
50
49
  end
51
50
 
52
51
  en.on_position_change do |device, encoder, time, position_change, obj|
52
+ encoder = "Encoder #{encoder.index} -- "
53
+ position = "Position: #{device.encoders[encoder.index].position} -- "
54
+ relative = "Relative Change: #{position_change} -- "
55
+ elapsed_time = "Elapsed Time: #{time}"
53
56
 
54
- puts "Encoder #{encoder.index} -- Position: #{device.encoders[encoder.index].position} -- Relative Change: #{position_change} -- Elapsed Time: #{time}"
57
+ puts "#{encoder} #{position} #{relative} #{elapsed_time}"
55
58
  end
56
59
 
57
60
  sleep 10
@@ -6,31 +6,30 @@ puts "Library Version: #{Phidgets::FFI.library_version}"
6
6
  fc = Phidgets::FrequencyCounter.new
7
7
 
8
8
  puts "Wait for PhidgetFrequncyCounter to attached..."
9
-
9
+
10
10
  #The following method runs when the PhidgetFrequencyCounter is attached to the system
11
11
  fc.on_attach do |device, obj|
12
-
13
- puts "Device attributes: #{device.attributes} attached"
14
- puts "Class: #{device.device_class}"
12
+ puts "Device attributes: #{device.attributes} attached"
13
+ puts "Class: #{device.device_class}"
15
14
  puts "Id: #{device.id}"
16
15
  puts "Serial number: #{device.serial_number}"
17
16
  puts "Version: #{device.version}"
18
17
  puts "# Inputs: #{device.inputs.size}"
19
18
 
20
- device.inputs[0].enabled = true
21
-
19
+ device.inputs[0].enabled = true
20
+
22
21
  device.inputs[0].filter_type = Phidgets::FFI::FrequencyCounterFilterTypes[:filter_type_zero_crossing]
23
22
 
24
23
  device.inputs[0].timeout = 100018
25
24
 
26
25
  sleep 1
27
-
26
+
28
27
  puts "Enabled: #{device.inputs[0].enabled}"
29
28
  puts "Filter Type: #{device.inputs[0].filter_type}"
30
29
  puts "Timeout: #{device.inputs[0].timeout}"
31
30
 
32
31
  end
33
-
32
+
34
33
  fc.on_detach do |device, obj|
35
34
  puts "#{device.attributes.inspect} detached"
36
35
  end
@@ -41,13 +40,13 @@ end
41
40
 
42
41
  fc.on_count do |device, input, time, count, obj|
43
42
  puts "Channel #{input.index}: #{count} pulses in #{time} microseconds"
44
- end
43
+ end
45
44
 
46
45
  sleep 5
47
46
 
48
47
  if(fc.attached?)
49
48
  5.times do
50
- begin
49
+ begin
51
50
  puts "Frequency[0]: #{fc.inputs[0].frequency}"
52
51
  puts "Total count: #{fc.inputs[0].total_count}"
53
52
  puts "Total time: #{fc.inputs[0].total_time} microseconds"
@@ -55,9 +54,9 @@ if(fc.attached?)
55
54
  rescue Phidgets::Error::UnknownVal => e
56
55
  puts "Exception caught: #{e.message}"
57
56
  end
58
- sleep 0.5
57
+ sleep 0.5
59
58
  end
60
59
  end
61
60
 
62
61
  sleep 1
63
- fc.close
62
+ fc.close
@@ -6,20 +6,18 @@ puts "Library Version: #{Phidgets::FFI.library_version}"
6
6
  gps = Phidgets::GPS.new
7
7
 
8
8
  puts "Wait for PhidgetGPS to attached..."
9
-
9
+
10
10
  #The following method runs when the PhidgetGPS is attached to the system
11
11
  gps.on_attach do |device, obj|
12
-
13
- puts "Device attributes: #{device.attributes} attached"
14
- puts "Class: #{device.device_class}"
12
+ puts "Device attributes: #{device.attributes} attached"
13
+ puts "Class: #{device.device_class}"
15
14
  puts "Id: #{device.id}"
16
15
  puts "Serial number: #{device.serial_number}"
17
16
  puts "Version: #{device.version}"
18
-
17
+
19
18
  puts "Waiting for position fix status to be acquired"
20
-
21
19
  end
22
-
20
+
23
21
  gps.on_detach do |device, obj|
24
22
  puts "#{device.attributes.inspect} detached"
25
23
  end
@@ -40,24 +38,24 @@ sleep 5
40
38
 
41
39
  if(gps.attached?)
42
40
  5.times do
43
- begin
41
+ begin
44
42
  puts "Latitude: #{gps.latitude} degrees"
45
43
  rescue Phidgets::Error::UnknownVal => e
46
44
  puts "Exception caught: #{e.message}"
47
45
  end
48
46
 
49
47
  begin
50
- puts "Longitude: #{gps.longitude} degrees"
48
+ puts "Longitude: #{gps.longitude} degrees"
51
49
  rescue Phidgets::Error::UnknownVal => e
52
50
  puts "Exception caught: #{e.message}"
53
51
  end
54
-
52
+
55
53
  begin
56
- puts "Altitude: #{gps.altitude} m"
54
+ puts "Altitude: #{gps.altitude} m"
57
55
  rescue Phidgets::Error::UnknownVal => e
58
56
  puts "Exception caught: #{e.message}"
59
57
  end
60
-
58
+
61
59
  begin
62
60
  puts "Heading: #{gps.heading} degrees"
63
61
  rescue Phidgets::Error::UnknownVal => e
@@ -69,23 +67,23 @@ if(gps.attached?)
69
67
  rescue Phidgets::Error::UnknownVal => e
70
68
  puts "Exception caught: #{e.message}"
71
69
  end
72
-
70
+
73
71
  begin
74
72
  puts "GPS Time(UTC): #{gps.time[:hours]}:#{gps.time[:minutes]}:#{gps.time[:seconds]}:#{gps.time[:milliseconds]}"
75
73
  rescue Phidgets::Error::UnknownVal => e
76
74
  puts "Exception caught: #{e.message}"
77
75
  end
78
-
76
+
79
77
  begin
80
78
  puts "GPS Date(UTC): #{gps.date[:month]}/#{gps.date[:day]}/#{gps.date[:year]}"
81
79
  rescue Phidgets::Error::UnknownVal => e
82
80
  puts "Exception caught: #{e.message}"
83
81
  end
84
-
82
+
85
83
  puts ''
86
- sleep 0.5
84
+ sleep 0.5
87
85
  end
88
86
  end
89
87
 
90
88
  sleep 50
91
- gps.close
89
+ gps.close