Create global object reference C++

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
I'm reworking a project from last year to run a 4 channel RGBW controller on an ESP32 platform written in the " Arduino Programming Language". APL is very similar to C++ but lacks a main() function, instead having a global include/define area, a setup(void) function and a loop(void) function.

I had been creating my objects in the global include/define area which was working well. However, I now need to control their initiation in the setup(void) code, and of need references to them in my loop(void).

So my question is how does one create a global object reference that is defined and used elsewhere in the program?
 

djsfantasi

Joined Apr 11, 2010
9,156
I'm reworking a project from last year to run a 4 channel RGBW controller on an ESP32 platform written in the " Arduino Programming Language". APL is very similar to C++ but lacks a main() function, instead having a global include/define area, a setup(void) function and a loop(void) function.

I had been creating my objects in the global include/define area which was working well. However, I now need to control their initiation in the setup(void) code, and of need references to them in my loop(void).

So my question is how does one create a global object reference that is defined and used elsewhere in the program?
The Arduino loop() function is pretty much equivalent to the main() function. Think of it that way.

To share references between setup() and loop(), define them in what you are calling the include/define area. References definitions are allowed there as well. I use this construct often.
 

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
Thanks djsfantasi. Yes I know the reference defs can go in the include/define area. I am unclear as to how these are created.

However the point is now moot as I realized I can leave the definitions as they were and just add an additional method called in the setup(void) that can take advantage of running code to customize the object creation.

<complete aside>
WHY I am doing this is a bit convoluted (isn't it always?). As is the controller code works fine, with the exception that on occasion the connection to the server I use (Sinric) is lost, and there seems to be no way to reconnect short of rebooting to reestablish this.

Server disconnect is an event I can trap and rebooting is simple enough, but this requires persisting the state of the objects that control the channel settings. Persisting is simple enough by moving the state info from structures inside the objects to global structs declared with the "RTC_NOINIT_ATTR" attribute (do not initialize). Thus a reset event keeps the same data. A power-up event is handles by saving a magic number and comparing this to the setting; the value does not survive without power and hence the condition of the state (restart vs power on) can be known.
</complete aside>
 
Top