phidgets-ffi 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +3 -0
  2. data/LICENSE +1 -1
  3. data/README.rdoc +92 -42
  4. data/examples/accelerometer.rb +39 -0
  5. data/examples/advanced_servo.rb +94 -0
  6. data/examples/analog.rb +43 -0
  7. data/examples/bridge.rb +57 -0
  8. data/examples/dictionary.rb +46 -31
  9. data/examples/encoder.rb +59 -0
  10. data/examples/frequency_counter.rb +63 -0
  11. data/examples/gps.rb +91 -0
  12. data/examples/interface_kit_with_block.rb +68 -0
  13. data/examples/interface_kit_without_block.rb +60 -0
  14. data/examples/ir.rb +157 -0
  15. data/examples/led.rb +36 -0
  16. data/examples/manager.rb +16 -10
  17. data/examples/motor_control.rb +108 -0
  18. data/examples/{ffi → raw-ffi}/dictionary.rb +11 -1
  19. data/examples/{ffi → raw-ffi}/interface_kit.rb +19 -2
  20. data/examples/{ffi → raw-ffi}/library_version.rb +0 -0
  21. data/examples/{ffi → raw-ffi}/log.rb +0 -0
  22. data/examples/{ffi → raw-ffi}/manager.rb +6 -3
  23. data/examples/rfid.rb +63 -0
  24. data/examples/servo.rb +45 -30
  25. data/examples/spatial.rb +75 -0
  26. data/examples/stepper.rb +87 -0
  27. data/examples/temperature_sensor.rb +49 -0
  28. data/examples/text_lcd.rb +101 -0
  29. data/lib/phidgets-ffi.rb +34 -3
  30. data/lib/phidgets-ffi/accelerometer.rb +122 -0
  31. data/lib/phidgets-ffi/advanced_servo.rb +304 -0
  32. data/lib/phidgets-ffi/analog.rb +111 -0
  33. data/lib/phidgets-ffi/bridge.rb +167 -0
  34. data/lib/phidgets-ffi/common.rb +506 -103
  35. data/lib/phidgets-ffi/dictionary.rb +136 -23
  36. data/lib/phidgets-ffi/encoder.rb +196 -0
  37. data/lib/phidgets-ffi/error.rb +8 -3
  38. data/lib/phidgets-ffi/ffi/accelerometer.rb +30 -0
  39. data/lib/phidgets-ffi/ffi/advanced_servo.rb +73 -0
  40. data/lib/phidgets-ffi/ffi/analog.rb +29 -0
  41. data/lib/phidgets-ffi/ffi/bridge.rb +44 -0
  42. data/lib/phidgets-ffi/ffi/common.rb +51 -34
  43. data/lib/phidgets-ffi/ffi/constants.rb +3 -1
  44. data/lib/phidgets-ffi/ffi/dictionary.rb +25 -20
  45. data/lib/phidgets-ffi/ffi/encoder.rb +32 -0
  46. data/lib/phidgets-ffi/ffi/frequency_counter.rb +38 -0
  47. data/lib/phidgets-ffi/ffi/gps.rb +32 -0
  48. data/lib/phidgets-ffi/ffi/interface_kit.rb +26 -23
  49. data/lib/phidgets-ffi/ffi/ir.rb +50 -0
  50. data/lib/phidgets-ffi/ffi/led.rb +40 -0
  51. data/lib/phidgets-ffi/ffi/log.rb +7 -6
  52. data/lib/phidgets-ffi/ffi/manager.rb +35 -20
  53. data/lib/phidgets-ffi/ffi/motor_control.rb +66 -0
  54. data/lib/phidgets-ffi/ffi/rfid.rb +36 -0
  55. data/lib/phidgets-ffi/ffi/servo.rb +16 -15
  56. data/lib/phidgets-ffi/ffi/spatial.rb +40 -0
  57. data/lib/phidgets-ffi/ffi/stepper.rb +56 -0
  58. data/lib/phidgets-ffi/ffi/temperature_sensor.rb +42 -0
  59. data/lib/phidgets-ffi/ffi/text_lcd.rb +55 -0
  60. data/lib/phidgets-ffi/frequency_counter.rb +148 -0
  61. data/lib/phidgets-ffi/gps.rb +181 -0
  62. data/lib/phidgets-ffi/interface_kit.rb +205 -92
  63. data/lib/phidgets-ffi/ir.rb +290 -0
  64. data/lib/phidgets-ffi/led.rb +112 -0
  65. data/lib/phidgets-ffi/log.rb +14 -2
  66. data/lib/phidgets-ffi/manager.rb +143 -26
  67. data/lib/phidgets-ffi/motor_control.rb +497 -0
  68. data/lib/phidgets-ffi/phidgets-ffi.rb +15 -2
  69. data/lib/phidgets-ffi/rfid.rb +220 -0
  70. data/lib/phidgets-ffi/servo.rb +103 -61
  71. data/lib/phidgets-ffi/spatial.rb +306 -0
  72. data/lib/phidgets-ffi/stepper.rb +370 -0
  73. data/lib/phidgets-ffi/temperature_sensor.rb +157 -0
  74. data/lib/phidgets-ffi/text_lcd.rb +298 -0
  75. data/lib/phidgets-ffi/version.rb +1 -1
  76. data/phidgets-ffi.gemspec +2 -2
  77. metadata +89 -76
  78. data/examples/ffi/servo.rb +0 -67
  79. data/examples/interface_kit.rb +0 -20
data/.gitignore CHANGED
@@ -1,3 +1,6 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ .svn/
5
+ .yardoc/
6
+ .doc/
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011, Kelley Reynolds
1
+ Copyright (c) 2011, Kelley Reynolds, Phidgets
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
data/README.rdoc CHANGED
@@ -5,47 +5,104 @@ available from http://www.phidgets.com.
5
5
 
6
6
  =Phidgets-FFI
7
7
 
8
- This is a set of FFI bindings for the Phidgets library available for most platforms.
9
- Currently, it only supports the devices available to me but it to but it supports them completely.
10
- It also supports the manager, dictionary, logging, callbacks function perfectly, and there
11
- are a set of idiomatic bindings that take the provided API and make it more accessible. Raw FFI
12
- examples are in examples/ffi and the idiomatic versions are in examples/.
8
+ This is a set of FFI bindings for the Phidgets library available for Mac OS X and Linux.
9
+ There are a set of idiomatic bindings that take the provided API and make it more accessible.
10
+ Idiomatic examples are in examples/ and the Raw FFI examples are in examples/raw-ffi.
13
11
 
14
- ==Quick Start
12
+ ==Environment and Libraries
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.
15
14
 
16
- To access the manager and see your attached devices:
15
+ We also recommend that you download the following reference materials:
16
+ * {Programming Manual}[http://www.phidgets.com/programming_resources.php]:
17
+ * The {Product Manual}[http://www.phidgets.com] for your device
18
+ * {RubyDoc API Documentation}[http://rubydoc.info/gems/phidgets-ffi/frames]
19
+ * {Ruby code samples}[https://github.com/kreynolds/phidgets-ffi/tree/master/examples]
17
20
 
18
- Phidgets::Manager.new do |manager|
19
- puts manager.devices.inspect
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 in the Programming Manual. The Product manual 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 manuals open while working through these instructions.
22
+
23
+ ==Installing the phidgets-ffi gem
24
+ To install:
25
+ gem install phidgets-ffi
26
+
27
+ phidgets-ffi is dependent on the ffi(>=1.0.9) gem. If the above command does not automatically install the gem for you, please enter:
28
+ gem install ffi
29
+
30
+ ==Coding for your Phidget
31
+ Before you can use the Phidget, you must include a reference to the phidgets-ffi gem:
32
+ require "rubygems"
33
+ require "phidgets-ffi"
34
+
35
+ Afterwards, the Phidget constructor method will need to be called to create the Phidget object. There are two methods of programming with Phidgets in Ruby: with a block and without a block. For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.
36
+
37
+ Without a block:
38
+ ifkit = Phidgets::InterfaceKit.new
39
+
40
+ With a block:
41
+ Phidgets::InterfaceKit.new do |ifkit|
42
+ ...
43
+ end
44
+
45
+ The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of this document will use the InterfaceKit object.
46
+
47
+ ==Connecting to the Phidget
48
+ The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides.
49
+
50
+ For example, the following will try to connect to the first Phidget it finds:
51
+ ifkit = Phidgets::InterfaceKit.new
52
+
53
+ The following will try to connect to a Phidget over the Phidget WebService with a serial number of 99999, and a server id of "myserver":
54
+
55
+ options = {:serial_number => 99999, :server_id => "myserver", :password => nil}
56
+ ifkit = Phidgets::InterfaceKit.new(options)
57
+
58
+ One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget Webservice
59
+
60
+ Phidgets::InterfaceKit.new will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected.
61
+
62
+ ==Non Block Programming
63
+ For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and tracking the on_attach and on_detach events, or by calling: wait_for_attachment. wait_for_attachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.
64
+
65
+ ifkit = Phidgets::InterfaceKit.new
66
+ ifkit.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected
67
+
68
+ Please also remember to call close at the end or the program to free any locks on the Phidget
69
+
70
+ ifkit.close
71
+
72
+ ==Block Programming
73
+ If you are programming inside a block, wait_for_attachment is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds. Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.
74
+
75
+ options = {:timeout => 2000)
76
+ Phidgets::InterfaceKit.new(options) do |ifkit|
77
+ ...
20
78
  end
21
-
22
- [{
23
- :type=>"PhidgetServo",
24
- :label=>"",
25
- :device_id=>:servo_1motor,
26
- :serial_number=>12345,
27
- :name=>"Phidget Servo Controller 1-motor",
28
- :device_class=>:servo,
29
- :version=>313
30
- }]
31
-
32
- To access that servo controller and do something with the servo:
33
-
34
- Phidgets::ServoController.new(12345) do |controller|
35
- servo = controller.servos[0]
36
-
37
- controller.on_change do |servo, position, data|
38
- print "Moving servo #{servo.index} to #{position}\n"
39
- end
40
-
41
- max = servo.max
42
- 10.times do
43
- servo.position = rand(max)
44
- sleep 1
45
- end
46
- servo.engaged = false
79
+
80
+ ==Event Driven Programming
81
+
82
+ We recommend the use of event driven programming when working with Phidgets. In Ruby, we can hook an event handler at loading with the following code:
83
+
84
+ ifkit.on_sensor_change do |device, input, value, obj|
85
+ puts "Sensor #{input.index}'s value has changed to #{value}"
47
86
  end
48
87
 
88
+ With this method, the block inside on_sensor_change will get executed every time the
89
+ PhidgetInterfaceKit reports a change on one of its analog inputs. Some events such as on_attach and on_detach belong to the base Phidget object and thus are common to all types of Phidgets. Please refer to the RubyDoc API documentation for a full list of events and their usage.
90
+
91
+ ==Working directly with the Phidget
92
+ Some values can be read and sent directly to the Phidget, simply use the instance members and properties. This is also how you would set properties on the Phidget such as the output state or sensor sensitivity. These functions can be used inside a polling loop as an alternative to event driven programming.
93
+
94
+ puts "Serial number: #{ifkit.serial_number}"
95
+ puts "Sensor value[0]: #{ifkit.sensors[0].value}"
96
+
97
+ ==Working with Multiple Phidgets
98
+ Multiple Phidgets of the same type can easily be run inside the same program. In our case, it requires another InterfaceKit instance to be defined and initialized. The new instance can then be set up, opened and used in the same process as the previous one. If the application needs to distinguish between the devices, the constructor can be called with the serial number or label of a specific Phidget.
99
+
100
+ ==Other Phidgets
101
+ The design given in this document can also be followed for almost all Phidgets. For example, if you were using a PhidgetRFID instead of a PhidgetInterfacekit, you would declare an RFID object instead of an InterfaceKit. The methods and events available would change but they can be accessed in a similar manner.
102
+
103
+ ==Raw FFI
104
+ As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in lib/phidgets-ffi/ffi/ to see a list of available methods, and the {C API}[http://www.phidgets.com/programming_resources.php] for usage. There are raw ffi examples in examples/raw-ffi/.
105
+
49
106
  ==Logging
50
107
 
51
108
  Logging can be accomplished by either accessing the C library directly or using the idiomatic method.
@@ -55,10 +112,3 @@ There are additional examples of each in the examples/ directory.
55
112
  Phidgets::Log.info('identifier', 'message')
56
113
  Phidgets::Log.error('identifier', 'message')
57
114
  Phidgets::Log.disable
58
-
59
- ==Notes
60
-
61
- Again, note that not every device is here. I'm more than happy to add support for it
62
- but I'll need a test device to make sure it works ;)
63
-
64
- P.S. Seriously, contact me if you want my mailing address to send me a device you'd like this library to support.
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'phidgets-ffi'
3
+
4
+ puts "Library Version: #{Phidgets::FFI.library_version}"
5
+
6
+ accel = Phidgets::Accelerometer.new
7
+
8
+ puts "Wait for PhidgetAccelerometer to attached..."
9
+
10
+ #The following method runs when the PhidgetAccelerometer is attached to the system
11
+ accel.on_attach do |device, obj|
12
+
13
+ puts "Device attributes: #{device.attributes} attached"
14
+ puts "Class: #{device.device_class}"
15
+ puts "Id: #{device.id}"
16
+ puts "Serial number: #{device.serial_number}"
17
+ puts "Version: #{device.version}"
18
+ puts "# Axes: #{device.axes.size}"
19
+
20
+ puts device.axes[0].inspect
21
+ puts "Accleration[0]: #{device.axes[0].acceleration}"
22
+ puts "Acceleration min[0]: #{device.axes[0].acceleration_min}"
23
+ puts "Acceleration max[0]: #{device.axes[0].acceleration_max}"
24
+ end
25
+
26
+ accel.on_detach do |device, obj|
27
+ puts "#{device.attributes.inspect} detached"
28
+ end
29
+
30
+ accel.on_error do |device, obj, code, description|
31
+ puts "Error #{code}: #{description}"
32
+ end
33
+
34
+ accel.on_acceleration_change do |device, axis, value|
35
+ puts "Axis #{axis.index}'s acceleration has changed to #{value}"
36
+ end
37
+
38
+ sleep 10
39
+ accel.close
@@ -0,0 +1,94 @@
1
+ require 'rubygems'
2
+ require 'phidgets-ffi'
3
+
4
+ puts "Library Version: #{Phidgets::FFI.library_version}"
5
+
6
+ adv = Phidgets::AdvancedServo.new
7
+
8
+ puts "Wait for PhidgetAdvancedServo to attached..."
9
+
10
+ #The following method runs when the PhidgetAdvancedServo is attached to the system
11
+ adv.on_attach do |device, obj|
12
+
13
+ puts "Device attributes: #{device.attributes} attached"
14
+ puts "Class: #{device.device_class}"
15
+ puts "Id: #{device.id}"
16
+ puts "Serial number: #{device.serial_number}"
17
+ puts "Version: #{device.version}"
18
+ puts "# Servos: #{device.advanced_servos.size}"
19
+
20
+ device.advanced_servos[0].engaged = true
21
+ sleep 1 #allow time for engaged to be set before event ends
22
+
23
+ end
24
+
25
+ adv.on_detach do |device, obj|
26
+ puts "#{device.attributes.inspect} detached"
27
+ end
28
+
29
+ adv.on_error do |device, obj, code, description|
30
+ puts "Error #{code}: #{description}"
31
+ end
32
+
33
+ adv.on_velocity_change do |device, servo, velocity, obj|
34
+ puts "Servo #{servo.index}'s velocity has changed to #{velocity}"
35
+ end
36
+
37
+ adv.on_position_change do |device, servo, position, obj|
38
+ puts "Servo #{servo.index}'s position has changed to #{position}"
39
+ end
40
+
41
+ adv.on_current_change do |device, servo, current, obj|
42
+ puts "Servo #{servo.index}'s current has changed to #{current}"
43
+ end
44
+
45
+ sleep 3
46
+
47
+ if(adv.attached?)
48
+
49
+ max = adv.advanced_servos[0].position_max
50
+ 3.times do
51
+ adv.advanced_servos[0].position = rand(max)
52
+ sleep 0.5
53
+ end
54
+
55
+ puts "Setting servo parameters: #{adv.advanced_servos[0].set_servo_parameters(600, 2000, 120, 1500)}"
56
+
57
+ puts "Acceleration: #{adv.advanced_servos[0].acceleration}"
58
+ puts "Acceleration min: #{adv.advanced_servos[0].acceleration_min}"
59
+ puts "Acceleration max: #{adv.advanced_servos[0].acceleration_max}"
60
+ puts "Current: #{adv.advanced_servos[0].current}"
61
+ puts "Speed ramp: #{adv.advanced_servos[0].speed_ramping}"
62
+ puts "Stopped: #{adv.advanced_servos[0].stopped}"
63
+ puts "Type: #{adv.advanced_servos[0].type}"
64
+ puts "Velocity: #{adv.advanced_servos[0].velocity}"
65
+ sleep 2
66
+
67
+ adv.advanced_servos[0].velocity_limit = 200
68
+ sleep 1
69
+ puts "Velocity limit: #{adv.advanced_servos[0].velocity_limit}"
70
+ puts "Velocity max: #{adv.advanced_servos[0].velocity_max}"
71
+ puts "Velocity min: #{adv.advanced_servos[0].velocity_min}"
72
+
73
+ adv.advanced_servos[0].acceleration = 89
74
+ adv.advanced_servos[0].position_max = 160
75
+ adv.advanced_servos[0].position_min = 5
76
+ adv.advanced_servos[0].speed_ramping = true
77
+ adv.advanced_servos[0].type = Phidgets::FFI::AdvancedServoType[:default]
78
+ puts "Type: #{adv.advanced_servos[0].type}"
79
+
80
+ begin
81
+ puts "Position: #{adv.advanced_servos[0].position}" #An error is raised when the position is unknown
82
+ rescue Phidgets::Error::UnknownVal => e
83
+ puts "Exception caught: #{e.message}"
84
+ end
85
+
86
+ puts "Position max: #{adv.advanced_servos[0].position_max}"
87
+ puts "Position min: #{adv.advanced_servos[0].position_min}"
88
+
89
+ end
90
+
91
+ sleep 2
92
+ adv.advanced_servos[0].engaged = false
93
+ sleep 1
94
+ adv.close
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'phidgets-ffi'
3
+
4
+ puts "Library Version: #{Phidgets::FFI.library_version}"
5
+
6
+ an = Phidgets::Analog.new
7
+
8
+ puts "Wait for PhidgetAnalog to attached..."
9
+
10
+ #The following method runs when the PhidgetAnalog is attached to the system
11
+ an.on_attach do |device, obj|
12
+
13
+ puts "Device attributes: #{device.attributes} attached"
14
+ puts "Class: #{device.device_class}"
15
+ puts "Id: #{device.id}"
16
+ puts "Serial number: #{device.serial_number}"
17
+ puts "Version: #{device.version}"
18
+ puts "# Outputs: #{device.outputs.size}"
19
+
20
+ puts "Voltage min: #{device.outputs[0].voltage_min}"
21
+ puts "Voltage max: #{device.outputs[0].voltage_max}"
22
+
23
+ device.outputs[0].enabled = true
24
+ sleep 1
25
+ device.outputs[0].voltage = 2.2
26
+
27
+ sleep 3
28
+
29
+ device.outputs[0].enabled = false
30
+ sleep 1
31
+ end
32
+
33
+ an.on_detach do |device, obj|
34
+ puts "#{device.attributes.inspect} detached"
35
+ end
36
+
37
+ an.on_error do |device, obj, code, description|
38
+ puts "Error #{code}: #{description}"
39
+ end
40
+
41
+ sleep 5
42
+
43
+ an.close
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'phidgets-ffi'
3
+
4
+ puts "Library Version: #{Phidgets::FFI.library_version}"
5
+
6
+ br = Phidgets::Bridge.new
7
+
8
+ puts "Wait for PhidgetBridge to attached..."
9
+
10
+ #The following method runs when the PhidgetBridge is attached to the system
11
+ br.on_attach do |device, obj|
12
+
13
+ puts "Device attributes: #{device.attributes} attached"
14
+ puts "Class: #{device.device_class}"
15
+ puts "Id: #{device.id}"
16
+ puts "Serial number: #{device.serial_number}"
17
+ puts "Version: #{device.version}"
18
+ puts "# Inputs: #{device.inputs.size}"
19
+
20
+ device.inputs[0].enabled = true
21
+ device.inputs[0].gain = Phidgets::FFI::BridgeGains[:gain_32]
22
+ device.data_rate = 64
23
+
24
+ sleep 1
25
+
26
+ puts "Enabled: #{device.inputs[0].enabled}"
27
+ puts "Gain: #{device.inputs[0].gain}"
28
+
29
+ end
30
+
31
+ br.on_detach do |device, obj|
32
+ puts "#{device.attributes.inspect} detached"
33
+ end
34
+
35
+ br.on_error do |device, obj, code, description|
36
+ puts "Error #{code}: #{description}"
37
+ end
38
+
39
+ br.on_bridge_data do |device, inputs, value|
40
+ puts "Bridge #{inputs.index}'s value changed to #{value}"
41
+ end
42
+
43
+ sleep 5
44
+
45
+ if(br.attached?)
46
+ 5.times do
47
+ begin
48
+ puts "Bridge Value[0]: #{br.inputs[0].bridge_value}"
49
+ rescue Phidgets::Error::UnknownVal => e
50
+ puts "Exception caught: #{e.message}"
51
+ end
52
+ sleep 0.5
53
+ end
54
+ end
55
+
56
+ sleep 5
57
+ br.close
@@ -4,43 +4,58 @@ require 'phidgets-ffi'
4
4
  Phidgets::Log.enable(:verbose)
5
5
 
6
6
  puts "Library Version: #{Phidgets::FFI.library_version}"
7
- Phidgets::Dictionary.new do |dict|
8
- dict.on_error do |obj, code, reason|
7
+
8
+ dict = Phidgets::Dictionary.new
9
+
10
+ dict.on_error do |obj, code, reason|
9
11
  puts "Error (#{code}): #{reason}"
10
12
  end
13
+
11
14
  dict.on_connect do |obj|
12
- print "Connected!\n"
13
- end
14
- dict.on_disconnect do |obj|
15
- print "Disconnected!\n"
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
29
+ end
30
+
31
+ dict.on_disconnect do |obj|
32
+ puts "Disconnected!"
16
33
  end
17
34
 
18
- dict.on_change('fnord') do |obj, key, val, reason|
19
- print "Fnord key: #{key} => #{val}, #{reason}\n"
20
- end
21
- dict.on_change do |obj, key, val, reason|
22
- print "Every key: #{key} => #{val}, #{reason}\n"
23
- end
24
- dict.on_change('test') do |obj, key, val, reason|
25
- print "Keys matching test: #{key} => #{val}, #{reason}\n"
26
- end
35
+ options = {:address => 'localhost', :port => 5001, :password => nil}
36
+ dict.open(options)
37
+
38
+ sleep 5
39
+ if dict.status.to_s == 'connected'
40
+ puts "Listening to: #{dict.listeners.inspect}"
27
41
 
28
- puts "Listening to: #{dict.listeners.inspect}"
42
+ dict.remove_on_change('snoop')
29
43
 
30
- dict.remove_on_change('fnord')
44
+ puts "Listening to: #{dict.listeners.inspect}"
31
45
 
32
- puts "Listening to: #{dict.listeners.inspect}"
33
46
 
34
- dict["fnord"] = "This is a fnordic key"
35
- dict["test"] = "This is a key that is called test"
36
- dict["fnord"] = nil
37
- dict["snoop"] = nil
38
- 20.times do |i|
39
- print "#{i}\n"
40
- begin
41
- dict["testerson"] = "This is another test matching key"
42
- rescue Phidgets::Error::NetworkNotConnected
43
- end
44
- sleep 1
45
- end
46
- end
47
+ dict["fnord"] = {:value => "This is a fnordic key", :persistent => true}
48
+ dict["test"] = {:value => "This is a key that is called test", :persistent => false}
49
+ dict["test"] = nil
50
+
51
+ 20.times do |i|
52
+ print "#{i}\n"
53
+ begin
54
+ dict["snoop"] = {:value => "value: #{i}"}
55
+ dict["testerson"] = {:value => "This is another test matching key"}
56
+
57
+ rescue Phidgets::Error::NetworkNotConnected
58
+ end
59
+ sleep 1
60
+ end
61
+ end