#include "My_Eeprom.h"
#include <EEPROM.h>
#include <Arduino.h>
#include <DS3231.h>
#include <Wire.h>

DS3231 clock;

//Ecrit le nombre de bit dans l'EEPROM a la suite de la derniere ecriture (Si l'EEPROM est plein n'ecrit pas return -1)
int Write_EEPROM(uint8_t *tab, int nb) {
  int add = EEPROM.read(0) << 8;
  add =  EEPROM.read(1);

  if ((add + nb) > MAXEEPROM)
    return -1;
  
  for (int i = 0; i < nb; i++) {
    EEPROM.update(add +2+ i, tab[i]);
  }
  add+=nb;
  EEPROM.update(0,add>>8);
  EEPROM.update(1,add);
  return 0;
}

//remet a 0 le pointeur d'ecriture
int erase() {
  EEPROM.update(0, 0);
  EEPROM.update(1, 0);
}

bool h12Flag;
bool pmFlag;
//renvoit la date au format 16 bit
void date(uint8_t *date) {
  unsigned int jour = clock.getDate();
  unsigned int heure = clock.getHour(h12Flag,pmFlag);
  unsigned int mine = clock.getMinute();
  unsigned int tmp = (jour << 12) + (heure * 60 + mine);
  date[0] = tmp >> 8;
  date[1] = tmp;
}

//decode la date
void readdate(uint16_t date, uint8_t* jour, uint8_t* heure, uint8_t* mine) {
  *jour	= (uint8_t)(date >> 12);
  int tmp = (date & 0x5FFF);
  int tm = tmp % 60;
  int th = (tmp-tm)/60;
  *heure = (uint8_t)th;
  *mine = (uint8_t)tm;
  
}

//Affiche le contenue de l'EEPROM de facon claire
void AfficheEEprom() {
  int add = EEPROM.read(0) << 8;
  add =  EEPROM.read(1);
  Serial.println("/***********DEBUT***********/");
  uint8_t jour, heure, mine;
  uint16_t date;
  uint8_t code;
  for ( int i = 2 ; i < add ; i+=3) {
    date = EEPROM.read(i) << 8;
    date += EEPROM.read(i + 1) ;
    readdate(date, &jour, &heure, &mine);
    
    Serial.print("J : ");
    Serial.print(jour);
    Serial.print(" ");
    Serial.print(heure);
    Serial.print("H");
    Serial.print(mine);
    switch (EEPROM.read(i+2)) {
      case (ECUON):
        Serial.println(" Ecumeur ON");
        break;
      case (ECUOF):
        Serial.println(" Ecumeur OFF");
        break;
      case (BR1ON):
        Serial.println(" BRASSAGE1 ON");
        break;
      case (BR1OF):
        Serial.println(" BRASSAGE1 OFF");
        break;
      case (BR2ON):
        Serial.println(" BRASSAGE2 ON");
        break;
      case (BR2OF):
        Serial.println(" BRASSAGE2 OFF");
        break;
      case (REMON):
        Serial.println(" REMONTER ON");
        break;
      case (REMOF):
        Serial.println(" REMONTER OFF");
        break;
      case (OSMON):
        Serial.println(" OSMOLATION ON");
        break;
      case (OSMOF):
        Serial.println(" OSMOLATION OFF");
        break;
      case (CHAON):
        Serial.println(" CHAUFFAGE ON");
        break;
      case (CHAOF):
        Serial.println(" CHAUFFAGE OFF");
        break;
      case (TEBON):
        Serial.println(" TECH BAS ON");
        break;
      case (TEBOF):
        Serial.println(" TECH BAS OFF");
        break;
      case (TEHON):
        Serial.println(" TECH HAUT ON");
        break;
      case (TEHOF):
        Serial.println(" TECH HAUT OFF");
        break;
      case (REBON):
        Serial.println(" RESERVE BAS ON");
        break;
      case (REBOF):
        Serial.println(" RESERVE BAS OFF");
        break;
      case (CUHON):
        Serial.println(" CUVE HAUT ON");
        break;
      case (CUHOF):
        Serial.println(" CUVE HAUT OFF");
        break;
      case (LUMON):
        Serial.println(" LUMIERE ON");
        break;
      case (LUMOF):
        Serial.println(" LUMIERE OFF");
        break;
    }
  }
  Serial.println("/************FIN************/");
}

//Affiche un instantané des sortie 
void instantane(){
  
}


//Enregistre dans l'Eeprom l'evenement en ecrivant la date + le numero de l'evenement
void Enregistre(uint8_t even) {
  uint8_t data[3];
  date(data);
  data[2] = even;
  Write_EEPROM(data, 3);
}
