Question about library scope and RadioHead

I'm using the ArduinoLowPower library with the SAMD21 M0 to handle deep sleep and interrupts, which works fine with small programs. The library automatically instantiates itself (in ArduinoLowPower.cpp) as

ArduinoLowPowerClass LowPower;

ArduinoLowPower.h includes this line:

extern ArduinoLowPowerClass LowPower;

However, when attempting to integrate the library into a much larger program using the RadioHead library, during compilation of RadioHead source code, the compiler or linker does not recognize the library instance.

For example (main source code)

#define SEROUT Serial1
#include <SPI.h>
#include "ArduinoLowPower.h"  //same error with #include <ArduinoLowPower.h>
#include <RH_RF95.h>

and adding this to RH_RF95.h

void RH_RF95::setModeRx()
{
    if (_mode != RHModeRx)
    {
	modeWillChange(RHModeRx);
	spiWrite(RH_RF95_REG_01_OP_MODE, RH_RF95_MODE_RXCONTINUOUS);
	spiWrite(RH_RF95_REG_40_DIO_MAPPING1, 0x00); // Interrupt on RxDone
	_mode = RHModeRx;

	// set deep sleep mode (from ArduinoLowPower library)
       LowPower.deepSleep();
	}
}

results in a scope error message:

C:\Users\Jim\Desktop\FeatherM0LoRa\libraries\RadioHead\RH_RF95.cpp: In member function 'void RH_RF95::setModeRx()':
C:\Users\Jim\Desktop\FeatherM0LoRa\libraries\RadioHead\RH_RF95.cpp:439:5: error: 'LowPower' was not declared in this scope
439 | LowPower.deepSleep();
| ^~~~~~~~

Hopefully, someone can explain what I'm doing wrong!

Just to verify spelling, etc., when putting that call in the main code, no error message is generated:

void loop()
{
  if (rf95.available())
  {
    // Should be a message for us now
    uint8_t len = sizeof(buf);

    if (rf95.recv((uint8_t *)buf, &len))
    {
      buf[len] = 0; //zero terminate
      SEROUT.println(buf);
    }
  }
  LowPower.deepSleep();  //dummy call to check whether the library is instantiated
}

Nevermind, it was the usual issue with IDE 1.8.x preprocessor.

Fixed by including the library header into the Radiohead library source code as well.

#include <ArduinoLowPower.h>

if I understand correctly , you are modifying a function from the the RH_RF95 class (RH_RF95::setModeRx()) , so it's an independent compilation unit which does not depend on your main sketch.

➜ if that's correct, then it's not the IDE. it's C++ and the issue is on you.

if you want the RH_RF95 class to know about the ArduinoLowPowerClass you definitely need to include ArduinoLowPower.h in the compile process of that class.

That makes sense, thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.