Jump to content

Ruby and enumeration


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?

Edited by Superp
a %#@& typo
Link to post
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 to post
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 to post
Share on other sites

So, I think I found a working solution, which I'll share here in case anyone else runs into the same limitation.

Tinkerforge.device_info 2103
=> [2103, "LED Strip Bricklet 2.0", ["Tinkerforge::BrickletLEDStripV2", "bricklet_led_strip_v2"]]

and

my_devices = Tinkerforge.connect.discover

Source here.

Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...