// Include the required Arduino libraries:
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <RTClib.h>
#include "Button.h"
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
//Per applicazione reale
//#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//Per simulazione
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define PAUSE_TIME    1000
#define SCROLL_SPEED  100
#define CLK_PIN 10  //Giallo
#define CS_PIN  9   //Verde
#define DATA_PIN 8  //Blu
// Button definitiom
//Button stateButton(3);
const int stateButton = 3;
// Stato del pulsante state
int state_ButtonState = 0;
// Ultimo stato del pulsante state
int state_LastButtonState = 0;
// Create RTC_DS3231 instance
RTC_DS3231 rtc;
// Create Parola instance
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// Date & time variable
DateTime dateTime;
// Variables to store date characters to be printed
char strDate[15] = "    ";
String dateLine = String();
// Variables to store time characters to be printed
char strTime[7] = "    ";
String timeLine = String();
// create array of a week days
char daysOfTheWeek[7][12] = {"Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"};
// State variable
static uint8_t state = 1;
void setup() {
  // put your setup code here, to run once:
    // initialize serial communication
    // Per simulazione
    Serial.begin(57600);
    // Per applicazione reale
    //Serial.begin(9600);
  // start I2C communication
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }
  // set initial Date-Time
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    rtc.adjust(DateTime(2023, 1, 21, 17, 20, 0));
    // rtc.adjust(DateTime("2023-01-21T11:30:00"));
  }
  else Serial.println("Rtc is running....");
  // Intialize the object:
  myDisplay.begin();
  // Set the intensity (brightness) of the display (0-15):
  myDisplay.setIntensity(0);
  // Clear the display:
  myDisplay.displayClear();
  // start button
  //stateButton.begin();
  //stateButton.set_repeat(1000, 700);
  pinMode(stateButton, INPUT);
}
void getDate()
{
  // read value from RTC module
  dateTime = rtc.now();
  dateLine.concat(daysOfTheWeek[dateTime.dayOfTheWeek()]);
  dateLine.concat(" ");
  dateLine.concat(dateTime.day());
  dateLine.concat("/");
  dateLine.concat(dateTime.month());
  dateLine.concat("/");
  dateLine.concat(dateTime.year());
  dateLine.toCharArray(strDate, 15);
  dateLine = "";
}
void setState()
{
  state_ButtonState = digitalRead(stateButton);
  if (state_ButtonState != state_LastButtonState)
  {
    if (state_ButtonState == HIGH)
    {
      state = state + 1;
      Serial.println(state);
    }
    else
    {}
    // Delay per evitare il bouncing
    delay(300);
  }
  state_LastButtonState = state_ButtonState;
  /*if (stateButton.pressed()) {
    state = state + 1;
    Serial.println(state);
  }*/
}
void getTime()
{
  // read value from RTC module
  dateTime = rtc.now();
  timeLine.concat(dateTime.hour());
  timeLine.concat(":");
  if (dateTime.minute() < 10)
  {
    timeLine.concat("0");
    timeLine.concat(dateTime.minute());
  }
  else
  {
    timeLine.concat(dateTime.minute());
  }
  timeLine.concat(dateTime.minute());
  timeLine.toCharArray(strTime, 6);
  timeLine = "";
}
void getHour()
{
  // read value from RTC module
  dateTime = rtc.now();
  timeLine.concat(dateTime.hour());
  timeLine.toCharArray(strTime, 4);
  timeLine = "";
}
void getMinute()
{
  // read value from RTC module
  dateTime = rtc.now();
  timeLine.concat(dateTime.minute());
  timeLine.toCharArray(strTime, 4);
  timeLine = "";
}
void loop() {
  setState();
  
  switch (state)
  {
    case 1:
      if (myDisplay.displayAnimate())
      {
        getTime();
        myDisplay.displayText(strTime, PA_CENTER, SCROLL_SPEED, PAUSE_TIME, PA_PRINT);
      }
      break;
    case 2:
      if (myDisplay.displayAnimate())
      {
        getHour();
        myDisplay.displayText(strTime, PA_CENTER, SCROLL_SPEED, PAUSE_TIME, PA_PRINT);
      }
      break;
    case 3:
      if (myDisplay.displayAnimate())
      {
        getMinute();
        myDisplay.displayText(strTime, PA_CENTER, SCROLL_SPEED, PAUSE_TIME, PA_PRINT);
      }
      break;
    case 4:
      if (myDisplay.displayAnimate())
      {
        myDisplay.displayScroll("Stato 4", PA_CENTER, PA_PRINT, 100);
      }
      break;
  }
}