phidgets 0.0.5 → 0.1.0

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.
Files changed (88) hide show
  1. data/History.txt +10 -2
  2. data/README.rdoc +41 -18
  3. data/Rakefile +31 -20
  4. data/bin/phidget +29 -44
  5. data/ext/phidgets/extconf.rb +14 -0
  6. data/ext/phidgets/phidgets.c +272 -0
  7. data/ext/phidgets/phidgets.h +82 -0
  8. data/ext/phidgets/phidgets_accelerometer.c +165 -0
  9. data/ext/phidgets/phidgets_advanced_servo.c +567 -0
  10. data/ext/phidgets/phidgets_analog.c +139 -0
  11. data/ext/phidgets/phidgets_bridge.c +263 -0
  12. data/ext/phidgets/phidgets_common.c +454 -0
  13. data/ext/phidgets/phidgets_dictionary.c +279 -0
  14. data/ext/phidgets/phidgets_encoder.c +249 -0
  15. data/ext/phidgets/phidgets_frequency_counter.c +241 -0
  16. data/ext/phidgets/phidgets_gps.c +235 -0
  17. data/ext/phidgets/phidgets_interface_kit.c +340 -0
  18. data/ext/phidgets/phidgets_ir.c +251 -0
  19. data/ext/phidgets/phidgets_led.c +178 -0
  20. data/ext/phidgets/phidgets_manager.c +366 -0
  21. data/ext/phidgets/phidgets_motor_control.c +642 -0
  22. data/ext/phidgets/phidgets_phsensor.c +208 -0
  23. data/ext/phidgets/phidgets_rfid.c +281 -0
  24. data/ext/phidgets/phidgets_servo.c +276 -0
  25. data/ext/phidgets/phidgets_spatial.c +369 -0
  26. data/ext/phidgets/phidgets_stepper.c +560 -0
  27. data/ext/phidgets/phidgets_temp_sensor.c +295 -0
  28. data/ext/phidgets/phidgets_text_lcd.c +381 -0
  29. data/ext/phidgets/phidgets_text_led.c +107 -0
  30. data/ext/phidgets/phidgets_weight_sensor.c +113 -0
  31. data/lib/phidgets/accelerometer.rb +25 -0
  32. data/lib/phidgets/advanced_servo.rb +49 -0
  33. data/lib/phidgets/analog.rb +8 -0
  34. data/lib/phidgets/bridge.rb +25 -0
  35. data/lib/phidgets/common.rb +75 -190
  36. data/lib/phidgets/dictionary.rb +53 -0
  37. data/lib/phidgets/encoder.rb +49 -0
  38. data/lib/phidgets/frequency_counter.rb +25 -0
  39. data/lib/phidgets/gps.rb +37 -0
  40. data/lib/phidgets/interfacekit.rb +38 -128
  41. data/lib/phidgets/ir.rb +50 -0
  42. data/lib/phidgets/led.rb +8 -0
  43. data/lib/phidgets/manager.rb +67 -119
  44. data/lib/phidgets/motor_control.rb +110 -0
  45. data/lib/phidgets/ph_sensor.rb +25 -0
  46. data/lib/phidgets/rfid.rb +38 -111
  47. data/lib/phidgets/servo.rb +12 -95
  48. data/lib/phidgets/spatial.rb +25 -0
  49. data/lib/phidgets/stepper.rb +61 -0
  50. data/lib/phidgets/temperature_sensor.rb +25 -0
  51. data/lib/phidgets/text_lcd.rb +8 -0
  52. data/lib/phidgets/text_led.rb +8 -0
  53. data/lib/phidgets/weight_sensor.rb +25 -0
  54. data/lib/phidgets.rb +22 -3
  55. data/phidgets.gemspec +42 -0
  56. data/test/test_accelerometer.rb +47 -0
  57. data/test/test_advanced_servo.rb +152 -0
  58. data/test/test_analog.rb +45 -0
  59. data/test/test_bridge.rb +77 -0
  60. data/test/test_common.rb +167 -0
  61. data/test/test_dictionary.rb +82 -0
  62. data/test/test_encoder.rb +67 -0
  63. data/test/test_frequency_counter.rb +67 -0
  64. data/test/test_gps.rb +67 -0
  65. data/test/test_helper.rb +1 -0
  66. data/test/test_interfacekit.rb +86 -182
  67. data/test/test_ir.rb +57 -0
  68. data/test/test_led.rb +55 -0
  69. data/test/test_manager.rb +94 -0
  70. data/test/test_motor_control.rb +172 -0
  71. data/test/test_phidgets.rb +14 -6
  72. data/test/test_phsensor.rb +62 -0
  73. data/test/test_rfid.rb +77 -0
  74. data/test/test_servo.rb +67 -0
  75. data/test/test_spatial.rb +112 -0
  76. data/test/test_stepper.rb +163 -0
  77. data/test/test_temp_sensor.rb +87 -0
  78. data/test/test_text_lcd.rb +115 -0
  79. data/test/test_text_led.rb +35 -0
  80. data/test/test_weight_sensor.rb +32 -0
  81. metadata +165 -75
  82. data/Manifest.txt +0 -21
  83. data/PostInstall.txt +0 -3
  84. data/README.txt +0 -87
  85. data/lib/phidgets/phidgets.rb +0 -225
  86. data/script/console +0 -10
  87. data/script/destroy +0 -14
  88. data/script/generate +0 -14
@@ -0,0 +1,49 @@
1
+
2
+
3
+ module Phidgets
4
+ class Encoder < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnInputChangeHandler(proc=nil, &block)
10
+ #
11
+ # Sets an input change handler. This is called when a digital input changes.
12
+ #
13
+ def setOnInputChangeHandler(cb_proc = nil, &cb_block)
14
+ @on_input_change_thread.kill if defined? @on_input_change_thread
15
+ callback = cb_proc || cb_block
16
+ @on_input_change_thread = Thread.new {ext_setOnInputChangeHandler(callback)}
17
+ end
18
+
19
+ # call-seq:
20
+ # setOnPositionChangeHandler(proc=nil, &block)
21
+ #
22
+ # Sets an encoder position change handler. This is called when an encoder position changes.
23
+ #
24
+ def setOnPositionChangeHandler(cb_proc = nil, &cb_block)
25
+ @on_position_change_thread.kill if defined? @on_position_change_thread
26
+ callback = cb_proc || cb_block
27
+ @on_position_change_thread = Thread.new {ext_setOnPositionChangeHandler(callback)}
28
+ end
29
+
30
+ # call-seq:
31
+ # setOnIndexHandler(proc=nil, &block)
32
+ #
33
+ # Sets an encoder index handler. This is called when there is a pulse on the index pin.
34
+ #
35
+ def setOnIndexHandler(cb_proc = nil, &cb_block)
36
+ @on_index_thread.kill if defined? @on_index_thread
37
+ callback = cb_proc || cb_block
38
+ @on_index_thread = Thread.new {ext_setOnIndexHandler(callback)}
39
+ end
40
+
41
+ alias :on_input_change :setOnInputChangeHandler
42
+ alias :on_position_change :setOnPositionChangeHandler
43
+ alias :on_index :setOnIndexHandler
44
+
45
+ end
46
+
47
+ end
48
+ end
49
+
@@ -0,0 +1,25 @@
1
+
2
+
3
+ module Phidgets
4
+ class FrequencyCounter < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnCountHandler(proc=nil, &block)
10
+ #
11
+ # Sets a count event handler. This is called when ticks have been counted on an input, or when the timeout has passed.
12
+ #
13
+ def setOnCountHandler(cb_proc = nil, &cb_block)
14
+ @on_count_thread.kill if defined? @on_count_thread
15
+ callback = cb_proc || cb_block
16
+ @on_count_thread = Thread.new {ext_setOnCountHandler(callback)}
17
+ end
18
+
19
+ alias :on_count :setOnCountHandler
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+
@@ -0,0 +1,37 @@
1
+
2
+
3
+ module Phidgets
4
+ class GPS < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnPositionChangeHandler(proc=nil, &block)
10
+ #
11
+ # Sets a position change event handler. This is called when any of latitude, longitude, or altitude change.
12
+ #
13
+ def setOnPositionChangeHandler(cb_proc = nil, &cb_block)
14
+ @on_position_change_thread.kill if defined? @on_position_change_thread
15
+ callback = cb_proc || cb_block
16
+ @on_position_change_thread = Thread.new {ext_setOnPositionChangeHandler(callback)}
17
+ end
18
+
19
+ # call-seq:
20
+ # setOnPositionFixStatusChangeHandler(proc=nil, &block)
21
+ #
22
+ # Sets a position fix status change event handler. This is called when a position fix is aquired or lost.
23
+ #
24
+ def setOnPositionFixStatusChangeHandler(cb_proc = nil, &cb_block)
25
+ @on_position_fix_status_change_thread.kill if defined? @on_position_fix_status_change_thread
26
+ callback = cb_proc || cb_block
27
+ @on_position_fix_status_change_thread = Thread.new {ext_setOnPositionFixStatusChangeHandler(callback)}
28
+ end
29
+
30
+ alias :on_position_change :setOnPositionChangeHandler
31
+ alias :on_position_fix_status_change :setOnPositionFixStatusChangeHandler
32
+
33
+ end
34
+
35
+ end
36
+ end
37
+
@@ -1,139 +1,49 @@
1
1
 
2
2
 
3
3
  module Phidgets
4
-
5
- extern "int CPhidgetInterfaceKit_create(void *)"
6
- extern "int CPhidgetInterfaceKit_getInputCount(void *, int *)"
7
- extern "int CPhidgetInterfaceKit_getInputState(void *, int, int *)"
8
- extern "int CPhidgetInterfaceKit_getOutputCount(void *, int *)"
9
- extern "int CPhidgetInterfaceKit_getOutputState(void *, int, int *)"
10
- extern "int CPhidgetInterfaceKit_setOutputState(void *, int, int)"
11
- extern "int CPhidgetInterfaceKit_getSensorCount(void *, int *)"
12
- extern "int CPhidgetInterfaceKit_getSensorValue(void *, int, int *)"
13
- extern "int CPhidgetInterfaceKit_getSensorRawValue(void *, int, int *)"
14
- extern "int CPhidgetInterfaceKit_getRatiometric(void *, int *)"
15
- extern "int CPhidgetInterfaceKit_setRatiometric(void *, int)"
16
-
17
-
18
4
  class InterfaceKit < Common
19
5
 
20
- # Create a new InterfaceKit object.
21
- # === Parameters
22
- # * _serial_number_ = Serial number of the phidget board to open. Specify -1 to open any.
23
- # * _timeout_ = Time to wait for attachment. Specify 0 to not call open.
24
- def initialize(serial_number=-1, timeout=0)
25
- super()
26
- create
27
- open(serial_number, timeout) if timeout > 0
28
- end
29
-
30
- # Gets the number of digital inputs supported by this board.
31
- def getInputCount
32
- cnt = Phidgets.malloc(SIZEOF_INT)
33
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getInputCount', @handle, cnt.ref)
34
- raise Phidgets::Exception.new(r) if r != 0
35
- cnt.free = nil
36
- cnt.to_i
37
- end
38
-
39
- # Gets the state of a digital input.
40
- # === Parameters
41
- # * _index_ = The input index.
42
- def getInputState(index)
43
- state = Phidgets.malloc(SIZEOF_INT)
44
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getInputState', @handle, index.to_i, state.ref)
45
- raise Phidgets::Exception.new(r) if r != 0
46
- state.free = nil
47
- state.to_i
48
- end
49
-
50
- # Gets the number of digital outputs supported by this board.
51
- def getOutputCount
52
- cnt = Phidgets.malloc(SIZEOF_INT)
53
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getOutputCount', @handle, cnt.ref)
54
- raise Phidgets::Exception.new(r) if r != 0
55
- cnt.free = nil
56
- cnt.to_i
57
- end
58
-
59
- # Gets the state of a digital output.
60
- # === Parameters
61
- # * _index_ = The output index.
62
- def getOutputState(index)
63
- state = Phidgets.malloc(SIZEOF_INT)
64
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getOutputState', @handle, index.to_i, state.ref)
65
- raise Phidgets::Exception.new(r) if r != 0
66
- state.free = nil
67
- state.to_i
68
- end
69
-
70
- # Sets the state of a digital output.
71
- # === Parameters
72
- # * _index_ = The output index.
73
- # * _state_ = The output state. Possible values are PTRUE and PFALSE.
74
- def setOutputState(index, state)
75
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_setOutputState', @handle, index.to_i, state.to_i)
76
- raise Phidgets::Exception.new(r) if r != 0
77
- end
78
-
79
- # Gets the number of sensor (analog) inputs supported by this board.
80
- def getSensorCount
81
- cnt = Phidgets.malloc(SIZEOF_INT)
82
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getSensorCount', @handle, cnt.ref)
83
- raise Phidgets::Exception.new(r) if r != 0
84
- cnt.free = nil
85
- cnt.to_i
86
- end
87
-
88
- # Gets a sensor value (0-1000).
89
- # === Parameters
90
- # * _index_ = The sensor index.
91
- def getSensorValue(index)
92
- state = Phidgets.malloc(SIZEOF_INT)
93
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getSensorValue', @handle, index.to_i, state.ref)
94
- raise Phidgets::Exception.new(r) if r != 0
95
- state.free = nil
96
- state.to_i
97
- end
98
-
99
- # Gets a sensor raw value (12-bit).
100
- # === Parameters
101
- # * _index_ = The sensor index.
102
- def getSensorRawValue(index)
103
- state = Phidgets.malloc(SIZEOF_INT)
104
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getSensorRawValue', @handle, index.to_i, state.ref)
105
- raise Phidgets::Exception.new(r) if r != 0
106
- state.free = nil
107
- state.to_i
108
- end
109
-
110
- # Gets the ratiometric state for this board.
111
- def getRatiometric
112
- ratio = Phidgets.malloc(SIZEOF_INT)
113
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_getRatiometric', @handle, ratio.ref)
114
- raise Phidgets::Exception.new(r) if r != 0
115
- ratio.free = nil
116
- ratio.to_i
117
- end
118
-
119
- # Sets the ratiometric state for this board.
120
- # === Parameters
121
- # * _ratiometric_ = The ratiometric state. Possible values are PTRUE and PFALSE.
122
- def setRatiometric(ratiometric)
123
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_setRatiometric', @handle, ratiometric.to_i)
124
- raise Phidgets::Exception.new(r) if r != 0
125
- end
126
-
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnInputChangeHandler(proc=nil, &block)
10
+ #
11
+ # Set a digital input change handler. This is called when a digital input changes.
12
+ #
13
+ def setOnInputChangeHandler(cb_proc = nil, &cb_block)
14
+ @on_input_change_thread.kill if defined? @on_input_change_thread
15
+ callback = cb_proc || cb_block
16
+ @on_input_change_thread = Thread.new {ext_setOnInputChangeHandler(callback)}
17
+ end
18
+
19
+ # call-seq:
20
+ # setOnOutputChangeHandler(proc=nil, &block)
21
+ #
22
+ # Set a digital output change handler. This is called when a digital output changes.
23
+ #
24
+ def setOnOutputChangeHandler(cb_proc = nil, &cb_block)
25
+ @on_output_change_thread.kill if defined? @on_output_change_thread
26
+ callback = cb_proc || cb_block
27
+ @on_output_change_thread = Thread.new {ext_setOnOutputChangeHandler(callback)}
28
+ end
29
+
30
+ # call-seq:
31
+ # setOnSensorChangeHandler(proc=nil, &block)
32
+ #
33
+ # Set a sensor change handler. This is called when a sensor value changes by more then the change trigger.
34
+ #
35
+ def setOnSensorChangeHandler(cb_proc = nil, &cb_block)
36
+ @on_sensor_change_thread.kill if defined? @on_sensor_change_thread
37
+ callback = cb_proc || cb_block
38
+ @on_sensor_change_thread = Thread.new {ext_setOnSensorChangeHandler(callback)}
39
+ end
40
+
41
+ alias :on_input_change :setOnInputChangeHandler
42
+ alias :on_output_change :setOnOutputChangeHandler
43
+ alias :on_sensor_change :setOnSensorChangeHandler
127
44
 
128
- private
129
-
130
- # Creates a Phidget InterfaceKit handle.
131
- def create
132
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetInterfaceKit_create', @handle.ref)
133
- raise Phidgets::Exception.new(r) if r != 0
134
45
  end
135
46
 
136
47
  end
137
-
138
48
  end
139
49
 
@@ -0,0 +1,50 @@
1
+
2
+
3
+ module Phidgets
4
+ class IR < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnCodeHandler(proc=nil, &block)
10
+ #
11
+ # Set a Code handler. This is called when a code has been received that could be
12
+ # automatically decoded.
13
+ #
14
+ def setOnCodeHandler(cb_proc = nil, &cb_block)
15
+ @on_code_thread.kill if defined? @on_code_thread
16
+ callback = cb_proc || cb_block
17
+ @on_code_thread = Thread.new {ext_setOnCodeHandler(callback)}
18
+ end
19
+
20
+ # call-seq:
21
+ # setOnLearnHandler(proc=nil, &block)
22
+ #
23
+ # Set a Learn handler. This is called when a code has been received for long enough to be learned.
24
+ #
25
+ def setOnLearnHandler(cb_proc = nil, &cb_block)
26
+ @on_learn_thread.kill if defined? @on_learn_thread
27
+ callback = cb_proc || cb_block
28
+ @on_learn_thread = Thread.new {ext_setOnLearnHandler(callback)}
29
+ end
30
+
31
+ # call-seq:
32
+ # setOnRawDataHandler(proc=nil, &block)
33
+ #
34
+ # Set a Raw Data handler. This is called when raw data has been read from the device.
35
+ #
36
+ def setOnRawDataHandler(cb_proc = nil, &cb_block)
37
+ @on_raw_data_thread.kill if defined? @on_raw_data_thread
38
+ callback = cb_proc || cb_block
39
+ @on_raw_data_thread = Thread.new {ext_setOnRawDataHandler(callback)}
40
+ end
41
+
42
+ alias :on_code :setOnCodeHandler
43
+ alias :on_learn :setOnLearnHandler
44
+ alias :on_raw_data :setOnRawDataHandler
45
+
46
+ end
47
+
48
+ end
49
+ end
50
+
@@ -0,0 +1,8 @@
1
+
2
+
3
+ module Phidgets
4
+ class LED < Common
5
+
6
+ end
7
+ end
8
+
@@ -1,132 +1,80 @@
1
1
 
2
2
  module Phidgets
3
-
4
- extern "int CPhidgetManager_create(void *)"
5
- extern "int CPhidgetManager_open(void *)"
6
- extern "int CPhidgetManager_close(void *)"
7
- extern "int CPhidgetManager_delete(void *)"
8
- extern "int CPhidgetManager_getAttachedDevices(void *, void *, int *)"
9
- extern "int CPhidgetManager_freeAttachedDevicesArray(void *)"
10
- extern "int CPhidgetManager_openRemote(void *, void *, void *)"
11
- extern "int CPhidgetManager_openRemoteIP(void *, char *, int, void *)"
12
-
13
-
14
3
  class Manager
15
4
 
16
- # Creates a new Manager object.
17
- def initialize
18
- @server = nil
19
- @port = nil
20
- @password = nil
21
- @handle = Phidgets.malloc(SIZEOF_VOIDP)
22
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_create', @handle.ref)
23
- raise Phidgets::Exception.new(r) if r != 0
5
+ # call-seq:
6
+ # open(args)
7
+ #
8
+ # Opens a Manager. Will attempt to make the correct open call (open, openRemote, openRemoteIP) based
9
+ # on the arguments passed in. Args is a hash and the following keys may be used:
10
+ # :server_id, :address, :port, :password.
11
+ #
12
+ def open(args={})
13
+ if args.key? :server_id
14
+ open_remote(args[:server_id], args[:password])
15
+ elsif args.key? :address
16
+ open_remote_ip(args[:address], args[:port], args[:password])
17
+ else
18
+ ext_open
19
+ end
24
20
  end
25
21
 
26
- # Opens a Manager.
27
- def open
28
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_open', @handle)
29
- raise Phidgets::Exception.new(r) if r != 0
30
22
 
31
- # a delay is required between the time the manager is opened and when it is used, otherwise it will report that no phidgets are attached. there really should be a better way.
32
- sleep 1
33
- end
34
-
35
- # Opens a Manager remotely by ServerID. Note that this requires Bonjour (mDNS) to be running on both the host and the server.
36
- # === Parameters
37
- # * _server_ = Server ID. Specify nil to open any.
38
- # * _password_ = Password. Can be nil if the server is running unsecured.
39
- def openRemote(server=nil, password=nil)
40
- @server = server
41
- @password = password
42
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_openRemote', @handle, server, password)
43
- raise Phidgets::Exception.new(r) if r != 0
23
+ unless RUBY_VERSION < '1.9.0'
24
+
25
+ # call-seq:
26
+ # setOnAttachHandler(proc=nil, &block)
27
+ #
28
+ # Sets an attach handler callback function. This is called when a Phidget is plugged into the system.
29
+ #
30
+ def setOnAttachHandler(cb_proc = nil, &cb_block)
31
+ @on_attach_thread.kill if defined? @on_attach_thread
32
+ callback = cb_proc || cb_block
33
+ @on_attach_thread = Thread.new {ext_setOnAttachHandler(callback)}
34
+ end
35
+
36
+ # call-seq:
37
+ # setOnDetachHandler(proc=nil, &block)
38
+ #
39
+ # Sets a detach handler callback function. This is called when a Phidget is unplugged from the system.
40
+ #
41
+ def setOnDetachHandler(cb_proc = nil, &cb_block)
42
+ @on_detach_thread.kill if defined? @on_detach_thread
43
+ callback = cb_proc || cb_block
44
+ @on_detach_thread = Thread.new {ext_setOnDetachHandler(callback)}
45
+ end
46
+
47
+ # call-seq:
48
+ # setOnServerConnectHandler(proc=nil, &block)
49
+ #
50
+ # Sets a server connect handler callback function. This is used for opening Phidget Managers remotely,
51
+ # and is called when a connection to the sever has been made.
52
+ #
53
+ def setOnServerConnectHandler(cb_proc = nil, &cb_block)
54
+ @on_server_connect_thread.kill if defined? @on_server_connect_thread
55
+ callback = cb_proc || cb_block
56
+ @on_server_connect_thread = Thread.new {ext_setOnServerConnectHandler(callback)}
57
+ end
58
+
59
+ # call-seq:
60
+ # setOnServerDisconnectHandler(proc=nil, &block)
61
+ #
62
+ # Sets a server disconnect handler callback function. This is used for opening Phidget Managers remotely,
63
+ # and is called when a connection to the server has been lost.
64
+ #
65
+ def setOnServerDisconnectHandler(cb_proc = nil, &cb_block)
66
+ @on_server_disconnect_thread.kill if defined? @on_server_disconnect_thread
67
+ callback = cb_proc || cb_block
68
+ @on_server_disconnect_thread = Thread.new {ext_setOnServerDisconnectHandler(callback)}
69
+ end
70
+
71
+ alias :on_attach :setOnAttachHandler
72
+ alias :on_detach :setOnDetachHandler
73
+ alias :on_server_connect :setOnServerConnectHandler
74
+ alias :on_server_disconnect :setOnServerDisconnectHandler
44
75
 
45
- # a delay is required between the time the manager is opened and when it is used, otherwise it will report that no phidgets are attached. there really should be a better way.
46
- sleep 2
47
76
  end
48
77
 
49
- # Opens a Manager remotely by address and port.
50
- # === Parameters
51
- # * _address_ = Address. This can be a hostname or IP address.
52
- # * _port_ = Port number. Default is 5001.
53
- # * _password_ = Password. Can be nil if the server is running unsecured.
54
- def openRemoteIP(address, port=5001, password=nil)
55
- @server = address
56
- @port = port.to_i
57
- @password = password
58
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_openRemoteIP', @handle, address, port.to_i, password)
59
- raise Phidgets::Exception.new(r) if r != 0
60
-
61
- # a delay is required between the time the manager is opened and when it is used, otherwise it will report that no phidgets are attached. there really should be a better way.
62
- sleep 2
63
- end
64
-
65
- # Closes a Manager.
66
- def close
67
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_close', @handle)
68
- raise Phidgets::Exception.new(r) if r != 0
69
- end
70
-
71
- # Frees a Manager handle.
72
- def delete
73
- @handle.free = nil
74
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_delete', @handle)
75
- raise Phidgets::Exception.new(r) if r != 0
76
- end
77
-
78
- # Gets a hash of all currently attached Phidgets. The indices into the hash are the serial numbers of the Phidgets.
79
- def getAttachedDevices
80
- handles = Phidgets.malloc(SIZEOF_VOIDP)
81
- count = Phidgets.malloc(SIZEOF_INT)
82
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_getAttachedDevices', @handle, handles.ref, count.ref)
83
- raise Phidgets::Exception.new(r) if r != 0
84
- count.free = nil
85
-
86
- devices = {}
87
- count.to_i.times { |i|
88
- handle = handles + (i * SIZEOF_VOIDP)
89
- serial = getSerialNumber(handle)
90
-
91
- devices[serial] = case getDeviceClass(handle)
92
- when CLASS_INTERFACEKIT
93
- InterfaceKit.new
94
- when CLASS_RFID
95
- RFID.new
96
- when CLASS_SERVO
97
- Servo.new
98
- end
99
-
100
- devices[serial].open(serial, 3000) unless @server
101
- devices[serial].openRemote(serial, @server, @password, 4000) if @server and !@port
102
- devices[serial].openRemoteIP(serial, @server, @port, @password, 4000) if @server and @port
103
- }
104
-
105
- r = Phidgets.send(FUNCTION_PREFIX + 'PhidgetManager_freeAttachedDevicesArray', handles)
106
- raise Phidgets::Exception.new(r) if r != 0
107
-
108
- devices
109
- end
110
-
111
- private
112
-
113
- def getDeviceClass handle
114
- dev_class = Phidgets.malloc(SIZEOF_INT)
115
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getDeviceClass', handle.ptr, dev_class.ref)
116
- raise Phidgets::Exception.new(r) if r != 0
117
- dev_class.free = nil
118
- dev_class.to_i
119
- end
120
-
121
- def getSerialNumber handle
122
- sn = Phidgets.malloc(SIZEOF_INT)
123
- r = Phidgets.send(FUNCTION_PREFIX + 'Phidget_getSerialNumber', handle.ptr, sn.ref)
124
- raise Phidgets::Exception.new(r) if r != 0
125
- sn.free = nil
126
- sn.to_i
127
- end
128
-
129
78
  end
130
-
131
79
  end
132
80
 
@@ -0,0 +1,110 @@
1
+
2
+
3
+ module Phidgets
4
+ class MotorControl < Common
5
+
6
+ unless RUBY_VERSION < '1.9.0'
7
+
8
+ # call-seq:
9
+ # setOnVelocityChangeHandler(proc=nil, &block)
10
+ #
11
+ # Sets a velocity change event handler. This is called when the velocity changes.
12
+ #
13
+ def setOnVelocityChangeHandler(cb_proc = nil, &cb_block)
14
+ @on_velocity_change_thread.kill if defined? @on_velocity_change_thread
15
+ callback = cb_proc || cb_block
16
+ @on_velocity_change_thread = Thread.new {ext_setOnVelocityChangeHandler(callback)}
17
+ end
18
+
19
+ # call-seq:
20
+ # setOnCurrentChangeHandler(proc=nil, &block)
21
+ #
22
+ # Sets a current change event handler. This is called when the current draw changes.
23
+ #
24
+ def setOnCurrentChangeHandler(cb_proc = nil, &cb_block)
25
+ @on_current_change_thread.kill if defined? @on_current_change_thread
26
+ callback = cb_proc || cb_block
27
+ @on_current_change_thread = Thread.new {ext_setOnCurrentChangeHandler(callback)}
28
+ end
29
+
30
+ # call-seq:
31
+ # setOnCurrentUpdateHandler(proc=nil, &block)
32
+ #
33
+ # Set a current update handler. This is called at a constant rate; every 8ms.
34
+ #
35
+ def setOnCurrentUpdateHandler(cb_proc = nil, &cb_block)
36
+ @on_current_update_thread.kill if defined? @on_current_update_thread
37
+ callback = cb_proc || cb_block
38
+ @on_current_update_thread = Thread.new {ext_setOnCurrentUpdateHandler(callback)}
39
+ end
40
+
41
+ # call-seq:
42
+ # setOnInputChangeHandler(proc=nil, &block)
43
+ #
44
+ # Set a digital input change handler. This is called when a digital input changes.
45
+ #
46
+ def setOnInputChangeHandler(cb_proc = nil, &cb_block)
47
+ @on_input_change_thread.kill if defined? @on_input_change_thread
48
+ callback = cb_proc || cb_block
49
+ @on_input_change_thread = Thread.new {ext_setOnInputChangeHandler(callback)}
50
+ end
51
+
52
+ # call-seq:
53
+ # setOnEncoderPositionChangeHandler(proc=nil, &block)
54
+ #
55
+ # Set an encoder position change handler. This is called when the encoder position changes.
56
+ #
57
+ def setOnEncoderPositionChangeHandler(cb_proc = nil, &cb_block)
58
+ @on_encoder_position_change_thread.kill if defined? @on_encoder_position_change_thread
59
+ callback = cb_proc || cb_block
60
+ @on_encoder_position_change_thread = Thread.new {ext_setOnEncoderPositionChangeHandler(callback)}
61
+ end
62
+
63
+ # call-seq:
64
+ # setOnEncoderPositionUpdateHandler(proc=nil, &block)
65
+ #
66
+ # Set an encoder position update handler. This is called at a constant rate; every 8ms, whether the encoder
67
+ # position has changed or not.
68
+ #
69
+ def setOnEncoderPositionUpdateHandler(cb_proc = nil, &cb_block)
70
+ @on_encoder_position_update_thread.kill if defined? @on_encoder_position_update_thread
71
+ callback = cb_proc || cb_block
72
+ @on_encoder_position_update_thread = Thread.new {ext_setOnEncoderPositionUpdateHandler(callback)}
73
+ end
74
+
75
+ # call-seq:
76
+ # setOnBackEMFUpdateHandler(proc=nil, &block)
77
+ #
78
+ # Set a back EMF update handler. This is called at a constant rate; every 16ms, when back EMF sensing is enabled for that motor.
79
+ #
80
+ def setOnBackEMFUpdateHandler(cb_proc = nil, &cb_block)
81
+ @on_back_emf_update_thread.kill if defined? @on_back_emf_update_thread
82
+ callback = cb_proc || cb_block
83
+ @on_back_emf_update_thread = Thread.new {ext_setOnBackEMFUpdateHandler(callback)}
84
+ end
85
+
86
+ # call-seq:
87
+ # setOnSensorUpdateHandler(proc=nil, &block)
88
+ #
89
+ # Set a sensor update handler. This is called at a constant rate; every 8ms.
90
+ #
91
+ def setOnSensorUpdateHandler(cb_proc = nil, &cb_block)
92
+ @on_sensor_update_thread.kill if defined? @on_sensor_update_thread
93
+ callback = cb_proc || cb_block
94
+ @on_sensor_update_thread = Thread.new {ext_setOnSensorUpdateHandler(callback)}
95
+ end
96
+
97
+ alias :on_velocity_change :setOnVelocityChangeHandler
98
+ alias :on_current_change :setOnCurrentChangeHandler
99
+ alias :on_current_update :setOnCurrentUpdateHandler
100
+ alias :on_input_change :setOnInputChangeHandler
101
+ alias :on_encoder_position_change :setOnEncoderPositionChangeHandler
102
+ alias :on_encoder_position_update :setOnEncoderPositionUpdateHandler
103
+ alias :on_back_emf_update :setOnBackEMFUpdateHandler
104
+ alias :on_sensor_update :setOnSensorUpdateHandler
105
+
106
+ end
107
+
108
+ end
109
+ end
110
+