I’m pretty new to PlatformIO and CPP. I have a question regarding organization of code and best practices.
In ‘main.cpp’ we have setup() and loop(). Lets say in setup we have opened a namespace in eeprom to write to. Now, lets say I have a function ‘myfunc.cpp’ that needs to write to eeprom. Of course it has no idea about this as is separate.
Is best practice to create an extern in ‘myfunc.h’ and do the writing back in ‘main.cpp’? Create some sort of ‘main.h’ maybe?
Can’t seem to get my head around accessing things in other functions outside main that are setup inside main’s setup()… If ya get what I mean
When I put functions in a separate .cpp file, I also add a .h file which declares the functions that the .cpp file exports. This is optional but I also put those functions in a namespace that match the file names. Here is an example
This line in setup()
Uses this function which is declared and implemented in a .h and .cpp file respectively
The namespaces are optional, to improve readability. Also, for functions and global variables that you don’t want to allow other files to use, also add ‘static’ at the beginning of the line.