/*
  Test sketch for RS485 interface
*/

#include <SPI.h>
#include <TFT_eSPI.h>      // Graphics library


#define mp(x) Serial.print(F(x))
#define mpl(x) Serial.println(F(x))
#define sp(x) Serial.print(x)
#define spl(x) Serial.println(x)

#define send(x) Logger.print(x)
#define sendln(x) Logger.println(x)



TFT_eSPI tft = TFT_eSPI(); // Invoke library
/* DJ Settings
  #define LOAD_GLCD   // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
  #define LOAD_FONT2  // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
  #define LOAD_FONT4  // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
  #define LOAD_FONT6  // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
  #define LOAD_FONT7  // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:.
  #define LOAD_FONT8  // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
  #define LOAD_GFXFF  // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts
*/

setup_t user; // The library defines the type "setup_t" as a struct
// Calling tft.getSetup(user) populates it with the settings
//------------------------------------------------------------------------------------------



void setup() {
  // Use serial port
  Serial.begin(115200);
  screenInit();
  //Output to RS485 on Serial 1
    //Logger.begin(9600, SERIAL_8N1, 16, 17);

  delay(500);
  showScreenSettings();
  mpl("\n\n=== RS485_1 Software Created " __DATE__" " __TIME__ );

  spl("Starting RS485 monitor ");
  //sp("DJTEST SET TO:");
  //spl(DJTEST);

  screenTest();
spl("Screen test completed");
  //Logger.println("Testing");
//spl("RS485 output completed");
}

void loop() {
	
	showSample();
	spl("Sample output completed");
}
	
	void showSample(){
		
  // Fill screen with random colour so we can see the effect of printing with and without
  // a background colour defined
  //tft.fillScreen(random(0xFFFF));
  clearScreen;

  // Set "cursor" at top left corner of display (0,0) and select font 2
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(0, 0, 2);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.setTextSize(1);

  // Change to font 2
  tft.setTextFont(2);
  // We can now plot text on screen using the "print" class
  tft.println("Rs-485 Monitor");
  delay(500);

  // Set the font colour to be yellow with no background, set to font 7
  tft.setTextColor(TFT_YELLOW);
  tft.setTextFont(7);
  tft.println("1234");

  // Set the font colour to be red with black background, set to font 4
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.setTextFont(4);
  tft.println("Font 4");
  tft.println((long)3735928559, HEX); // Should print DEADBEEF

  // Set the font colour to be green with black background, set to font 4
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.setTextFont(4);
  tft.println("Font 4 ");
  tft.println("I implore thee");
  clearScreen();

  // Change to font 2
  tft.setTextFont(2);
  tft.println(F("Font 2")); // Can store strings in FLASH to save RAM
  // This next line is deliberately made too long for the display width to test
  // automatic text wrapping onto the next line
  tft.println("This next line is deliberately made too long for the display width to test automatic text wrapping onto the next line");

  // Test some print formatting functions
  float fnumber = 12.9;
  // Set the font colour to be blue with no background, set to font 4
  tft.setTextColor(TFT_BLUE);
  tft.setTextFont(4);
  tft.println("Font 4 ");
  tft.print("Float = "); tft.println(fnumber);           // Print floating point number
  tft.print("Binary = "); tft.println((int)fnumber, BIN); // Print as integer value in binary
  tft.print("Hexadecimal = "); tft.println((int)fnumber, HEX); // Print as integer number in Hexadecimal
  clearScreen();

}

void screenInit() {
  // Initialise the TFT screen
  tft.init();
  tft.getSetup(user); //
  tft.setRotation(0);
  tft.fillScreen(TFT_BLACK);
  // Font and background colour, background colour is used for anti-alias blending
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  // Set "cursor" at top left corner of display (0,0)
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(0, 0);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
}

void showScreenSettings() {

  sp ("TFT_eSPI ver = "); spl(user.version);
  printProcessorName();

#if defined (ESP32) || defined (ESP8266)
  if (user.esp < 0x32F000 || user.esp > 0x32FFFF) {
    sp("Frequency    = ");
    sp(ESP.getCpuFreqMHz());
    spl("MHz");
  }
#endif

#ifdef ESP8266
  sp("Voltage      = "); sp(ESP.getVcc() / 918.0); spl("V"); // 918 empirically determined
#endif
  sp("Transactions = "); spl((user.trans  ==  1) ? "Yes" : "No");
  sp("Interface    = "); spl((user.serial ==  1) ? "SPI" : "Parallel");
#ifdef ESP8266
  if (user.serial ==  1) {
    sp("SPI overlap  = ");
    spl((user.overlap == 1) ? "Yes\n" : "No\n");
  }
#endif
  if (user.tft_driver != 0xE9D) // For ePaper displays the size is defined in the sketch
  {
    sp("Display driver = "); Serial.println(user.tft_driver, HEX); // Hexadecimal code
    sp("Display width  = "); spl(user.tft_width);  // Rotation 0 width and height
    sp("Display height = "); spl(user.tft_height);
    spl();
  }
  else if (user.tft_driver == 0xE9D) spl("Display driver = ePaper\n");


#ifdef ESP32
  if (user.pin_tft_mosi != -1) {
    sp("MOSI    = ");
    sp("GPIO ");
    spl(user.pin_tft_mosi);
  }
  if (user.pin_tft_miso != -1) {
    sp("MISO    = ");
    sp("GPIO ");
    spl(user.pin_tft_miso);
  }
  if (user.pin_tft_clk  != -1) {
    sp("SCK     = ");
    sp("GPIO ");
    spl(user.pin_tft_clk);
  }

#else
  if (user.pin_tft_mosi != -1) {
    sp("MOSI    = ");
    sp("GPIO ");
    sp(getPinName(user.pin_tft_mosi));
    spl(user.pin_tft_mosi);
  }
  if (user.pin_tft_miso != -1) {
    sp("MISO    = ");
    sp("GPIO ");
    sp(getPinName(user.pin_tft_miso));
    spl(user.pin_tft_miso);
  }
  if (user.pin_tft_clk  != -1) {
    sp("SCK     = ");
    sp("GPIO ");
    sp(getPinName(user.pin_tft_clk));
    spl(user.pin_tft_clk);
  }
#endif

  String pinNameRef = "GPIO ";


    if (user.pin_tft_cs != -1) { sp("TFT_CS   = " + pinNameRef); spl(getPinName(user.pin_tft_cs)); }
    if (user.pin_tft_dc != -1) { sp("TFT_DC   = " + pinNameRef); spl(getPinName(user.pin_tft_dc)); }
    if (user.pin_tft_rst!= -1) { sp("TFT_RST  = " + pinNameRef); spl(getPinName(user.pin_tft_rst)); }

    if (user.pin_tch_cs != -1) { sp("TOUCH_CS = " + pinNameRef); spl(getPinName(user.pin_tch_cs)); }

    if (user.pin_tft_wr != -1) { sp("TFT_WR   = " + pinNameRef); spl(getPinName(user.pin_tft_wr)); }
    if (user.pin_tft_rd != -1) { sp("TFT_RD   = " + pinNameRef); spl(getPinName(user.pin_tft_rd)); }

    if (user.pin_tft_d0 != -1) { sp("\nTFT_D0   = " + pinNameRef); spl(getPinName(user.pin_tft_d0)); }
    if (user.pin_tft_d1 != -1) { sp("TFT_D1   = " + pinNameRef); spl(getPinName(user.pin_tft_d1)); }
    if (user.pin_tft_d2 != -1) { sp("TFT_D2   = " + pinNameRef); spl(getPinName(user.pin_tft_d2)); }
    if (user.pin_tft_d3 != -1) { sp("TFT_D3   = " + pinNameRef); spl(getPinName(user.pin_tft_d3)); }
    if (user.pin_tft_d4 != -1) { sp("TFT_D4   = " + pinNameRef); spl(getPinName(user.pin_tft_d4)); }
    if (user.pin_tft_d5 != -1) { sp("TFT_D5   = " + pinNameRef); spl(getPinName(user.pin_tft_d5)); }
    if (user.pin_tft_d6 != -1) { sp("TFT_D6   = " + pinNameRef); spl(getPinName(user.pin_tft_d6)); }
    if (user.pin_tft_d7 != -1) { sp("TFT_D7   = " + pinNameRef); spl(getPinName(user.pin_tft_d7)); }
  
#if defined (TFT_BL)
  sp("\nTFT_BL           = " + pinNameRef); spl(getPinName(user.pin_tft_led));
#if defined (TFT_BACKLIGHT_ON)
  sp("TFT_BACKLIGHT_ON = "); spl(user.pin_tft_led_on == HIGH ? "HIGH" : "LOW");
#endif
#endif

  spl();

  uint16_t fonts = tft.fontsLoaded();
  
    if (fonts & (1 << 1))        sp("Font GLCD   loaded\n");
    if (fonts & (1 << 2))        sp("Font 2      loaded\n");
    if (fonts & (1 << 4))        sp("Font 4      loaded\n");
    if (fonts & (1 << 6))        sp("Font 6      loaded\n");
    if (fonts & (1 << 7))        sp("Font 7      loaded\n");
    if (fonts & (1 << 9))        sp("Font 8N     loaded\n");
    else
    if (fonts & (1 << 8))        sp("Font 8      loaded\n");
    if (fonts & (1 << 15))       sp("Smooth font enabled\n");
    sp("\n");


    if (user.serial==1)        { sp("Display SPI frequency = "); spl(user.tft_spi_freq/10.0); }
    if (user.pin_tch_cs != -1) { sp("Touch SPI frequency   = "); spl(user.tch_spi_freq/10.0); }
  
  spl("[/code]");
}

//------------------------------------------------------------------------------------------
void clearScreen() {
  delay(1000);
  tft.setCursor(0, 0);
  tft.setTextSize(1);
  tft.fillScreen(TFT_BLACK);
}


void printProcessorName(void)
{
  sp("Processor    = ");
  if ( user.esp == 0x8266) spl("ESP8266");
  if ( user.esp == 0x32)   spl("ESP32");
  if ( user.esp == 0x32F)  spl("STM32");
  if ( user.esp == 0x0000) spl("Generic");
}

// Get pin name
int8_t getPinName(int8_t pin)
{
  // For ESP32 pin labels on boards use the GPIO number
  if (user.esp == 0x32) return pin;

  if (user.esp == 0x32F) return pin;

  return -1; // Invalid pin
}

void screenTest() {
  // We can now plot text on screen using the "print" class
  tft.println("Hello World!");
  // Set the font colour to be red
  tft.setTextColor(TFT_RED, TFT_BLACK);
  tft.println((uint32_t)3735928559, HEX); // Should print DEADBEEF

  // Set the font colour to be green with black background
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.setTextSize(2);
  tft.println("Text size 2 - Anti-aliased font!");
  tft.println("");

  // Test some print formatting functions
  float fnumber = 123.45;

  // Set the font colour to be blue
  tft.setTextColor(TFT_BLUE, TFT_BLACK);
  tft.print("Float = ");       tft.println(fnumber);           // Print floating point number
  tft.print("Binary = ");      tft.println((int)fnumber, BIN); // Print as integer value in binary
  tft.print("Hexadecimal = "); tft.println((int)fnumber, HEX); // Print as integer number in Hexadecimal


  
}