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.

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).

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. 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.
    – Majenko
    Mar 8, 2018 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.