Go Down

Topic: Is there a way to reference one variable with another? Preferably by name. (Read 1 time) previous topic - next topic

Dyslexicbloke

I have, will have, a text file on SD card, it will hold the text I want displayed on an LCD

Obviously I can open the file and read a line or character which I then print on the LCD, hardly rocket science ....
However I want to have the text work like a mark-up language so that I can include dynamic data.
Suppose I decide that text in square brackets needs to be replaced with the contents of a string array ...
My file might look like this ...
      - Battery Status -
Voltage:          [BatVolts]V
Temperature:  [BatTemp]C

When stepping through the file I would want to replace [BatVolts] with the contents of of the char array BatVoltrs.

Obviously I could have a huge list of If's or a case statement but I don't like either of options much ...
If there is better way I would very much appreciate some help, I'v been digging and cant find what I need.

Thanks
Al




If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

PaulS

Quote
Obviously I could have a huge list of If's or a case statement but I don't like either of options much

That's too bad.

At compile time, the names go away, so the Arduino has no way of knowing which array you mean when you wish to refer to an array by name.

You can either do as you suggest or create a map (a collection of key, value pairs) at compile time. The key will be the array name. The value will be the address where the data is stored.

When you encounter a name in your file, see if the name is in your map. If so, you know the address where the data is.

wildbill

Another way to do the same thing would be to make your file like this:
Code: [Select]

     - Battery Status -
Voltage:          [%1]V
Temperature:  [%2]C


Set up a 2d array of char and when you see %<n>, send the nth row to the LCD. Of course, you'll need to populate the array appropriately, which effectively means you're replicating the mapping functionality.

guix

An easier solution maybe:
Code: [Select]

char strFromFile[] = "     - Battery Status -\nVoltage:          %sV\nTemperature:  %sC"; //normally in your file...

char dest[128];
sprintf( dest, strFromFile, BatVolts, BatTemp );
...


Or if you really want to replace the [keywords], you could make a strreplace function (many examples on google) and then use it like:
Code: [Select]

strreplace( strFromFile, "[BatVolts]", BatVolts);
...

Or even
Code: [Select]

#define ReplaceKeyword(keyword) strreplace(strFromFile, "["#keyword"]", keyword)

ReplaceKeyword( BatVolts );
...


Just giving ideas out of my head, I don't know if it will help :)

PeterH


When stepping through the file I would want to replace [BatVolts] with the contents of of the char array BatVoltrs.


And I assume that there are other variables that you would want to be able to refer to.

Some implementations of printf() and its friends support positional specifiers. That means that your format string can refer to a specified argument, instead of just referring to the next unused argument as it normally would. As far as I remember, the syntax for invoking this would be something like %1$d (to insert the first argument as a decimal number) instead of just %d to print the next argument as a decimal number. Obviously it works for all format directives and arguments. However, I don't know if the AVR libc supports it - and I'm too lazy to go find out. If it's available, it would give you a nice simple, flexible solution as long as the list of variables that you wanted access to was fixed and known in advance.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up