c++ question for ADC library

Status
Not open for further replies.

Pedvide

Senior Member+
This isn't a Teensy question, but I'm trying to extend the ADC library and I'm having problems with something.
The current structure of the library is as follows:
ADC class instantiates one or more ADC_Modules, each one representing and controlling a physical ADC.

My idea now is to create a ADC_Module_Base interface that all ADC_Modules will have to derive from. Then ADC will have an array of pointers to ADC_Module_Base. Depending on the hardware, different ADC modules (implementations of ADC_Module_Base) will be added to that array (including possible external ADCs). All ADC_Module share the same interface and so it's possible for ADC to operate with them.

So far it's easy, my problem now is this: right now ADC_Module has a function to save and load the configuration of the ADC, so that the ADC class can save it and restore when interrupting a measurement. For example if you do a synchronized measurement (both ADCs at the same time) it does:
Code:
ADC_Module::ADC_Config old_adc0_config = {0};
uint8_t wasADC0InUse = adcMod0->isConverting(); // is the ADC running now?
if(wasADC0InUse) { // this means we're interrupting a conversion
   // save the current conversion config, the adc isr will restore the adc
   __disable_irq();
   adcMod0->saveConfig(&old_adc0_config);
   __enable_irq();
}

And when the synchronized measurement is done:
Code:
// if we interrupted a conversion, set it again
if (wasADC0InUse) {
    adcMod0->loadConfig(&old_adc0_config);
}

The key is that it's saved to a config (ADC_Module::ADC_Config old_adc0_config) so even if this synchronized conversion gets interrupted it can restore the correct settings. Otherwise saveConfig and loadConfig wouldn't need to accept arguments and would save the config directly inside the ADC_Module.
My problem is that the implementation of ADC_Module::ADC_Config is of course hardware dependent, and so it should be a different class for each different implementation of ADC_Module_Base.
If I create a ADC_Config_Base class and derive from it, the function's argument doesn't match the prototype of ADC_Config_Base:
Code:
//! Save config of the ADC to the ADC_Config_Base struct
virtual void saveConfig(ADC_Config_Base* config) = 0;

//! Load config to the ADC
virtual void loadConfig(ADC_Config_Base* config) = 0;
because the implementation would have
Code:
//! Save config of the ADC to the ADC_Config struct
oid saveConfig(ADC_Config* config) = 0;

//! Load config to the ADC
oid loadConfig(ADC_Config* config) = 0;

How can I solve this problem?
 
Status
Not open for further replies.
Back
Top