Colleagues,
I realize this is more of a c question than a TM4C one... or maybe at least an "embedded c". But I'll assume some good help can appear here.
Let's say I have a library created to deal with measuring a certain sensor. This sensor has parameters that could be organized together inside a struct called sensorStruct_t, such as:
typedef struct
{
uint32_t sensor.outbase;
uint32_t sensor.outpin;
uint32_t sensor.inbase;
uint32_t sensor.inpin;
uint32_t sensor.readrate;
float sensor.temperature;
float sensor.humidity;
}
sensorStruct_t;
I find that most libraries for any similar purpose tend to have an "initialization" call. If I can live with the application having access to all elements of the sensor above, the initalization could be something like:
sensorStruct_t sensorData1; sensorInitialize(&sensorData1);
That is clean and organized. But I am unsure of how to manage a variable number of sensors...
One solution I've already used is to predetermine the maximum number of structures inside the library, and instead of passing a pointer from the application, to pass the actual parameters and have the library side manage some sort of "array of structures":
localSensor = sensorInitialize(outbase,outpin,inbase,inpin,readrate);
Where localSensor would just be the pointer to an address returned by the function. The initialize function adds the sensor to "the next available slot".
Again, might be c-programming class #12 (which I skipped), but I honestly don't know the best way to initialize "as many sensors as needed"... For now I've been actually creating a maximum quantity in the library side, and wasting the unused memory locations.
I hope this question is properly explained, as it did become a bit hard to make a clear point with a concise example... Any comments or guidance to specific literature are most welcome.
Regards
Bruno