Text scroll ping-pong effect

This commit is contained in:
Tomislav Kopić 2024-11-02 09:24:24 +01:00
parent ef6327e2eb
commit 5e78e8e369

View File

@ -18,7 +18,15 @@ bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
display.setCursor(0, 0); display.setCursor(0, 0);
display.setTextSize(1); display.setTextSize(1);
display.setTextColor(WHITE); display.setTextColor(WHITE);
display.print("Fetching OctoPrint Version..."); display.print("Connecting to");
display.setCursor(0, 10);
display.print("OctoPrint");
display.setCursor(0, 20);
display.print("Host: ");
display.print(OCTOPRINT_HOST);
display.setCursor(0, 30);
display.print("Port: ");
display.print(OCTOPRINT_PORT);
display.display(); display.display();
if (api.getOctoprintVersion()) { if (api.getOctoprintVersion()) {
@ -49,7 +57,7 @@ bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
} }
int scrollPos = 0; // Global variable to keep track of the scroll position int scrollPos = 0; // Global variable to keep track of the scroll position
int scrollDirection = 1; // 1 for left, -1 for right int scrollDirection = 1; // 1 for left, -1 for right
unsigned long lastScrollTime = 0; // To control the scroll speed unsigned long lastScrollTime = 0; // To control the scroll speed
const int scrollDelay = 100; // Delay in milliseconds between scroll updates const int scrollDelay = 100; // Delay in milliseconds between scroll updates
@ -99,23 +107,26 @@ bool fetchPrintingStatus(Adafruit_SSD1306& display) {
const char* fileName = api.printJob.jobFileName.c_str(); // Convert to const char* 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 fileNameWidth = strlen(fileName) * 6; // Approximate width of the file name in pixels
int displayWidth = 127; int displayWidth = 127;
// Clear the area for the file name to avoid overlap
display.fillRect(0, 26, displayWidth, 11, BLACK);
// Check if its time to scroll based on the delay // Check if its time to scroll based on the delay
if (millis() - lastScrollTime > scrollDelay) { if (millis() - lastScrollTime > scrollDelay) {
scrollPos += scrollDirection; // Move the scroll position by the direction // Update scroll position based on the current direction
scrollPos += scrollDirection;
lastScrollTime = millis();
// Check bounds to reverse direction // Reverse direction if the text reaches either edge
if (scrollPos < -fileNameWidth) { // If it scrolled past left if (scrollPos > 0) { // Reached the left edge
scrollDirection = 1; // Change direction to right scrollDirection = -1; // Start moving left
} else if (scrollPos > displayWidth) { // If it scrolled past right } else if (scrollPos < -fileNameWidth + displayWidth) { // Reached the right edge
scrollDirection = -1; // Change direction to left scrollDirection = 1; // Start moving right
} }
lastScrollTime = millis(); // Reset scroll timing
} }
// Draw the file name with current scroll position // Draw the file name with current scroll position
display.setCursor(scrollPos, 27); // Use scrollPos directly display.setCursor(scrollPos, 27);
display.print(fileName); display.print(fileName);
// Clear the area for "Time left" display before printing // Clear the area for "Time left" display before printing
@ -149,7 +160,7 @@ bool fetchPrintingStatus(Adafruit_SSD1306& display) {
int textY = barY + (barHeight - 8) / 2; // Center the text vertically within the bar int textY = barY + (barHeight - 8) / 2; // Center the text vertically within the bar
// Clear the area for text and print centered percentage // Clear the area for text and print centered percentage
display.fillRect(textX - 1, textY, textWidth + 2, 8, BLACK); display.fillRect(textX - 1, textY -1, textWidth + 2, 10, BLACK);
display.setCursor(textX, textY); display.setCursor(textX, textY);
display.print(percentText); display.print(percentText);
display.drawLine(0, 38, 127, 38, WHITE); display.drawLine(0, 38, 127, 38, WHITE);