0

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.

Graben
  • 202
  • 3
  • 8
  • Just found this, I guess it cannot be done with defines inside defines. http://stackoverflow.com/questions/6148066/preprocessor-directives-inside-define But it could be done with int const for example. – Graben Jan 29 '14 at 05:36

1 Answers1

2

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.

zmo
  • 24,463
  • 4
  • 54
  • 90
  • What I want to achieve here is to make the compiler produce an error if pin 3 for example is used in multiple libraries I made. I want the compiler to produce an error which I will see at build/compile time. Of course the way to claim pin use at the top of each object/library should be the same for everything I will be using. – Graben Jan 29 '14 at 16:57
  • In the mean time I stumbled upon Cosa which seems nice and has a Pin allocator object. That does the job too but takes memory & space. – Graben Jan 29 '14 at 17:00
  • BTW, graben is me, the one who posted the question in the first place! I commented my own question. – Graben Jan 29 '14 at 17:01
  • I don't have the solution, but I have one ingredient for your macro: If you create a switch statement that uses the defined pins values as case values, the compiler will (likely) complain if there are duplicate case values. – Dithermaster Jan 31 '14 at 00:49