Help coding: adding functionality relying on MySensors-func/library (saveState/loadState) (c++)



  • Im using the mySensors functions saveState and loadState to read/write to EEPROM. To facilitate I have written some small functions that do the casting so that I can save uint16_T/float/other by single function call. I would like to organize these functions in a separate file so that I can add this functionality easily to each project. However I struggle to get the structure correct and overcome a compiler warning.

    I have collected the functions in .h-file and .cpp-file
    Example:

    //mysEeprom.h - declaration of my help-files
    void save_uint16(uint8_t position, uint16_t value)
    //mysEeprom.cpp
    void save_uint16(uint8_t position, uint16_t value) {
        saveState(position, (uint8_t)value);
        saveState(position+1, (uint8_t)(value>>8));
    }
    
    //Project file main.cpp 
    (Radio-declaration etc.) ie #define MY_RADIO_RFM69 
    #include <mysensors.h>
    #include <mysEeprom.h>
    
    save_uint16(position, value);
    

    The compiler responds that saveState "was not declared in this scope" (the saveState is decleared inside MySensors/core/MySensorsCore.h.).

    To tell the compiler where to look for saveState I add an #include <mySensors.h>-statement:

    //In MysEeprom.h-file
    #include "mySensors.h"
    

    This time the compiler seems to believe that I want to includes "double" mySensors.h and responds with:

    .pio\libdeps\pro8MHzatmega328\MySensors/mysensors.h:426:2: error: #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
    

    I had this message earlier when the #include <mySensors.h>-statement was positioned before #define MY_RADIO_RFM69 . Putting MY_RADIO_RFM69-statement before include <mySensors.h> solved it (ie my include-statements are put after definitions, just before declaration of global variables).

    I have also experimented organizing my eeprom-functions inside a namespace and a class, but so far have not found a working solution. Help is appreciated, how should this be done, thanks!



  • If you cannot include the whole mysensors header, you maybe can just forward declare the function. (basically, just copy the line from the mysensors.h in your mysEeprom.cpp file.

    /*extern*/ void saveState(uint8_t pos, uint8_t value);
    // extern probably not needed, that's real old C, but you I have had 
    // surprises with the arduino compiler… You might need to add it.
    

    What's the issue with namespaces or classes?



  • @boum said in Help coding: adding functionality relying on MySensors-func/library (saveState/loadState) (c++):

    /extern/

    Wonderful, that did the trick! Thank you very much!

    For namespace/class basically the problem was the same, without "mySensors.h" couldnt find saveState and with "include <mySensors.h>" I had "#error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless."



  • If you want to use library functions outside the main sketch, you can also just include the core header, e.g.

    #include "core/MySensorsCore.h"
    


  • @electrik

    Tested and worked as well. I think this is preferable over the first solution (forward function declaration) as it gives a reference where the functions are defined. Thank you!


Log in to reply
 

Suggested Topics

  • 3
  • 5
  • 8
  • 6
  • 6

51
Online

11.4k
Users

11.1k
Topics

112.6k
Posts