Arduino - How to write Macro that define used pins by name and number and throw compiler error if pin is used elsewhere

StackOverflow https://stackoverflow.com/questions/21422617

  •  04-10-2022
  •  | 
  •  

Question

I have been playing with this for the last 2 hours now. It should be simple but it does not work. I am not really familiar with macros and I never used them really because of their known instability. But in this case... I don't see any other better way to not use any chip memory.

What I want is not to use memory on chip for this so I choose precompiler directives, especially macros. The macro just have to define stuff, not return anything. I want that macro to be equivalent to this code :

#define PIN3 = 13;
#define PINLED = 13; 

And it should be called like that :

P(13,LED);

So that way I can reference PINLED in my code and get a compiler error if any other library or code I use happens to use PIN13 when I put the P(13,LED) in the top of all the files that uses this pin in my project. I want something that names all pins the same way.

I want the 2 constants/defines to be "defined" so PIN13 cause a compiler error, but PINLED might be named different in many projects

I have tried this :

#define P(no_,name_) \
if (true) { \
PIN##name_ = no_; \
PIN##no_ = no_; \
}\
else true

This works but does only 1 define instead of 2 :

#define P(no_,name_) PIN##name_ = no_ 

This was suggested by many as the correct syntax. I also tried with the do... while(0) syntax and other tricks so I can use the macro as a function with a ; after it but is does not work and always throws some error.

I am using the Ino project to build because I cannot live with the arduino IDE which is pure crap compared to other IDEs.

Was it helpful?

Solution

Sorry, but your question is hardly understandable. You say you want to check whether a pin has already been used in another part of the project, and in the same time you're showing code for defining macros in macros.

But that's where it hurts, like @graben showed, it's simply not possible to achieve in C. First of all both of your syntaxes are wrong:

#define P(no_,name_) PIN##name_ = no_ 

you're not creating a macro name PINLED to which you assign 13, but you're assigning to the C variable PINLED the value 13. To make your PIN definition macro work, you'll need to use const int variables, which usually are easily optimized by the compiler.

Now, to get to the goal you say you want to achieve, I think it's very unlikely you can do it in macro processor code, at least in an elegant way...

And I don't think that's even necessary!

If you design well your libraries, you should not be using the pin number throughout your code and libraries, but design them so you define pins for each library at the library initialization stage. That's why usually Arduino libraries work in three steps:

  1. allocate the memory (which is done by calling the constructor, which is often made in the included header file as a global object) ;
  2. configure the instance (which is done with the .begin() method)
  3. use the instance

so basically, if you have all your pins defined in the same file, you should not run into pin reuse elsewhere in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top