Added comments and cleanup

This commit is contained in:
sam
2024-03-08 14:25:24 +01:00
parent 77636f2a60
commit 60d86bc0d5
3 changed files with 11 additions and 32 deletions

View File

@@ -41,25 +41,3 @@ void loop() {
displayText.writeText(Answer[i], 3, 0, 200, 0, true, true); displayText.writeText(Answer[i], 3, 0, 200, 0, true, true);
delay(20000); delay(20000);
} }
// void writeText(char* text, int size, int posX, int posY, int screenTime, bool center){
// //if true overwrites x
// if (center){
// posX = centerText(text);
// }
// tft.setCursor(posX, posY); // Set the cursor to the top left corner
// tft.setTextSize(size); // Set textsize
// tft.println(text);
// delay(screenTime);
// }
// int centerText(char* text) {
// int16_t x1, y1;
// uint16_t w, h;
// tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); // Calculate the width of the text
// int x = (tft.width() - w) / 2; // Calculate the x-coordinate for the centered text
// return x;
// }

View File

@@ -6,7 +6,7 @@ DisplayText::DisplayText(Adafruit_ST7796S_kbv& tftDisplay) : tft(tftDisplay) {
tft.setCursor(0,0); tft.setCursor(0,0);
tft.fillScreen(ST7796S_BLACK); 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) { void DisplayText::writeText(char* text, int size, int posX, int posY, int screenTime, bool center, bool bottom) {
if (center) { if (center) {
posX = centerText(text); posX = centerText(text);
@@ -19,7 +19,7 @@ void DisplayText::writeText(char* text, int size, int posX, int posY, int screen
printWordsFull(text); printWordsFull(text);
delay(screenTime); delay(screenTime);
} }
//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;
uint16_t w, h; uint16_t w, h;
@@ -27,7 +27,7 @@ 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) { int DisplayText::bottomText(char* text) {
int16_t x1, y1; int16_t x1, y1;
uint16_t w, h; uint16_t w, h;
@@ -36,13 +36,14 @@ int DisplayText::bottomText(char* text) {
return y; return y;
} }
//attempt to write the text out in full (wip)
void DisplayText::printWordsFull(char* text) { void DisplayText::printWordsFull(char* text) {
char* textArray[100]; char* textArray[100];
int i = 0; int i = 0;
//copy the text into a variable so the string can be re-used later
char* textCopy = strdup(text); char* textCopy = strdup(text);
//split the text into words and put them into a array
char* word = strtok(text, " "); char* word = strtok(text, " ");
while (word != NULL) { while (word != NULL) {
textArray[i] = word; textArray[i] = word;
@@ -53,20 +54,22 @@ void DisplayText::printWordsFull(char* text) {
int lineWidth = 0; int lineWidth = 0;
int maxLineWidth = 320; // replace with the width of your TFT display int maxLineWidth = 320; // replace with the width of your TFT display
int charWidth = 11; // replace with the width of your characters int charWidth = 11; // replace with the width of your characters
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
//count the amount of letters in the word
int wordLength = strlen(textArray[j]); int wordLength = strlen(textArray[j]);
// If the word won't fit on the current line, move to the next line // If the word won't fit on the current line, move to the next line
if (lineWidth + wordLength * charWidth > maxLineWidth) { if (lineWidth + wordLength * charWidth > maxLineWidth) {
tft.println(); tft.println();
//reset lineWidth
lineWidth = 0; lineWidth = 0;
} }
//print the text
tft.print(textArray[j]); tft.print(textArray[j]);
tft.print(" "); tft.print(" ");
lineWidth += (wordLength + 1) * charWidth; // +1 for the space lineWidth += (wordLength + 1) * charWidth; // +1 for the space
} }
//removes textCopy out of the memory
free(textCopy); free(textCopy);
} }

View File

@@ -3,8 +3,6 @@
#include "Adafruit_ST7796S_kbv.h" #include "Adafruit_ST7796S_kbv.h"
#include "displayText.h" #include "displayText.h"
#define TFT_CS 14 #define TFT_CS 14
#define TFT_DC 13 #define TFT_DC 13
#define TFT_RST 12 #define TFT_RST 12