Jump to content

Ruby and enumeration


Superp

Recommended Posts

Hello,

I have a question about discovery of devices with Ruby. Enumeration supplies the numeric device_identifier for each device, for instance 2103 for a 'LED Strip Bricklet 2.0'.

See documentation.

Next steps would be to load the file defining the matching class, and to initialize an instance of that class:

require 'tinkerforge/bricklet_led_strip_v2'

Tinkerforge::BrickletLEDStripV2.new ...

In other words, I need the name of the matching class for a '2103 device' and the file which defines that class.

Tinkerforge::DEVICE_DISPLAY_NAMES only maps device_identifier to device_display_name.

How do I map numeric device_identifier to class and file?

One way would be to load all 139 device classes, and than loop through all Tinkerforge::Device descendants until I find the one with the matching DEVICE_IDENTIFIER constant. That sounds like a really bad idea.

Has anyone figured this out yet?

bearbeitet von Superp
a %#@& typo
Link zu diesem Kommentar
Share on other sites

Tinkerforge::DEVICE_DISPLAY_NAMES is internal for error message purposes.

What you're looking for is a device factory. The Ruby bindings don't have that.

7 hours ago, Superp said:

One way would be to load all 139 device classes, and than loop through all Tinkerforge::Device descendants until I find the one with the matching DEVICE_IDENTIFIER constant. That sounds like a really bad idea.

That's not a bad idea at all, that basically how Brick Viewer solves this problem. But instead of doing a linear search, Brick Viewer builds a dictionary that maps device identifier to device class and then uses that dictionary to do the lookup efficiently.

What are you trying to build that requires a device factory?

Link zu diesem Kommentar
Share on other sites

Hi Photron,

Thanks for responding. I am basically just a lazy programmer who likes magic.

What I have done so far is write a little bit of Ruby that 1) takes stock of Device descendants. 2) requires one of TF's source-files. 3) takes stock again and compares to (1) to identify any newly defined class. 4) gets DEVICE_IDENTIFIER and DEVICE_DISPLAY_NAME from the newly defined class. 5) Repeats for all source-files.

This generates a device_info.txt which maps device_identifier, device class, source-file, and device_display_name. You can use any of these four to look up the other three.

This is my third week of using TF and I hope to make it part of a fairly big project. 

Link zu diesem Kommentar
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...