I have created an Arduino Library for a device, of which it can be generally configured in several ways. E.g. use interrupts or poll it. From other examples I have created the following files: foo.h, fooConfig.h and foo.cpp shown below for the library. Where fooConfig.h contains the configuration of how to use the shield. Such as with or without interrupts etc...

In doing so I would like the main sketch's INO file to be able to over-ride default settings, which have been declared using #define's. Including in the library's instance. The results show it really does. At least the way I am doing it.

The below code is a simplified example with the problem:

definetest.ino

#define BAR USE_POLL
#include <foo.h>

foo test;

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.print(F("setup's defined BAR was "));
  Serial.println(BAR);

  Serial.print(F("inside foo.begin defined BAR was "));
  Serial.println(test.begin());
}

void loop() {
}

foo.h

#ifndef FOO_h
#define FOO_h

#include "FOOConfig.h"

class foo {
  public:
    int begin();
};
#endif // FOO_h

FooConfig.h

 #ifndef FOOCONFIG_h
  #define FOOCONFIG_h

  #define USE_INT      1
  #define USE_POLL     2

  #ifndef BAR
    //default to using interrupts
    #define BAR USE_INT
  #endif // BAR

#endif  // FOOCONFIG_h

foo.cpp

#include <foo.h>

int foo::begin() {
#if defined(BAR) && BAR == USE_INT
  Timer1.attachInterrupt( isr );  // error here, because of the define...
  return 1;
#elif defined(BAR) && BAR == USE_POLL
  return 2;
#endif
  return 0;
}

Yields the following serial output:

setup's defined BAR was 2
inside foo.begin defined BAR was 1

Where it was desired to have the BAR inside the foo.begin equal to 2, not 1. N

Edit
Report