Fully functioning screen code
This commit is contained in:
106
arduino/Screen code/Screen-code-final/displayText.cpp
Normal file
106
arduino/Screen code/Screen-code-final/displayText.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#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, bottom);
|
||||
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, bool bottom) {
|
||||
const int screenWidth = 480; // replace with your TFT display width
|
||||
const int lineHeight = 30; // replace with your text line height
|
||||
//the double copy is needed so it doesnt alter the original text
|
||||
char* newtext1 = strdup(text); // Create a copy of the string for the first strtok_r
|
||||
char* newtext2 = strdup(text); // Create a second copy for the second strtok_r
|
||||
char* saveptr1, *saveptr2;
|
||||
char* word = strtok_r(newtext1, " ", &saveptr1);
|
||||
char line[100] = "";
|
||||
int lineCount = 0;
|
||||
int lineWidth = 0;
|
||||
|
||||
// Calculate total number of lines
|
||||
int totalLines = 0;
|
||||
char* tempWord = strtok_r(newtext2, " ", &saveptr2);
|
||||
while (tempWord != NULL) {
|
||||
int16_t x1, y1;
|
||||
uint16_t w, h;
|
||||
tft.getTextBounds(tempWord, 0, 0, &x1, &y1, &w, &h);
|
||||
if (lineWidth + w > screenWidth && strlen(line) > 0) {
|
||||
//if the line width is greater than the screen width, then we need to add a new line
|
||||
totalLines++;
|
||||
lineWidth = w;
|
||||
//otherwise we just add the width of the word to the line width
|
||||
} else {
|
||||
lineWidth += w;
|
||||
}
|
||||
tempWord = strtok_r(NULL, " ", &saveptr2);
|
||||
}
|
||||
totalLines++; // Add one for the last line
|
||||
|
||||
// Reset variables for actual printing
|
||||
strcpy(line, "");
|
||||
lineWidth = 0;
|
||||
|
||||
while (word != NULL) {
|
||||
int16_t x1, y1;
|
||||
uint16_t w, h;
|
||||
tft.getTextBounds(word, 0, 0, &x1, &y1, &w, &h);
|
||||
|
||||
if (lineWidth + w > screenWidth && strlen(line) > 0) {
|
||||
int y = bottom ? tft.height() - lineHeight * (totalLines - lineCount) : lineHeight * lineCount;
|
||||
tft.setCursor(0, y);
|
||||
tft.println(line);
|
||||
lineCount++;
|
||||
strcpy(line, word);
|
||||
strcat(line, " ");
|
||||
lineWidth = w;
|
||||
} else {
|
||||
strcat(line, word);
|
||||
strcat(line, " ");
|
||||
lineWidth += w;
|
||||
}
|
||||
|
||||
word = strtok_r(NULL, " ", &saveptr1);
|
||||
}
|
||||
|
||||
// print the last line
|
||||
int y = bottom ? tft.height() - lineHeight * (totalLines - lineCount) : lineHeight * lineCount;
|
||||
tft.setCursor(0, y);
|
||||
tft.println(line);
|
||||
|
||||
free(newtext1); // Free the memory allocated by strdup
|
||||
free(newtext2); // Free the memory allocated by strdup
|
||||
}
|
Reference in New Issue
Block a user