Weather Simulation for Dimming expansion module

Share you PDE file with our community

Posts: 293
Joined: Tue Oct 25, 2011 7:39 am
PostPosted: Fri Mar 29, 2013 11:02 am
Got it significantly cleaned up... will be testing soon

Posts: 293
Joined: Tue Oct 25, 2011 7:39 am
PostPosted: Wed Apr 03, 2013 2:37 pm
Question-

Can I use structures or is this going to cause problems in porting this into the library?

Thinking of creating a structure (basically a class) to hold the diverse globals I use then I can pass that around to modify individual components in subroutines. But... it requires a .h file to be linked which contains the structure declaration and I am not sure if this will munge things up later.

I should also admit I am not certain I am going to do this... but it looks to be a nice compact way to hold globals allowing almost a hash like database... enabling me to rapidly add a "global" variable when I need by simply assigning a new member to the structure. In some ways it seems like this would in fact be better for incorporation into a code library as it "isolates" my stuff from everything else. But I will have to change a lot more of the code so I am not going here until I can figure out if its supported or advised.
Kurt? or Roberto? others who know?? thoughts?

I am likely exposing my ignorance here... cause its probably obvious- but only if you know it :)
User avatar
Posts: 5421
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Wed Apr 03, 2013 3:20 pm
My moon simulation uses a struct but a related code was stuck in the .h file also the sunlocation and tide classes that you have to add to the libraries. So anyway you choose to go.

Posts: 293
Joined: Tue Oct 25, 2011 7:39 am
PostPosted: Wed Apr 03, 2013 3:55 pm
Sent you a PM... but to include others.

If I make a big structure to hold the necessary globals we now have a new .h file - seemingly not a big deal just one include line in the main. But, then any edits to the program for say, inclusion of a new variable or something require the .h file also be edited or (seemingly not a good choice) a new struct be created that inherits the old and includes the new elements. This gets nasty fast so I am not proposing that but am I missing anything with the idea of using a structure to hold all the globals.

Thinking I can then just initialize them as type and bounds within the structure and then in the code simply fill them as they are created. Seems like a good way of avoiding collision between variable names and its no big deal to simply edit the .h file containing the structure if we need a new variable. But I am not seeing this as saving many lines of code... just not sure I should through to the effort.

Proposing something like this


new .h file...

typedef struct{
long rise;
long set;
byte ChMAX[8];
etc...
}Weather


Then in code...
include Weather.h

Weather.rise=sunriseFunc(timenow);
and another example...
Weather.ChMAX[0] = calculation result for that channel intensity;

But this of course means that I need to go through every line of code and for all variables switch to the structure format ..

e.g. rise now becomes Weather.rise etc etc so is it worth the effort for name space preservation?
User avatar
Posts: 5421
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Wed Apr 03, 2013 4:18 pm
I think you can just define the struct in the main ino file. You only need a separate declaration file if your going to share between other c files. I believe...

Are you planning to have seperate c files or put all the code into the .h file?

Posts: 293
Joined: Tue Oct 25, 2011 7:39 am
PostPosted: Thu Apr 04, 2013 11:24 am
Having not yet done this, I can only say that there are numerous posts on Arduino forums stating that the compiler has issues with package creation occurring in what amounts to an inappropriate order for it to be included in with the code so the recommendations there for simplest fix is just to use an external file and include it. Thus that was going to be my method of choice (its not like I can't then test this by copy and paste so whichever works I guess). In terms of going to a full object oriented implementation, that would probably in this case be more work than required and do little other than split it up over a bunch of files and objects. If it was going to be a core implementation that would spit out stuff like rise and set and cloud times and storm stuff across a widely dispersed set of implementations it might be worth it but since its running on the controller and is basically designed to be a compact package its kinda not worth it IMHO.
User avatar
Posts: 2781
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
PostPosted: Sat Apr 06, 2013 6:24 am
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.

Posts: 293
Joined: Tue Oct 25, 2011 7:39 am
PostPosted: Wed Apr 10, 2013 12:35 pm
Thanks Kurt-

Class structured (OO whatever) C++ is not something I have done before but I am using Object Oriented structure in Perl for work so its not really foreign. Let me make something up that I think will mostly work out and have you take a look at it via PM. This is going to take a little longer cause the coding part of my brain is currently being completely fried by projects at work... so I need to finish a few things to free up some time to play!

Thanks- PM will eventually show up
User avatar
Posts: 2781
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
PostPosted: Wed Apr 10, 2013 3:13 pm
rufessor wrote:Thanks Kurt-

Class structured (OO whatever) C++ is not something I have done before but I am using Object Oriented structure in Perl for work so its not really foreign. Let me make something up that I think will mostly work out and have you take a look at it via PM. This is going to take a little longer cause the coding part of my brain is currently being completely fried by projects at work... so I need to finish a few things to free up some time to play!

Thanks- PM will eventually show up

haha. Yeah, I can COMPLETELY understand about your brain being fried. Mine gets that way too from work and play.

Posts: 293
Joined: Tue Oct 25, 2011 7:39 am
PostPosted: Fri May 10, 2013 8:53 am
ALmost there at least in terms of a first try at this... probably much will be obviously a bit wrong in the implementation...


Lenovo and Kurt- I will PM you with some code- if you do not mind can you just LOOK at it. I have not tested it I just want to know if I have the structure correct.

I took your advice and tried this as a class... it will be much more portable that way. But this is my first and only attempt at this in C... its different than Perl so its likely I have things just a bit stupid..


Don't worry about doing any debugging or compile just need to know if it looks right for this. Your help will greatly speed my getting this done. Working off examples only takes you so far...
Last edited by rufessor on Fri May 10, 2013 8:58 am, edited 3 times in total.
PreviousNext

Return to My PDE/INO file

Who is online

Users browsing this forum: No registered users and 1 guest

cron