/*
 * Created by Laurent CLaude
 *
 * This code is in license GPL v3
 *
 * Horloge Nixie basée sur ESP + module RTC-DS1307, avec fonctionnalités wifi pour synchro NTP
 */

#include "hardware.h"
#include "nixie.h"
#include "horlogerie.h"
#include "secrets.h"

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <RTClib.h>

// Les affectations physiques

// event at to 14:45 (for tests)
uint8_t DAILY_EVENT_HH = 14; // event start time: hour
uint8_t DAILY_EVENT_MM = 45; // event start time: minute

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {
  "Dimanche",
  "Lundi",
  "Mardi",
  "Mercredi",
  "Jeudi",
  "Vendredi",
  "Samedi"
};

void setup () {
  Serial.begin(115200);
  Wire.begin(I2C_SDA,I2C_SCL); // Broches (SDA,SCL) de l'I2C pour la RTC

  // SETUP RTC MODULE
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  // sets the RTC to the date & time on PC this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  // sets the RTC with an explicit date & time, for example to set
  // January 21, 2021 at 3am you would call:
  // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0));
  initwifintp();
}

void loop () {
  DateTime now = rtc.now();
  printTime(now);
  if (now.hour()   == DAILY_EVENT_HH &&
      now.minute() == DAILY_EVENT_MM) {
    Serial.println("It is on scheduled time");
    // TODO: write your code"
  } else {
    Serial.println("It is NOT on scheduled time");
  }
  delay(1000);
}

void printTime(DateTime time) {
  Serial.print("Date : ");
  Serial.print(time.year(), DEC);
  Serial.print('/');
  Serial.print(time.month(), DEC);
  Serial.print('/');
  Serial.print(time.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[time.dayOfTheWeek()]);
  Serial.print(") - Heure : ");
  Serial.print(time.hour(), DEC);
  Serial.print(':');
  Serial.print(time.minute(), DEC);
  Serial.print(':');
  Serial.println(time.second(), DEC);
}