Help me understand this class/ program structure

I'm working with a library for the esp32 called fabgl, It drives a vga monitor. This class has pretty extensive UI features built in & provides an example of exactly the sort of GUI I want to build. (full class here) . The example I'll be referring to is located in /examples/vga/graficaluserinterface/. I am just barley understanding how to use this class, so ANYTHING you can tell me about the way its structured (or why) would be of great help. I will also pose a few specific questions in my post:

The trouble I'm having is understanding how the program is setup / how it flows. lets start with main.cpp (includes omitted):

fabgl::VGA16Controller DisplayController;
fabgl::PS2Controller   PS2Controller;

fabgl::FontInfo dataCon24= fabgl::FONT_dataCtr24;


void setup()
{
  Serial.begin(115200);
  PS2Controller.begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);

  DisplayController.begin();
  DisplayController.setResolution(VGA_640x480_60Hz);
}
void loop()
{
   MyApp().runAsync(&DisplayController, 3500).joinAsyncRun();  // why this? Just to use a larger stack!
}

This I'm mostly good with, setup runs & then anything before the myApp call runs once, then the app gets run? (explanation of the authors note: "bigger stack" would be cool).

then we have app.h. This seems to replace most of what would normally be in main.cpp?

this line confuses me, specifically the single colon, which I only know with respect to initializing lists

class MyApp : public uiApp 

init() seems to be roughly equivalent to setup(), in that it runs once. Through out there, all the gui objects get created for the first frame. as well as objects for other frames, defined in their own .h file.

two primary questions around this:
a) since loop() in main doesn't actually loop, where is my main loop?
b) i see a lot of syntax that looks like this:

testButton->onClick = [&](){ testButtonClick();  };

I can copy it & it works, but I'd like to know what the leading "[&] () " means/does.

App.h (6.7 KB)
GraphicalUserInterface.ino (1.3 KB)
TestControlsFrame.h (4.6 KB)

no takers, eh? :thinking:

I doubt it. How did you reach that conclusion?

[&]()

means, the address of a pointer to a function returning 'void'. I think.

Wouldn't that be the address of a void lambda?
:open_mouth:

(I haven't looked at the code yet!)

Good, so you can explain it. :slight_smile:

That just specifies the inheritance(s)

(Initialising lists come after the constructor, IIRC.
Disclaimer: I know virtually zero about C++)

I ran a serial print before the run line, which prints a single time. Moved that print to after the run call & it doesn't print at all. I learned a little more last night, this app runs a "main event loop" that handles UI events (clicks, keystrokes, timers, ect)...but there is no main loop to directly code in, the way you would in a normal arduino project.

the ( ) being the void part of it?

Ok, I think I sort of new that. Let me refine the question... uiapp is a class within fabgl, it has a definition, yet it seems like in the block of code following class MyApp...., is also a definition of the class..at least it seems structured that way. So am I defining a specific (and custom) instance of of uiapp, called MyApp? Is that how this works?

Thank you both for your responses, this is by far the most advanced (or at least different) programming I've done & its making my head hurt!

main is hidden in the Arduino code. It amounts to a bit more than this:

void main()
{
    init();  
    setup();
    while(1)
    {
        loop();
    }
}
testButton->onClick = [&](){ testButtonClick();  };

That defines a lambda function. Google "c++ lambda", and prepare to do some reading.

No, the [&] is capture clause for the lambda. The '&' (as opposed to '=') means the variables in the surrounding scope are captured and access by Reference (vs value).

Did you give your example the name main.cpp or was that name already given?
If you use your own main.cpp then you have to implement setup() and loop() yourself. See post #8.
More usual is to use a name with the .ino extension then setup(), loop() etc. are defined for you.

sorry, i should have been more specific, I was referring to the whole of main.cpp. But since you showed that example, I think thats the sort of thing thats happening in the App's loop...just with its own event loop.

thank you, I will. I don't mind reading, its just sometimes even coming up with the right search term is a challenge. Might try to find a good lecture on the subject now that I know what I am looking for.

should have mentioned, i'm coding in PIO, so there are no ino files. main.cpp is auto generated with setup & loop in the same format as an ino--I copied the ino of the example into my main.cpp. I've done that before, thats the way its meant to be in PIO & it works.

in this case, I took a copy of the full example program (which compiles & runs) and stripped it back to scavenge the general structure & played with methods to to figure it out how to do what I needed to do. However, main.cpp is exactly as the original(INO). I have played with it to try to figure some things out, but reverted to an exact copy of the original. and the app I built runs, it just takes control of everything with its own "event loop" that I think I need to break into in order to do my own stuff(non gui related). There is an insertEvent method, yet to work out how to use it though.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.