From ee2581b3efe5c914b30fea80ff4c00b88bc470ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Kopi=C4=87?= Date: Sat, 2 Nov 2024 09:46:29 +0100 Subject: [PATCH] Added loop with timeout for version fetch --- src/functions.h | 3 ++- src/octoprint.h | 63 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/functions.h b/src/functions.h index 3ac8f47..c71bacf 100644 --- a/src/functions.h +++ b/src/functions.h @@ -160,9 +160,10 @@ void commonButtonHandler() { if (rightPressed && (currentMillis - rightPressStart > 2000)) { if (!is_display_off) { + beep(1300); display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display is_display_off = true; - playTune(3); // Play beep sound + beep(1000); } } diff --git a/src/octoprint.h b/src/octoprint.h index 1a1376d..0c7338f 100644 --- a/src/octoprint.h +++ b/src/octoprint.h @@ -14,6 +14,10 @@ unsigned long api_mtbs = OCTOPRINT_API_REFRESH_INTERVAL; unsigned long api_lasttime = 0; bool displayOctoPrintVersion(Adafruit_SSD1306& display) { + const int maxRetries = 3; // Maximum number of attempts + int attempts = 0; + + // Initial display of connecting message with host and port display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); @@ -28,33 +32,54 @@ bool displayOctoPrintVersion(Adafruit_SSD1306& display) { display.print("Port: "); display.print(OCTOPRINT_PORT); display.display(); - delay(500); - if (api.getOctoprintVersion()) { + delay(500); // Delay to allow message display before attempting connection + + // Attempt to connect up to maxRetries times + while (attempts < maxRetries) { + if (api.getOctoprintVersion()) { + // Connection successful; display version information + display.clearDisplay(); + display.setCursor(0, 0); + display.setTextSize(1); + display.setTextColor(WHITE); + display.print("OctoPrint Version:"); + display.setCursor(0, 10); + display.setTextSize(2); + display.print(api.octoprintVer.octoprintServer); + display.display(); + playTune(2); // Success tune + return true; // Exit function, indicating success + } + + attempts++; // Increment attempt count + + // If the connection attempt fails, clear the display and show retry status display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); display.setTextColor(WHITE); - display.print("OctoPrint Version:"); + display.print("Retry "); + display.print(attempts); + display.print(" of "); + display.print(maxRetries); display.setCursor(0, 10); - display.setTextSize(2); - display.print(api.octoprintVer.octoprintServer); + display.print("Failed to connect."); display.display(); - playTune(2); - return true; - } else { - display.clearDisplay(); - display.setCursor(0, 0); - display.setTextSize(1); - display.setTextColor(WHITE); - display.print("Failed to get"); - display.setCursor(0, 10); - display.print("OctoPrint version."); - display.display(); - playTune(3); - return false; + delay(1000); // Wait before retrying } - delay(1000); + + // If all attempts fail, display final failure message + display.clearDisplay(); + display.setCursor(0, 0); + display.setTextSize(1); + display.setTextColor(WHITE); + display.print("Failed to get"); + display.setCursor(0, 10); + display.print("OctoPrint version."); + display.display(); + playTune(3); // Failure tune + return false; } int scrollPos = 0; // Global variable to keep track of the scroll position