75 lines
2.2 KiB
C++
75 lines
2.2 KiB
C++
#include "displayText.h"
|
|
#include "Arduino.h"
|
|
|
|
//constructor
|
|
DisplayText::DisplayText(Adafruit_ST7796S_kbv& tftDisplay) : tft(tftDisplay) {
|
|
tft.setCursor(0,0);
|
|
tft.fillScreen(ST7796S_BLACK);
|
|
}
|
|
//display text public function
|
|
void DisplayText::writeText(char* text, int size, int posX, int posY, int screenTime, bool center, bool bottom) {
|
|
if (center) {
|
|
posX = centerText(text);
|
|
}
|
|
if (bottom) {
|
|
posY = bottomText(text);
|
|
}
|
|
tft.setCursor(posX, posY);
|
|
tft.setTextSize(size);
|
|
printWordsFull(text);
|
|
delay(screenTime);
|
|
}
|
|
//to center the text when enabled in the public function
|
|
int DisplayText::centerText(char* text) {
|
|
int16_t x1, y1;
|
|
uint16_t w, h;
|
|
tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
|
|
int x = (tft.width() - w) / 2;
|
|
return x;
|
|
}
|
|
//to dijsplay the text at the bottom when enabled in the public function
|
|
int DisplayText::bottomText(char* text) {
|
|
int16_t x1, y1;
|
|
uint16_t w, h;
|
|
tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
|
|
int y = (tft.height() - h);
|
|
return y;
|
|
}
|
|
|
|
//attempt to write the text out in full (wip)
|
|
void DisplayText::printWordsFull(char* text) {
|
|
|
|
char* textArray[100];
|
|
int i = 0;
|
|
//copy the text into a variable so the string can be re-used later
|
|
char* textCopy = strdup(text);
|
|
//split the text into words and put them into a array
|
|
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++) {
|
|
//count the amount of letters in the word
|
|
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();
|
|
//reset lineWidth
|
|
lineWidth = 0;
|
|
}
|
|
//print the text
|
|
tft.print(textArray[j]);
|
|
tft.print(" ");
|
|
lineWidth += (wordLength + 1) * charWidth; // +1 for the space
|
|
}
|
|
//removes textCopy out of the memory
|
|
free(textCopy);
|
|
|
|
} |