/*
	Capitulo 54 de Arduino desde cero en Español.
	Visualizacion en pantalla TFT bus SPI ILI9341 de cantidad de segundos transcurridos
	desde iniciado el programa. Requiere librerias Adafruit ILI9341 y Adafruit GFX

	Autor: bitwiseAr  
  完整參數
  http://www.cplusplus.com/reference/ctime/strftime/
*/
 
#include <SPI.h>		// incluye libreria bus SPI
#include <Adafruit_GFX.h>	// incluye libreria para manejo de graficos
#include <Adafruit_ILI9341.h>	// incluye libreria para controlador ILI9341
 
#define TFT_DC 2		// define constante TFT_DC con numero 9
#define TFT_CS 5		// define constante TFT_CS con numero 10

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);	// crea objeto
 


#include <WiFi.h>
#include <TimeLib.h>
#include <Wire.h>


const char* ntpServer = "pool.ntp.org"; //NTP主機位址
const long  gmtOffset_sec = 28800; //時區(以秒為單位) 8*60*60  //https://time.artjoey.com/
const int   daylightOffset_sec = 0; //定義夏令時的偏移量(以秒為單位)

// char *weekdayStr[7] = {"日", "一", "二", "三", "四", "五", "六"};

uint32_t targetTime = 0;       // for next 1 second timeout
char dateStringBuff[50];    //日期
char timeStringBuff[50];    //時間
char WeekStringBuff[50];   //星期
char hhStringBuff[10];    //時
char mmStringBuff[10];    //分
char ssStringBuff[10];    //秒
uint8_t hh , mm, ss;

void setup() {
  Serial.begin(115200);

 tft.begin();
  tft.setRotation(3);

  WiFi.begin("Wokwi-GUEST", "", 6);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    targetTime = millis() + 1000;
}

void loop() {
  printLocalTime();
//   Serial.print("時間:");
// Serial.println(timeStringBuff);

}

//------網路ntpServer獲取時間-------------
void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("未能獲得時間");
    return;
  }

  strftime(dateStringBuff, sizeof(dateStringBuff), "%F", &timeinfo);  //年-月-日
  strftime(timeStringBuff, sizeof(timeStringBuff), "%T", &timeinfo);  //幾時:幾分:幾秒
  strftime(WeekStringBuff, sizeof(WeekStringBuff), "%w", &timeinfo);  //星期 星期日為 0 (0-6)
  strftime(hhStringBuff, sizeof(hhStringBuff), "%H", &timeinfo);  //幾時
  strftime(mmStringBuff, sizeof(mmStringBuff), "%M", &timeinfo);  //幾分
  strftime(ssStringBuff, sizeof(ssStringBuff), "%S", &timeinfo);  //幾秒
  String HH, MM, SS;
  HH = hhStringBuff;
  MM = mmStringBuff;
  SS = ssStringBuff;
  hh = HH.toInt();
  mm = MM.toInt();
  ss = SS.toInt();
  //---------------模擬用--------------
  if (targetTime < millis()) {
    targetTime = millis() + 1000;

    tft.setTextSize(3);
    tft.setCursor(30,50);		// ubica cursor
    tft.setTextColor(ILI9341_BLUE );
    tft.println(dateStringBuff); // Overwrite the text to clear it

    tft.setTextSize(3);
    tft.setCursor(30,100);		// ubica cursor
    tft.setTextColor(0x39C4, ILI9341_BLACK);
    tft.println("88:88:88"); // Overwrite the text to clear it

    tft.setCursor(30,100);		// ubica cursor
    tft.setTextColor(ILI9341_RED);
    tft.println(timeStringBuff); // Overwrite the text to clear it

    tft.setTextSize(3);
    tft.setCursor(30,150);		// ubica cursor
    tft.setTextColor(ILI9341_PURPLE );
    tft.println("Week:" + String(WeekStringBuff)); // Overwrite the text to clear it
    

    Serial.println(timeStringBuff);
  }
//------實際用這個------TFT_eSPI tft = TFT_eSPI();       
  // if (targetTime < millis()) {
  //   targetTime = millis() + 1000;
  //   tft.setTextColor(0x39C4, TFT_BLACK);
  //   tft.drawString("88:88:88", 0, 120, 7); // Overwrite the text to clear it
  //   tft.setTextColor(0xFBE0);
  //   tft.drawString(timeStringBuff, 0, 120, 7); // Overwrite the text to clear it
  // }

}