Symbol

Beschreibung

Symbole sind geometrische Formen, die vom Entwickler eingefügt werden können, um diese auf Elementen anzeigen zu lassen. Die Klasse erbt von Item.

Funktionen

Symbol (const symbol_func_ptr symbol, uint16_t width, uint16_t height, const Color& color = Color(0, 0, 0), uint8_t borderSize = 0)

static Symbol*

create (const std::string& symbol, uint16_t width, uint16_t height, const Color& color = Color(0, 0, 0), uint8_t borderSize = 0, const double scale = 1)

void

draw (uint16_t x, uint16_t y, Display* disp, const Color& color) override

void

draw (uint16_t x, uint16_t y, Display* disp) override

void

drawOn (uint16_t x, uint16_t y, Display* disp) override

void

drawOff (uint16_t x, uint16_t y, Display* disp) = 0

void

setSize (const uint16_t size, const bool override = false) override

void

setResolution (const uint16_t width, const uint16_t height, const bool override = false) override

uint16_t

getWidth () override

uint16_t

getHeight () override

uint8_t

getSize () override

static void

drawError (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawTriangleUp (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawTriangleDown (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawTriangleRight (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawTriangleLeft (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawRect (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawCrosslines (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawToggleSwitch (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawPlus (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

static void

drawMinus (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

symbol_func_ptr

drawSymbol

Variablen und Konstanten

uint16_t

width

uint16_t

height

uint8_t

borderSize

Color

color

Funktionen Beschreibung

void setResolution(const uint16_t width, const uint16_t height, const bool override = false) override

1void Symbol::setResolution(const uint16_t w, const uint16_t h, const bool override) {
2    if (!override) {
3        width = w;
4        height = h;
5    }
6}

Mit dieser Funktion wird die Auflösung von Objekten der Symbol Klasse gesetzt. Dafür wird width auf den Wert von w gesetzt und height auf den Wert von h, solange override true ist.

static Symbol* create(const std::string& symbol, uint16_t width, uint16_t height, const Color& color = Color(0, 0, 0), uint8_t borderSize = 0, const double scale = 1)

 1Symbol* Symbol::create(const std::string& symbol, uint16_t width, uint16_t height, const Color& symbolColor, uint8_t borderSize, const double scale) {
 2
 3    const uint16_t w = width*scale;
 4    const uint16_t h = width*scale;
 5
 6    if (symbol == "TriangleUp")    return new Symbol(drawTriangleUp,    w, h, symbolColor, borderSize);
 7    if (symbol == "TriangleDown")  return new Symbol(drawTriangleDown,  w, h, symbolColor, borderSize);
 8    if (symbol == "TriangleRight") return new Symbol(drawTriangleRight, w, h, symbolColor, borderSize);
 9    if (symbol == "TriangleLeft")  return new Symbol(drawTriangleLeft,  w, h, symbolColor, borderSize);
10    if (symbol == "Rect")          return new Symbol(drawRect,          w, h, symbolColor, borderSize);
11    if (symbol == "ToggleSwitch")  return new Symbol(drawToggleSwitch,  w, h, symbolColor, borderSize);
12    if (symbol == "Plus")          return new Symbol(drawPlus,          w, h, symbolColor, borderSize);
13    if (symbol == "Minus")         return new Symbol(drawMinus,         w, h, symbolColor, borderSize);
14    if (symbol == "Crosslines")      return new Symbol(drawCrosslines,      w, h, symbolColor, borderSize);
15
16    return new Symbol(drawError, w, h, symbolColor, borderSize);
17}

Diese Funktion ist verantwortlich für das Erzeugen von neuen Symbol Objekten. Als Parameter nimmt sie neben der gewünschten Breite (width), Höhe (height) Farbe (symbolColor), Umrandungsdicke (borderSize) und Skalierungsfaktor (scale) einen String symbol. Dieser String soll den Namen der gewünschten Funktion tragen, aus der ein neues Symbol entstehen soll. Hier ist es essenziell, dass der Entwickler den Code anpasst, wenn dieser eigene Symbole einbinden will. Standardmäßig existiert nur die Funktionen drawTriangleUp, drawTriangleDown, drawTriangleRight, drawTriangleLeft, drawRect, drawCrosslines, drawToggleSwitch, drawPlus und drawMinus weswegen die if Anweisung nach dem String TriangleUp, TriangleDown, TriangleRight, TriangleLeft, Rect, Crosslines, ToggleSwitch, Plus bzw. Minus fragt, um ein neues Symbol zu erstellen. Stimmt der angegebene String mit keiner abgefragten if Bedingung überein, wird stattdessen ein drawError Symbol erstellt, um anzuzeigen, dass der Erstellungsprozess eines Symbols fehlgeschlagen ist.

Symbol(const symbol_func_ptr symbol, uint16_t width, uint16_t height, const Color& color = Color(0, 0, 0), uint8_t borderSize = 0)

1Symbol::Symbol(const symbol_func_ptr symbol, uint16_t width, uint16_t height, const Color& color, uint8_t borderSize): 
2    width(width), height(height), borderSize(borderSize), color(color), drawSymbol(symbol) {}

Dieser Konstruktor hat als Parameter eine Pointe auf eine symbol_func_ptr Funktion (siehe symbol_func_ptr drawSymbol), die gewünschte Breite und Höhe des Symbols (width bzw height), die Farbe des Symbols (color), die standardmäßig schwarz ist und eine Umrandungsdicke (bordersize). In der Initialisierungsliste werden die Variablen width, heighth, borderSize, color und drawSymbol mit den entsprechenden Parametern initialisiert.

virtual void draw(uint16_t x, uint16_t y, Display* disp, const Color& color) override

1void Symbol::draw(uint16_t x, uint16_t y, Display* disp, const Color& textColor) {
2    drawSymbol(x, y, width, height, borderSize, disp, textColor);
3}

Diese Funktion hat die Parameter x und y, die die X- bzw. Y-Koordinaten repräsentieren, bei welchen das Symbol angezeigt werden soll, sowie die Farbe textColor, die das Symbol haben soll. Sie ruft drawSymbol auf.

virtual void draw(uint16_t x, uint16_t y, Display* disp) override

1void Symbol::draw(uint16_t x, uint16_t y, Display* disp) {
2    draw(x, y, disp, color);
3}

Ruft die draw Funktion mit color als Argument auf.

virtual void drawOn(uint16_t x, uint16_t y, Display* disp) override

1void Symbol::drawOn(uint16_t x, uint16_t y, Display* disp) {
2    draw(x, y, disp, color);
3}

Ruft die draw Funktion mit color als Argument auf. Diese Funktion wird verwendet, wenn ein Element einen aktivierten und deaktivierten Zustand hat, und ein Symbol für den aktivierten Zustand gezeichnet werden soll.

virtual void drawOff(uint16_t x, uint16_t y, Display* disp) override

1void Symbol::drawOff(uint16_t x, uint16_t y, Display* disp) {
2    draw(x, y, disp, color.getSecondaryColor() + color);
3}

Ruft die draw Funktion mit color.getSecondaryColor() als Argument auf. Diese Funktion wird verwendet, wenn ein Element einen aktivierten und deaktivierten Zustand hat, und ein Symbol für den deaktivierten Zustand gezeichnet werden soll.

virtual void setSize(const uint8_t size, const bool override = false) override

1void Symbol::setSize(const uint8_t _size, const bool override) {
2    if (!override) borderSize = _size;
3}

Mit dieser Funktion lässt sich eine neue borderSize setzen.

virtual uint16_t getWidth() override

1uint16_t Symbol::getWith() {return width;}

Gibt width zurück.

virtual uint16_t getHeight() override

1uint16_t Symbol::getHeight() {return height;}

Gibt height zurück.

virtual uint8_t getSize() override

1uint16_t Symbol::getHeight() {return height;}

Gibt size zurück.

static void drawError(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

1static void drawError(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
2    display->text_center(x, y, 2, "Symbol not found", COLOR_RED);
3}

Diese Funktion zeichnet eine Fehlermeldung auf das Display, wenn „create“ fehlgeschlagen ist. Dafür wird die „text_center Funktion des „Display“ Objektes aufgerufen.

static void drawTriangleUp(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

1static void drawTriangleUp(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
2    display->triangle(x, y - h/2, x + w/2, y + h/2, x - w/2, y + h/2, borderSize, color.getBorderColor(), color);
3}

Diese Funktion zeichnet ein Dreieck, bei welchem eine Ecke nach oben zeigt und die anderen beiden Ecken sich unterhalb dieser Ecke auf der selben Höhe befinden. Dafür wird die „triangle“ Funktion des „Display“ Objektes aufgerufen. Die Parameter x und y stehen hierfür für die X- bzw. Y-Koordinaten, bei welchen das Dreieck gezeichnet werden soll, h steht für die gewünschte Höhe und w für die gewünschte Breite des Dreiecks.

static void drawTriangleDown(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

1static void drawTriangleDown(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
2    display->triangle(x, y + h/2, x + w/2, y - h/2, x - w/2, y - h/2, borderSize, color.getBorderColor(), color);
3}

Diese Funktion zeichnet ein Dreieck, bei welchem eine Ecke nach unten zeigt und die anderen beiden Ecken sich oberhalb dieser Ecke auf der selben Höhe befinden. Dafür wird die „triangle“ Funktion des „Display“ Objektes aufgerufen. Die Parameter x und y stehen hierfür für die X- bzw. Y-Koordinaten, bei welchen das Dreieck gezeichnet werden soll, h steht für die gewünschte Höhe und w für die gewünschte Breite des Dreiecks.

static void drawTriangleRight(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

1static void drawTriangleRight(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
2    display->triangle(x + w/2, y, x - w/2, y - h/2, x - w/2, y + h/2, borderSize, color.getBorderColor(), color);
3}

Diese Funktion zeichnet ein Dreieck, bei welchem eine Ecke nach rechts zeigt und die anderen beiden Ecken sich links dieser Ecke auf der selben Breite befinden. Dafür wird die „triangle“ Funktion des „Display“ Objektes aufgerufen. Die Parameter x und y stehen hierfür für die X- bzw. Y-Koordinaten, bei welchen das Dreieck gezeichnet werden soll, h steht für die gewünschte Höhe und w für die gewünschte Breite des Dreiecks.

static void drawTriangleLeft(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

1static void drawTriangleLeft(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
2    display->triangle(x - w/2, y, x + w/2, y - h/2, x + w/2, y + h/2, borderSize, color.getBorderColor(), color);
3}

Diese Funktion zeichnet ein Dreieck, bei welchem eine Ecke nach links zeigt und die anderen beiden Ecken sich reechts dieser Ecke auf der selben Breite befinden. Dafür wird die „triangle“ Funktion des „Display“ Objektes aufgerufen. Die Parameter x und y stehen hierfür für die X- bzw. Y-Koordinaten, bei welchen das Dreieck gezeichnet werden soll, h steht für die gewünschte Höhe und w für die gewünschte Breite des Dreiecks.

static void drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

1static void drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
2    display->rect(x - w/2, y - h/2, w, h, borderSize, 0, color.getBorderColor(), color);
3}
4

Diese Funktion zeichnet ein Rechteck. Dafür wird die „rect“ Funktion des „Display“ Objektes aufgerufen. Die Parameter x und y stehen hierfür für die X- bzw. Y-Koordinaten, bei welchen das Rechteck gezeichnet werden soll, h steht für die gewünschte Höhe und w für die gewünschte Breite des Rechtecks.

static void drawCrosslines(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

1static void drawCrosslines(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
2    display->line(x - w/2, y - h/2, x + w/2, y + h/2, color);
3    display->line(x + w/2, y - h/2, x - w/2, y + h/2, color);
4}

Diese Funktion zeichnet ein zwei sich kreuzende Linien. Dafür werden zwei line Funktionen des Display Objektes aufgerufen. Die Parameter x und y stehen hierfür für die X- bzw. Y-Koordinaten, bei welchen die Linien sich kreuzen sollen, h steht für die gewünschte Höhe und Breite für die gewünschte Breite des Kreuzes.

static void drawToggleSwitch(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

 1static void drawToggleSwitch(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
 2    uint16_t switchX = x + w/2;
 3    uint16_t switchHeight = h / 2;
 4    uint16_t switchTop = y + switchHeight*0.6;
 5    uint16_t switchBottom = y + switchHeight*1.4;
 6    uint16_t d = std::min(w, h) * 0.5;
 7
 8    display->circle(switchX, switchTop, d, borderSize, color.getBorderColor(), color);
 9    display->circle(switchX, switchBottom, d, borderSize, color.getSecondaryBorderColor(), color.getSecondaryBorderColor());
10    
11    display->text_center(switchX, switchTop , switchHeight * 0.75, "ON", COLOR_BLACK);
12    display->text_center(switchX, switchBottom , switchHeight * 0.75, "OFF", COLOR_BLACK);
13}

Diese Funktion zeichnet einen Schalter aus zwei teilweise überlappenden Kreisen, bei dem angeschalteter und ausgeschalteter Zustand gleichzeitig sichtbar sind.

    uint16_t switchX = x + w/2;
    uint16_t switchHeight = h / 2;
    uint16_t switchTop = y + switchHeight*0.6;
    uint16_t switchBottom = y + switchHeight*1.4;
    uint16_t d = std::min(w, h) * 0.5;

Dafür werden zuerst die X-Koordinate des Switches (switchX), die Höhe des gesamten Switches (switchHeight), die Y-Koordinate der oberen Hälfte (switchTop) und der unteren Hälfte (switchBottom) des Switches, sowie dem Durchmesser (d) jeder Hälfte des Schalters.

    display->circle(switchX, switchTop, d, borderSize, color.getBorderColor(), color);
    display->circle(switchX, switchBottom, d, borderSize, color.getSecondaryBorderColor(), color.getSecondaryBorderColor());

Nun wird mit circle zuerst die obere Hälfte und dann die untere Häfte gezeichnet.

    display->text_center(switchX, switchTop , switchHeight * 0.75, "ON", COLOR_BLACK);
    display->text_center(switchX, switchBottom , switchHeight * 0.75, "OFF", COLOR_BLACK);

Anschließend werden mit text_center die Texte ON und OFF auf die obere bzw. untere Hälfte gezeichnet.

static void drawPlus(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

 1static void drawPlus(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
 2    const uint16_t tmpW = w/4;
 3    const uint16_t tmpH = h/4;
 4    const uint16_t tmpX = x - tmpW/2;
 5    const uint16_t tmpY = y - tmpH/2;
 6    
 7    if (borderSize > 0) {
 8        display->rect(tmpX, 0, tmpW, h, 0, 0, color.getBorderColor());
 9        display->rect(0, tmpY, w, tmpH, 0, 0, color.getBorderColor());
10    }
11
12    display->rect(tmpX + borderSize, borderSize, tmpW-borderSize, h-borderSize, 0, 0, color);
13    display->rect(borderSize, tmpY + borderSize, w-borderSize, tmpH-borderSize, 0, 0, color);
14}

Diese Funktion zeichnet ein Plus.

    const uint16_t tmpW = w/4;
    const uint16_t tmpH = h/4;
    const uint16_t tmpX = x - tmpW/2;
    const uint16_t tmpY = y - tmpH/2;

Dafür werden zuerst einige Hilfsvariable bestimmt. tmpW und tmpH geben die Breite bzw. Höhe des Symbols an, tmpX und tmpY die X- bzw. Y-Koordinaten, bei welchen das Plus gezeichnet werden soll.

    if (borderSize > 0) {
        display->rect(tmpX, 0, tmpW, h, 0, 0, color.getBorderColor());
        display->rect(0, tmpY, w, tmpH, 0, 0, color.getBorderColor());
    }

Sollte das Symbol einen Border haben, so werden zuerst zwei Rechtecke mit rect in der Farbe der Umrandung gezeichnet (eines vertikal und das andere horizontal).

    display->rect(tmpX + borderSize, borderSize, tmpW-borderSize, h-borderSize, 0, 0, color);
    display->rect(borderSize, tmpY + borderSize, w-borderSize, tmpH-borderSize, 0, 0, color);

Danach werden zwei kleinere Rechtecke über die zuvor gezeichneten Rechtecke gezeichnet, sodass die Umrandung in der gewünschten Breite sichtbar bleibt.

static void drawMinus(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color)

 1static void drawMinus(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color) {
 2    const uint16_t tmpH = h/4;
 3    const uint16_t tmpY = y - tmpH/2;
 4    
 5    if (borderSize > 0) {
 6        display->rect(0, tmpY, w, tmpH, 0, 0, color.getBorderColor());
 7    }
 8
 9    display->rect(borderSize, tmpY + borderSize, w-borderSize, tmpH-borderSize, 0, 0, color);
10}

Diese Funktion zeichnet ein Minus.

    const uint16_t tmpH = h/4;
    const uint16_t tmpY = y - tmpH/2;

Dafür werden zuerst zwei Hilfsvariable bestimmt. tmpH gibt die Höhe des Symbols an, tmpY die Y-Koordinaten, bei welchen das Minus gezeichnet werden soll.

    if (borderSize > 0) {
        display->rect(0, tmpY, w, tmpH, 0, 0, color.getBorderColor());
    }

Sollte das Symbol einen Border haben, so wird zuerst ein Rechteck mit rect in der Farbe der Umrandung gezeichnet.

    display->rect(borderSize, tmpY + borderSize, w-borderSize, tmpH-borderSize, 0, 0, color);

Danach wird ein kleineres Rechteck über das zuvor gezeichnete Rechtecke gezeichnet, sodass die Umrandung in der gewünschten Breite sichtbar bleibt.

Variablen und Konstanten Beschreibung

uint16_t width

Die Höhe (also Größe in Y-Richtung) des Symbols.

uint16_t height

Die Höhe (also Größe in X-Richtung) des Symbols.

uint8_t borderSize

Die Dicke der Umrandung des Symbols.

Color color

Die Farbe des Symbols.

symbol_func_ptr drawSymbol

1    typedef void (*symbol_func_ptr) (uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t borderSize, Display* display, const Color& color);
2    symbol_func_ptr drawSymbol;

drawSymbol ist ein Pointer, der es ermöglicht, auf die oben definierten Symbol-Funktionen (zum zeichnen verschiedener Symbol) zu zeigen, um diese wie dort beschrieben zu zeichnen.