C Definition in sketch to configure library



  • Hello,

    I'm trying to write own library.
    I would like to library was configurable from sketch by #define MY_USE_ADC_FILTER similar like configuration MySensors.
    But my library don't see definition from sketch in my .h and .cpp files.

    I don't know how it is done in MySensors library.
    Could someone just explain how to achieve similar behaviour?

    Thanks
    Robert


  • Mod

    @Robinek70 are the defines in the sketch before including the library? That should be sufficient. If they already are, could you share your code?



  • it is something like below.
    Definitions
    #define MY_USE_ADC_FILTER
    #define MY_ADC_PIN
    are not visible in .h and .cpp.
    I don't know what I'm doing wrong 😞

    //test.ino
    #define MY_RADIO_NRF24
    
    #define MY_USE_ADC_FILTER
    #define MY_ADC_PIN
    
    #include <RPlibs.h>
    #include <MySensors.h> 
    ...
    
    //RPlibs.h
    
    #ifndef MY_ADC_PIN
      #defie MY_ADC_PIN  A0
    #endif
    
    #define DT_FILTER  10
    int getAdc();
    
    //RPlibs.cpp
    #include <RPlibs.h>
    static int prevValue ;
    int getAdc() {
      int value = analogRead(MY_ADC_PIN);
      #ifdef MY_USE_ADC_FILTER
          prevValue = (prevValue *DT_FILTER  + value) / (DT_FILTER  + 1);
      #else
          prevValue = value;
      #endif
      return prevValue 
    }
    

  • Mod

    This works for me
    sketch.ino

    #define MY_RADIO_NRF24
    
    #define MY_USE_ADC_FILTER
    #define MY_ADC_PIN
    
    #include "RPlibs.h"
    

    RPlibs.h:

    #ifdef MY_USE_ADC_FILTER
    #error "Define ADC set!"
    #else
    #error "Define ADC not set!"
    #endif
    

    With the define, the build process outputs

    In file included from C:\Users\Micke\AppData\Local\Temp\arduino_modified_sketch_472392\sketch_sep20a.ino:6:0:
    
    RPlibs.h:2: error: #error "Define ADC set!"
    
     #error "Define ADC set!"
    

    and without the define it outputs

    In file included from C:\Users\Micke\AppData\Local\Temp\arduino_modified_sketch_303778\sketch_sep20a.ino:6:0:
    
    RPlibs.h:4: error: #error "Define ADC not set!"
    
     #error "Define ADC not set!"
    


  • it's very strange for me.
    I checked under two environments:
    Arduino 1.6.7 and Visual Studio 2012 with plugin Visual Micro.
    And my result of tests:
    directly under Arduino, .h file see definition from sketch, but .cpp file don't 😞
    under VS definition isn't visible in .h and .cpp files.



  • full test files

    File in path Arduino/Test/

    //test.ino
    #define MY_RADIO_NRF24
    
    #define MY_USE_ADC_FILTER 
    
    #include <RPlib.h>
    
    void setup() {
    }
    
    void loop() {
      Serial.println(getAdc());
    }
    

    Files in path Arduino/libraries/RPlib/

    //RPlib.h
    #ifndef MY_ADC_PIN
      #define MY_ADC_PIN  A0
    #endif
    
    #define DT_FILTER  10
    
    int getAdc();
    
    //RPlib.cpp
    #include <RPlib.h>
    #include <Arduino.h>
    static int prevValue;
    
    int getAdc() {
      int value = analogRead(MY_ADC_PIN);
      #ifdef MY_USE_ADC_FILTER
    		#error  FILTER
          prevValue = (prevValue *DT_FILTER  + value) / (DT_FILTER  + 1);
      #else
    	#error  NO FILTER
          prevValue = value;
      #endif
      return prevValue;
    }
    

    I always got
    #error NO FILTER


Log in to reply
 

288
Online

6.7k
Users

7.6k
Topics

80.4k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.