From aa6c35044ef07adcbb229d27f164094ab2268017 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 7 Mar 2024 19:54:31 +0100 Subject: [PATCH] Added a function to attempt to diplay the words in full --- .../Screen-code-full/displayText.cpp | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/arduino/Screen code/Screen-code-full/displayText.cpp b/arduino/Screen code/Screen-code-full/displayText.cpp index 99d6db6..9044558 100644 --- a/arduino/Screen code/Screen-code-full/displayText.cpp +++ b/arduino/Screen code/Screen-code-full/displayText.cpp @@ -4,6 +4,7 @@ //constructor DisplayText::DisplayText(Adafruit_ST7796S_kbv& tftDisplay) : tft(tftDisplay) { tft.setCursor(0,0); + tft.fillScreen(ST7796S_BLACK); } void DisplayText::writeText(char* text, int size, int posX, int posY, int screenTime, bool center, bool bottom) { @@ -15,12 +16,12 @@ void DisplayText::writeText(char* text, int size, int posX, int posY, int screen } tft.setCursor(posX, posY); tft.setTextSize(size); - tft.println(text); + printWordsFull(text); delay(screenTime); } int DisplayText::centerText(char* text) { - int16_t x1, y1; + int16_t x1, y1; uint16_t w, h; tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); int x = (tft.width() - w) / 2; @@ -33,4 +34,39 @@ int DisplayText::bottomText(char* text) { tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); int y = (tft.height() - h); return y; +} + +void DisplayText::printWordsFull(char* text) { + + char* textArray[100]; + int i = 0; + + char* textCopy = strdup(text); + + char* word = strtok(text, " "); + while (word != NULL) { + textArray[i] = word; + word = strtok(NULL, " "); + i++; + } + + int lineWidth = 0; + int maxLineWidth = 320; // replace with the width of your TFT display + int charWidth = 11; // replace with the width of your characters + + for (int j = 0; j < i; j++) { + int wordLength = strlen(textArray[j]); + + // If the word won't fit on the current line, move to the next line + if (lineWidth + wordLength * charWidth > maxLineWidth) { + tft.println(); + lineWidth = 0; + } + + tft.print(textArray[j]); + tft.print(" "); + lineWidth += (wordLength + 1) * charWidth; // +1 for the space + } + free(textCopy); + } \ No newline at end of file