/*
#include <SPI.h>		// incluye libreria bus SPI
   Arduino चा प्रयोग Adafruit ILI9341 या  टीएफटी स्क्रीन चा वापर 
   spi, Adafruit ILI9341 आणि Adafruit GFX लायब्ररी आवश्यक आहे
	Autor: bitwiseAr   हा मूळ रचनाकार आहे  
  संदर्भ 
  http://www.cplusplus.com/reference/ctime/strftime/

कार्य सुरू केल्यावर saevpratham


*/
 
#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 = 19800; ///टाइमझोन (सेकंदांमध्ये)भारतासाठी const char* ntpServer = "pool.ntp.org"; //NTPहोस्ट पत्ता) 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
  // }

}