diff --git a/src/functions.h b/src/functions.h index 02c2b4d..3ac8f47 100644 --- a/src/functions.h +++ b/src/functions.h @@ -168,12 +168,8 @@ void commonButtonHandler() { // Combination of Left and Middle long press if (leftPressed && middlePressed && - (currentMillis - leftPressStart > 2000) && - (currentMillis - middlePressStart > 2000)) { - if (!leftBeeped && !middleBeeped) { + (currentMillis - leftPressStart > 2000) && (currentMillis - middlePressStart > 2000)) { beep(1000); // Play beep sound ESP.restart(); - // Handle left and middle long press action here - } } } diff --git a/src/octoprint.h b/src/octoprint.h index 76fdbc3..873c671 100644 --- a/src/octoprint.h +++ b/src/octoprint.h @@ -48,45 +48,113 @@ bool displayOctoPrintVersion(Adafruit_SSD1306& display) { delay(1000); } +int scrollPos = 0; // Global variable to keep track of the scroll position +int scrollDirection = 1; // 1 for left, -1 for right +unsigned long lastScrollTime = 0; // To control the scroll speed +const int scrollDelay = 100; // Delay in milliseconds between scroll updates + bool fetchPrintingStatus(Adafruit_SSD1306& display) { - if (millis() - api_lasttime > api_mtbs || api_lasttime == 0 && !is_display_off) { - if (api.getPrinterStatistics()) { - display.clearDisplay(); - display.setCursor(0, 0); - display.setTextSize(1); - display.setTextColor(WHITE); - // Display printer state - display.println(api.printerStats.printerState); - display.drawLine(0,10,127,10,WHITE); - display.drawLine(63,10,63,22,WHITE); - // Display nozzle and bed temperature - display.drawBitmap(10, 13, bitmap_nozzle, 8, 8, WHITE); - display.setCursor(21, 13); - display.print(api.printerStats.printerTool0TempActual); - display.drawBitmap(74, 13, bitmap_bed, 8, 8, WHITE); - display.setCursor(85, 13); - display.print(api.printerStats.printerBedTempActual); - - //Estimated print time if availble in human readable time HH:MM:SS - int estrunHours= api.printJob.estimatedPrintTime/3600; - int estsecsRemaining=api.printJob.estimatedPrintTime%3600; - int estrunMinutes=estsecsRemaining/60; - int estrunSeconds=estsecsRemaining%60; - char estbuf[31]; - sprintf(estbuf,"%02d:%02d:%02d",estrunHours,estrunMinutes,estrunSeconds); - display.setCursor(80, 0); - display.println(estbuf); - - // const float temp_percent = floor(api.printJob.progressCompletion*100)/100; - // display.print(temp_percent); - // display.println("%"); - display.display(); - api_lasttime = millis(); // Update the last fetch time - return true; + // Fetch printer statistics and update the display every api_mtbs milliseconds + if ((millis() - api_lasttime > api_mtbs || api_lasttime == 0) && !is_display_off) { + if (api.getPrinterStatistics() && api.getPrintJob()) { + // Update last fetch time + api_lasttime = millis(); } else { Serial.println("Failed to fetch printer statistics."); return false; } } - return false; // No fetch was done (within the time interval) -} + + // Regardless of whether the fetch was successful, continue with the display updates + display.clearDisplay(); + display.setCursor(0, 0); + display.setTextSize(1); + display.setTextColor(WHITE); + + // Display lines + display.println(api.printerStats.printerState); + display.drawLine(0, 10, 127, 10, WHITE); + display.drawLine(63, 10, 63, 22, WHITE); + display.drawLine(0, 22, 127, 22, WHITE); + + // Display nozzle and bed temperature + display.drawBitmap(10, 13, bitmap_nozzle, 8, 8, WHITE); + display.setCursor(21, 13); + display.print(api.printerStats.printerTool0TempActual); + display.drawBitmap(74, 13, bitmap_bed, 8, 8, WHITE); + display.setCursor(85, 13); + display.print(api.printerStats.printerBedTempActual); + + // Estimated print time in HH:MM:SS format + int estrunHours = api.printJob.estimatedPrintTime / 3600; + int estsecsRemaining = api.printJob.estimatedPrintTime % 3600; + int estrunMinutes = estsecsRemaining / 60; + int estrunSeconds = estsecsRemaining % 60; + char estbuf[31]; + sprintf(estbuf, "%02d:%02d:%02d", estrunHours, estrunMinutes, estrunSeconds); + display.setCursor(80, 0); + display.println(estbuf); + + // Display and scroll the file name + const char* fileName = api.printJob.jobFileName.c_str(); // Convert to const char* + int fileNameWidth = strlen(fileName) * 6; // Approximate width of the file name in pixels + int displayWidth = 127; + + // Check if it’s time to scroll based on the delay + if (millis() - lastScrollTime > scrollDelay) { + scrollPos += scrollDirection; // Move the scroll position by the direction + + // Check bounds to reverse direction + if (scrollPos < -fileNameWidth) { // If it scrolled past left + scrollDirection = 1; // Change direction to right + } else if (scrollPos > displayWidth) { // If it scrolled past right + scrollDirection = -1; // Change direction to left + } + + lastScrollTime = millis(); // Reset scroll timing + } + + // Draw the file name with current scroll position + display.setCursor(scrollPos, 27); // Use scrollPos directly + display.print(fileName); + + // Clear the area for "Time left" display before printing + display.fillRect(0, 35, displayWidth, 9, BLACK); // Clear area for "Time left" + // Print time left in HH:MM:SS + int runHours = api.printJob.progressPrintTimeLeft / 3600; + int secsRemaining = api.printJob.progressPrintTimeLeft % 3600; + int runMinutes = secsRemaining / 60; + int runSeconds = secsRemaining % 60; + char buf[31]; + sprintf(buf, "Time left: %02d:%02d:%02d", runHours, runMinutes, runSeconds); + display.setCursor(0, 42); + display.println(buf); + + // Progress bar and percentage + const float temp_percent = floor(api.printJob.progressCompletion * 100) / 100; + int barY = 52; // Position just above the bottom of a 64-pixel-high display + int barWidth = 127; // Full width of the display + int barHeight = 12; // Set the height of the progress bar to 12 pixels + int filledWidth = (temp_percent / 100.0) * barWidth; + + // Draw the progress bar + display.drawRect(0, barY, barWidth, barHeight, WHITE); // Outline of the bar + display.fillRect(0, barY, filledWidth, barHeight, WHITE); // Filled portion + + // Display centered percentage text in the progress bar + char percentText[6]; + sprintf(percentText, "%.0f%%", temp_percent); + int textWidth = strlen(percentText) * 6; // Approximate text width + int textX = (barWidth - textWidth) / 2; // Center the text horizontally + int textY = barY + (barHeight - 8) / 2; // Center the text vertically within the bar + + // Clear the area for text and print centered percentage + display.fillRect(textX - 1, textY, textWidth + 2, 8, BLACK); + display.setCursor(textX, textY); + display.print(percentText); + display.drawLine(0, 38, 127, 38, WHITE); + + display.display(); + + return true; // Return true to indicate the display was updated +} \ No newline at end of file