class Tinkerforge::Device
Attributes
Returns the device's Display Name.
Returns the device's numeric Device
Identifier.
Returns the device's IPConnection
object.
Returns the device's UID. Not to be confused with uid, which returns the numeric UID.
Public Class Methods
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
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
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
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
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
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
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
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
# 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