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).
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.
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.
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.
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?
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?
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.
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?
pugglewuggle:
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?"
This is nothing to do with the AVR. This is a straight C++ question.
pugglewuggle:
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?
Can you post a test case to prove this point? "Arduino doesn't seem to like ..." is just wrong. It's g++, an industry-standard compiler.
We seem to be a fair way into this thread without a line of code being posted by you. I would be happier if you would post some examples. Not necessarily all 400 configuration variables, but examples of what you are talking about.
... all standard C++ practices, books, and instructors say that it is bad to have objects at the global scope ...
Yes but this is a microprocessor. You need something at global scope generally speaking. I suppose you can code in a way that there is nothing at global scope, but bearing in mind space limitations the results might be even messier.
Please post examples to describe all this. This is getting very hypothetical indeed. You have unnamed sources that don't like global variables, unposted code that "barfs". Nothing to really get one's teeth into.
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.
A lot of the supplied libraries do this. You could look at their code. For example, the HardwareSerial library, the SPI library, the Wire library, the SoftwareSerial library, there are lots of examples. And they all compile with separate .h and .cpp files.
pugglewuggle:
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.
This may be true, but in the world of microcontrollers that have very limited resources, it is often a luxury that simply cannot be afforded. Therefore the pragmatic and usually more resource-efficient route is quite often taken. Don't lose too much sleep over it, and we won't report you to the OO Style Cops XD