Look for help to understand programming style

Hi All,

I am looking at the example code for the Arduino BasicOTA that is installed with the ESP32 extension. I have attached the relevant clip below:

At line 35, I think I am seeing a class.method “ArduinoOTA” is the class and onStart() is the method?
But what is at line 45, 49 & 53? Methods without specifying a Class?

Next, at line 63, a syntax that I think I am familiar with. But what are the parameters that are passed to ArduinoOTA.onStart()? I don’t think I have seen lines of code being passed before (but there are lots of things that I have never seen before :o

I am confused…

Regards,

BasicOTA.png

Having provided the entire source, I think it would be helpful if you would post excerpts showing the segments of code you're talking about. I don't want to have to wade through thousands of lines.

Thank you for your comments.

But I am puzzled because I did not include the full program just for the reasons you mentioned.
However, as you can't (or at least I can't find out how to) include an in-line image it is provided an attachment. A single click brings up the snippet of code. An image was chosen so that line numbers could be used as reference to the question.

I am not asking for an explanation of how the code is working, only the any pointers to the programming style or pattern.
I have not seen anything like it before in C, C++ or Aurduino'ise.

Regards,

Not too difficult. :grinning:
BasicOTA.png
However posting a picture of text is about as useful as sending it in a fax.

You need to read the instructions (point No. 7) to understand how to post code in a usable manner. :cold_sweat:

I believe if I read this correctly, aarg suggest you post the complete code - since snippets alone are useless - and also refer to the section that you believe is causing a problem.

I think it’s pretty clear: what does the “.method” syntax do without an object name in front of it?
Or maybe the ? Those are lambdas, right?

westfw:
Or maybe the ? Those are lambdas, right?

Ah… Yes. AKA “Anonymous Functions”. Thanks for the pointer…

I think it’s pretty clear: what does the “.method” syntax do without an object name in front of it?

Nope! Sorry. Not even with the help of Google, is it clear to me. Any other clues you would care to offer?

Regards,

Each method is returning a reference to the object so the next method is acting on the reference returned by the previous method call. I just looked at the example sketch and in my installation they stopped using that trick. They put a ';' after the closing parenthesis and added "ArduinoOTA" before the '.'. I don't know why my BasicOTA is different from yours:

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }


    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();

Thank you John for explaining that. All clear now.

Not that it matters, but I am running the the IDE 1.8.13 for Linux with the ESP32 v1.04 board package.

Regards, Martin

Each method is returning a reference to the object so the next method is acting on the reference returned by the previous method call.

Ah. “Cute” C++ programming tricks. Thanks for the explanation. I hate cute programming tricks (and yet, they (language developers) keep adding more and more of them. Sigh.)

I think it’s pretty clear: what does the “.method” …

Nope! Sorry. Not even with the help of Google

I meant that your QUESTION was clear. I didn’t know the answer either.

That is quite an obscure way of presenting an object to a method call in a sort of “chain” construct.

I guess the class definition here is the one used: arduino-esp32/ArduinoOTA.cpp at master · espressif/arduino-esp32 · GitHub

and the onStart method looks like:

ArduinoOTAClass& ArduinoOTAClass::onStart(THandlerFunction fn) {
    _start_callback = fn;
    return *this;
}

which is exactly as @johnwasser has already pointed out, that the method returns a reference to its parent object, allowing the programmer to showcase some arcane technique for saving a few keystrokes.