#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <EMLib.h>
#include <SD.h>

#define SDA_PIN 4
#define SCL_PIN 5
#define SD_CS_PIN 2

#define Sprintln(x) Serial.println(x)
#define Sprint(x) Serial.print(x)
#define wait(x) delay(x);

Adafruit_SSD1306 display(128, 64, &Wire);
RTC_DS3231 rtc;
Sd2Card SDcard;
long int milli = 0;
float gbCardSize = 0;

void setup() {
  Wire.begin(SDA_PIN,SCL_PIN);
  pinMode(0,INPUT);
  Serial.begin(9600);
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Sprintln(F("SSD1306 allocation failed"));
    while(true); // Don't proceed, loop forever
  }

  if(! rtc.begin()) {
    Sprintln("Couldn't find RTC");
    while(true);
  }

  if(rtc.lostPower()) {
    Sprintln("RTC lost power !");
  }

  if(! SDcard.init(SPI_FULL_SPEED, SD_CS_PIN))
  {
    Sprintln("Couldn't init SDCard");
    while(true);
  }

  gbCardSize = ((float)SDcard.cardSize())*512.0/1024.0/1024.0/1024.0;
  
  
  Sprintln("Empty display :");
  display.display();
  wait(1000);
  Sprintln("Clearing display:");
  display.clearDisplay();
  display.setRotation(2);
  wait(1000);
  Sprintln("Printing greating message :");
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0,0);
  display.setTextColor(WHITE);

  display.println(F("Hello YOU !!!"));
  display.display();
  wait(1000);
  Sprintln("Inverting colors:");
  display.invertDisplay(true);
  wait(1000);
  Sprintln("Inverting colors:");
  display.invertDisplay(false);
  wait(1000);
  Sprintln("Dimming :");
  display.dim(true);
  wait(1000);
  Sprintln("Full brightness :");
  display.dim(false);
  
  wait(1000);
  Sprintln("Printing greating message rotation 0 :");
  display.clearDisplay();
  display.setRotation(0);
  display.setTextSize(2);
  display.setCursor(0,0);
  display.setTextColor(WHITE);

  display.println(F("Hello YOU !!!"));
  display.display();
  wait(1000);
  Sprintln("Printing greating message rotation 1 :");
  display.clearDisplay();
  display.setRotation(1);
  display.setTextSize(2);
  display.setCursor(0,0);
  display.setTextColor(WHITE);

  display.println(F("Hello YOU !!!"));
  display.display();
  wait(1000);
  Sprintln("Printing greating message rotation 2 :");
  display.clearDisplay();
  display.setRotation(2);
  display.setTextSize(2);
  display.setCursor(0,0);
  display.setTextColor(WHITE);

  display.println(F("Hello YOU !!!"));
  display.display();
  wait(1000);
  Sprintln("Printing greating message rotation 3 :");
  display.clearDisplay();
  display.setRotation(3);
  display.setTextSize(2);
  display.setCursor(0,0);
  display.setTextColor(WHITE);

  display.println(F("Hello YOU !!!"));
  display.display();
  wait(1000);
  Sprintln("Clearing display:");
  display.clearDisplay();
  display.setRotation(2);
  display.setTextSize(1);
  
}

void loop() {
  if(millis() - milli > 1000)
  {
    milli = millis();
    // put your main code here, to run repeatedly:
    DateTime time = rtc.now();
    char time_str[300] = "";
    sprintf(time_str,"RTC:time--> %d:%d:%d\ndate--> %d/%d/%d\nFree RAM : %d\nSD size :\n%.2f GByte",time.hour(), time.minute(), time.second(), time.day(), time.month(), time.year(),ESP.getFreeHeap(), gbCardSize);
    display.setCursor(0,0);
    display.println(time_str);
    display.display();
    display.clearDisplay();
  }
  if(digitalRead(0) == false)
  {
    Sprintln("Flash button pushed");
    //rtc.adjust(DateTime(2019, 3, 3, 12, 30, 0));
  }
}
