Multiple classes in a single library with shared functionality?

Well I just had a crash course in classes and I think I get the fundamentals, there is bound to loads I haven't appreciated, however moving on.
Is the following a reasonable design premise or should I be thinking differently?

Please don't get too hung up on the functionality of the actual methods and functions, I have those working.
currently my counter vars are in an array so that I can access then individually, as in check a counter, or loop through them, as in update all the counters.
Porting them to a library and looping through separate instances of a variable is the issue.

I want a library that has at least two classes ...
I will call them:-
CountAndCompare
Counters

CountAndCompare will have functions/methods, not sure what the correct terminology is, that allow me to manipulate its public variables.
These will need to include a count, a set-point and a few flags.
Lets assume it has a function called 'Check' that returns true if its count exceeds its set-point.
This will then be instanced, in my main function, an indeterminate number of times, several not hundreds.

Counters will have a single method, Update, and will only be instanced once but when Update is called it will need to modify 1 or more instances of CountAndCompare variables.

Assume ...

Counters AllCounters;
CountAndCompare Counter1;
CountAndCompare Counter2;
CountAndCompare Counter3;

void loop()
{
  AllCounters.Update;
  ..... stuff
  if(Counter1.Check){
    Other stuff ...;
  }
  ..... stuff
  if(Counter3.Check){
    Other stuff ...;
  }
  ..... stuff
  if(Counter2.Check){
    Other stuff ...;
  }
}

Assuming that the above isn't totally stupid is there a way to reference an instance of a variable using its parent class def and an ordinal?
IE if foo3.myvar is the third instance of foo is there a way to say myvar[3] or perhaps foo::myvar[3]

Lets address the last one first.

Assuming that the above isn't totally stupid is there a way to reference an instance of a variable using its parent class def and an ordinal?
IE if foo3.myvar is the third instance of foo is there a way to say myvar[3] or perhaps foo::myvar[3]

The answer is no. foo3.myvar is not the third instance of foo. foo3 might be. foo3.myvar is field belonging the foo3 instance.

You can create arrays of instances of classes, just like arrays of ints or arrays of chars.

foo myfoos[3];

Will create an array of instances of the foo class, assuming that there is a no-argument constructor. If the class constructors takes arguments, then the constructors need to be implicitly invoked.

foo myfoos[3] = { foo(12), foo(23), foo(-99) };

(Assuming that foo::foo() has one argument of type int.)

Counters will have a single method, Update, and will only be instanced once but when Update is called it will need to modify 1 or more instances of CountAndCompare variables.

As laid out, neither class knows anything about the other class, or instances of the other class. So, that will not be possible.

Now, if Counters was somehow informed about the instances of CountAndCompare as they are created, or it causes their creation, then it could invoke methods on the instances.

That's all great info thanks, it hadn't occurred to me that I could create an array if instances, explicitly defined or otherwise ....
Most interesting.
I expect that means that types are also possible but that would be another question.

" foo3.myvar is not the third instance of foo. foo3 might be. "
I do get that but looking at my question I can see that I wasn't clear.
I think I should have said ... If foo3.myvar is a member of the third instance of foo

Your final comment is the one that I would like to pursue ...

Assume I have 1 instance of bar.
bar has a method/function that needs to be aware of any and all instances of foo, so it can manipulate their public's variables or functions.

The rational is thus ....
foo will be instanced several times and will expose functions and variables unique to a specific instance, nothing odd about that.
However ...
One function of the foo class instance, without bar, would be fooX.Update
This would need to be called once on every program scan and will need to calculate the previous scan time.
I could call fooX.Update for every instance of foo but that would mean calculating the scan time Xmax times.

What I would like to do is call bar1.Update, calculate the scan time just once, and then loop through all instances of foo and modify them accordingly.

That takes me back to your last comment ...
Should foo tell bar that a new instance of foo has been created, and supply handle.
OR
Should bar be responsible for creating instances of foo in the first place

Whatever your thoughts the basic question is how... My head hurts, I should have learned C and not VB

FYI:

  AllCounters.Update;

... is not a function call. You need brackets.

Point taken but I am sure you get the gist ...
What I am asking for is advice about general structure and I expect variable scope to boot.
Obviously I will need to get the syntax correct to do anything but right now I don't actually know what what to attempt to build, never mind exactly how exactly to implement it.

Thanks
Al

Bump

I hope someone answers this, I'm curious myself.

I know you can pass pointers to classes. My gut says that I would want bar to instance foos but I'd be worried that foo would only scope to bar. When an instance of bar is declared you could tell it how many foos you want.

I think to do that, you would need the foo class already defined and prototyped in the bar class header.

I'm learning too. :smiley:

Your question is a bit convoluted. It is hard to answer. In particular it sounds like a general C++ question rather than something relating to the Arduino in particular. If you were to give a more real-life example, it would help, rather than:

I want a library that has at least two classes ...
I will call them:-
CountAndCompare
Counters

I don't particularly like the notion of an instance of a class modifying other instances of the same class. That sounds circular.

It sounds to me like you have a container (Counters) which contains things. The functionality of counting the things should be in the container, not the thing.

You may want to look into the STL (Standard Template Library) which implements many different types of containers, for virtually any "thing" inside the container. There is an implementation of STL on the Arduino.

I know you can pass pointers to classes. My gut says that I would want bar to instance foos but I'd be worried that foo would only scope to bar. When an instance of bar is declared you could tell it how many foos you want.

I think to do that, you would need the foo class already defined and prototyped in the bar class header.

A more concrete example, again, would help.

I hadn't thought of a container model, that makes much more logical sense than a potentially circular update, however I would still have no idea how to go about building one.

I think to get specifics I will need to be more specific.
My project, and I use the term loosely because it is anything but planned, involve charging batteries from solar panels and a generator.
I have the kit in place with various cobbled together systems 'mostly' controlling it.
There is currently no central control or HMI functionality and I just bought some additional PV's, far more than my charge regulator can handle, so I have to turn the PV's off if I am out to avoid the possibility of damaging the batteries.

What I want to end up with is a system that will :-
Manage temperature compensated charging of my main batteries, AGM's
Start - Manage, including battery charge algorithm - Stop the generator.
Manage opportunity charging from the PV's and generate the appropriate PWM signals to modulate the PV output.
Log data, voltage current temperatures etc.
Provide a web based interface that will mimic the loacal HMI and provide access to the logs.
Possibly - Drive a solar tracker if I ever get around to building one for my higher output PV's
Possibly - Manage a wind turbine if I ever get around to building one.
Turn local loads on and off including checking internet connectivity and restarting the satellite modem if required.

Prerequisites and assumptions ...
I don't want to build the core processor boards but I am happy to build the power electronics and sensor interfaces.
I appreciate that I will probably need to employ distributed control, IE several processors with a communication link of some sort.

Arduino, in its various forms, seemed like a quick win. Plenty of code and examples, a large active community, various implementations on several scales and pre built processor boards as well as difficult to construct items like USB and Ethernet interfaces.

SO ...
I bought a Mega 2560 and an Ethernet shield, with SD, to get me started.
I have put together a small 20X4 display with 4 keys arranged as 'soft function' keys, two at either end and a 5 key 'Nav-pad' below.

The keyboard scan, 3 rows by 3 columns, is working just fine with a global denounce routine looking for a stable output, from the binary weighted keys.
The display looks good using the standard LiquidCrystal library.
essentially with each 'screen' defining the key function and being repainted when its display data changes.

I have a good idea how I want to proceed with the program divided into two sections, HMI control and equipment control.
Each section will then be divided into functions, some functions will always run whilst others will only be called when it is applicable.
On the control side the status of my batteries for example, voltage and current will always need to be monitored, whilst I will only need to run generator routines when the generator is operating.
The HMI routines will also be divided to keep it all manageable, dividing the functionality into pages or screens.
For a given page specific data will be displayed and buttons will have a given function, press a button that is assigned to load a different page and everything changes.

OK I know that is a big task but its constituent parts are not that complex.
I could for example code all the pages/screens as functions just giving them an ordinal to track them.
MY loop could then use a case or a load of if's check the number and call a function that displayed what I wanted and performed actions based on the current key-code.

However I know that is not the best way to go ...
Far better to have a class look at a definition file, display some text with replaceable parameters.
O and if I am doing that then my key-scan routines should work the same way, load a definition that maps keys to functions, be that load a new page or move the cursor or modify the number at that cursor address.

All of this will need to interrogate and update functions, and possibly classes, that are interacting with the real world ...

OK I know its a little board not a full blown OO environment DCOM fingers in loads of external pies but still, if I keep the actual implementation relatively simple I see no reason why this approach shouldn't work.
In fact I expect that if I run out of processing power and have to distribute my logic and functionality then this approach will be a positive advantage.

And all of that brings me back to getting classes to track each others existence, or at the very least have some centralised class be aware of neighbours and progeny.
Perhaps I am shooting for the moon but it seems to me that if I am going to write a definition file that specifies displaying data from a particular source or taking a specific action then the code that implements that should at least be aware of the subjects exist!!

When all is said and done, I actually don't know what I want to do, not in detail anyway.
This thread was born out of my requirement to make a simple timer for the key repeat function.
I tried an interrupt based timer and found that it messed with the LCD and as I don't need anything like that accuracy I decided to write code that monitors the scan time and maintains an array of count and compare registers, one seemed silly.
When that worked I decided to turn it into a library and found that library's were actually classes which meant that I could potentially ditch the array, use class variables.
Again it works just fine but now if I implement 3 timers from my GeneralTimers library I need 3 Update timer calls where it would be much nicer to have one that would update any instances of a generaltimer.

I want to explore possibilities ... Learn what is practical ... Test a few scenarios and then plan the detail of my project.
With any luck I will gain a little something along the way which will be useful to many other folk as well.

Al

Hmm. I would advise getting some experience working on smaller things. This sounds to me like a more complex solution than you may need. A simple array of these items (whatever they are exactly) that you want to track might be all you need. However again, the STL is very good at that.

... if I run out of processing power ...

I don't see how you would run out of processing power monitoring charging some batteries, but perhaps it is more involved than that. :slight_smile:

Hi Dyslexicbloke,
I read this as I am looking for an answer to a question I have, but I find it sounds like your project is very close to what I am working on this very moment (read past weeks, and probably future weeks)!

My setup is PV panels and also my micro-hydro turbine system feeding into a 24 volt battery bank.
I am using a Mega 2560, from freetronics which has already the ethernet and SD built on-board.
A Plamatronics PL40 has been doing the job of charging the batteries so far, and it has been great, but I envisage it will be replaced with this unit I am describing.

For this project I moved away from the Arduino IDE and moved to using Eclipse with the Arduino plugin.

Using Eclipse I find is a far better way to construct such a in-depth project.
In Eclipse I also, at the same time develop the web based interface.
Just, quickly, the web based side is using JQuery, JQery-mobile and a few other libraries to make the whole lot far easier.
The client side (your browser) simply requests JSON data from the Arduino and plugs that into a nice web interface.
I am designing it so it works very nicely on smartphones as well as your standard laptop/desktop.

The is logging of battery data happening now, it will be viewable as a trend on the web interface, with scrolling backwards in time.
I have the basic web interface happening with real data being displayed.
I have the Arduino getting current time from a NTP, instead of need extra hardware of RTC, and it is working well now.

I would be willing to share what I have with you, though I still have some way to go yet.
We could maybe start a new thread in the appropriate place for a work-in-progress type of project.
I have been wanting to start a blog on the project, but that would take me away from the actual doing.

My background has been more with industrial automation and programming PLC's and SCADA systems, typically AB SLC/Control Logix and Citect, so am using some of those concepts I have used in the past into this Arduino based project for my home. (far more energy efficient than PLC's and dedicated SCADA/HMI computer)

Paul

Paul,
I don't particularly want to be messing with Eclipse, unless I have to, part of my design premise was to use relatively simple tools to make relatively simple modules.

Having said that it sounds as though we will be working towards a similar goal from a remarkably similar starting point and I would be delighted to collaborate.
I think we should keep most if no all communication on the forum to stay within the spirit of open source. I know I will need help from the community and I hope our work will be useful to others in general.

I haven't progressed much from the planning stage, I have been looking at concepts, and I think I will need some form of multi drop serial communications so that I can place functionality in distributed nodes, Nano's or Mini's I thought.
CAN-BUS would fit the bill nicely for several reasons but I don't want to buy hardware and I expect I could get a way with a less complex, slower version than the standard implementation. I haven't found anything yet that implements CAN-BUS in software, SoftSerial esque.

Some time ago I roughed out a half duplex device to devices protocol that was address-less with a rolling message priority and packet data type ID and was a little suprised to find just how similar to CAN that it was once I started searching for the correct concepts.
I would live to build a software library to implement a CAN type protocol specifically for Arduino based devices, but perhaps that is a step too far.
Enough with the flight of fancy .... There may well be something out there already that is a quick win and even if there isn't I think some more research is required before I go committing to building a whole new protocol.

I have some experience of MicroLogix and RedLion, although probably not as much as you, but I suspect that my conceptual plan is broadly similar to yours.

Did you grasp my pseudo web page for liquid crystal idea, what do you think?
The thought process is to have the 'page', just a 20X4 text display with some soft keys, display text that can contain replaceable parameters from named variables or possibly arrays. Each 'page file' would define what to display, how to display it and what the function of any button is.
I envisage standard functions like load a new page or modify the value at display address X.X with the definition file being something akin to XML, stored on the SD card.

If you are up for it we should decide on a Blog, Blog's or thread name, perhaps a combination of all three.
Obviously it would make sense to have the projects outlined and documented in a Blog but perhaps we should keep individual concepts in threads so that they don't get too long and will still search well.

Good to chat, thoughts?
Al