DisplayTFTeSPI

Beschreibung

Die Klasse DisplayTFTeSPI erbt von der Klasse Display und nutzt für ihre Ausgabe die Bibliothek TFTeSPI.

Funktionen

uint16_t

color565 (const Color& color)

uint16_t

color565 (uint8_t r, uint8_t g, uint8_t b)

DisplayTFTeSPI ()

DisplayTFTeSPI (std::array<uint16_t, 5> calData)

DisplayTFTeSPI (const DisplayTFTeSPI& other)

DisplayTFTeSPI& operator= (const DisplayTFTeSPI& other)

void

init () override

void

rect (const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color, const Color& infill_color) override

void

rect_center (const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color, const Color& infill_color) override

void

rect (const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color) override

void

rect_center (const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color) override

void

circle (const uint16_t pos_x, const uint16_t pos_y, const uint16_t d, const uint8_t border_size, const Color& border_color, const Color& infill_color) override

void

circle (const uint16_t pos_x, const uint16_t pos_y, const uint16_t d, const uint8_t border_size, const Color& border_color) override

void

triangle (const uint16_t pos_x0, const uint16_t pos_y0, const uint16_t pos_x1, const uint16_t pos_y1, const uint16_t pos_x2, const uint16_t pos_y2, const uint8_t border_size, const Color& border_color, const Color& infill_color) override

void

triangle (const uint16_t pos_x0, const uint16_t pos_y0, const uint16_t pos_x1, const uint16_t pos_y1, const uint16_t pos_x2, const uint16_t pos_y2, const uint8_t border_size, const Color& border_color) override

bool

containsInvalidCharacters (const char* input)

void

text (const uint16_t pos_x, const uint16_t pos_y, const uint16_t width, const uint16_t height, const char* text, const Color& text_color) override

void

text_center (const uint16_t pos_x, const uint16_t pos_y, const uint8_t size, const char* text, const Color& text_color) override

void

line (const uint16_t x1, const uint16_t y1, const uint16_t x2, const uint16_t y2, const Color& color) override

void

point (const uint16_t x1, const uint16_t y1, const Color& color) override

void

fillScreen (const Color& color) override

void

drawBitmap (const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint8_t* bitmap, const Color& fgcolor) override

void

drawBitmap (const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint8_t* bitmap, const Color& fgcolor, const Color& bgcolor) override

int16_t

getHeight () override

int16_t

getWidth () override

uint8_t

getRotation () override

uint8_t

setRotation () override

bool

getTouch (uint16_t* x, uint16_t* y) override

TFT_eSPI&

getTFTObjekt ()

Variablen und Konstanten

TFT_eSPI

tft

unit8_t

rotation = 0

std::array<uint16_t, 5>

calData

Funktionen Beschreibung

uint16_t colorTo565(const Color& color)

1uint16_t DisplayTFTeSPI::colorTo565(const Color& color) {
2    return colorTo565(color.getRed(), color.getGreen(), color.getBlue());
3}

Diese Funktion ruft den colorTo565 Konstruktor mit den RGB-Werten (durch getRed, getGreen und getBlue) des Color Objekts auf, um es vom 24 Bit RGB888-Format in das 16 Bit RGB565-Format umzuwandeln.

uint16_t colorTo565(uint8_t r, uint8_t g, uint8_t b)

1uint16_t DisplayTFTeSPI::colorTo565(const uint8_t r, const uint8_t g, const uint8_t b) {
2    return tft.color565(r, g, b);  // Rückgabe des Farbwerts im 565-Format
3}

Diese Funktion ruft die color565 Funktion des tft Objekts auf, um eine Farbe im RGB565-Format zu erzeugen.

DisplayTFTeSPI ()

1DisplayTFTeSPI::DisplayTFTeSPI():
2    tft(),
3    calData({0,0,0,0,0})
4{}

Der Konstruktor initiallisiert das „tft“ Objekt und das „calData“ Array.

DisplayTFTeSPI(std::array<uint16_t, 5> calData)

KEINE DEFINITION

DisplayTFTeSPI (const DisplayTFTeSPI& other)

KEINE DEFINITION

DisplayTFTeSPI& operator= (const DisplayTFTeSPI& other)

1DisplayTFTeSPI& DisplayTFTeSPI::operator= (const DisplayTFTeSPI& other){
2    //tft = other.tft;
3    return *this;
4}

Gibt die aktuelle Instanz zurück.

void init()

 1void DisplayTFTeSPI::init() {
 2    tft.begin();
 3    tft.setRotation(1);
 4    //tft.setTouch(calData.data());
 5    //tft.setSwapBytes(true);
 6    // if (!LittleFS.begin()) {
 7    //     Serial.println("Fehler beim Initialisieren von LittleFS");
 8    //     return;
 9    // }
10}

Setzt das „tft“ Objekt erst zurück und initialisiert es dann. (siehe begin(uint8_t tc)) Danach wird mit setRotation die Bildschirmausrichtung angepasst.

virtual void rect(const uint16_t pos_x, const uint16_t pos_y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color, const Color& infill_color)

1void DisplayTFTeSPI::rect(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color, const Color& infill_color) {
2    tft.fillRoundRect(x, y, width, height, border_radius, colorTo565(border_color));
3    tft.fillRoundRect(x + border_size, y + border_size, width - (border_size*2), height - (border_size*2), border_radius, colorTo565(infill_color));
4}
Diese Funktion zeichnet ein gefülltes Rechteck. Als Parameter nimmt sie die X- und Y-Koordinate (pos_x und pos_y), bei welchen das Rechteck gezeichnet werden soll und die Breite (width) und Höhe (height) des Rechtecks. Die Dicke der Umrandung (border_size) und um wie viel Grad die Ecken gekrümmt sein sollen (border_radius), sowie die Farbe der Umrandung (border_color) und die Farbe der Rechteckfläche (infill_color).

Für das tft Objekt wird zuerst die fillRoundRect Funktion aufgerufen, die ein Rechteck mit abgerundeten Ecken in den Farben der Umrandung zeichnet. Dieses Rechteck wird anschließend von einem weiteren, kleineren Rechteck in der Farbe der Rechteckfläche überzeichnet, wodurch das gewünschte Rechteck samt Umrandung entsteht.

virtual void rect_center(const uint16_t pos_x, const uint16_t pos_y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color, const Color& infill_color)

1void DisplayTFTeSPI::rect_center(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color, const Color& infill_color) {
2    rect (x - width/2, y - height/2, width, height, border_size, border_radius, border_color, infill_color);
3}

Ruft die rect Funktion auf und übergibt anstelle der übergebenen X- und Y-Koordinaten (pos_x und pos_y) den Mittelpunkt des Screens, um das Rechteck zentral auf dem Bildschirm zu zeichnen. Das zu zeichnende Rechteck hat sowohl eine gefärbte Umrandung als auch einen gefärbten Flächeninhalt.

virtual void rect(const uint16_t pos_x, const uint16_t pos_y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color)

1void DisplayTFTeSPI::rect(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color) {
2    for (uint8_t i = 0; i < border_size; i++) {
3        tft.drawRoundRect(x - i, y - i, width + 2 * i, height + 2 * i, border_radius + i, colorTo565(border_color));
4    }
5}
Diese Funktion zeichnet ein leeres Rechteck. Als Parameter nimmt sie die X- und Y-Koordinate (pos_x und pos_y), bei welchen das Rechteck gezeichnet werden soll und die Breite (width) und Höhe (height) des Rechtecks. Die Dicke der Umrandung (border_size) und um wie viel Grad die Ecken gekrümmt sein sollen (border_radius), sowie die Farbe der Umrandung (border_color).

Innerhalb der for Schleife wird für das tft Objekt die drawRoundRect Funktion aufgerufen, die die Umrandung eines Rechtecks ohne Flächeninhalt zeichnet. Die for-Schleife lässt diese Umrandung so häufig überzeichnen, bis sie so dick ist, wie gewünscht.

virtual void rect_center(const uint16_t pos_x, const uint16_t pos_y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color)

1void DisplayTFTeSPI::rect_center(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t border_size, const uint8_t border_radius, const Color& border_color) {
2    rect (x - width/2, y - height/2, width, height, border_size, border_radius, border_color);
3}

Ruft die rect Funktion auf und übergibt anstelle der übergebenen X- und Y-Koordinaten (pos_x und pos_y) den Mittelpunkt des Screens, um das Rechteck zentral auf dem Bildschirm zu zeichnen. Das zu zeichnende Rechteck hat nur eine gefärbte Umrandung und keinen gefärbten Flächeninhalt.

virtual void circle(const uint16_t pos_x, const uint16_t pos_y, const uint16_t d, const uint8_t border_size, const Color& border_color, const Color& infill_color)

1void DisplayTFTeSPI::circle(const uint16_t pos_x, const uint16_t pos_y, const uint16_t d, const uint8_t border_size, const Color& border_color, const Color& infill_color) {
2    // LOGGER_PATTERN("border=_, d=_", border_size, d)
3    uint16_t r = 0;
4    if (d >= 2) r = d/2;
5
6    if (border_size >= 2) tft.fillCircle(pos_x, pos_y, r, colorTo565(border_color));
7    tft.fillCircle(pos_x, pos_y, r - (border_size/2), colorTo565(infill_color));
8}

Diese Funktion zeichnet einen gefüllten Kreis. Als Parameter nimmt sie die X- und Y-Koordinate (pos_x und pos_y), bei welchen der Kreis gezeichnet werden soll und den Durchmesser (d) des Kreises, die Dicke der Umrandung (border_size), sowie die Farbe der Umrandung (border_color) und die Farbe der Kreisfläche (infill_color).

    uint16_t r = 0;
    if (d >= 2) r = d/2;

Zuerst wird die Variable r erstellt und gleich 0 gesetzt, diese repräsentiert den Radius des Kreises. Falls der Durchmesser d>=2, wird der Radius auf die Hälfte des Durchmessers gesetzt. Andernfalls wird mit dem Radius r = 0 fortgefahren.

    if (border_size >= 2) tft.fillCircle(pos_x, pos_y, r, colorTo565(border_color));
    tft.fillCircle(pos_x, pos_y, r - (border_size/2), colorTo565(infill_color));

Falls border_size >=2 wird für das „tft“ Objekt die fillCircle Funktion aufgerufen, die einen Kreis in der Farbe der Umrandung zeichnet. Dieser Kreis wird anschließend von einem weiteren, kleineren Kreis in der Farbe der der Kreisfläche überzeichnet, wodurch der gewünschte Kreis samt Umrandung entsteht. Ist border_size <2, wäre die Umrandung zu dünn, um sie zu darstellen zu können.

virtual void circle(const uint16_t pos_x, const uint16_t pos_y, const uint16_t d, const uint8_t border_size, const Color& border_color)

1void DisplayTFTeSPI::circle(const uint16_t x, const uint16_t y, const uint16_t d, const uint8_t border_size, const Color& border_color) {
2    tft.drawCircle(x, y, d/2, colorTo565(border_color));
3}
Diese Funktion zeichnet einen leeren Kreis. Als Parameter nimmt sie die X- und Y-Koordinate (pos_x und pos_y), bei welchen der Kreis gezeichnet werden soll und den Durchmesser (d) des Kreises. Die Dicke der Umrandung (border_size) und sowie die Farbe der Umrandung (border_color).

Für das „tft“ Objekt wird die drawCircle Funktion aufgerufen, die die Umrisse eines Kreises ohne Flächeninhalte zeichnet. Dabei wird als 3. Argument der Radius in der Form d/2 übergeben.

void triangle(const uint16_t pos_x0, const uint16_t pos_y0, const uint16_t pos_x1, const uint16_t pos_y1, const uint16_t pos_x2, const uint16_t pos_y2, const uint8_t border_size, const Color& border_color, const Color& infill_color)

1void DisplayTFTeSPI::triangle(const uint16_t pos_x0, const uint16_t pos_y0, const uint16_t pos_x1, const uint16_t pos_y1, const uint16_t pos_x2, const uint16_t pos_y2, const uint8_t border_size, const Color& border_color, const Color& infill_color) {
2    tft.fillTriangle(pos_x0, pos_y0, pos_x1, pos_y1, pos_x2, pos_y2, colorTo565(infill_color));
3    // triangle(pos_x0, pos_y0, pos_x1, pos_y1, pos_x2, pos_y2, border_size, border_color);
4}
Diese Funktion zeichnet ein gefülltes Dreieck. Als Parameter nimmt sie die jeweils drei X- und Y-Koordinaten für drei verschiedene Punkte des Dreiecks(pos_x0 und pos_y0, pos_x1 und pos_y1, pos_x2 und pos_y2), bei welchen das Dreieck gezeichnet werden soll, die Dicke der Umrandung (border_size), sowie die Farbe der Umrandung (border_color) und die Farbe der Dreiecksfläche (infill_color).

Für das tft Objekt wird die fillTriangle Funktion aufgerufen, die das gewünschte Dreieck zeichnet.

void triangle(const uint16_t pos_x0, const uint16_t pos_y0, const uint16_t pos_x1, const uint16_t pos_y1, const uint16_t pos_x2, const uint16_t pos_y2, const uint8_t border_size, const Color& border_color)

1void DisplayTFTeSPI::triangle(const uint16_t pos_x0, const uint16_t pos_y0, const uint16_t pos_x1, const uint16_t pos_y1, const uint16_t pos_x2, const uint16_t pos_y2, const uint8_t border_size, const Color& border_color) {
2    tft.drawLine(pos_x0, pos_y0, pos_x1, pos_y1, colorTo565(border_color));
3    tft.drawLine(pos_x1, pos_y1, pos_x2, pos_y2, colorTo565(border_color));
4    tft.drawLine(pos_x2, pos_y2, pos_x0, pos_y0, colorTo565(border_color));
5}
Diese Funktion zeichnet ein leeres Dreieck. Als Parameter nimmt sie die jeweils drei X- und Y-Koordinaten für drei verschiedene Punkte des Dreiecks(pos_x0 und pos_y0, pos_x1 und pos_y1, pos_x2 und pos_y2), bei welchen das Dreieck gezeichnet werden soll, die Dicke der Umrandung (border_size), sowie die Farbe der Umrandung (border_color).

Dafür werden insgesamt drei Linien gezeichnet. Für das tft Objekt wird die drawLine Funktion aufgerufen und dann erst eine Linie von pos_x0 und pos_y0 zu pos_x1 und pos_y1 gezogen, dann von pos_x1 und pos_y1 zu pos_x2 und pos_y2 und zum Schluss von pos_x2 und pos_y2 zurück zu pos_x0 und pos_y0.

bool containsInvalidCharacters(const char* input)

 1bool DisplayTFTeSPI::containsInvalidCharacters(const char* input) {
 2    while (*input) {
 3        char currentChar = *input;
 4        // Überprüfe, ob das aktuelle Zeichen kein gültiger Buchstabe ist
 5        if (!isdigit(currentChar) && currentChar != '.' && currentChar != '-' && currentChar != ':') {
 6                return true;
 7        }
 8        input++;
 9    }
10    return false;
11}

Mit dieser Funktion wird überprüft, ob in der Eingabe ungültige Zeichen/ Symbole enthalten sind. Zu diesen gehören ., - und :. Dafür wird Zeichen für Zeichen überprüft, ob das aktuelle Zeichen keines dieser Symbole ist.

virtual void text(const uint16_t pos_x, const uint16_t pos_y, const uint16_t width, const uint16_t height, const char* text, const Color& text_color)

 1void DisplayTFTeSPI::text(const uint16_t posX, const uint16_t posY, const uint16_t width, const uint16_t height, const char* text, const Color& text_color) {
 2    tft.setTextColor(colorTo565(text_color));
 3    
 4    const uint8_t num = 22;
 5    const uint16_t fontSize[num] = {16, 16, 16, 26, 32, 48, 52, 64, 75, 78, 80, 96, 104, 112, 128, 130, 144, 146, 150, 160, 156, 182};
 6    const uint8_t fonts[num]     = {2 , 2 , 2 , 4 , 2 , 6 , 4 , 2 , 8 , 4 , 2 , 6 , 4  , 2  , 2  , 4  , 6  , 4  , 8  , 2  , 4  , 4  };
 7    const uint8_t size[num]      = {1 , 1 , 1 , 1 , 2 , 1 , 2 , 4 , 1 , 3 , 5 , 2 , 4  , 7  , 8  , 5  , 3  , 6  , 2  , 10 , 6  , 7  };
 8
 9    tft.drawRect(posX - width/2, posY - height/2 - 2, width, height, TFT_WHITE);
10    tft.setTextDatum(4);
11
12    if (height < 16) {
13        tft.setTextFont(1);
14        tft.setTextSize(1);
15
16        tft.drawString(text, posX, posY);
17        return;
18    }
19
20    bool isDigit = !containsInvalidCharacters(text);
21    for(uint8_t i = 2; i < num; i++) {
22        tft.setTextSize(size[i]);
23        if ((fontSize[i] > height || tft.textWidth(text, fonts[i]) > width)) {
24            // LOGGER(i);
25            for (; i > 0 && !(isDigit || fonts[i] <= 3); i--);
26            tft.setTextFont(fonts[i]);
27            tft.setTextSize(size[i]);
28            // tft.fontHeight(fonts[i]);
29
30            // LOGGER_PATTERN("_: schreibe text '_' (_) in Schriftart _, höhe _, breite _ und multiplikator _ in w:_, h:_", i, text, isDigit? "nur Zahlen" : "mit Buchstaben", fonts[i], fontSize[i], tft.textWidth(text, fonts[i]), size[i], width, height)
31
32            // Zeichne den Text auf dem Display
33            tft.drawString(text, posX, posY);
34            return;
35        }
36    }
37    tft.setTextFont(1);
38    tft.setTextSize(1);
39    tft.setTextDatum(4);
40
41    // LOGGER_PATTERN("zu klein: schreibe text '_' (_) in Schriftart _, höhe _, breite _ und multiplikator _ in w:_, h:_", text, isDigit? "nur Zahlen" : "mit Buchstaben", fonts[num-1], fontSize[num-1], tft.textWidth(text, fonts[num-1]), size[num-1], width, height)
42
43    // Zeichne den Text auf dem Display
44    tft.drawString(text, posX, posY);
45}

Diese Funktion zeichnet einen Text, der automatisch formatiert wird. Als Parameter nimmt sie die X- und Y-Koordinate (pos_x und pos_y), bei welchen der Text gezeichnet werden soll, die Breite (width) und Höhe (height) der Fläche, auf welchen der Text gezeichnet werden soll, den Text selbst (text) und die Farbe des Texts (text_color)

    tft.setTextColor(colorTo565(text_color));

Für das tft Objekt wird die setTextColor Funktion aufgerufen, die die Farbe des Textes festlegt, indem text_color mit Hilfe von colorTo565 in das richtige farb-Format umgewandelt wird.

    const uint8_t num = 22;
    const uint16_t fontSize[num] = {16, 16, 16, 26, 32, 48, 52, 64, 75, 78, 80, 96, 104, 112, 128, 130, 144, 146, 150, 160, 156, 182};
    const uint8_t fonts[num]     = {2 , 2 , 2 , 4 , 2 , 6 , 4 , 2 , 8 , 4 , 2 , 6 , 4  , 2  , 2  , 4  , 6  , 4  , 8  , 2  , 4  , 4  };
    const uint8_t size[num]      = {1 , 1 , 1 , 1 , 2 , 1 , 2 , 4 , 1 , 3 , 5 , 2 , 4  , 7  , 8  , 5  , 3  , 6  , 2  , 10 , 6  , 7  };

Nun werden drei Arrays festgelegt, in denen num-verschiedene Kombinationen von Multipliyern (size), Schriftarten (fonts) und die tatsächliche Schriftgröße, die sich aus den Schriftarten und Multiplyern ergeben (fonzSize). Das fontSize Array wird später relevant, um festzustellen, wie groß der angezeigte text auf der festgelegten Fläche maximal sein kann. Die anderen beiden werden genutzt, um diesen Text dann zu generieren.

    tft.drawRect(posX - width/2, posY - height/2 - 2, width, height, TFT_WHITE);
    tft.setTextDatum(4);

Wir zeichnen mit drawRect ein weißes Rechteck an der richtigen Position, welches als Textbox für den Text dient. Mit setDatum wird der Bezugspunkt der Textposition festgelegt, also wo genau auf dem Display der Text angezeigt werden soll.

    if (height < 16) {
        tft.setTextFont(1);
        tft.setTextSize(1);

        tft.drawString(text, posX, posY);
        return;
    }

Sollte die Höhe der Textbox kleiner sein als 16 (welches die minimalste fonzSize ist), wird ein Text in der kleinstmöglichen Art und Weise mittels setTextFont(1) und setTextSize(1) erstellt und mit drawString an der gewünschten Stelle gezeichnet.

    bool isDigit = !containsInvalidCharacters(text);

Andernfalls wird zuerst mit containsInvalidCharacters getestet, ob der Text ungültige Zeichen enthält (., - oder :). Das Ergebnis wird negiert und in in isDigit gespeichert. isDigit wird in einem späteren Codeabschnitt relevant.

    for(uint8_t i = 2; i < num; i++) {

Es folgt eine for Schleife, welche so lange läuft, bis entweder mehr Durchläufe als num groß ist geschehen oder bis der größtmöglich zeichenbare Text ermittelt wurde.

        if ((fontSize[i] > height || tft.textWidth(text, fonts[i]) > width)) {
            // LOGGER(i);
            for (; i > 0 && !(isDigit || fonts[i] <= 3); i--);
            tft.setTextFont(fonts[i]);
            tft.setTextSize(size[i]);
            // tft.fontHeight(fonts[i]);

            // LOGGER_PATTERN("_: schreibe text '_' (_) in Schriftart _, höhe _, breite _ und multiplikator _ in w:_, h:_", i, text, isDigit? "nur Zahlen" : "mit Buchstaben", fonts[i], fontSize[i], tft.textWidth(text, fonts[i]), size[i], width, height)

            // Zeichne den Text auf dem Display
            tft.drawString(text, posX, posY);
            return;
        }
    }

Sollte die fontSize beim aktuellen i größer sein als die Höhe der Textbox oder sollte der Text breiter sein als die breite des Textbox, so wird der Text in der nächstgrößten möglichen Art und Weise erstellt (mit setTextFont und setTextSize) und mit drawString an der gewünschten Stelle gezeichnet.

    tft.setTextFont(1);
    tft.setTextSize(1);
    tft.setTextDatum(4);

    // LOGGER_PATTERN("zu klein: schreibe text '_' (_) in Schriftart _, höhe _, breite _ und multiplikator _ in w:_, h:_", text, isDigit? "nur Zahlen" : "mit Buchstaben", fonts[num-1], fontSize[num-1], tft.textWidth(text, fonts[num-1]), size[num-1], width, height)

    // Zeichne den Text auf dem Display
    tft.drawString(text, posX, posY);

Wenn die Funktion jetzt noch weiterlaufen sollte, wird (ebenfalls wie oben) der Text in der kleinstmöglichen Art und Weise gezeichnet.

virtual void text_center(const uint16_t pos_x, const uint16_t pos_y, const uint8_t size, const char* text, const Color& text_color)

1void DisplayTFTeSPI::text_center(const uint16_t pos_x, const uint16_t pos_y, const uint8_t size, const char* text, const Color& text_color) {
2    tft.setTextSize(size); // Textgröße festlegen
3    tft.setTextDatum(MC_DATUM); // Mittelpunkt als Bezugspunkt für den Text setzen
4    //tft.setCursor(pos_x, pos_y); // Textposition festlegen
5    tft.setTextColor(colorTo565(text_color)); // Textfarbe festlegen (hier: Weiß)
6    tft.setTextFont(1); // Textschriftart festlegen (optional, falls gewünscht)
7
8    tft.drawString(text, pos_x, pos_y); // Text auf dem Display zeichnen
9}

Diese Funktion zeichnet einen zentrierten Text mit einer beliebigen Größe zentriert auf dem Display. Als Parameter nimmt sie die X- und Y-Koordinate (pos_x und pos_y), bei welchen der Text gezeichnet werden soll, die Textgröße (size), den Text selbst (text) und die Farbe des Texts (text_color).

    tft.setTextSize(size); // Textgröße festlegen
    tft.setTextDatum(MC_DATUM); // Mittelpunkt als Bezugspunkt für den Text setzen

Anschließend wird für das tft Objekt die setTextSize Funktion aufgerufen, welche die Größe des Textes festlegt. Mit setTextDatum wird der Bezugspunkt der Textposition festgelegt, also wo genau auf dem Display der Text angezeigt werden soll. MCDatum steht für „Middle centre“, sprich der Text wird im exakten Mittelpunkt des Displays angezeigt.

    tft.setTextColor(colorTo565(text_color)); // Textfarbe festlegen (hier: Weiß)
    tft.setTextFont(1); // Textschriftart festlegen (optional, falls gewünscht)

Mit setTextColor wird der Text die übergebene Farbe annehmen und mit setTextFont wird die Schriftart festgelegt. 1 ist hier der Standardwert.

    tft.drawString(text, pos_x, pos_y); // Text auf dem Display zeichnen

Zum Schluss wird mit drawString der gewünschte text bei den gewünschten X- und Y-Koordinaten gezeichnet.

virtual void line(const uint16_t x1, const uint16_t y1, const uint16_t x2, const uint16_t y2, const Color& color)

1void DisplayTFTeSPI::line(const uint16_t x1, const uint16_t y1, const uint16_t x2, const uint16_t y2, const Color& color) {
2    tft.drawLine(x1, y1, x2, y2, colorTo565(color));
3}
Diese Funktion zeichnet eine Linie. Als Parameter nimmt sie je X- und Y-Koordinaten für zwei verschiedene Punkte des Dreiecks(pos_x0 und pos_y0, pos_x1 und pos_y1), bei welchen die Linie gezeichnet werden soll, sowie die Farbe der Linie (color).

Für das tft Objekt wird die drawLine Funktion aufgerufen und dann eine Linie von pos_x0 und pos_y0 zu pos_x1 und pos_y1 gezogen.

virtual void point(const uint16_t x1, const uint16_t y1, const Color& color)

1void DisplayTFTeSPI::point(const uint16_t x1, const uint16_t y1, const Color& color) {
2    tft.drawPixel(x1, y1, colorTo565(color));
3}
Diese Funktion zeichnet einen Punkt. Als Parameter nimmt sie eine X- und Y-Koordinaten (pos_x0 und pos_y0), bei welchen der Punkt gezeichnet werden soll, sowie die Farbe des Punktes (color).

Für das tft Objekt wird die drawPixel Funktion aufgerufen.

virtual void fillScreen (const Color& color)

1void DisplayTFTeSPI::fillScreen(const Color& color) {
2    tft.fillScreen(colorTo565(color));
3}
Diese Funktion färbt den gesamten Bildschirm in eine übergebene Farbe.
Für das tft Objekt wird die fillScreen Funktion aufgerufen.

int16_t getHeight()

1uint16_t DisplayTFTeSPI::getHeight() {
2    return tft.height();
3}
Diese Funktion gibt die Höhe des Displays zurück.
Für das tft Objekt wird die height Funktion aufgerufen.

int16_t getWidth()

1uint16_t DisplayTFTeSPI::getWidth() {
2    return tft.width();
3}
Diese Funktion gibt die Breite des Displays zurück.
Für das tft Objekt wird die width Funktion aufgerufen.

uint8_t getRotation()

1uint8_t DisplayTFTeSPI::getRotation() {
2    return tft.getRotation();
3}

Diese Funktion gibt die rotation des Displays zurück.

uint8_t setRotation()

1void DisplayTFTeSPI::setRotation(uint8_t rotation) {
2    tft.setRotation(rotation);
3}

Diese Funktion übergibt rotation an setRotation, um das Display wie gewünscht zu drehen.

bool getTouch(uint16_t* x, uint16_t* y)

 1bool DisplayTFTeSPI::getTouch(uint16_t* x, uint16_t* y) {
 2    #ifndef TOUCH_CS
 3
 4    // if no touchscreen has been defined in the UserSetup.h of the TFT_eSPI library
 5    return false;
 6
 7    #else
 8
 9    if (!x || !y) LOGGER_ERROR("x oder y ist ein Null Pointer")
10    bool isTouched = tft.getTouch(x, y) != 0;
11    
12    // calculate Coordinates depending on the rotation
13    if (!isTouched);
14    else if (getRotation() == 0) {
15        uint16_t tmpY = map(*x, 0, getWidth(), 0, getHeight());
16        uint16_t tmpX = map(*y, 0, getHeight(), 0, getWidth());
17        *y = tmpY;
18        *x = tmpX;
19    } else if (getRotation() == 1) {
20        *y = getHeight() - *y;
21    } else if (getRotation() == 2) {
22        uint16_t tmpY = map(*x, 0, getWidth(), 0, getHeight());
23        uint16_t tmpX = map(*y, 0, getHeight(), 0, getWidth());
24        *y = getHeight() - tmpY;
25        *x = getWidth() - tmpX;
26    } else if (getRotation() == 3) {
27        *x = getWidth() - *x;
28    }
29
30    return isTouched;
31
32    #endif
33}

Diese Funktion berechnet die Koordinaten, auf welchem das Display berührt, je nachdem, wie der Bildschirm durch rotation ausgerichtet ist.

    if (!x || !y) LOGGER_ERROR("x oder y ist ein Null Pointer")

Wenn x oder y der Null Pointer sind, existiert kein Berührungspunkt auf dem Bildschirm.

    bool isTouched = tft.getTouch(x, y) != 0;

In der Variable isTouched wird gespeichert, ob der Bildschirm berührt wird oder nicht.

    if (!isTouched);

Sollte der Bildschirm nicht berührt werden, ist es auch nicht notwendig, Koordinaten zu bestimmen.

    else if (getRotation() == 0) {
        uint16_t tmpY = map(*x, 0, getWidth(), 0, getHeight());
        uint16_t tmpX = map(*y, 0, getHeight(), 0, getWidth());
        *y = tmpY;
        *x = tmpX;

Andernfalls werden für jede der vier möglichen Bildschirmausrichtungen die Koordinaten bestimmt. Zuerst für eine rotation von 0, also der Standardausrichtung. Dafür werden temporäre Variablen angelegt, die das Ergebnis der map Funktion speichert. Diese Funktion mapped für tmpX die mit getHeight ermittelte Höhe auf die mit getWidth ermittelte Breite und für tmpY die Breite auf die Höhe. so werden Seitenverhältnisse beibehalten. Nun müssen die Pointer für x und y nur noch auf die Speicheradressen der temporären Variablen zeigen.

    } else if (getRotation() == 1) {
        *y = getHeight() - *y;

Da der Bildschirm bei rotation = 1 auf dem Kopf steht, muss nur der Kehrwert von y bestimmt werden.

    } else if (getRotation() == 2) {
        uint16_t tmpY = map(*x, 0, getWidth(), 0, getHeight());
        uint16_t tmpX = map(*y, 0, getHeight(), 0, getWidth());
        *y = getHeight() - tmpY;
        *x = getWidth() - tmpX;

Anders als beim vorletzten Codeabschnitt, wird hier bei der der Zuweisung von x und y nach dem Mappen der Kehrwert der temporären Variablen als neuer Wert gespeichert.

    } else if (getRotation() == 3) {
        *x = getWidth() - *x;

Hier wird der Kehrwert von x bestimmt, da der Bildschirm um 270° gedreht ist.

TFT_eSPI& getTFTObjekt();

1TFT_eSPI& DisplayTFTeSPI::getTFTObjekt() {
2    return tft;
3}

Diese Funktion gibt das „tft“ Objekt zurück.

void drawBitmap(const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint8_t* bitmap, const Color& fgcolor)

1void DisplayTFTeSPI::drawBitmap(const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint8_t* bitmap, const Color& fgcolor){
2    tft.drawBitmap(x, y, bitmap, w, h, colorTo565(fgcolor));
Diese Funktion zeichnet ein Bild. Als Parameter nimmt sie die X- und Y-Koordinate (x und y), bei welchen das Dreieckk gezeichnet werden soll. Die Breite (w) und Höhe (h) des Bildes, das Bild selbst (bitmap) und eine Vordergrundfarbe (fgcolor).

Für das „tft“ Objekt wird die drawBitmap Funktion (siehe Zeile 2760) aufgerufen.

void drawBitmap(const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint8_t* bitmap, const Color& fgcolor, const Color& bgcolor)

1void DisplayTFTeSPI::drawBitmap(const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint8_t* bitmap, const Color& fgcolor, const Color& bgcolor){
2    tft.drawBitmap(x, y, bitmap, w, h, colorTo565(fgcolor), colorTo565(bgcolor));
3}
Diese Funktion zeichnet ein Bild. Als Parameter nimmt sie die X- und Y-Koordinate (x und y), bei welchen das Dreieckk gezeichnet werden soll. Die Breite (w) und Höhe (h) des Bildes, das Bild selbst (bitmap), eine Vordergrundfarbe (fgcolor) und eine Hintergrundfarbe (bgcolor).

Für das „tft“ Objekt wird die drawBitmap Funktion (siehe Zeile 2784) aufgerufen.

Variablen und Konstanten Beschreibung

TFT_eSPI tft

Ein Objekt der Klasse TFT_eSPI. TFT_eSPI ist eine Library mit vielen verschiedenen Grafiken speziell für Arduino IDE und wird in diesem Projekt als Grundlage für die meisten geometrischen Formen der verschiedenen „Elemente“ genutzt.

uint8_t rotation = 0

Gibt an, wie das Bild auf dem phyischen Display angezeigt bzw. ausgerichtet wird. Bei rotation = 0 wird das angezeigte Bild in der Standardausrichtung angezeigt (also bei einem waagerechten Bildschrim waagerecht und bei einem senkrechten Bildschirm senkrecht). Bei rotation = 1 wird das Bild um 90° gedreht, bei rotation = 2 um 180° und bei rotation = 3 um 270°.

std::array<uint16_t, 5> calData

Dieses Array speichert Kalibrierungsdaten.