Pages: [1] 2   Go Down
Author Topic: How to handle [very frequently accessed] persistent variables  (Read 1161 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The first thing that comes to my mind is: global objects.

This seems easy: collect a bunch of very frequently access variables and related functions that need to persist across iterations of loop() into a class and instantiate it globally (one copy only).

Pretty much all of these are accessed from other objects and functions all the time.

Do any of you have any suggestions about how to/if I should try to avoid global objects? I know that global objects are frowned upon, but I don't know how else to handle this problem other than having tons of variables and functions either as globals or in different namespaces (ick to both, in my mind).

Ideas?
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd just use global variables.
Logged

There's always a better way!

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't know how else to handle this problem other than having tons of variables ...

What's the difference? What is the difference between lots of global variables, and lots of members of a global object? Except that the global object would be slower.

Besides, what are these "tons" of variables?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Settings parameters. There are about 400 of them that change to control various aspect of the program. All of them have at least 2 functions associated (often more) and, of course, some of them are related. It makes sense to me that this is a good time to use objects (even if there is only a single instance of each class) to organize the program modules and make it more logically organized.

What say ye?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46007
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Settings parameters. There are about 400 of them that change to control various aspect of the program.
You'll have to be more specific, I'm afraid. What are you doing that needs 400 settings parameters? Which Arduino is this on?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fancy stuff. Lol

It is a Mega 2560 so I have enough RAM.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What say ye?

Och.

Use an array laddie.

Or the STL.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24319
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I know that global objects are frowned upon
They are?
Oops.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you elaborate more Nick? I was under the impression that a lot of STL features are not available on Arduino, such as vectors, etc.

Also, how would you propose to use the STL to solve this problem?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think nick and a few other people have implemented an Arduino safe STL copy. Nick probably has it on his own website ( link in his post signature ). Search the forums here and you will find it also.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can read about STL for the Arduino here:

http://www.gammon.com.au/forum/?id=11119

Basically it links to:

http://andybrown.me.uk/ws/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/

Quote
There are about 400 of them that change to control various aspect of the program.

What Arduino is this? To have 400 settings held in 2K of RAM sounds like you are going to run out of RAM. Especially if they are ints or floats.

My suggestion for the STL was you could conceivably map (say) a string to an int, so it might look like this:

Code:
int widget_count = settings ["widget_count"];

But for hundreds of them, I don't know. I doubt it will fit. A simple array might work:

const int WIDGET_COUNT = 1;

Code:
int widget_count = settings [WIDGET_COUNT];

So you have an array of 400 items, and grab them by position.

This is all pretty hypothetical. Perhaps more details about your application...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It is a Mega 2560 so I have enough RAM.

Like I said, I just want the settings variables to be (effectively) globally available. Is that not a common thing? How does, say, Excel run without keeping settings globally available? Or rather, how is that normally managed in C++? Or specifically on AVR?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This seems easy: collect a bunch of very frequently access variables and related functions that need to persist across iterations of loop() ... Pretty much all of these are accessed from other objects and functions all the time.

I'm confused, I must admit. You want a whole lot of variables to persist, and be accessible from lots of functions, but you don't want to use global variables. Why not? Isn't that what they are for? What is the real objection? You could use a namespace, or you could use a single array. Can you clarify really what this is about?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Like I said, I obviously need to have them present. An array seems like much trouble  global variables seem messy. It makes sense to me that to organize them in a reasonable manner that objects would be used, to tie functions and related variables together.

What I am asking, and it doesn't sound like a crazy question to me, is: "Is there some other way that this works or some special, efficient way of doing this on MCUs such as AVR?"

Arduino doesn't seem to like objects spanning multiple source files in a library - it barfs if I put the class declaration in the .h and the class definition in .cpp. I must specify the object as static (cannot use volatile or other keywords) in the .h or it will not compile. It compiles and runs with the static in front of the object instantiation in the .h file. Is there some trick that I am just blind to?

The reason I am asking this is because all standard C++ practices, books, and instructors say that it is bad to have objects at the global scope. So I say, considering Arduino's "nuances," is there some other way of doing such a thing other than having global objects in Arduino since the data needs to persist across loop()s? In regular C++ you create objects and pass pointers around. This doesn't seem to make a lot of since in Arduino as the hardware capabilities are limited - 16MHz 8-bit MCU w/ 8K or RAM. There is no way, from my observation, to make data persist in this manner unless you 1) create static global objects or 2) malloc them and have global pointers to that type.

So the question I'm asking is: Does this sound right or am I crazy? And in either case, what would you do. I cannot use arrays nor vectors in the place of named members in objects as it is too much trouble to write [readable] code in anything other than a small library if using this method.

Let me know what you think. Thanks!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46007
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The reason I am asking this is because all standard C++ practices, books, and instructors say that it is bad to have objects at the global scope.
Minimizing scope is a good thing. But, consider this. If you create some class (and you should be able to; specifics of your issue(s) would help), that class instance needs to be global, so that all other files/instances/etc. can access it. What it the difference between a global array of values and a global instance that wraps those values?
Logged

Pages: [1] 2   Go Up
Jump to: