Go Down

Topic: Different board defines/functions/includes in a library (Read 108 times) previous topic - next topic

Adminius

Hallo community,

i have a library, that should work on different boards/platforms (e.g. 328p,32u4, SAMD21, ESP8266...)

Library has a lot of classes in it that are splitted in it own header files:

Class A.cpp
Class A.h
Class B.cpp
Class B.h

Class A uses FuncA that is different on each board
Class B uses FuncB that is different on each board

My idea:
boarddefs.h:
Code: [Select]
#ifdef __AVR_ATmega328P__
#include "328p.h"
#elif __AVR_ATmega32U4__
#include "boarddefs/32u4.h"
#endif


328p.h:
Code: [Select]
void FuncA(){
 //do something for 328p
}
void FuncB(){
 //do something for 328p
}



32u4.h:
Code: [Select]
void FuncA(){
 //do something for 32u4
}
void FuncA(){
 //do something for 32u4
}



in this case i include boarddefs.h in each class-header, but IDE says:
"Multiple definition of 'FuncA()'"
"Multiple definition of 'FuncB()'"

because each class has both FuncA and FuncB

if i do 328p_funcA.h and include it in ClassA.cpp and 328p_funcB.h in ClassB.cpp it works, but i need a lot of files for each board.


Is there a better solution for my problem?

pert

I'd guess that even though the functions are different for each board they will be similar and so it doesn't make sense to have a bunch of duplicate code spread out over different files. Consider just having one of each of these functions that uses preprocessor conditionals to adjust the code for the different models. That seems much easier to maintain and also solves your problem.

Adminius

the functions are not complettly different.
today i have something like this:
Code: [Select]
#ifdef ESP8266
    DEBUG_PRINTLN(F("ESP8266 restart"));
    ESP.restart();
#elif __SAMD21G18A__
    // do reset of arduino zero
    setupWDT(0); // minimum period
    while (1) {
    }
#elif __AVR_ATmega328P__
    // to overcome WDT infinite reboot-loop issue
    // see: https://github.com/arduino/Arduino/issues/4492
    DEBUG_PRINTLN(F("software reset NOW"));
    delay(500);
    asm volatile ("  jmp 0");
#else     
    DEBUG_PRINTLN(F("WDT reset NOW"));
    wdt_enable(WDTO_500MS);
    while (1) {
    }
#endif


on different places in the lib... and this lib a has thousands lines of code. If i will add a new board, a have to go through all lines to add board specific exceptions...

Go Up