Storing button coordinates inside key-value pairs

Hello.

I would really appreciate if anyone told me how to make key-value pairs. I am trying to store button coordinates (for a screen) inside an array but I want them to be retrievable by a key instead of a number index.

I don't want to define x1, x2, y1, y2 like:

int coords[] =
{
  20, 93, 40, 83
};

I want to rather do it something like

int coords[] =
{
  {"buttonOnex1", 20},
  {"buttonOney1", 93},
  {"buttonOnex2", 40},
  {"buttonOney2", 83}
}

I've tried a lot of googling, stack overflow, and digging on the arduino forums and couldn't find a solution. Is there any way to achieve my goal?

Also one thing, these values are not going to change, they will stay constant. So a workaround involving me being able to write values to the key-value array is not required.

Quick answer with a question.

Have you learned about the struct qualifier keyword in C/C++?

It lets you package up variables of different types that woukd logically like to travel around together.

coords could be an array of structured variables.

a7

What mechanism in C/C++ do you imagine using for the key?

Already available is enum, which substitutes a name for a number.

1 Like

looks like you are searching for a structure

struct Button{
  const char[16];
  uint8_t x;
};
Button coords[] =
{
  {"buttonOnex1", 20},
  {"buttonOney1", 93},
  {"buttonOnex2", 40},
  {"buttonOney2", 83}
};

You basically want a dictionary/map structure.

You can try linking in std::map in your code, but even if it works, I doubt that it would be advisable on anything below an ESP32 or STM32.

#include <map>

using namespace std;

void setup() 
{
  std::map<const char*, int> mp;
  mp["one"] = 1;
  mp["two"] = 2;
  mp["three"] = 3;

}

void loop() 
{
}

That works for a single integer. For a pair, make a struct or a class or std::tuple and put that in the map instead.

HTH

This compiles and runs on wokwi, but that's as far as I'm going to go :slight_smile: Don't recall if I ever used C++ tuples and the syntax looks different from C#, which I'm used to.

#include <map>
#include <tuple> 
using namespace std;

void setup() 
{
  std::map<const char*, tuple<int,int>> mp;
  mp["one"] = make_tuple(1,2);
  mp["two"] = make_tuple(11,222);
  mp["three"] = make_tuple(100,20);
}

Does anyone know how to get Wokwi to show how big the compiled executable is?

Eh, not exactly, I want to store an integer value in each "key".

Oh wow, that's exactly what I need, thanks!

LOL, not even close! As you can see, some of us misinterpreted your request and offered suggestions or solutuions of varying complexity.

You wanted was some symbols to be equal to some numbers.

@Delta_G's

enum CoordEnum {BUTTON_ONE_X_1, BUTTON_ONE_Y_1, BUTTON_ONE_X_2, BUTTON_ONE_Y_2};

does what you need. There's not much more to the *enum*, but you can read about a few things that example does not illustrate or exploit.

You might have thought of your own solution using no more than you know:

int BUTTON_ONE_X_1 = 0;
int BUTTON_ONE_Y_1 = 1;
int BUTTON_ONE_X_2 = 2;
int BUTTON_ONE_Y_2 = 3;

perhaps with byte as the data type.

In the 20th century you might have used

# define BUTTON_ONE_X_1     0
# define BUTTON_ONE_Y_1     1
# define BUTTON_ONE_X_2     2
# define BUTTON_ONE_Y_2     3

Now it would be written

const int BUTTON_ONE_X_1 = 0;
const int BUTTON_ONE_Y_1 = 1;
const int BUTTON_ONE_X_2 = 2;
const int BUTTON_ONE_Y_2 = 3;

But the enum feature of C/C++ makes all those very much tidier. And indeed, anyone writing out those four lines would either quickly be pointed to enum if we saw them here, or realize herself that it's just numbers in order and an enum is the ticket.

There are subtle and not so subtle differences between the three ways, but logically in your code you will end up with… symbols that can be used instead of numbers that you knew and could associate them with at the time you wrote the code.

Sorry to sidetrack you with anything else.

a7

I just wonder why a "name button" couldn't just hold its two x and y coordinates

struct Coord{
   uint8_t x1;
   uint8_t y1;
   uint8_t x2;
   uint8_t y2;
};
Coord buttonOne {20, 93, 40, 83};

straight forward to make a second button.

Then the next step is create a button class, holding its coordinates, size, colour as well as logic for checking presses, holds, drawing etc

Then a container array to hold all the buttons

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