Skip class / methods that are not defined?

This may sound like an odd question, but perhaps there's a solution I am missing.

I've written some code for an MP3 Player module that many other people are using. I upgraded my MP3 player, and unfortunately, its library is not backwards compatible. Short of creating two versions of my code, I am trying to load the correct library with a preprocessor conditional (which works just fine), however, any reference to the older MP3 library / class / methods breaks with "undeclared" errors.

Is there some hack or way around this? Below is a snippet of code hopefully illustrating the issue... the error comes from line "DFPlayer.EQ(DFPLAYER_EQ_ROCK)" because the newer version of the library doesn't include the EQ method

#define MP3_PLAYER_TYPE 1 //set to 1 for Mini Pro or to 0 for Mini

#if MP3_PLAYER_TYPE > 0
byte mp3_player = 1;
#include <DFRobot_PLAY.h>
DFRobot_PLAY DFPlayer;
#else
byte mp3_player = 0;
#include <DFRobotDFPlayerMini.h>
DFRobotDFPlayerMini DFPlayer;
#endif

void setup() {

if (mp3_player == 1) {
DFPlayer.setPlayMode(DFPlayer.SINGLE);
DFPlayer.setPrompt(false);
DFPlayer.switchFunction(DFPlayer.MUSIC);
sounds_sd = DFPlayer.getTotalFile();
} else {
DFPlayer.EQ(DFPLAYER_EQ_ROCK);
sounds_sd = DFPlayer.readFileCounts();
}

}

You can't have code that will get compiled using the functions from the wrong library. You will need to use the same #if pattern in setup that you used to control the library include.

You might want to hide all that stuff in your own class that has a standardized set of functions each of which uses the appropriate library function controlled by #if.

1 Like

thanks for the response!

I was afraid of that, and makes sense - it can't compile if the methods don't exist.

do you mean I can use the #if structure within code, too, and not just as preprocessor?

otherwise, yes, I guess I will need to class it all out, else make other uses comment / uncomment the appropriate code (but that's lazy and unfair of me! lol)

What do you mean "within code"? You would just be using the same type of macro sequences to select the proper calls within a function rather than for selecting which global variables to define. But it's still the preprocessor ... not the compiler and not at runtime.

I meant exactly that... "within code" meaning anything in loop() or functions - I thought maybe the preprocessor #if structure could only be used in header of code.

so I can do something like this within setup, loop, or my own functions?

#ifndef DFPlayer.EQ()
//its not defined, so other player class is loaded
DFPlayer.setPlayMode(DFPlayer.SINGLE);
DFPlayer.setPrompt(false);
DFPlayer.switchFunction(DFPlayer.MUSIC);
sounds_sd = DFPlayer.getTotalFile();
#else
DFPlayer.EQ(DFPLAYER_EQ_ROCK);
sounds_sd = DFPlayer.readFileCounts();
#endif

You can use preprocessor directives wherever you like.

1 Like

That solves that, then! I assumed since I can't use a normal if statement outside of setup, loop, or functions, that I couldn't use preprocessor directives inside of those.... silly assumption, I should've just tried it lol

thanks so much for the help, all!