diff --git a/CHANGELOG.md b/CHANGELOG.md index b2610a4e..05e877f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,7 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed driver temp checking. It's not accurate on the ESP32. - Peloton resistance limit enhancements. - Continue updating power metrics to other clients if one client disconnects. -- Freed 13k of ram by consolidating tasks and using timers instead of delays. +- Freed 19k of ram by consolidating tasks and using timers instead of delays. ### Hardware - added Yesoul S3. diff --git a/include/BLE_Common.h b/include/BLE_Common.h index 4d7338e2..a9b450bd 100644 --- a/include/BLE_Common.h +++ b/include/BLE_Common.h @@ -33,11 +33,9 @@ // Setup void setupBLE(); -extern TaskHandle_t BLECommunicationTask; extern TaskHandle_t BLEClientTask; // ***********************Common********************************** -void BLECommunications(void *pvParameters); - +void BLECommunications(); // *****************************Server**************************** class MyServerCallbacks : public NimBLEServerCallbacks { public: diff --git a/include/settings.h b/include/settings.h index cbf7fbdf..102a9ddc 100644 --- a/include/settings.h +++ b/include/settings.h @@ -312,8 +312,7 @@ const char* const DEFAULT_PASSWORD = "password"; #define BLE_RECONNECT_SCAN_DURATION 5 // Task Stack Sizes -#define MAIN_STACK 5000 -#define BLE_COMM_STACK 6000 +#define MAIN_STACK 6000 #define BLE_CLIENT_STACK 5500 // Uncomment to enable stack size debugging info diff --git a/src/BLE_Client.cpp b/src/BLE_Client.cpp index 5948580f..9ab26d0d 100644 --- a/src/BLE_Client.cpp +++ b/src/BLE_Client.cpp @@ -58,7 +58,8 @@ static void onNotify(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t } } -// BLE Client loop task +// BLE Client loop task. +// Manages device connections and scanning. void bleClientTask(void *pvParameters) { long int scanDelay = millis(); for (;;) { diff --git a/src/BLE_Common.cpp b/src/BLE_Common.cpp index 4252cf6b..ff419d74 100644 --- a/src/BLE_Common.cpp +++ b/src/BLE_Common.cpp @@ -17,13 +17,10 @@ bool hr2p = false; -TaskHandle_t BLECommunicationTask; - -void BLECommunications(void *pvParameters) { - for (;;) { - // if (!spinBLEClient.dontBlockScan) { - // NimBLEDevice::getScan()->stop(); // stop routine scans - // } +void BLECommunications() { + static unsigned long int bleCommTimer = millis(); + if (((millis() - bleCommTimer) > BLE_NOTIFY_DELAY) && !ss2k->isUpdating) { + bleCommTimer = millis(); // **********************************Client*************************************** for (auto &_BLEd : spinBLEClient.myBLEDevices) { // loop through discovered devices if (_BLEd.connectedClientID != BLE_HS_CONN_HANDLE_NONE) { @@ -63,10 +60,7 @@ void BLECommunications(void *pvParameters) { spinBLEClient.handleBattInfo(pClient, false); } else if (!pClient->isConnected()) { // This shouldn't ever be - // called... - // if (pClient->disconnect() == 0) { // 0 is a successful disconnect - // BLEDevice::deleteClient(pClient); - // vTaskDelay(100 / portTICK_PERIOD_MS); + // called... SS2K_LOG(BLE_COMMON_LOG_TAG, "Workaround connect"); _BLEd.doConnect = true; //} @@ -129,9 +123,5 @@ void BLECommunications(void *pvParameters) { } else { digitalWrite(LED_PIN, HIGH); } - vTaskDelay((BLE_NOTIFY_DELAY) / portTICK_PERIOD_MS); -#ifdef DEBUG_STACK - Serial.printf("BLEComm: %d \n", uxTaskGetStackHighWaterMark(BLECommunicationTask)); -#endif // DEBUG_STACK } } \ No newline at end of file diff --git a/src/BLE_Setup.cpp b/src/BLE_Setup.cpp index 91185a5f..7d427c44 100644 --- a/src/BLE_Setup.cpp +++ b/src/BLE_Setup.cpp @@ -17,16 +17,6 @@ void setupBLE() { // Common BLE setup for both client and server BLEDevice::setMTU(515); //-- enabling this is very important for BLE firmware updates. spinBLEClient.start(); startBLEServer(); - - xTaskCreatePinnedToCore(BLECommunications, /* Task function. */ - "BLECommunicationTask", /* name of task. */ - BLE_COMM_STACK, /* Stack size of task*/ - NULL, /* parameter of the task */ - 3, /* priority of the task*/ - &BLECommunicationTask, /* Task handle to keep track of created task */ - 1); /* pin task to core */ - - SS2K_LOG(BLE_SETUP_LOG_TAG, "BLE Notify Task Started"); SS2K_LOG(BLE_SETUP_LOG_TAG, "%s %s %s", userConfig->getConnectedPowerMeter(), userConfig->getConnectedHeartMonitor(), userConfig->getConnectedRemote()); SS2K_LOG(BLE_SETUP_LOG_TAG, "End BLE Setup"); } diff --git a/src/ERG_Mode.cpp b/src/ERG_Mode.cpp index 63c36a13..a7b1b1a4 100644 --- a/src/ERG_Mode.cpp +++ b/src/ERG_Mode.cpp @@ -21,17 +21,15 @@ PowerTable* powerTable = new PowerTable; // Create a torque table representing 0w-1000w in 50w increments. // i.e. powerTable[1] corresponds to the incline required for 50w. powerTable[2] is the incline required for 100w and so on. +static unsigned long int ergTimer = millis(); + void PowerTable::runERG() { static ErgMode ergMode; static PowerBuffer powerBuffer; - - // ergMode._writeLogHeader(); static bool hasConnectedPowerMeter = false; static bool simulationRunning = false; static int loopCounter = 0; - static unsigned long int ergTimer = millis(); - if ((millis() - ergTimer) > ERG_MODE_DELAY) { // reset the timer. ergTimer = millis(); @@ -1129,17 +1127,15 @@ void ErgMode::_setPointChangeState(int newCadence, Measurement& newWatts) { SS2K_LOG(ERG_MODE_LOG_TAG, "SetPoint changed:%dw PowerTable Result: %d", newWatts.getTarget(), tableResult); _updateValues(newCadence, newWatts, tableResult); - int i = 0; - while (rtConfig->getTargetIncline() != rtConfig->getCurrentIncline()) { // wait while the knob moves to target position. - vTaskDelay(100 / portTICK_PERIOD_MS); - if (i > 50) { // failsafe for infinite loop - SS2K_LOG(ERG_MODE_LOG_TAG, "Stepper didn't reach target position"); - break; + if (rtConfig->getTargetIncline() != rtConfig->getCurrentIncline()) { // add some time to wait while the knob moves to target position. + int timeToAdd = abs(rtConfig->getCurrentIncline() - rtConfig->getTargetIncline()); + if (timeToAdd > 5000) { // 5 seconds + SS2K_LOG(ERG_MODE_LOG_TAG, "Capping ERG seek time to 5 seconds"); + timeToAdd = 5000; } - i++; + ergTimer += timeToAdd; } - - vTaskDelay((ERG_MODE_DELAY * 2) / portTICK_PERIOD_MS); // Wait for power meter to register new watts + ergTimer += (ERG_MODE_DELAY * 2); // Wait for power meter to register new watts } void ErgMode::_inSetpointState(int newCadence, Measurement& newWatts) { diff --git a/src/HTTP_Server_Basic.cpp b/src/HTTP_Server_Basic.cpp index 9373f2c9..1e4a76c3 100644 --- a/src/HTTP_Server_Basic.cpp +++ b/src/HTTP_Server_Basic.cpp @@ -189,8 +189,7 @@ void HTTP_Server::start() { SS2K_LOG(HTTP_SERVER_LOG_TAG, "Rebooting from Web Request"); String response = "Rebooting...."; server.send(200, "text/html", response); - vTaskDelay(100 / portTICK_PERIOD_MS); - ESP.restart(); + ss2k->rebootFlag = true; }); server.on("/hrslider", []() { @@ -365,8 +364,7 @@ void HTTP_Server::start() { server.send(200, "text/plain", "Littlefs Uploaded Successfully. Rebooting..."); userConfig->saveToLittleFS(); userPWC->saveToLittleFS(); - vTaskDelay(100); - ESP.restart(); + ss2k->rebootFlag == true; } else { Update.printError(Serial); } @@ -413,7 +411,7 @@ void HTTP_Server::start() { void HTTP_Server::webClientUpdate() { static unsigned long int _webClientTimer = millis(); if (millis() - _webClientTimer > WEBSERVER_DELAY) { - _webClientTimer = millis(); + _webClientTimer = millis(); static unsigned long mDnsTimer = millis(); // NOLINT: There is no overload in String for uint64_t server.handleClient(); if (WiFi.getMode() != WIFI_MODE_STA) { @@ -644,8 +642,7 @@ void HTTP_Server::settingsProcessor() { "setTimeout(\"location.href = 'http://" + myIP.toString() + "/bluetoothscanner.html';\",5000);"; server.send(200, "text/html", response); - vTaskDelay(100 / portTICK_PERIOD_MS); - ESP.restart(); + ss2k->rebootFlag = true; } server.send(200, "text/html", response); } diff --git a/src/Main.cpp b/src/Main.cpp index 1b53a9b2..28aaf54b 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -51,9 +51,7 @@ WebSocketAppender webSocketAppender; void SS2K::startTasks() { SS2K_LOG(MAIN_LOG_TAG, "Start BLE + ERG Tasks"); spinBLEClient.intentionalDisconnect = 0; - if (BLECommunicationTask == NULL) { - setupBLE(); - } + setupBLE(); } void SS2K::stopTasks() { @@ -65,10 +63,6 @@ void SS2K::stopTasks() { ss2k->stopTasks(); } SS2K_LOG(MAIN_LOG_TAG, "Stop BLE + ERG Tasks"); - if (BLECommunicationTask != NULL) { - vTaskDelete(BLECommunicationTask); - BLECommunicationTask = NULL; - } if (BLEClientTask != NULL) { vTaskDelete(BLEClientTask); BLEClientTask = NULL; @@ -180,6 +174,8 @@ void SS2K::maintenanceLoop(void *pvParameters) { while (true) { vTaskDelay(5 / portTICK_RATE_MS); + // Run what used to be in the BLECommunications Task. + BLECommunications(); // send BLE notification for any userConfig values that changed. BLE_ss2kCustomCharacteristic::parseNemit(); // Run What used to be in the Stepper Task. @@ -231,6 +227,7 @@ void SS2K::maintenanceLoop(void *pvParameters) { // Handle flag set for rebooting if (ss2k->rebootFlag) { static bool _loopOnce = false; + vTaskDelay(1000 / portTICK_RATE_MS); // Let the main task loop complete once before rebooting if (_loopOnce) { // Important to keep this delay high in order to allow coms to finish. @@ -437,8 +434,7 @@ void SS2K::moveStepper() { } } rtConfig->setCurrentIncline((float)stepper->getCurrentPosition()); - vTaskDelay(50 / portTICK_PERIOD_MS); - + if (connectedClientCount() > 0) { stepper->setAutoEnable(false); // Keep the stepper from rolling back due to head tube slack. Motor Driver still lowers power between moves stepper->enableOutputs();