Class: Tinkerforge::DeviceCollection

Inherits:
Hash
  • Object
show all
Defined in:
lib/tinderfridge/device_collection.rb

Instance Method Summary collapse

Instance Method Details

#blackoutObject

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

#configObject

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.

Raises:

  • (ArgumentError)


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

#disconnectObject

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.

Examples:

Select by Device Identifier

# Remote Switch Bricklet 2.0
tf = Tinkerforge.connect('myhost.local').discover(1)
tf.find 289


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.

Examples:

Select by class name and Device Display Name

# All 'analog' devices
tf = Tinkerforge.connect.discover(1)
tf.find_all /analog/


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_temperatureObject

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_identityObject

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_countObject

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_configObject

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

#ipconsObject

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

#lsObject

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_viewerObject 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_documentationObject 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

#propertiesObject 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

#stateObject

Returns the state of devices in the collection.



78
79
80
# File 'lib/tinderfridge/device_collection.rb', line 78

def state
  smap 'state'
end