diff --git a/Software/src/constants/copy/en-us.h b/Software/src/constants/copy/en-us.h index d494c38..049aab7 100644 --- a/Software/src/constants/copy/en-us.h +++ b/Software/src/constants/copy/en-us.h @@ -27,6 +27,7 @@ const char en_Stroke[] PROGMEM = "Stroke"; const char en_StrokeEngine[] PROGMEM = "Stroke Engine"; const char en_StrokeTooShort[] PROGMEM = "Stroke too short. Please check your drive belt."; const char en_Pair[] PROGMEM = "Pair Device"; +const char en_PairingFailed[] PROGMEM = "Pairing failed. Please try again."; const char en_PairingInstructions[] PROGMEM = "Enter the following code on the dashboard"; const char en_Update[] PROGMEM = "Update"; const char en_UpdateMessage[] PROGMEM = "Update is in progress. This may take up to 60s."; @@ -79,6 +80,7 @@ static const LanguageStruct enUs = { .StrokeEngine = en_StrokeEngine, .StrokeTooShort = en_StrokeTooShort, .Pair = en_Pair, + .PairingFailed = en_PairingFailed, .PairingInstructions = en_PairingInstructions, .Update = en_Update, .UpdateMessage = en_UpdateMessage, diff --git a/Software/src/constants/copy/fr.h b/Software/src/constants/copy/fr.h index e196712..1b6dcd6 100644 --- a/Software/src/constants/copy/fr.h +++ b/Software/src/constants/copy/fr.h @@ -28,6 +28,7 @@ const char fr_Stroke[] PROGMEM = "Coup"; const char fr_StrokeEngine[] PROGMEM = "Stroke Engine"; const char fr_StrokeTooShort[] PROGMEM = "Course trop courte. Veuillez vérifier votre courroie d'entraînement."; const char fr_Pair[] PROGMEM = "Jumeler l'appareil"; +const char fr_PairingFailed[] PROGMEM = "L'appairage a échoué. Veuillez réessayer."; const char fr_PairingInstructions[] PROGMEM = "Entrez le code suivant sur le tableau de bord"; const char fr_Update[] PROGMEM = "Mettre à jour"; const char fr_UpdateMessage[] PROGMEM = "La mise à jour est en cours. Ça peut prendre jusqu'à 60 secondes."; @@ -81,6 +82,7 @@ static const LanguageStruct fr = { .StrokeEngine = fr_StrokeEngine, .StrokeTooShort = fr_StrokeTooShort, .Pair = fr_Pair, + .PairingFailed = fr_PairingFailed, .PairingInstructions = fr_PairingInstructions, .Update = fr_Update, .UpdateMessage = fr_UpdateMessage, diff --git a/Software/src/main.cpp b/Software/src/main.cpp index ab61c0f..eae9b62 100644 --- a/Software/src/main.cpp +++ b/Software/src/main.cpp @@ -46,14 +46,15 @@ void setup() { button.attachLongPressStart([]() { ossm->sm->process_event(LongPress{}); }); xTaskCreate( - [](void* pvParameters) { - while (true) { - button.tick(); - ossm->wm.process(); - vTaskDelay(10); - } - }, - "buttonTask", 4 * configMINIMAL_STACK_SIZE, nullptr, 1, nullptr); + [](void *pvParameters) { + while (true) { + button.tick(); + vTaskDelay(10); + ossm->wm.process(); + vTaskDelay(10); + } + }, + "buttonTask", 5 * configMINIMAL_STACK_SIZE, nullptr, 1, nullptr); }; void loop() { vTaskDelete(nullptr); }; \ No newline at end of file diff --git a/Software/src/ossm/OSSM.WiFi.cpp b/Software/src/ossm/OSSM.WiFi.cpp index bad9a14..0600156 100644 --- a/Software/src/ossm/OSSM.WiFi.cpp +++ b/Software/src/ossm/OSSM.WiFi.cpp @@ -28,6 +28,30 @@ void OSSM::drawWiFi() { wm.startConfigPortal("OSSM Setup"); } +auto pairingTask = [](void *pvParameters) { + OSSM *ossm = (OSSM *) pvParameters; + int taskStart = millis(); + auto isInCorrectState = [](OSSM *ossm) { + // Add any states that you want to support here. + return ossm->sm->is("pair"_s) || ossm->sm->is("pair.idle"_s); + }; + + while (isInCorrectState(ossm)) { + vTaskDelay(10); + int timeElapsed = millis() - taskStart; + if (timeElapsed > 300000) { +// 5 minutes have passed + ossm->errorMessage = UserConfig::language.PairingTookTooLong; + ossm->sm->process_event(Error{}); + break; + } + + } + + vTaskDelete(nullptr); + +}; + void OSSM::drawPairing() { String id = getId(); @@ -52,6 +76,10 @@ void OSSM::drawPairing() { display.sendBuffer(); displayMutex.unlock(); + // start a task to wait 5 minutes then throw an error: + xTaskCreate(pairingTask, + "pairingTask", 3 * configMINIMAL_STACK_SIZE, this, 1, nullptr); + // prepare the payload. DynamicJsonDocument doc(1024); @@ -78,39 +106,15 @@ void OSSM::drawPairing() { http.addHeader("Authorization", "Bearer cchzYsEaUEy7zoqfYZO2loHg4pKIcIQAvCo3LW9aKYg="); int httpCode = http.POST(payload); - if (httpCode > 0) { + if (httpCode == HTTP_CODE_OK) { ESP_LOGD("PAIRING", "Response: %s", payload.c_str()); } else { ESP_LOGE("PAIRING", "Error: %s", http.errorToString(httpCode).c_str()); + errorMessage = String(UserConfig::language.PairingFailed) + " Code: " + httpCode; + sm->process_event(Error{}); } http.end(); - // start a task to wait 5 minutes then throw an error: - xTaskCreate( - [](void *pvParameters) { - OSSM *ossm = (OSSM *) pvParameters; - int taskStart = millis(); - auto isInCorrectState = [](OSSM *ossm) { - // Add any states that you want to support here. - return ossm->sm->is("pair"_s) || ossm->sm->is("pair.idle"_s); - }; - - while (isInCorrectState(ossm)) { - vTaskDelay(10); - int timeElapsed = millis() - taskStart; - if (timeElapsed > 300000) { - // 5 minutes have passed - ossm->errorMessage = UserConfig::language.PairingTookTooLong; - ossm->sm->process_event(Error{}); - break; - } - - } - - vTaskDelete(nullptr); - - }, - "buttonTask", 3 * configMINIMAL_STACK_SIZE, this, 1, nullptr); } diff --git a/Software/src/ossm/OSSM.h b/Software/src/ossm/OSSM.h index f849cc2..97bb85b 100644 --- a/Software/src/ossm/OSSM.h +++ b/Software/src/ossm/OSSM.h @@ -258,7 +258,6 @@ class OSSM { bool isForward = true; Menu menuOption; - String errorMessage = ""; SettingPercents setting = {.speed = 0, .stroke = 0, @@ -347,6 +346,7 @@ class OSSM { sm = nullptr; // The state machine WiFiManager wm; + String errorMessage = ""; }; #endif // OSSM_SOFTWARE_OSSM_H diff --git a/Software/src/structs/LanguageStruct.h b/Software/src/structs/LanguageStruct.h index 9d115e8..5a1e604 100644 --- a/Software/src/structs/LanguageStruct.h +++ b/Software/src/structs/LanguageStruct.h @@ -24,6 +24,7 @@ struct LanguageStruct { const char* StrokeEngine; const char* StrokeTooShort; const char* Pair; + const char* PairingFailed; const char* PairingInstructions; const char* PairingTookTooLong; const char* Update;