In embedded C, it’s natural to have some fixed / general algorithm, but more than one possible implementation. This is due to several product presentations, sometimes options, while its just part of the product’s roadmap strategies, such as additional RAM, a different MC-set MCU, increased frequency, etc.
In most of my projects, I understand that separating the main material, algorithm and logical architecture from the actual functions that implement an external state assessment, saving time, memory, etc.
Naturally, I use the C function pointer mechanism, and I use a set of meaningful names for these pointers. For example.
unsigned char (*ucEvalTemperature)(int *);
The one who stores the temperature in int and returns OKness.
Now imagine that for a specific product configuration I have
unsigned char ucReadI2C_TMP75(int *);
a function that reads the temperature on the I2C bus from the TMP75 device and
unsigned char ucReadCh2_ADC(unsigned char *);
a function that reads the voltage drop of the diode read by the ADC, which is a way to estimate the temperature with very wide strokes.
This is the same basic functionality as for different products with options.
In some configurations, I will have ucEvalTemperature set to ucReadI2C_TMP75, and on the other ucReadCh2_ADC. In this mild case, in order to avoid problems, I have to change the type of the argument to int, because the pointer is always the same size, but the function signature is not, and the compiler will complain. Okay ... this is not a killer.
, . , Fpointers.
, :
- , char Func (void);
- , /;
- JMP/LCALL (, , ), .
, ... /?