I am programming C in an embedded target. Due to the increasing complexity and testability issues, modularity is a must.
At a glance, the program is a control loop. Read physical inputs using internal hardware, apply some calculations, and apply calculated outputs. However, the control is quite sophisticated, and has many internal states and changing variables.
This control is divided into different modules, bundling the functionalities of the different states. Common tasks/calculations are provided in separate modules and are invoked in the different modules, in order to keep myself DRY. For enum and type consistency across the entire project, a top .H file is used (as OO strategies such as inheritance are not an option in my framework, as far as I am concerned).
My problem arises when deciding how to pass variables to and from modules.
My initial approach is:
mymodule.H:
struct Inputs{
int input1;
...
int inputN;
}
struct Outputs{
int output1;
...
int outputN;
}
void mymodule(Inputs in,Outputs* out);
And in the main function (or the module that invokes this module) have "Inputs" and "Outputs" type structs created.
Then, the variables are copied to the Inputs struct, the function is invoked (referencing the Outputs struct), and once completed, the content of this struct is used for further calculations.
However, this would result in a big memory footprint, as each module requires instances of InputType and OutputType to be created in the calling module. It is not an ellegant solution in my opinion. Dynamic allocation, by the way, is not allowed in my project.
Could you provide me some guidelines and/or alternate ideas to get to a good solution?
Thank you.
Added
One of the solutions could be passing InputStruct also as pointer, but as they are effectively inputs to the module, how could I assure that they are not modified along the code?
Added
By the way,another problem that arises is the fact that not all the modules receive the same variables, and with no inheritance mechanism available (as this is C), each module's structure has to be loaded with the proper values. I am quite obfuscated...