I'm trying to write up some code I've developed in to a library I can make more broad use of, putting it in to h and cpp files and such so as to be able to call it from other sketch code.
But in some of the code I want to "libraryise", I've got things like this which happen inside a function:
if(RealTimeVariable==1){
Length=sizeof(Type1Array);
memmove(ClonedArray,Type1Array,sizeof(ClonedArray));
Ready=1;
}else if(RealTimeVariable==2){
Length=sizeof(Type2Array);
memmove(ClonedArray,Type2Array,sizeof(ClonedArray));
Ready=1;
}else if(RealTimeVariable==5){
Length=sizeof(Type4Array);
memmove(ClonedArray,Type4Array,sizeof(ClonedArray));
Ready=1;
}else if(RealTimeVariable==7){
Length=sizeof(Type2Array);
memmove(ClonedArray,Type2Array,sizeof(ClonedArray));
Ready=1;
}
Where a variable within the function I want to libraryise, "RealTimeVariable" is used to select which of a bunch of different arrays should be cloned in to a copy array. Later processing is then done on ClonedArray.
There is a mapping here between RealTimeVariable and which array gets copied:
RTV=1, Type1Array
RTV=2, Type2Array
RTV=5, Type4Array
RTV=7, Type2Array
I want to make this mapping "user definable", in the ino sketch code when using the library. Ideally not having to force the arrays, in the finalised version, to be named as TypeXArray either.
As you can see, the X in TypeXArray doesn't necessarily correspond to the value of RealTimeVariable, and there are options for one-to-one and many-to-one mappings, for any RealTimeVariable value there will be only one array type to select, but multiple RealTimeVariable values may sometimes lead to the same TypeXArray being cloned.
If RealTimeVariable isn't equal to any option on the mapping then no cloning occurs and the Ready flag is not set to 1.
The TypeXArray arrays are not all the same length, but they are all guaranteed to be shorter than the size available for ClonedArray. Where the memmove is done, the proper things of TypeXArray get copied over, as does "junk" following them from whatever areas of memory next get read due to the copying of a whole ClonedArray worth of bytes. The Length measure is used by later code to know to ignore and write zeroes over the later parts in ClonedArray.
I can guarantee that these arrays are always arrays of uint8_t variables.
Wat then is the best way to "libraryise" this functionality, so a user can define the mapping to use?
Can a library function call a user defiend function in the ino sketch which does this?
Is there a way to supply this mapping to the library code via some sort of "constructor" function which can be used to tell the rest of the library's functions in advance how to arrange the cloning?
This setting of mappings only needs to be done at compile time, not runtime. RealTimeVariable gets measured while running, and detremines which cloning to make, but the mapping for these clonings can be set at compile time and does not change.
What is the best way to proceed, thanks