76 lines
2.0 KiB
C++
76 lines
2.0 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 display 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) {
|
|
const int screenWidth = 320; // replace with your TFT display width
|
|
const int lineHeight = 22; // replace with your text line height
|
|
|
|
char* word = strtok(text, " ");
|
|
char line[100] = "";
|
|
int lineCount = 0;
|
|
|
|
while (word != NULL) {
|
|
char tempLine[100];
|
|
strcpy(tempLine, line);
|
|
strcat(tempLine, word);
|
|
strcat(tempLine, " ");
|
|
|
|
int16_t x1, y1;
|
|
uint16_t w, h;
|
|
tft.getTextBounds(tempLine, 0, 0, &x1, &y1, &w, &h);
|
|
|
|
if (w > screenWidth && strlen(line) > 0) {
|
|
tft.setCursor(0, lineHeight * lineCount);
|
|
tft.println(line);
|
|
lineCount++;
|
|
strcpy(line, word);
|
|
strcat(line, " ");
|
|
} else {
|
|
strcpy(line, tempLine);
|
|
}
|
|
|
|
word = strtok(NULL, " ");
|
|
}
|
|
|
|
// print the last line
|
|
tft.setCursor(0, lineHeight * lineCount);
|
|
tft.println(line);
|
|
} |