-1

In a arduino application i am working on, i need to find a #define that the application don`t really know the "name" of. I have the following defines that have been created by another application (Sigma Studio) included in a header:

#define MOD_PLUGINS_HP81_ALG0_STAGE0_B2_ADDR           4160
#define MOD_PLUGINS_HP81_ALG0_STAGE0_B1_ADDR           4161
#define MOD_PLUGINS_HP81_ALG0_STAGE0_B0_ADDR           4162
.....
#define MOD_PLUGINS_HP71_ALG0_STAGE0_B2_ADDR           4165

Obviously i can see these defines, but there are 500+ of these defines. What is of interest is to be able to use integer with a string to find correct define. Is it possible? I can generate the needed definition string like this:

  int channelId = 7;
  int presetId = 1;
  char knownValue1[15] = "MOD_PLUGINS_HP";
  char knownValue2[21] = "_ALG0_STAGE0_B2_ADDR";
  int charSize = sizeof(knownValue1) + sizeof(knownValue2) + sizeof(channelId) + sizeof(presetId);
  char findThisDef[charSize];
  strncpy(findThisDef, knownValue1, charSize);
  char buffer[sizeof(channelId)];
  sprintf(buffer, "%d", channelId);
  char buffer2[sizeof(presetId)];
  sprintf(buffer2, "%d", presetId);
  strcat(findThisDef, buffer); // Includes channel number
  strcat(findThisDef, buffer2); // Includes preset number
  strcat(findThisDef, knownValue2);

So now the application knows the char of the define i need "MOD_PLUGINS_HP71_ALG0_STAGE0_B2_ADDR", how can i now fetch the number "4165" to my application?

Stian Blåsberg
  • 115
  • 2
  • 8
  • 7
    You can't. Preprocessor tokens get replaced before the code is even compiled and don't exist at runtime – UnholySheep Jan 13 '22 at 19:37
  • As said by others, `#define` is compile time only. You will have to make your own data structure and initialize to fetch this data during runtime. I suggest you review `std::map` – infixed Jan 13 '22 at 19:43
  • Sounds like you want to map strings to the corresponding value of a define. Dead easy with `std::map` in regular C++, but I don't think that exists in Arduino's reduced Standard Library. You could build your own mapping table, and since you know all of the contents ahead of time it shouldn't be too brutal a task. – user4581301 Jan 13 '22 at 19:45
  • 1
    These look highly regular so it may be easy to write a function to compute the value based on the variable parts of the constant names. – romkey Jan 14 '22 at 00:34

1 Answers1

0

What you are asking for is not possible. The preprocessor runs at compile time, just replacing all occurences of string defined in #define with corresponding value. The running assembly knows nothing about these macros.

You can define a structure containing a key and a value and store them in array. To get the value you can search through the array looking for given key. However if you have a lot of these entries then it can be really slow.

Kamil B
  • 33
  • 9