From 355a2dfd4d54143c32c63eb6334a6cd4b165240a Mon Sep 17 00:00:00 2001 From: Gregg Date: Fri, 2 Sep 2022 18:47:15 -0500 Subject: [PATCH] Updated rest of code to reflect latest version of statusLED Removed all checks to see if statusLED is defined, since it will now ALWAYS be defined, even if the statusDevice is set to NULL. --- src/HAP.cpp | 6 ++-- src/HomeSpan.cpp | 67 ++++++++++++++++-------------------------- src/HomeSpan.h | 2 +- src/Network.cpp | 24 +++++---------- src/extras/Blinker.cpp | 2 -- 5 files changed, 36 insertions(+), 65 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index a44b9ee..4b163af 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -638,8 +638,7 @@ int HAPClient::postPairSetupURL(){ LOG1("\n*** ACCESSORY PAIRED! ***\n"); - if(homeSpan.statusLED) - homeSpan.statusLED->on(); + homeSpan.statusLED->on(); if(homeSpan.pairCallback) // if set, invoke user-defined Pairing Callback to indicate device has been paired homeSpan.pairCallback(true); @@ -1638,8 +1637,7 @@ void HAPClient::removeController(uint8_t *id){ removeControllers(); LOG1("That was last Admin Controller! Removing any remaining Regular Controllers and unpairing Accessory\n"); mdns_service_txt_item_set("_hap","_tcp","sf","1"); // set Status Flag = 1 (Table 6-8) - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_PAIRING_NEEDED); + homeSpan.statusLED->start(LED_PAIRING_NEEDED); if(homeSpan.pairCallback) // if set, invoke user-defined Pairing Callback to indicate device has been paired homeSpan.pairCallback(false); } diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 88481b4..be8eb37 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -57,7 +57,7 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa this->modelName=modelName; sprintf(this->category,"%d",(int)catID); - statusLED=new Blinker(statusDevice,autoOffLED); + statusLED=new Blinker(statusDevice,autoOffLED); // create Status LED, even is statusDevice is NULL esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(0)); // required to avoid watchdog timeout messages from ESP32-C3 @@ -189,10 +189,9 @@ void Span::pollTask() { processSerialCommand("A"); } else { Serial.print("YOU MAY CONFIGURE BY TYPING 'W '.\n\n"); - if(statusLED) - statusLED->start(LED_WIFI_NEEDED); + statusLED->start(LED_WIFI_NEEDED); } - } else if(statusLED) { + } else { statusLED->start(LED_WIFI_CONNECTING); } @@ -291,12 +290,11 @@ void Span::pollTask() { if(spanOTA.enabled) ArduinoOTA.handle(); - if(controlButton && controlButton->primed() && statusLED) + if(controlButton && controlButton->primed()) statusLED->start(LED_ALERT); if(controlButton && controlButton->triggered(3000,10000)){ - if(statusLED) - statusLED->off(); + statusLED->off(); if(controlButton->type()==PushButton::LONG){ controlButton->wait(); processSerialCommand("F"); // FACTORY RESET @@ -305,8 +303,7 @@ void Span::pollTask() { } } - if(statusLED) - statusLED->check(); + statusLED->check(); } // poll @@ -326,7 +323,7 @@ int Span::getFreeSlot(){ void Span::commandMode(){ - if(!statusLED){ + if(!statusDevice){ Serial.print("*** ERROR: CAN'T ENTER COMMAND MODE WITHOUT A DEFINED STATUS LED***\n\n"); return; } @@ -409,8 +406,7 @@ void Span::checkConnect(){ connected++; waitTime=60000; alarmConnect=0; - if(statusLED) - statusLED->start(LED_WIFI_CONNECTING); + statusLED->start(LED_WIFI_CONNECTING); } if(WiFi.status()!=WL_CONNECTED){ @@ -437,12 +433,10 @@ void Span::checkConnect(){ return; } - if(statusLED){ - if(!HAPClient::nAdminControllers()) - statusLED->start(LED_PAIRING_NEEDED); - else - statusLED->on(); - } + if(!HAPClient::nAdminControllers()) + statusLED->start(LED_PAIRING_NEEDED); + else + statusLED->on(); connected++; @@ -767,12 +761,10 @@ void Span::processSerialCommand(const char *c){ Serial.print("\nDEVICE NOT YET PAIRED -- PLEASE PAIR WITH HOMEKIT APP\n\n"); mdns_service_txt_item_set("_hap","_tcp","sf","1"); // set Status Flag = 1 (Table 6-8) - if(statusLED){ - if(strlen(network.wifiData.ssid)==0) - statusLED->start(LED_WIFI_NEEDED); - else - statusLED->start(LED_PAIRING_NEEDED); - } + if(strlen(network.wifiData.ssid)==0) + statusLED->start(LED_WIFI_NEEDED); + else + statusLED->start(LED_PAIRING_NEEDED); } break; @@ -789,8 +781,7 @@ void Span::processSerialCommand(const char *c){ nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data nvs_commit(wifiNVS); // commit to NVS Serial.print("\n*** WiFi Credentials SAVED! Re-starting ***\n\n"); - if(statusLED) - statusLED->off(); + statusLED->off(); delay(1000); ESP.restart(); } @@ -823,8 +814,7 @@ void Span::processSerialCommand(const char *c){ } Serial.print("\n*** Re-starting ***\n\n"); - if(statusLED) - statusLED->off(); + statusLED->off(); delay(1000); ESP.restart(); // re-start device } @@ -832,8 +822,7 @@ void Span::processSerialCommand(const char *c){ case 'X': { - if(statusLED) - statusLED->off(); + statusLED->off(); nvs_erase_all(wifiNVS); nvs_commit(wifiNVS); Serial.print("\n*** WiFi Credentials ERASED! Re-starting...\n\n"); @@ -852,8 +841,7 @@ void Span::processSerialCommand(const char *c){ case 'H': { - if(statusLED) - statusLED->off(); + statusLED->off(); nvs_erase_all(HAPClient::hapNVS); nvs_commit(HAPClient::hapNVS); Serial.print("\n*** HomeSpan Device ID and Pairing Data DELETED! Restarting...\n\n"); @@ -864,8 +852,7 @@ void Span::processSerialCommand(const char *c){ case 'R': { - if(statusLED) - statusLED->off(); + statusLED->off(); Serial.print("\n*** Restarting...\n\n"); delay(1000); ESP.restart(); @@ -874,8 +861,7 @@ void Span::processSerialCommand(const char *c){ case 'F': { - if(statusLED) - statusLED->off(); + statusLED->off(); nvs_erase_all(HAPClient::hapNVS); nvs_commit(HAPClient::hapNVS); nvs_erase_all(wifiNVS); @@ -892,8 +878,7 @@ void Span::processSerialCommand(const char *c){ case 'E': { - if(statusLED) - statusLED->off(); + statusLED->off(); nvs_flash_erase(); Serial.print("\n*** ALL DATA ERASED! Restarting...\n\n"); delay(1000); @@ -2117,8 +2102,7 @@ void SpanOTA::start(){ Serial.printf("\n*** Current Partition: %s\n*** New Partition: %s\n*** OTA Starting..", esp_ota_get_running_partition()->label,esp_ota_get_next_update_partition(NULL)->label); otaPercent=0; - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_OTA_STARTED); + homeSpan.statusLED->start(LED_OTA_STARTED); } /////////////////////////////// @@ -2127,8 +2111,7 @@ void SpanOTA::end(){ nvs_set_u8(homeSpan.otaNVS,"OTA_REQUIRED",safeLoad); nvs_commit(homeSpan.otaNVS); Serial.printf(" DONE! Rebooting...\n"); - if(homeSpan.statusLED) - homeSpan.statusLED->off(); + homeSpan.statusLED->off(); delay(100); // make sure commit it finished before reboot } diff --git a/src/HomeSpan.h b/src/HomeSpan.h index c973a30..d378a47 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -260,7 +260,7 @@ class Span{ void setStatusPin(uint8_t pin){statusDevice=new LED(pin);} // sets Status Device to a simple LED on specified pin void setStatusDevice(Blinkable *dev){statusDevice=dev;} // sets Status Device to generic Blinkable object void setStatusAutoOff(uint16_t duration){autoOffLED=duration;} // sets Status LED auto off (seconds) - int getStatusPin(){return(statusLED?statusLED->getPin():-1);} // get Status Pin (returns -1 if undefined) + int getStatusPin(){return(statusLED->getPin());} // get Status Pin (getPin will return -1 if underlying statusDevice is undefined) int getControlPin(){return(controlButton?controlButton->getPin():-1);} // get Control Pin (returns -1 if undefined) void setApSSID(const char *ssid){network.apSSID=ssid;} // sets Access Point SSID void setApPassword(const char *pwd){network.apPassword=pwd;} // sets Access Point Password diff --git a/src/Network.cpp b/src/Network.cpp index 3118133..9a3dbc2 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -116,8 +116,7 @@ void Network::apConfigure(){ Serial.print(apPassword); Serial.print("\n"); - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_AP_STARTED); + homeSpan.statusLED->start(LED_AP_STARTED); Serial.print("\nScanning for Networks...\n\n"); @@ -155,12 +154,10 @@ void Network::apConfigure(){ if(homeSpan.controlButton && homeSpan.controlButton->triggered(9999,3000)){ Serial.print("\n*** Access Point Terminated."); - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_ALERT); + homeSpan.statusLED->start(LED_ALERT); homeSpan.controlButton->wait(); Serial.print(" Restarting... \n\n"); - if(homeSpan.statusLED) - homeSpan.statusLED->off(); + homeSpan.statusLED->off(); ESP.restart(); } @@ -179,11 +176,9 @@ void Network::apConfigure(){ Serial.print("\n*** Access Point: Configuration Canceled."); } Serial.print(" Restarting...\n\n"); - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_ALERT); + homeSpan.statusLED->start(LED_ALERT); delay(1000); - if(homeSpan.statusLED) - homeSpan.statusLED->off(); + homeSpan.statusLED->off(); ESP.restart(); } } @@ -278,8 +273,7 @@ void Network::processRequest(char *body, char *formData){ getFormValue(formData,"network",wifiData.ssid,MAX_SSID); getFormValue(formData,"pwd",wifiData.pwd,MAX_PWD); - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_WIFI_CONNECTING); + homeSpan.statusLED->start(LED_WIFI_CONNECTING); responseBody+="" "

Initiating WiFi connection to:

" + String(wifiData.ssid) + "

"; @@ -326,8 +320,7 @@ void Network::processRequest(char *body, char *formData){ } else { - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_AP_CONNECTED); // slow double-blink + homeSpan.statusLED->start(LED_AP_CONNECTED); // slow double-blink responseBody+="

SUCCESS! Connected to:

" + String(wifiData.ssid) + "

"; responseBody+="

You may enter new 8-digit Setup Code below, or leave blank to retain existing code.

"; @@ -347,8 +340,7 @@ void Network::processRequest(char *body, char *formData){ LOG1("In Landing Page...\n"); - if(homeSpan.statusLED) - homeSpan.statusLED->start(LED_AP_CONNECTED); + homeSpan.statusLED->start(LED_AP_CONNECTED); waitTime=2; responseBody+="

Welcome to HomeSpan! This page allows you to configure the above HomeSpan device to connect to your WiFi network.

" diff --git a/src/extras/Blinker.cpp b/src/extras/Blinker.cpp index 93130a0..e97ee34 100644 --- a/src/extras/Blinker.cpp +++ b/src/extras/Blinker.cpp @@ -74,7 +74,6 @@ void Blinker::start(int period, float dutyCycle, int nBlinks, int delayTime){ this->nBlinks=nBlinks; stop(); - Serial.printf("Starting Blink Task\n"); xTaskCreate( blinkTask, "BlinkTask", 1024, (void *)this, 2, &blinkHandle ); pauseTime=millis(); @@ -89,7 +88,6 @@ void Blinker::stop(){ return; if(blinkHandle!=NULL){ - Serial.printf("Deleting Blink Task\n"); vTaskDelete(blinkHandle); blinkHandle=NULL; }