6 years, 3 months ago.

Conditional 'global' pin declaration in library init

Upon initialisation of a library, I want to declare a pin as AnalogOut only when a certain pinname is passed to the library and access this analog output throughout other functions in the library. Obviously, when I use the code below, the analog output is only accessible during init, hence using dac12 in the threshold function is not possible.

I know it can be solved by not using AnalogOut and writing directly to the 12-bit DAC registers (did it with the internal 6-bit DAC on the KL25Z) but it would be nicer using AnalogOut. Is there another solution to this problem?

ComparatorIn.cpp

ComparatorIn::ComparatorIn(PinName pinP, PinName pinM)
{
    .....
    if((CMPnumberP == 4) || (CMPnumberM == 4)) AnalogOut dac12 (PTE30);  // When PTE30 is selected, use it as 12-bit DAC
    .....
};

...

void ComparatorIn::treshold(float vo_pct)
{
    
    if(vo_pct < 0.0) vo_pct = 0.0;
    if(vo_pct > 1.0) vo_pct = 1.0;;

    if((CMPnumberP == 7) || (CMPnumberM == 7))
    {
        dac6_write(vo_pct * (float)0x3F);
    }
    if((CMPnumberP == 4) || (CMPnumberM == 4))
    {
        dac12 = vo_pct;
    }
}

SOLVED

:) Quite funny because this is the second time I post a question and immediately afterwards found the solution myself (I did search for a solution before posting on both occasions) - weird, very weird.. Anyhow, the solution is to use a dynamic declaration (found on https://mbed.org/questions/1079/Dynamically-reconfiguring-BusOut-and-Bus/):

ComparatorIn.cpp

AnalogOut *_dac12;

ComparatorIn::ComparatorIn(PinName pinP, PinName pinM)
{
    .....
    if((CMPnumberP == 4) || (CMPnumberM == 4)) _dac12 = new AnalogOut (PTE30);  // When PTE30 is selected, use it as 12-bit DAC
    .....
};

...

void ComparatorIn::treshold(float vo_pct)
{
    
    if(vo_pct < 0.0) vo_pct = 0.0;
    if(vo_pct > 1.0) vo_pct = 1.0;;

    if((CMPnumberP == 7) || (CMPnumberM == 7))
    {
        dac6_write(vo_pct * (float)0x3F);
    }
    if((CMPnumberP == 4) || (CMPnumberM == 4))
    {
        _dac12->write( vo_pct);
    }
}

@mbed staff

Sorry for 'bloating' the Questions list with issues that were already answered.
Perhaps it would be a good idea to add a knowledge base to the mbed site? I know the forum and questions can be used as a knowledge base but it is sometimes difficult to quickly find an answer because we have to read through all answers.
On the other hand, setting up a knowledge base based on the current info on the mbed site would be a massive job.

Comment on this question
Be the first to answer this question.

You need to log in to post a question