Page 1 of 1

Macro Concatenation

Posted: Fri Nov 20, 2015 11:55 am
by SeeCwriter
This may not be the right group, but I had a question about using macro concatenation feature of the compiler.

I'm using v2.7.3 on a Nano. This is the code:

Code: Select all

#define HEARTBEAT_PIN	12	// Heartbeat LED pin.
#define GPIO_FUNCTION(p)	(PIN_ ## p ## _GPIO)
...

Pins[HEARTBEAT_PIN].function( GPIO_FUNCTION( HEARTBEAT_PIN ) );

I get error 'PIN_HEARTBEAT_PIN_GPIO' not declared.

Why doesn't the 'p' in the macro get replaced with the number 12?

Re: Macro Concatenation

Posted: Fri Nov 20, 2015 1:15 pm
by sulliwk06
I always find that the way some macros get resolved messes with my head. I think this may do what you're looking for:

Code: Select all

#define CONCATENATE_3(x,y,z) x##y##z
#define GPIO_FUNCTION(p) CONCATENATE_3(PIN_,p,_GPIO)

Re: Macro Concatenation

Posted: Fri Nov 20, 2015 1:31 pm
by SeeCwriter
That works. Thank you.