// Include the required Arduino libraries:
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.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
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define PAUSE_TIME    1000
#define SCROLL_SPEED  50
#define CLK_PIN 10
#define CS_PIN  9
#define DATA_PIN 8
// HARDWARE SPI
//MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// create RTC_DS1307 instance
RTC_DS1307 rtc;
// Variables to store date characters to be printed
char strDate[MAX_DEVICES] = "    ";
String dateLine = String();
// Variables to store time characters to be printed
char strTime[MAX_DEVICES] = "    ";
String timeLine = String();
// Boolean for RTC state
boolean check = false;
void setup() {
  // start serial comunication
  Serial.begin(115200);
  // start I2C communication
  /*while(!check)
    if (rtc.begin())
      check = true;
    else Serial.println("begin....");
  // set initial Date-Time
  check = false;
  while(!check){
    if (rtc.isrunning()) {
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
      check = true;
    }
    else Serial.println("isrunning....");
    delay(500);
  }*/
 
  // Intialize the object:
  myDisplay.begin();
  // Set the intensity (brightness) of the display (0-15):
  myDisplay.setIntensity(0);
  // Clear the display:
  myDisplay.displayClear();
}
void loop() {
  Wire.begin();
  rtc.begin();
  // read value from RTC module
  DateTime dateTime = rtc.now();
  dateLine = dateTime.toString("DD/MM/YY");
  dateLine.toCharArray(strDate, MAX_DEVICES + 2);
  timeLine = dateTime.toString("hh:mm");
  timeLine.toCharArray(strTime, MAX_DEVICES + 2);
  timeLine = sprintf("%d:%d", dateTime.hour(), dateTime.minute());
  char *msg[] =
  {
    strDate,
    strTime,
  };
  static uint8_t cycle = 0;
  if (myDisplay.displayAnimate())
  {
    //myDisplay.displayClear();
    // set up the string
    myDisplay.displayText(strTime/*msg[cycle]*/, PA_CENTER, SCROLL_SPEED, PAUSE_TIME, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
    // prepare for next pass
    cycle = (cycle + 1) % ARRAY_SIZE(msg);
  }
}