Added loop with timeout for version fetch

This commit is contained in:
Tomislav Kopić 2024-11-02 09:46:29 +01:00
parent 1107b563d4
commit ee2581b3ef
2 changed files with 46 additions and 20 deletions

View File

@ -160,9 +160,10 @@ void commonButtonHandler() {
if (rightPressed && (currentMillis - rightPressStart > 2000)) { if (rightPressed && (currentMillis - rightPressStart > 2000)) {
if (!is_display_off) { if (!is_display_off) {
beep(1300);
display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display
is_display_off = true; is_display_off = true;
playTune(3); // Play beep sound beep(1000);
} }
} }

View File

@ -14,6 +14,10 @@ unsigned long api_mtbs = OCTOPRINT_API_REFRESH_INTERVAL;
unsigned long api_lasttime = 0; unsigned long api_lasttime = 0;
bool displayOctoPrintVersion(Adafruit_SSD1306& display) { 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.clearDisplay();
display.setCursor(0, 0); display.setCursor(0, 0);
display.setTextSize(1); display.setTextSize(1);
@ -28,33 +32,54 @@ bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
display.print("Port: "); display.print("Port: ");
display.print(OCTOPRINT_PORT); display.print(OCTOPRINT_PORT);
display.display(); 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.clearDisplay();
display.setCursor(0, 0); display.setCursor(0, 0);
display.setTextSize(1); display.setTextSize(1);
display.setTextColor(WHITE); display.setTextColor(WHITE);
display.print("OctoPrint Version:"); display.print("Retry ");
display.print(attempts);
display.print(" of ");
display.print(maxRetries);
display.setCursor(0, 10); display.setCursor(0, 10);
display.setTextSize(2); display.print("Failed to connect.");
display.print(api.octoprintVer.octoprintServer);
display.display(); display.display();
playTune(2); delay(1000); // Wait before retrying
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);
// 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 int scrollPos = 0; // Global variable to keep track of the scroll position