Added class

This commit is contained in:
sam
2024-03-06 15:46:39 +01:00
parent 284071b4ee
commit 5a8e13c0d6

View File

@@ -2,6 +2,7 @@
#include "Adafruit_GFX.h" #include "Adafruit_GFX.h"
#include "Adafruit_ST7796S_kbv.h" #include "Adafruit_ST7796S_kbv.h"
#include "headerFile.h" #include "headerFile.h"
#include "displayText.h"
int i = 0; int i = 0;
char* Question[] = { char* Question[] = {
@@ -22,7 +23,7 @@ char* Answer[] = {
//simplify the name of the library //simplify the name of the library
Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST, MISO); Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST, MISO);
DisplayText displayText(tft);
void setup() { void setup() {
@@ -40,32 +41,29 @@ void loop() {
tft.fillScreen(ST7796S_BLACK); // Fill the screen with black color tft.fillScreen(ST7796S_BLACK); // Fill the screen with black color
// writeText(Question[0], 2, 50, 0, 0); // writeText(Question[0], 2, 50, 0, 0);
// writeText(Answer[0], 2, 50, 300, 4000); // writeText(Answer[0], 2, 50, 300, 4000);
writeText(Question[i], 3, 0, 0, 0, true); displayText.writeText(Question[i], 3, 0, 0, 0, true);
writeText(Answer[i], 3, 0, 200, 0, true); displayText.writeText(Answer[i], 3, 0, 200, 0, true);
delay(10000); delay(10000);
} }
void writeText(char* text, int size, int posX, int posY, int screenTime, bool center){ // void writeText(char* text, int size, int posX, int posY, int screenTime, bool center){
//if true overwrites x // //if true overwrites x
if (center){ // if (center){
posX = centerText(text); // posX = centerText(text);
} // }
tft.setCursor(posX, posY); // Set the cursor to the top left corner // tft.setCursor(posX, posY); // Set the cursor to the top left corner
tft.setTextSize(size); // Set textsize // tft.setTextSize(size); // Set textsize
tft.println(text); // tft.println(text);
delay(screenTime); // delay(screenTime);
} // }
int centerText(char* text) { // int centerText(char* text) {
int16_t x1, y1; // int16_t x1, y1;
uint16_t w, h; // uint16_t w, h;
tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); // Calculate the width of the text // 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 // int x = (tft.width() - w) / 2; // Calculate the x-coordinate for the centered text
return x; // return x;
} // }