How to design, and implement simple interface

Our embedded product is based on Renesas RH850F1L Microcontroller.  We have large code base written in 'C' language.

The following function returns EEPROM value to calling library.

signed short GetDeltaMin ( void )  
{
     signed short param;
     NVM_Read ( ( unsigned char * ) &param );
     return (signed short) param;
}

REQUIREMENT:
Another software module needs to control value returned by GetDeltaMin ( ).   If another software module passes 1, return value from EEPROM, just like it's implemented above.  
If another module passes 0, return 0 to calling library instead of returning value from EEPROM.

How to design, and code this requirement ?

I'm thinking about providing following input interface to the user or another module.
I'll create new .c  and .h file as follows:

New File Activate_Deactive_I.c

void Enable_Disable ( Boolean State)
{
     /* user will pass 1 or 0 to this function. */
    /* Please finish wirting this function. */
   /* How will this function interact with GetDeltaMin ( ) function above ?  Please modify GetDeltaMin ( ).   */
}

New File Activate_Deactivate.h
extern void Enable_Disable ( Boolean State);   /* user will need to include this header file */
/* I don't think Activate_Deactive_I.c needs to include this header file ?   */
LVL 1
naseeamAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
From your post, it's not particularly clear what your question is. Can you be more specific on what it is you are asking the experts? The more specific your question the more chance there is we can help you, specially when you are dealing with something like micro-controllers, which are not ubiquitously understood.
naseeamAuthor Commented:
// Some_Filename.c  -  This file already exists
// This function fetches two bytes from Non-Volatile Memory(NVM) and returns to the calling library.
signed short GetDeltaMin ( void )  
 {
      signed short param;
      NVM_Read ( ( unsigned char * ) &param );
      return (signed short) param;
 }

New File -  Activate_Deactive_I.c

 void Enable_Disable ( Boolean State)
 {
      /* user will pass 1 or 0 to this function. */
     /* If State =1, there is no change to GetDeltaMin ( ).  It'll still pass two byte value from NVRAM.  */
    /* If State = 0, GetDeltaMin ( ) needs to pass 0 to calling library instead of two byte value from
         from NVRAM. */
    /* Please write this function and modify GetDeltaMin ( ).  */
 }

New File -  Activate_Deactivate.h
 extern void Enable_Disable ( Boolean State);   /* This header file must be included in file from where Enable_Disable (  ) will be called.  */
 /* I don't think Activate_Deactive_I.c needs to include this header file ?   */
naseeamAuthor Commented:
When Enable_Disable( ) is called, I can assign value of state to a global variable. Then, in GetDeltaMin( ), I can use the value of global variable to decide whether to return value from NVM or 0 to the calling library.

 I'm asking for a better solution than a global variable.
sarabandeCommented:
if you won't change your existing code to calling MyGetDeltaMin(1) everywhere (what would be the best way in my opinion) you could do the following

put that #define into a global header
#define GetDeltaMin OldGetDeltaMin // redirects GetDeltaMin calls to OldGetDeltaMin

signed short OldGetDeltaMin()
{
    unsigned short ret = 0;
#undef GetDeltaMin
    ret = GetDeltaMin();        // calls old function without argument
#define GetDeltaMin OldGetDeltaMin    
    return ret;
}

signed short NewGetDeltaMin(int onOff)
{
    unsigned short ret = 0;    // the default return is 0
    if (onOff == 1)
    {
#undef GetDeltaMin
       ret = GetDeltaMin();                     // calls olfd function without argument
#define GetDeltaMin OldGetDeltaMin
    }
    return ret;
}


int f()
{

  printf("%s %hd\n", "GetDeltaMin: ", GetDeltaMin());

  return 0;
} 

int g()
{

  printf("%s %hd\n", "NewGetDeltaMin(1): ", NewGetDeltaMin(1));

  return 0;
} 

int h()
{

  printf("%s %hd\n", "NewGetDeltaMin(0): ", NewGetDeltaMin(0));

  return 0;
} 

int main()
{
    f();
    g();
    h();
    return 0;
}

Open in new window



note, the above actually is a hack. you better would try to use a new function wherever you need it.

Sara

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
naseeamAuthor Commented:
Thank  you for providing a very different solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.