/*
Name:    Display.cpp
Author:  Roman Schönfeld & Daniel Hausherr
*/

/***********************************************************************************************
This work is licensed under a Creative Commons Attribution 4.0 International (CC BY 4.0) License.

You are free to:

Share — copy and redistribute the material in any medium or format
Adapt — remix, transform, and build upon the material
for any purpose, even commercially.

The licensor cannot revoke these freedoms as long as you follow the license terms.

Under the following terms:

Attribution — You must give appropriate credit, provide a link to the license, and indicate if
changes were made. You may do so in any reasonable manner, but not in any way that suggests
the licensor endorses you or your use.

No additional restrictions — You may not apply legal terms or technological measures that
legally restrict others from doing anything the license permits.

Credits:
Laboratory for Physics and Illumination Technologies,
University of Applied Sciences Southern Westfalia
Germany

Make Light! Support by the BMBF in the development of this device is gratefully acknowledged
***************************************************************************************************/
#include "Display.h"

TFT tft = TFT(_CS, _DC, _RES);

// Initialisiert das Display
void init_tft() {
	tft.initR(INITR_BLACKTAB);
	tft.fillScreen(ST7735_BLACK);
	tft.setTextSize(1);
	tft.setRotation(135);
}

// Setzt die Helligkeit des Displays
void set_brightness_tft(uint8_t light) {
	analogWrite(_BL, light);
}

// Setzt die Zeichen größe
void set_text_size_tft(uint8_t text_size) {
	tft.setTextSize(text_size);
}

// Gibt Textzeichen aus
void print_text_tft(char *text, uint16_t color) {
	tft.setTextColor(color);
	tft.print(text);
}

// Gibt Zahlen aus
void print_value_tft(unsigned int value, uint16_t color) {
	tft.setTextColor(color);
	tft.print(value);
}

// Gibt Gleitkommazahlen aus
void print_value_tft_double(double value, uint16_t color) {
	tft.setTextColor(color);
	tft.print(value);
}

// Setzt den Kurser an die gewünschte Koordinate
void set_cursor_tft(uint8_t x, uint8_t y) {
	tft.setCursor(x, y);
}

// Füllt das Display aus
void clear_area_tft(uint8_t x, uint8_t y, uint8_t size_x, uint8_t size_y, uint16_t color) {
	tft.fillRect(x, y, size_x, size_y, color);
}

extern void drawArrowUp(uint8_t x, uint8_t y, char *text, uint16_t color) {

  tft.drawLine(x, y, x, y - 5, color);

  tft.drawPixel(x, y - 6, color);
  tft.drawPixel(x - 1, y - 5, color);
  tft.drawPixel(x + 1, y - 5, color);
  tft.drawPixel(x - 2, y - 4, color);
  tft.drawPixel(x + 2, y - 4, color);

  tft.setTextColor(color);
  tft.setCursor(x + 6, y - 6);
  tft.print(text);

}
extern void drawArrowDown(uint8_t x, uint8_t y, char *text, uint16_t color) {

  tft.drawLine(x, y, x, y - 5, color);

  tft.drawPixel(x, y - 6, color);
  tft.drawPixel(x - 1, y - 1, color);
  tft.drawPixel(x + 1, y - 1, color);
  tft.drawPixel(x - 2, y - 2, color);
  tft.drawPixel(x + 2, y - 2, color);

  tft.setTextColor(color);
  tft.setCursor(x + 6, y - 6);
  tft.print(text);

}
extern void drawArrowRight(uint8_t x, uint8_t y, char *text, uint16_t color) {

  tft.drawLine(x, y, x + 5, y, color);

  tft.drawPixel(x + 6, y, color);
  tft.drawPixel(x + 5, y - 1, color);
  tft.drawPixel(x + 5, y + 1, color);
  tft.drawPixel(x + 4, y - 2, color);
  tft.drawPixel(x + 4, y + 2, color);

  tft.setTextColor(color);
  tft.setCursor(x + 10, y - 3);
  tft.print(text);

}
extern void drawArrowLeft(uint8_t x, uint8_t y, char *text, uint16_t color) {

  tft.drawLine(x, y, x + 5, y, color);

  tft.drawPixel(x + 6, y, color);
  tft.drawPixel(x + 1, y - 1, color);
  tft.drawPixel(x + 1, y + 1, color);
  tft.drawPixel(x + 2, y - 2, color);
  tft.drawPixel(x + 2, y + 2, color);

  tft.setTextColor(color);
  tft.setCursor(x + 10, y - 3);
  tft.print(text);
}

void drawMicro(uint8_t x, uint8_t y, uint8_t i, uint16_t color) {

  tft.drawLine(x, y, x, y + (5 * i), color);

  tft.drawLine(x, y + (3 * i), x + (3 * i), y + (3 * i), color);

  tft.drawLine(x + (3 * i), y, x + (3 * i), y + (4 * i), color);
}
