code issue with defines?

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need to find a way to get my defines to run time data. Or at least to satisfy the function that needs it.

For example, and I know this wont work, but like this

if(1)
{
	#define CFG_DEVICE_NAME     't','e','s','t',
	#define CFG_DEVICE_NAME_SIZE  4
}
else
{
	#define CFG_DEVICE_NAME     'T','E','S','T'
	#define CFG_DEVICE_NAME_SIZE  4
}

int  descStringDevice[] PROGMEM= { CFG_DEVICE_NAME_SIZE | (3<<8), CFG_DEVICE_NAME } ;

Naturally you will get the second as you have two defines. I tried this.
extern char CFG_DEVICE_NAME [] ={'t','e','s','t' };
CFG_DEVICE_NAME_SIZE = 4;
and all sorts of ways to make things work but I get initializer element is not constant for the CFG_DEVICE_NAME_SIZE. The compiler is ok with CFG_DEVICE_NAME, buts its not showing the string right.

Is there any way to set these defines dynamically?

Last Edited: Thu. Jul 2, 2009 - 11:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You want to look at #if and #ifdef

Mike

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
I need to find a way to get my defines to run time data. Or at least to satisfy the function that needs it.

..
Is there any way to set these defines dynamically?

#define is a pre-processor directive. The pre-processor simply replaces all occurances using the #define. #if and #endif are also observed by the pre-processor, so you can't use these for changes at run-time.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

telliott wrote:
S_K_U_N_X wrote:
I need to find a way to get my defines to run time data. Or at least to satisfy the function that needs it.

..
Is there any way to set these defines dynamically?

#define is a pre-processor directive. The pre-processor simply replaces all occurances using the #define. #if and #endif are also observed by the pre-processor, so you can't use these for changes at run-time.

It is ok that I never change it but I need to build it based on a few detections.

Quote:
You want to look at #if and #ifdef
But I thought those where compiler ifs? I need to do some probing before I set my data, am I sol?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, I think I misunderstood your question. If you want to switch between data at runtime you need to create all the data possibilities and then point to the desired one based on your runtime needs. What you need is a level of indirection, so start reading up on pointers and taking addresses of data (or code).

Mike

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
I need to do some probing before I set my data, am I sol?

There is always a way! If you just need to change the case of the device ID, why not just modify the upper/lower case bits. If it is more complicated than that, you might store all the possible variations in flash and retreive the proper one with an index or something like that.. Maybe this is something you could configure via a terminal emulator and store in eeprom?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
you might store all the possible variations in flash and retreive the proper one with an index or something like that.

Yeah this is sort what I was thinking I'd have to do, but I may not have enough room for it.

Thx guy, I look in to these ideas.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just a quick follow up question if I could.

referencing https://www.avrfreaks.net/index.p...

under the text.

Quote:

Now that the PROGMEM string has a "const" modifier, we cannot try to modify it in our code. The pgmspace.h header also exposes a neat little macro, PSTR, which by some GCC magic allows you to create inline strings:

He goes on to say "The downside to using the PSTR macro rather than a PROGMEM modified string variable is that you can only use the PSTR string once. " in my case, and if I understand this right is perfect. I do need it change just once..

but for the life of me I dont understand the function_P ? He uses this USART_TxString_P and for his example that good and all but what, if any, function would I need to make?

I could do my one time int like so

#define CFG_DEVICE_NAME     't','e','s','t'
#define CFG_DEVICE_NAME_SIZE  4
int  descStringDevice0[] PROGMEM= { CFG_DEVICE_NAME_SIZE | (3<<8), CFG_DEVICE_NAME } ;

but I dont see how I can func_P(PSTR("FLASH STRING")); and over right it?

maybe I'm wasting time here, but wanted to ask.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
but for the life of me I dont understand the function_P ? He uses this USART_TxString_P and for his example that good and all but what, if any, function would I need to make?
It doesn't have to be in a function. But you will need to use functions like pgm_read_word() to access the data in the program space.

This should be fairly straight foward with pointers. if you want Config-1 point to Program Memory that holds the string for Config-1 else point to different location that holds another string.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the main problem is (PSTR) needs to be inside a function can't be in an initializer. And I cant find a way to point it after words.