Skip to content

Commit

Permalink
consolidated blecomm
Browse files Browse the repository at this point in the history
  • Loading branch information
doudar committed Sep 2, 2024
1 parent 0e75789 commit ae0f80d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 61 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions include/BLE_Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/BLE_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (;;) {
Expand Down
20 changes: 5 additions & 15 deletions src/BLE_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
//}
Expand Down Expand Up @@ -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
}
}
10 changes: 0 additions & 10 deletions src/BLE_Setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
22 changes: 9 additions & 13 deletions src/ERG_Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 4 additions & 7 deletions src/HTTP_Server_Basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ void HTTP_Server::start() {
SS2K_LOG(HTTP_SERVER_LOG_TAG, "Rebooting from Web Request");
String response = "Rebooting....<script> setTimeout(\"location.href = 'http://" + myIP.toString() + "/index.html';\",500); </script>";
server.send(200, "text/html", response);
vTaskDelay(100 / portTICK_PERIOD_MS);
ESP.restart();
ss2k->rebootFlag = true;
});

server.on("/hrslider", []() {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -644,8 +642,7 @@ void HTTP_Server::settingsProcessor() {
"setTimeout(\"location.href = 'http://" +
myIP.toString() + "/bluetoothscanner.html';\",5000);</script></html>";
server.send(200, "text/html", response);
vTaskDelay(100 / portTICK_PERIOD_MS);
ESP.restart();
ss2k->rebootFlag = true;
}
server.send(200, "text/html", response);
}
Expand Down
14 changes: 5 additions & 9 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ae0f80d

Please sign in to comment.