-->
Page 1 of 1

OTA Why callbacks in Setup void ?

PostPosted: Sat Apr 09, 2016 12:01 pm
by danbicks
Hi guys,

Going to ask a silly one here, In the basicOta sketch, why are the callbacks located within the setup subroutine?

Can they be placed outside of setup or is there a specific reason. I can understand that ArduinoOTA.begin(); needs to be there, but don't quite get why a separate callback such as

ArduinoOTA.onProgress( {
Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
});

is located within the setup scope.

Cheers

Dans

Re: OTA Why callbacks in Setup void ?

PostPosted: Sat Apr 09, 2016 12:43 pm
by martinayotte
Those callbacks are some kind of "anonymous" functions. The call in setup is only initialize the pointer to those function. The callbacks themselves could be easily placed outside. For example :

Code: Select allvoid myOnProgress() {
  Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
}

void setup() {
  [...code...]
  ArduinoOTA.onProgress(myOnProgress);
  [...code...]
}

Re: OTA Why callbacks in Setup void ?

PostPosted: Sun Apr 10, 2016 6:31 am
by danbicks
Thanks Martin,

Now makes sense :)

Dans