Skip to content

Commit 7890e11

Browse files
0xPITme-no-dev
authored andcommitted
Allow chaining of methods for more concise code (espressif#809)
1 parent 21026e2 commit 7890e11

File tree

3 files changed

+56
-43
lines changed

3 files changed

+56
-43
lines changed

libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino

+26-23
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,34 @@ void setup() {
3030
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
3131
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
3232

33-
ArduinoOTA.onStart([]() {
34-
String type;
35-
if (ArduinoOTA.getCommand() == U_FLASH)
36-
type = "sketch";
37-
else // U_SPIFFS
38-
type = "filesystem";
33+
ArduinoOTA
34+
.onStart([]() {
35+
String type;
36+
if (ArduinoOTA.getCommand() == U_FLASH)
37+
type = "sketch";
38+
else // U_SPIFFS
39+
type = "filesystem";
40+
41+
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
42+
Serial.println("Start updating " + type);
43+
})
44+
.onEnd([]() {
45+
Serial.println("\nEnd");
46+
})
47+
.onProgress([](unsigned int progress, unsigned int total) {
48+
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
49+
})
50+
.onError([](ota_error_t error) {
51+
Serial.printf("Error[%u]: ", error);
52+
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
53+
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
54+
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
55+
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
56+
else if (error == OTA_END_ERROR) Serial.println("End Failed");
57+
});
3958

40-
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
41-
Serial.println("Start updating " + type);
42-
});
43-
ArduinoOTA.onEnd([]() {
44-
Serial.println("\nEnd");
45-
});
46-
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
47-
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
48-
});
49-
ArduinoOTA.onError([](ota_error_t error) {
50-
Serial.printf("Error[%u]: ", error);
51-
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
52-
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
53-
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
54-
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
55-
else if (error == OTA_END_ERROR) Serial.println("End Failed");
56-
});
5759
ArduinoOTA.begin();
60+
5861
Serial.println("Ready");
5962
Serial.print("IP address: ");
6063
Serial.println(WiFi.localIP());

libraries/ArduinoOTA/src/ArduinoOTA.cpp

+20-10
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,70 @@ ArduinoOTAClass::~ArduinoOTAClass(){
3131
_udp_ota.stop();
3232
}
3333

34-
void ArduinoOTAClass::onStart(THandlerFunction fn) {
34+
ArduinoOTAClass& ArduinoOTAClass::onStart(THandlerFunction fn) {
3535
_start_callback = fn;
36+
return *this;
3637
}
3738

38-
void ArduinoOTAClass::onEnd(THandlerFunction fn) {
39+
ArduinoOTAClass& ArduinoOTAClass::onEnd(THandlerFunction fn) {
3940
_end_callback = fn;
41+
return *this;
4042
}
4143

42-
void ArduinoOTAClass::onProgress(THandlerFunction_Progress fn) {
44+
ArduinoOTAClass& ArduinoOTAClass::onProgress(THandlerFunction_Progress fn) {
4345
_progress_callback = fn;
46+
return *this;
4447
}
4548

46-
void ArduinoOTAClass::onError(THandlerFunction_Error fn) {
49+
ArduinoOTAClass& ArduinoOTAClass::onError(THandlerFunction_Error fn) {
4750
_error_callback = fn;
51+
return *this;
4852
}
4953

50-
void ArduinoOTAClass::setPort(uint16_t port) {
54+
ArduinoOTAClass& ArduinoOTAClass::setPort(uint16_t port) {
5155
if (!_initialized && !_port && port) {
5256
_port = port;
5357
}
58+
return *this;
5459
}
5560

56-
void ArduinoOTAClass::setHostname(const char * hostname) {
61+
ArduinoOTAClass& ArduinoOTAClass::setHostname(const char * hostname) {
5762
if (!_initialized && !_hostname.length() && hostname) {
5863
_hostname = hostname;
5964
}
65+
return *this;
6066
}
6167

6268
String ArduinoOTAClass::getHostname() {
6369
return _hostname;
6470
}
6571

66-
void ArduinoOTAClass::setPassword(const char * password) {
72+
ArduinoOTAClass& ArduinoOTAClass::setPassword(const char * password) {
6773
if (!_initialized && !_password.length() && password) {
6874
MD5Builder passmd5;
6975
passmd5.begin();
7076
passmd5.add(password);
7177
passmd5.calculate();
7278
_password = passmd5.toString();
7379
}
80+
return *this;
7481
}
7582

76-
void ArduinoOTAClass::setPasswordHash(const char * password) {
83+
ArduinoOTAClass& ArduinoOTAClass::setPasswordHash(const char * password) {
7784
if (!_initialized && !_password.length() && password) {
7885
_password = password;
7986
}
87+
return *this;
8088
}
8189

82-
void ArduinoOTAClass::setRebootOnSuccess(bool reboot){
90+
ArduinoOTAClass& ArduinoOTAClass::setRebootOnSuccess(bool reboot){
8391
_rebootOnSuccess = reboot;
92+
return *this;
8493
}
8594

86-
void ArduinoOTAClass::setMdnsEnabled(bool enabled){
95+
ArduinoOTAClass& ArduinoOTAClass::setMdnsEnabled(bool enabled){
8796
_mdnsEnabled = enabled;
97+
return *this;
8898
}
8999

90100
void ArduinoOTAClass::begin() {

libraries/ArduinoOTA/src/ArduinoOTA.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,35 @@ class ArduinoOTAClass
3333
~ArduinoOTAClass();
3434

3535
//Sets the service port. Default 3232
36-
void setPort(uint16_t port);
36+
ArduinoOTAClass& setPort(uint16_t port);
3737

3838
//Sets the device hostname. Default esp32-xxxxxx
39-
void setHostname(const char *hostname);
39+
ArduinoOTAClass& setHostname(const char *hostname);
4040
String getHostname();
4141

4242
//Sets the password that will be required for OTA. Default NULL
43-
void setPassword(const char *password);
43+
ArduinoOTAClass& setPassword(const char *password);
4444

4545
//Sets the password as above but in the form MD5(password). Default NULL
46-
void setPasswordHash(const char *password);
46+
ArduinoOTAClass& setPasswordHash(const char *password);
4747

4848
//Sets if the device should be rebooted after successful update. Default true
49-
void setRebootOnSuccess(bool reboot);
49+
ArduinoOTAClass& setRebootOnSuccess(bool reboot);
5050

5151
//Sets if the device should advertise itself to Arduino IDE. Default true
52-
void setMdnsEnabled(bool enabled);
52+
ArduinoOTAClass& setMdnsEnabled(bool enabled);
5353

5454
//This callback will be called when OTA connection has begun
55-
void onStart(THandlerFunction fn);
55+
ArduinoOTAClass& onStart(THandlerFunction fn);
5656

5757
//This callback will be called when OTA has finished
58-
void onEnd(THandlerFunction fn);
58+
ArduinoOTAClass& onEnd(THandlerFunction fn);
5959

6060
//This callback will be called when OTA encountered Error
61-
void onError(THandlerFunction_Error fn);
61+
ArduinoOTAClass& onError(THandlerFunction_Error fn);
6262

6363
//This callback will be called when OTA is receiving data
64-
void onProgress(THandlerFunction_Progress fn);
64+
ArduinoOTAClass& onProgress(THandlerFunction_Progress fn);
6565

6666
//Starts the ArduinoOTA service
6767
void begin();

0 commit comments

Comments
 (0)