Renaming function variables on the fly

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Jim2386
 
Posts: 285
Joined: Fri Nov 14, 2014 11:58 pm

Renaming function variables on the fly

Post by Jim2386 »

Hi guys,

Forgive me for probably not using the correct terms, but I'm wondering if there's an easy way to do something in arduino code that i'm not aware of.

I'm running multiple fram cards, in order to write to them you call a command like

fram1.write8(b+maxb, swayFRAM);

where fram1 is the name of the initialized fram card at the top of the code. So, in some cases you could have:

fram1.write8(b+maxb, swayFRAM);
fram2.write8(b+maxb, swayFRAM);

and write to two different fram cards. However, in my code, I have a need for a conditional. For example:

"If I push button X, I'd like to write all my data to fram card 2. If I don't push button X I'd like to write to fram card 1."

Obviously you could do

Code: Select all

if (button == 1)
{   fram1.write8(b+maxb, swayFRAM);
}
else
{   fram2.write8(b+maxb, swayFRAM);

However, my code is more like "I'm going to push a button at the beginning of the looooong code and any time you see a fram write command, I want it to fram card 1. If it's not pushed I was all the fram write codes to refer to fram card 2.

Is there a way to put something like:

Code: Select all

void loop(){
*read button input*
if(button==1)
{xxx = fram1;
}
else
{xxx=fram2;
}

   xxx.write8(b+maxb, swayFRAM);

    **do some other stuff**

     xxx.write8(b+maxb, swayFRAM);

} //end  loop




I hope I am being clear. I know you can do a #define at the top of code to define the value of an actual variable ithe code. However, does that work with function calls? I need the xxx to have the ability to be redefined as the program loops as we check to see if the button is pushed on not.

Id rather not have to do it the long way and write a IF THEN around each time I use a fram.write8 command. That seems like a terribly inefficient way to go.


Thanks for any help you can provide.

-Jim

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Renaming function variables on the fly

Post by dastels »

Yes, you can do pretty much exactly that, except that I'd use pointers to fram objects

Create your fram objects as usual and do the begin calls to initialize ad associate them to an I2C address.

Now it gets a bit different.

Code: Select all

Adafruit_FRAM_I2C *active_fram;

void loop(){
*read button input*
if (button == 1) {
    active_fram = &fram1;
} else {
    active_fram = &fram2;
}

//...

   active_fram->write8(b+maxb, swayFRAM);

//...
Take note of the & which retsults in the address on an object (fram1 or fram2 in this case), and -> which does the same thing as . but with a pointer to an object rather than the object.

That should do it.

Here's something on pointers in C++ https://www.cplusplus.com/doc/tutorial/pointers/

Dave

User avatar
Jim2386
 
Posts: 285
Joined: Fri Nov 14, 2014 11:58 pm

Re: Renaming function variables on the fly

Post by Jim2386 »

And it would be

active_fram.write8(); any time I want to write?

Ill read up on the pointers link tonight after work!

Thank you!

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Renaming function variables on the fly

Post by dastels »

No. As I put in the example code:

Code: Select all

active_fram->write8(b+maxb, swayFRAM)
You need to use the pointer dereferencing ->.

Dave

User avatar
Jim2386
 
Posts: 285
Joined: Fri Nov 14, 2014 11:58 pm

Re: Renaming function variables on the fly

Post by Jim2386 »

Oh I’m sorry. I was seeing this on my phone so the last line was hidden!

Thanks! I’ll try some sample stuff!!!!

Jim

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Renaming function variables on the fly

Post by dastels »

Pointers really open up what you can do. The only downside is if you start allocating memory you have to manage it and free it as appropriate otherwise you have memory "leaks" and eventually run out and crash. But you aren't doing that so it isn't a concern. You are statically creating objects and just managing a pointer to one of the existing objects. It's a great way to get familiar with pointers.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”