class Tinkerforge::Device

Attributes

device_display_name[R]

Returns the device's Display Name.

device_identifier[R]

Returns the device's numeric Device Identifier.

ipcon[R]

Returns the device's IPConnection object.

uid_string[R]

Returns the device's UID. Not to be confused with uid, which returns the numeric UID.

Public Class Methods

class_map() click to toggle source

Returns a map of currently defined device classes.

# File lib/tinderfridge/device.rb, line 22
def class_map
  descendants.map do |klass|
    if klass.const_defined? 'DEVICE_IDENTIFIER'
      [
        klass.const_get('DEVICE_IDENTIFIER'),
        klass.const_get('DEVICE_DISPLAY_NAME'),
        klass,
        File.basename(klass.instance_method('initialize').source_location.first, '.rb')
      ]
    else
      nil
    end
  end.compact.sort_by { |i| i[0] }
end
descendants() click to toggle source

Returns all currently defined classes that inherited from this class.

With help from:

# File lib/tinderfridge/device.rb, line 17
def descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

Private Class Methods

inherited(klass) click to toggle source

Primitive superhook: Every time a class inherits from the Device class, attempts to load an extension for that new class.

# File lib/tinderfridge/device.rb, line 42
def inherited(klass)
  if info = Tinkerforge.device_info(klass)
    begin
      require("tinderfridge/devices/#{info[2][1]}/#{info[2][1]}")
    rescue LoadError
      # No extension found for this device
    end
  end
end

Public Instance Methods

device_info() click to toggle source

Returns device information.

Device information is an array:

  • 0 : Device Identifier

  • 1 : Device Display Name

  • 2 : Associated class name and source file

# File lib/tinderfridge/device.rb, line 76
def device_info
  Tinkerforge.device_info device_identifier
end
doc()
Alias for: open_documentation
identify(seconds=10) click to toggle source

Identifies a Tinkerforge device by blinking its status led.

Supports recent devices. When invoked on older devices, does nothing.

# File lib/tinderfridge/device.rb, line 135
def identify(seconds=10)
  if (respond_to? 'get_status_led_config') and (respond_to? 'set_status_led_config')
    seconds = seconds.to_i
    state = get_status_led_config

    (seconds*2).times do |n|
      set_status_led_config n.remainder(2)
      sleep 0.5
    end

    set_status_led_config state
    seconds
  else
    false
  end
end
inspect() click to toggle source

Returns a programmer-friendly representation of the device.

# File lib/tinderfridge/device.rb, line 81
def inspect
  "%s (%s@%s:%s)" % [self.class, @uid_string, ipcon.host, ipcon.port]
end
open_documentation() click to toggle source

Opens the online documentation for the device (Mac OS only).

When the URL for the documentation is not known, does nothing.

# File lib/tinderfridge/device.rb, line 121
def open_documentation
  if properties['documentation_en_url'] and ( RUBY_PLATFORM =~ /darwin/ )
    `open #{properties['documentation_en_url']}`
    properties['documentation_en_url']
  else
    nil
  end
end
Also aliased as: doc
properties() click to toggle source

Returns the device's properties.

# File lib/tinderfridge/device.rb, line 86
def properties
  @properties ||= {
    'device_identifier'   => device_identifier,
    'device_display_name' => device_display_name,
  }.merge load_properties
end
Also aliased as: props
props()
Alias for: properties
state() click to toggle source

Returns the device's state.

# File lib/tinderfridge/device.rb, line 96
def state

  # BrickDaemon inherits from Device, but has no #get_identity.
  return {} unless respond_to? 'get_identity'

  identity = get_identity

  [
    [ 'uid'                , uid_string            ],
    [ 'update_time'        , Time.now.gmtime       ],
    [ 'firmware_version'   , identity[4].join('.') ],

    [ 'connected', { 'uid'  => identity[1], 'position' => identity[2] } ],
    [ 'ipcon'    , { 'host' => ipcon.host , 'port'     => ipcon.port  } ],

    respond_to?('get_chip_temperature'  ) ? [ 'chip_temperature'  , get_chip_temperature   ] : nil,
    respond_to?('get_spitfp_error_count') ? [ 'spitfp_error_count', get_spitfp_error_count ] : nil,
    respond_to?('get_status_led_config' ) ? [ 'status_led_config' , get_status_led_config  ] : nil,

  ].compact.to_h
end

Private Instance Methods

load_properties() click to toggle source
# File lib/tinderfridge/device.rb, line 154
def load_properties
  if device_info
    properties_file = File.join(
      File.dirname(__FILE__),
      'devices',
      device_info[2][1],
      device_info[2][1],
    ) + '.json'

    if File.readable? properties_file
      begin
        require 'json'
        JSON.load File.read properties_file
      rescue
        {}
      end
    else
      {}
    end
  else
    {}
  end
end