Just to chime in. Structures and classes are virtually the same. A structure is simply a class without functions (yes, I'm paraphrasing it).
If you plan on only using the variables in one file (Weather.cpp), then you won't need to have a header file. You can simply put all the variables at the top of the file (like you are already doing).
Ideally, you would have a header file and an implementation file for all the functions and you would turn it into a class to keep everything grouped together nicely. Then the class would only use the variables that are associated with it. This would make things simpler to deal with.
If you were to put all of your functions into a separate file and then create the variables in the sketch, you would run into problems like you were discussing. The compiler will not be able to locate the variables for use with your functions because they are located elsewhere. This is why a header file is needed. (which I'm sure you already knew that).
You mentioned that it could get "nasty fast" by having to include lots of variables in the header file. I would think that if you created the functions properly, there really wouldn't be a need to continually edit the header file with the variables. If you have to modify values, the best route would be to create a class and then have functions to initialize the variables and update them as needed for use in the code.
If you have to create more variables and functions in the code, then that's just how it goes. Having them located in 1 or 2 files and separate from the other code is much easier to deal with. Then you just have a class to included and start using.
With a class, you could simply have 1 function call to calculate all the values and store them in the proper variables and then have a function call to return the values.
IMO, have the class would be the best solution because of consolidation and isolation of the code and maintaining simplicity.
Going off of your example, here's a way it could potentially be used:
Code: Select all
class Weather {
public:
Weather();
// updates the values
// calls CalculateSunrise, CalculateSunset, CalculateIntensity
void Update();
long GetSunrise();
long GetSunset();
byte GetIntensity(int channel);
private:
// calculates sunrise and stores it in rise
void CalculateSunrise();
// calculates sunset and stores it in set
void CalculateSunset();
// calculates the intensity for channels and stores in ChMax
void CalculateIntensity();
private:
long rise;
long set;
byte ChMax[8];
};
Inside your INO file, you include "Weather.h"
Then have a global Weather variable:
Weather w;
Inside your loop you would have something like this:
Code: Select all
void loop() {
// code
// or have code that only updates 1 time per day
w.Update();
// this is not the proper format, just an example
PWMSlope(w.GetSunrise(), w.GetSunset());
// code
}
You could even use it inside the DrawCustomMain() function.
This way you don't have to worry about creating all the variables you need, you can simply keep them isolated and have a common interface to be used just like the rest of the libraries.
Just throwing out some ideas for you. Personally, I think the class is more readable, but that's just my opinion.