5

I am using RunningMedian Arduino library in a project.

In the library header file the MEDIAN_MAX_SIZE is preset to 19.

     #define MEDIAN_MAX_SIZE     19  // adjust if needed

I need to override the header to make MEDIAN_MAX_SIZE 30 without changing the library files so updates can still be done in the future.

My declatations:

     #define RunningMedian::MEDIAN_MAX_SIZE 30     // library over ride ??
     #define ACTIVE_MAX 30 // max active buffer size
     RunningMedian ActiveSamples( ACTIVE_MAX ); // FIFO readings

     This will not compile.

The library code will not create a buffer greater than MEDIAN_MAX_SIZE.

How can I override the 19 for 30 without changing the RunningMedian.h file and still change MEDIAN_MAX_SIZE size in it's class?

1
  • Change it in RunningMedian.h, if RunningMedian.cpp is separately compiled into an object file then it won't see your re-definition if you place it somewhere else. Commented Feb 10, 2017 at 6:27

3 Answers 3

4

You can #undef MEDIAN_MAX_SIZE and the redefine it, like this:

#ifdef MEDIAN_MAX_SIZE //if the macro MEDIAN_MAX_SIZE is defined 
#undef MEDIAN_MAX_SIZE //un-define it
#define MEDIAN_MAX_SIZE 30//redefine it with the new value
#endif 

You may want to redefine it to its original value after you are done using it with your adjusted value, just in case something else depends on that value being the original.

Sign up to request clarification or add additional context in comments.

1 Comment

@user1213320 Perhaps it is worth mentioning that this code needs to appear right after the first definition, otherwise the old value might be used somewhere else before being redefined. e.g. RunningMedian.cpp might be separately compiled before the header file containing the re-definition is ever included, therefore it will use the old values
4

I think it should be possible to create new header file (e.g. MyRunningMedian.h) in your project, containing just your changed definitions:

#ifndef MEDIAN_MAX_SIZE
#define MEDIAN_MAX_SIZE     30
#endif

Then, you need to pass this header file to preprocessor, to include it as a first #include for the RunningMedian. You can do this by using -include directive, see https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html.

E.g. in PlatformIO you can do it in platformio.ini this way:

[common]
build_flags = 
    -include "include/MyRunningMedian.h"

Comments

3

Alex Zywicki's answer is perfect for cases where it doesn't matter if you redefine the macro. But, to be safe or if you need to ensure the macro gets defined back to its previous definition, you can use the push_macro and pop_macro pragmas (assuming your compiler supports it):

#ifdef MEDIAN_MAX_SIZE
#pragma push_macro("MEDIAN_MAX_SIZE")
#define MEDIAN_MAX_SIZE 30

// Use the modified value for MEDIAN_MAX_SIZE here

#pragma pop_macro("MEDIAN_MAX_SIZE")

This allows you to temporarily redefine macros for a block of code.

Comments

Your Answer

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.