1

I've been using the Arduino IDE for some time now, and I want to shift to coding for the bare AVR microcontroller. So I wanted to begin by porting required arduino libraries to C/C++. I started with the Radiohead library, but I've noticed a lot of variables/tokens that are directly used without a definition for them. For example:

 // path :RadioHead/RH_ASK.cpp
#if (RH_PLATFORM == RH_PLATFORM_GENERIC_AVR8)
 #ifdef RH_ASK_PTT_PIN 
 RH_ASK_PTT_DDR |= (1<<RH_ASK_PTT_PIN); 
 RH_ASK_TX_DDR |= (1<<RH_ASK_TX_PIN);
 RH_ASK_RX_DDR &= ~(1<<RH_ASK_RX_PIN);
 #else
 RH_ASK_TX_DDR |= (1<<RH_ASK_TX_PIN);
 RH_ASK_RX_DDR &= ~(1<<RH_ASK_RX_PIN);
 #endif
#else
 // Set up digital IO pins for arduino
 pinMode(_txPin, OUTPUT);
 pinMode(_rxPin, INPUT);
 pinMode(_pttPin, OUTPUT);
#endif

None of the tokens in this snippet like "RH_ASK_PTT_PIN" or "RH_ASK_TX_PIN" were mentioned anywhere in the code before or any other file that was included.

I understand what the code does, but how does the compiler know what these tokens mean? I tried to compile this code using Atmel Studio but it gives me an error (As it should): "RH_ASK_PTT_PIN" not declared in this scope So how does this library work with the Arduino IDE?

Link to the library : https://github.com/PaulStoffregen/RadioHead

Also can someone suggest tags for this post? Its my first post here.

asked Mar 8, 2018 at 21:10

1 Answer 1

3

They're not defined. If you notice that whole block is wrapped in a #ifdef which will be false unless you're on some specific platform, where the platform defines those macros.

Everything else uses the last little bit:

pinMode(_txPin, OUTPUT);
pinMode(_rxPin, INPUT);
pinMode(_pttPin, OUTPUT);

You only need to care about that bit - nothing else is of any concern unless you're using a "RH_PLATFORM_GENERIC_AVR8" (whatever that is).

answered Mar 8, 2018 at 21:17
2
  • Thanks for the prompt reply, I understand that to rewrite this I don't need to care about the #ifdef block but how does this file compile in the first place without actually defining the macros? I added this library as a .zip file using the library manager of the Arduino IDE, the entire folder does not mention them. Commented Mar 8, 2018 at 21:41
  • 2
    The top two thirds of the block only gets compiled if they have been defined. Since they haven't been defined it gets completely ignored. C compiling is done in multiple phases. The first is the "preprocessor" that looks at the #ifdef etc bits and expands them where it can. The C compiler doesn't see the top part at all because the preprocessor deletes it. Commented Mar 8, 2018 at 21:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.