attempt to write text in full
This commit is contained in:
@@ -39,5 +39,5 @@ void loop() {
|
|||||||
// writeText(Answer[0], 2, 50, 300, 4000);
|
// writeText(Answer[0], 2, 50, 300, 4000);
|
||||||
displayText.writeText(Question[i], 3, 0, 0, 0, true, false);
|
displayText.writeText(Question[i], 3, 0, 0, 0, true, false);
|
||||||
displayText.writeText(Answer[i], 3, 0, 200, 0, true, true);
|
displayText.writeText(Answer[i], 3, 0, 200, 0, true, true);
|
||||||
delay(20000);
|
delay(2000);
|
||||||
}
|
}
|
||||||
|
@@ -11,14 +11,15 @@ void DisplayText::writeText(char* text, int size, int posX, int posY, int screen
|
|||||||
if (center) {
|
if (center) {
|
||||||
posX = centerText(text);
|
posX = centerText(text);
|
||||||
}
|
}
|
||||||
if (bottom) {
|
// if (bottom) {
|
||||||
posY = bottomText(text);
|
// posY = bottomText(text);
|
||||||
}
|
// }
|
||||||
tft.setCursor(posX, posY);
|
tft.setCursor(posX, posY);
|
||||||
tft.setTextSize(size);
|
tft.setTextSize(size);
|
||||||
printWordsFull(text);
|
printWordsFull(text);
|
||||||
delay(screenTime);
|
delay(screenTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
//to center the text when enabled in the public function
|
//to center the text when enabled in the public function
|
||||||
int DisplayText::centerText(char* text) {
|
int DisplayText::centerText(char* text) {
|
||||||
int16_t x1, y1;
|
int16_t x1, y1;
|
||||||
@@ -27,49 +28,49 @@ int DisplayText::centerText(char* text) {
|
|||||||
int x = (tft.width() - w) / 2;
|
int x = (tft.width() - w) / 2;
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
//to dijsplay the text at the bottom when enabled in the public function
|
|
||||||
int DisplayText::bottomText(char* text) {
|
// //to display the text at the bottom when enabled in the public function
|
||||||
int16_t x1, y1;
|
// int DisplayText::bottomText(char* text) {
|
||||||
uint16_t w, h;
|
// int16_t x1, y1;
|
||||||
tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
|
// uint16_t w, h;
|
||||||
int y = (tft.height() - h);
|
// tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
|
||||||
return y;
|
// int y = (tft.height() - h);
|
||||||
}
|
// return y;
|
||||||
|
// }
|
||||||
|
|
||||||
//attempt to write the text out in full (wip)
|
//attempt to write the text out in full (wip)
|
||||||
void DisplayText::printWordsFull(char* text) {
|
void DisplayText::printWordsFull(char* text) {
|
||||||
|
const int screenWidth = 320; // replace with your TFT display width
|
||||||
char* textArray[100];
|
const int lineHeight = 22; // replace with your text line height
|
||||||
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, " ");
|
char* word = strtok(text, " ");
|
||||||
|
char line[100] = "";
|
||||||
|
int lineCount = 0;
|
||||||
|
|
||||||
while (word != NULL) {
|
while (word != NULL) {
|
||||||
textArray[i] = word;
|
char tempLine[100];
|
||||||
word = strtok(NULL, " ");
|
strcpy(tempLine, line);
|
||||||
i++;
|
strcat(tempLine, word);
|
||||||
}
|
strcat(tempLine, " ");
|
||||||
|
|
||||||
int lineWidth = 0;
|
int16_t x1, y1;
|
||||||
int maxLineWidth = 320; // replace with the width of your TFT display
|
uint16_t w, h;
|
||||||
int charWidth = 11; // replace with the width of your characters
|
tft.getTextBounds(tempLine, 0, 0, &x1, &y1, &w, &h);
|
||||||
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 (w > screenWidth && strlen(line) > 0) {
|
||||||
if (lineWidth + wordLength * charWidth > maxLineWidth) {
|
tft.setCursor(0, lineHeight * lineCount);
|
||||||
tft.println();
|
tft.println(line);
|
||||||
//reset lineWidth
|
lineCount++;
|
||||||
lineWidth = 0;
|
strcpy(line, word);
|
||||||
|
strcat(line, " ");
|
||||||
|
} else {
|
||||||
|
strcpy(line, tempLine);
|
||||||
}
|
}
|
||||||
//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);
|
|
||||||
|
|
||||||
|
word = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// print the last line
|
||||||
|
tft.setCursor(0, lineHeight * lineCount);
|
||||||
|
tft.println(line);
|
||||||
}
|
}
|
@@ -8,7 +8,7 @@ class DisplayText {
|
|||||||
private:
|
private:
|
||||||
Adafruit_ST7796S_kbv& tft;
|
Adafruit_ST7796S_kbv& tft;
|
||||||
int centerText(char* text);
|
int centerText(char* text);
|
||||||
int bottomText(char* text);
|
// int bottomText(char* text);
|
||||||
void printWordsFull(char* text);
|
void printWordsFull(char* text);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Reference in New Issue
Block a user