Best way to query array by int/byte?

I have something that seems trivial to solve so probably it's just my lack of skill.
I have to classes, one with enum

enum availableColors {
        COLOR_1 = 0x41,
        COLOR_2 = 0x45,
        COLOR_3 = 0x49,
        COLOR_4 = 0x4D,
        COLOR_5 = 0x51,
        COLOR_DEFAULT = 0x49,
    };

and one with array

uint16_t syncColors[8][3] = {
        {80, 208, 255 },  // default
        {80, 208, 255},
        {255, 160, 16},
        {0, 32, 255},
        {255, 0, 0},
        {0, 255, 0},
        {0, 0, 255},
        {255, 0, 255},
    };

I can change them both to suit my needs, but what't the best approach that I can query syncColors with value/enum from availableColors ?

Something like syncColors[COLOR_1] or syncColors[0x41] (just to ilustrate what I need)

What do you mean by "query syncColors"?

This would not be a valid construction:
syncColors[0x41]

well, so code is incomplete - because I need to somehow mark syncColors so I can query them when receiving value from syncColors. e.g. I receive COLOR_1 which is 0x41, and that corresponds to syncColor[0].
but how to do it without having additional array for mapping.

And yes I am aware that syncColors[0x41] is no valid, it was just to illustrate what I want to achieve

Not possible. Valid values for the first index of the syncColors array are 0 through 7.

yes, I am aware of that... but how can I shape those arrays/enums so I can have something common in both to query them by the same value. like e.g 0x41 could be converted to integer and by this integer maybe I can query syncColors.

Something like this would be valid.

byte red = syncColors[COLOR_1 - 0x41][0];

But the steps between availableColors is not 1, so you need a more general mapping.

Put a bit more planning into the choice of variables and program design.

That's why I am asking now :wink: I am in planning phase and trying to thing how I can have those 2 different arrays associated together maybe something like Associative arrays in C++ - GeeksforGeeks
But that seems not to be available in arudino ide

If you are stuck with those two definitions, use a lookup array.

byte first_color=syncColors[index_lookup[availableColors]][0]

It is a dangerous approach, because it is easy to go out of bounds and get nonsense.

That's the thing, I am not stuck with them and I can change them as I wish, but I just don't have a clever Idea. My first thought was just because there is only couple of them, I could create some helper method with switch in it to find my what I need. but wanted to be clever

Then change availableColors to be integers 0 to 7 (whatever matches the index range of syncColors).

problem with that is I receive values in bytes, and I have to match syncColors by byte somehow.
I've found that Arduino-libraries/HelloHashMap.pde at master · vlast3k/Arduino-libraries · GitHub

which looks promising

The numbers are all pretty close to one another. You oculd sort them out and see if you could do some transform. floor((c(0x41,0x45,0x49,0x4d,0x51)-0x40)^.6)-1 looks promising...but unmaintainable.

A lookup array or a switch would be sufficiently clever.

If the upper and lower values are not too different than I would simply put up with the fact that you are going to waste some memory holding intermediate but unused array elements.

Put the array in PROGMEM and it is not going to matter much anyway

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.