Class: Tinkerforge::DeviceCollection
- Inherits:
-
Hash
- Object
- Hash
- Tinkerforge::DeviceCollection
- Defined in:
- lib/tinderfridge/device_collection.rb
Instance Method Summary collapse
-
#blackout ⇒ Object
Turns off light sources such as screens and RGB LEDs for devices in the collection.
-
#configure ⇒ Object
Configures the devices in the collection by applying settings.
-
#disconnect ⇒ Object
Disconnects IP Connections used by devices in the collection, and removes all devices from the collection.
-
#find(selector) ⇒ Object
Returns the first device in the collection matching the selector.
-
#find_all(selector) ⇒ Object
Returns an array of devices in the collection matching the selector.
-
#get_chip_temperature ⇒ Object
Returns the temperatures as measured inside the microcontrollers of devices in the collection.
-
#get_identity ⇒ Object
Returns identity information for devices in the collection.
-
#get_spitfp_error_count ⇒ Object
Returns the error counts for devices in the collection.
-
#get_status_led_config ⇒ Object
Returns the status LED configuration for devices in the collection.
-
#ipcons ⇒ Object
Returns a list of unique IP Connections used by devices in the collection.
-
#ls ⇒ Object
Prints a list of devices in the collection.
-
#open_brick_viewer ⇒ Object
(also: #brickv)
On Mac OS, opens a new Brick Viewer for each unique IP Connection used by devices in the collection.
-
#open_documentation ⇒ Object
(also: #doc)
Opens the online documentation for the devices in the collection (Mac OS only).
-
#properties ⇒ Object
(also: #props)
Returns the properties of devices in the collection.
-
#set_status_led_config(state) ⇒ Object
Sets the status LED configuration for devices in the collection.
-
#settings ⇒ Object
(also: #config)
Returns settings for the devices in the collection.
-
#settings=(settings_hash) ⇒ Object
(also: #config=)
Defines settings for the devices in the collection.
-
#state ⇒ Object
Returns the state of devices in the collection.
Instance Method Details
#blackout ⇒ Object
Turns off light sources such as screens and RGB LEDs for devices in the collection.
Ignores devices that do not support the blackout method.
130 131 132 |
# File 'lib/tinderfridge/device_collection.rb', line 130 def blackout smap 'blackout' end |
#configure ⇒ Object
Configures the devices in the collection by applying settings.
102 103 104 |
# File 'lib/tinderfridge/device_collection.rb', line 102 def configure smap 'configure' end |
#disconnect ⇒ Object
Disconnects IP Connections used by devices in the collection, and removes all devices from the collection.
140 141 142 143 144 |
# File 'lib/tinderfridge/device_collection.rb', line 140 def disconnect result = ipcons.map { |i| [i, (i.get_connection_state == 0 ? nil : i.disconnect) ] }.to_h clear result end |
#find(selector) ⇒ Object
Returns the first device in the collection matching the selector.
Selector argument can be:
-
Device Identifier
-
Class name or Device Display Name (Regexp)
Selection by regular expression is case-insensitive by default.
178 179 180 |
# File 'lib/tinderfridge/device_collection.rb', line 178 def find(selector) find_all(selector).first end |
#find_all(selector) ⇒ Object
Returns an array of devices in the collection matching the selector.
Selector argument can be:
-
Device Identifier
-
Class name or Device Display Name (Regexp)
Selection by regular expression is case-insensitive by default.
157 158 159 160 161 162 163 164 165 |
# File 'lib/tinderfridge/device_collection.rb', line 157 def find_all(selector) case selector when Integer values.select { |v| v.device_identifier == selector} when Regexp r = Regexp.new selector.source, Regexp::IGNORECASE values.select { |v| v.class.to_s.split('::').last =~ r || v.device_display_name =~ r } end end |
#get_chip_temperature ⇒ Object
Returns the temperatures as measured inside the microcontrollers of devices in the collection.
Nil for devices that do not support the get_chip_temperature method.
Note: most devices return temperature in °C, but a few return temperature in 1/10 °C.
12 13 14 |
# File 'lib/tinderfridge/device_collection.rb', line 12 def get_chip_temperature smap 'get_chip_temperature' end |
#get_identity ⇒ Object
Returns identity information for devices in the collection.
Identity information is returned as an array:
-
0 : UID
-
1 : Connected UID
-
2 : Connected port (position)
-
3 : Hardware version
-
4 : Firmware version
-
5 : Device Identifier
Nil for devices that do not support the get_identity method.
27 28 29 |
# File 'lib/tinderfridge/device_collection.rb', line 27 def get_identity smap 'get_identity' end |
#get_spitfp_error_count ⇒ Object
Returns the error counts for devices in the collection.
Nil for devices that do not support the get_spitfp_error_count method.
34 35 36 |
# File 'lib/tinderfridge/device_collection.rb', line 34 def get_spitfp_error_count smap 'get_spitfp_error_count' end |
#get_status_led_config ⇒ Object
Returns the status LED configuration for devices in the collection.
Nil for devices that do not support the get_status_led_config method.
41 42 43 |
# File 'lib/tinderfridge/device_collection.rb', line 41 def get_status_led_config smap 'get_status_led_config' end |
#ipcons ⇒ Object
Returns a list of unique IP Connections used by devices in the collection.
135 136 137 |
# File 'lib/tinderfridge/device_collection.rb', line 135 def ipcons smap('ipcon').values.compact.uniq end |
#ls ⇒ Object
Prints a list of devices in the collection.
64 65 66 67 68 |
# File 'lib/tinderfridge/device_collection.rb', line 64 def ls keys.sort_by(&:downcase).each do |k| puts "%-8s %.40s" % [k, self[k].device_display_name] end.size end |
#open_brick_viewer ⇒ Object Also known as: brickv
On Mac OS, opens a new Brick Viewer for each unique IP Connection used by devices in the collection.
Not supported on other platforms.
Requires Brick Viewer version 2.4.23 or later.
121 122 123 |
# File 'lib/tinderfridge/device_collection.rb', line 121 def open_brick_viewer ipcons.map { |i| i.open_brick_viewer } end |
#open_documentation ⇒ Object Also known as: doc
Opens the online documentation for the devices in the collection (Mac OS only).
When the URL for a device’s documentation is not known, does nothing.
109 110 111 |
# File 'lib/tinderfridge/device_collection.rb', line 109 def open_documentation smap 'open_documentation' end |
#properties ⇒ Object Also known as: props
Returns the properties of devices in the collection.
71 72 73 |
# File 'lib/tinderfridge/device_collection.rb', line 71 def properties smap 'properties' end |
#set_status_led_config(state) ⇒ Object
Sets the status LED configuration for devices in the collection.
Ignores devices that do not support the set_status_led_config method.
Argument can be an integer (e.g. 0=off, 1=on), or a hash (as returned by #get_status_led_config).
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tinderfridge/device_collection.rb', line 50 def set_status_led_config(state) case state when Integer map { |k,d| d.respond_to?('set_status_led_config') ? d : nil}.compact.each { |d| d.set_status_led_config(state) }.size when Hash state.keys.select { |k| self[k].respond_to?('set_status_led_config') }.each do |k| self[k].set_status_led_config(state[k]) end.size else raise ArgumentError, 'Unknown state' end end |
#settings ⇒ Object Also known as: config
Returns settings for the devices in the collection.
83 84 85 |
# File 'lib/tinderfridge/device_collection.rb', line 83 def settings smap 'settings' end |
#settings=(settings_hash) ⇒ Object Also known as: config=
Defines settings for the devices in the collection.
90 91 92 93 94 95 96 97 |
# File 'lib/tinderfridge/device_collection.rb', line 90 def settings=(settings_hash) raise ArgumentError, 'invalid settings' unless (settings_hash.class == Hash) each do |k,v| if settings_hash[k] v.settings = settings_hash[k] end end end |
#state ⇒ Object
Returns the state of devices in the collection.
78 79 80 |
# File 'lib/tinderfridge/device_collection.rb', line 78 def state smap 'state' end |