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.
-
#config ⇒ Object
Returns configuration data of devices in the collection.
-
#config=(configuration) ⇒ Object
Sets configuration data of devices in the collection.
-
#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.
-
#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.
121 122 123 |
# File 'lib/tinderfridge/device_collection.rb', line 121 def blackout smap 'blackout' end |
#config ⇒ Object
Returns configuration data of devices in the collection.
83 84 85 |
# File 'lib/tinderfridge/device_collection.rb', line 83 def config smap 'config' end |
#config=(configuration) ⇒ Object
Sets configuration data of devices in the collection.
88 89 90 91 92 93 94 95 |
# File 'lib/tinderfridge/device_collection.rb', line 88 def config=(configuration) raise ArgumentError, 'invalid configuration' unless (configuration.class == Hash) each do |k,v| if configuration[k] v.config = configuration[k] end end end |
#disconnect ⇒ Object
Disconnects IP Connections used by devices in the collection, and removes all devices from the collection.
131 132 133 134 135 |
# File 'lib/tinderfridge/device_collection.rb', line 131 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.
169 170 171 |
# File 'lib/tinderfridge/device_collection.rb', line 169 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.
148 149 150 151 152 153 154 155 156 |
# File 'lib/tinderfridge/device_collection.rb', line 148 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.
126 127 128 |
# File 'lib/tinderfridge/device_collection.rb', line 126 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.
112 113 114 |
# File 'lib/tinderfridge/device_collection.rb', line 112 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.
100 101 102 |
# File 'lib/tinderfridge/device_collection.rb', line 100 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 |
#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 |