#include <EEPROM.h>
#include <Wire.h>
#include "RTClib.h"
#include "DFRobotDFPlayerMini.h"
#include <LiquidCrystal_I2C.h>
RTC_DS1307 rtc;  // đặt tên rtc
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define nut_chuyen 7
#define nut_giam 14
#define nut_tang 9
#define nut_chon 8

int address = 0;
byte value;

boolean set_nhac[37];

int giao_dien = 0;
byte che_do;

byte cai_dat[50];

byte eeprom_gio[150];
byte eeprom_phut[150];

boolean eeprom_cd[50];
byte eeprom_nhac[50];

boolean z = false;
///
byte degree[8] = {
  0B00000,
  0B01000,
  0B01100,
  0B01110,
  0B01110,
  0B01100,
  0B01000,
  0B00000
};
///
void(* resetFunc) (void) = 0;

void doc_eeprom()
{
  for (int i = 1; i < 37; i++)
  {
    eeprom_gio[i] = EEPROM.read(i);
    delay(1);
  }
  for (int i = 37; i < 74; i++)
  {
    int x = i - 36;
    eeprom_phut[x] = EEPROM.read(i);
    delay(1);
  }
  for (int i = 74; i < 111; i++)
  {
    int x = i - 73;
    eeprom_cd[x] = EEPROM.read(i);
    delay(1);
  }
  for (int i = 111; i < 148; i++)
  {
    int x = i - 110;
    eeprom_nhac[x] = EEPROM.read(i);
    delay(1);
  }
}

void chon_khung_gio()
{
  if (z == 0)
  {
    lcd.setCursor(1, 0);
    lcd.print("CHON KHUNG GIO");
    lcd.setCursor(1, 1);
    lcd.print("SO:");
    lcd.print(che_do);
    
    if (digitalRead(nut_tang) == 0)
    {
      delay(300);
      che_do ++;
      lcd.setCursor(4, 1);
      lcd.print("     ");
      if (che_do > 36)
      {
        che_do = 1;
      }
    }
    if (digitalRead(nut_giam) == 0)
    {
      delay(300);
      che_do --;
      lcd.setCursor(4, 1);
      lcd.print("     ");
      if (che_do < 1)
      {
        che_do = 36;
      }
    }
  }
  delay(2);
}

void tg_thuc()
{
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print(" THONG BAO AUTO");
  lcd.setCursor(1, 1);
  lcd.print("TIME: ");
  lcd.setCursor(6, 1);
  if (now.hour() <= 9)
  {
    lcd.print("0");
    lcd.print(now.hour());
  }
  else {
    lcd.print(now.hour());
  }
  lcd.print(':');
  if (now.minute() <= 9)
  {
    lcd.print("0");
    lcd.print(now.minute());
  }
  else {
    lcd.print(now.minute());
  }
  lcd.print(':');
  if (now.second() <= 9)
  {
    lcd.print("0");
    lcd.print(now.second());
  }
  else {
    lcd.print(now.second());
  }
  lcd.print("  ");
}

void chuyen()
{
  if (digitalRead(nut_chuyen) == 0)
  {
    delay(300); // trễ 300ms
    while (digitalRead(nut_chuyen) == 0);
    lcd.clear();
    giao_dien++;
  }
}

void set_che_do (byte c_d, byte e1, byte e2, byte e3, byte e4)
{
  if (che_do == c_d)
  {
    if (digitalRead(nut_chon) == 0) // nếu nút chon được nhấn
    {
      delay(20); // trễ 20 ms
      while (digitalRead(nut_chon) == 0);  // kiểm tra trạng thái nút nhấn
      lcd.clear();//xóa mọi văn bản hoặc dữ liệu đã được hiển thị LCD
      cai_dat[c_d]++;
      z = 1;
    }

    if (cai_dat[c_d] == 1)
    {
      lcd.setCursor(0, 0);
      lcd.print("  CAI DAT GIO");
      lcd.setCursor(0, 1);
      lcd.print("THOI GIAN");
      lcd.write(1);


      (eeprom_gio[c_d] <= 9) ? lcd.print("0"), lcd.print(eeprom_gio[c_d]) : lcd.print(eeprom_gio[c_d]);
      lcd.print("h");
      //phut
      (eeprom_phut[c_d] <= 9) ? lcd.print("0"), lcd.print(eeprom_phut[c_d]) : lcd.print(eeprom_phut[c_d]);
      lcd.print("p");

      if (digitalRead(nut_tang) == 0)
      {
        delay(200);
        eeprom_gio[c_d] ++;
        if (eeprom_gio[c_d] > 24)
        {
          eeprom_gio[c_d] = 0;
        }
      }

      if (digitalRead(nut_giam) == 0)
      {
        delay(200);
        eeprom_gio[c_d] --;
        if ((eeprom_gio[c_d] < 0) || (eeprom_gio[c_d] > 200))
        {
          eeprom_gio[c_d] = 24;
        }
      }
    }
    // cai_phut1
    if (cai_dat[c_d] == 2)
    {
      lcd.setCursor(0, 0);
      lcd.print("  CAI DAT PHUT");
      lcd.setCursor(0, 1);
      lcd.print("THOI GIAN");
      lcd.write(1);
      //
      (eeprom_gio[c_d] <= 9) ? lcd.print("0"), lcd.print(eeprom_gio[c_d]) : lcd.print(eeprom_gio[c_d]);
      lcd.print("h");
      (eeprom_phut[c_d] <= 9) ? lcd.print("0"), lcd.print(eeprom_phut[c_d]) : lcd.print(eeprom_phut[c_d]);
      lcd.print("p");

      if (digitalRead(nut_tang) == 0)
      {
        delay(200);
        eeprom_phut[c_d]++;
        if (eeprom_phut[c_d] > 60)
        {
          eeprom_phut[c_d] = 0;
        }
      }
      if (digitalRead(nut_giam) == 0)
      {
        delay(200);
        eeprom_phut[c_d]--;
        if ((eeprom_phut[c_d] < 0) || (eeprom_phut[c_d] > 200))
        {
          eeprom_phut[c_d] = 60;
        }
      }
    }

    if (cai_dat[c_d] == 3)
    {
      lcd.setCursor(0, 0);
      lcd.print("CHON NHAC:");
      lcd.print(eeprom_nhac[c_d]);
      lcd.print("   ");

      if (digitalRead(nut_tang) == 0)
      {
        delay(200);
        eeprom_nhac[c_d] += 1;
        if (eeprom_nhac[c_d] > 255)
        {
          eeprom_nhac[c_d] = 1;
        }
      }
      if (digitalRead(nut_giam) == 0)
      {
        delay(200);
        eeprom_nhac[c_d] -= 1;
        if (eeprom_nhac[c_d] < 1)
        {
          eeprom_nhac[c_d] = 255;
        }
      }
    }
    if (cai_dat[c_d] == 4)
    {
      lcd.setCursor(0, 0);
      lcd.print("CHE DO:");

      if (digitalRead(nut_tang) == 0)
      {
        delayMicroseconds(100);
        eeprom_cd[c_d] = true;
      }

      if (eeprom_cd[c_d] == true)
      {
        lcd.setCursor(7, 0);
        lcd.print("ON");
        lcd.print("   ");
      }

      if (digitalRead(nut_giam) == 0)
      {
        delayMicroseconds(100);
        eeprom_cd[c_d] = false;
      }

      if (eeprom_cd[c_d] == false)
      {
        lcd.setCursor(7, 0);
        lcd.print("OFF");
      }
    }

    if (cai_dat[c_d] == 5)
    {
      delay(5);
      EEPROM.update(e1, eeprom_gio[c_d]);
      delay(5);
      EEPROM.update(e2, eeprom_phut[c_d]);
      delay(5);
      EEPROM.update(e3, eeprom_cd[c_d]);
      delay(5);
      EEPROM.update(e4, eeprom_nhac[c_d]);
      delay(5);

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("CAI DAT XONG");
      delay(150);
      lcd.print(".");
      delay(150);
      lcd.print(".");
      delay(150);
      lcd.print(".");
      delay(600);
      lcd.clear();
      giao_dien = 0;
    }
  }
}

void doc_EEPROM_Serial()
{
  value = EEPROM.read(address);
  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();

  // Tăng giá trị ô nhớ lên 1
  address = address + 1;

  // chúng ta chỉ có 512 ô nhớ (số thứ tự từ 0-511)
  // nên khi address (số thứ tự) == 512 thì ta quay về
  // thứ tự là 0
  if (address == 512)
    address = 0;
  delay(5);
}
