/*

https://github.com/Makuna/Rtc
Arduino Real Time Clock library.

https://github.com/avishorp/TM1637
Arduino library for TM1637 (LED Driver)

*/

#include <ThreeWire.h>
#include <RtcDS1302.h>
#include "TM1637Display.h"

#define CLK1 9
#define DIO1 10
#define CLK2 11
#define DIO2 12
#define countof(a) (sizeof(a) / sizeof(a[0]))

ThreeWire myWire(6,7,5); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);

uint8_t prev_second;
const char data[] = "Hello World";

void setup () 
{
    Serial.begin(115200);
    Rtc.Begin();
    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    Rtc.SetDateTime(compiled);
    display1.setBrightness(7);
    display2.setBrightness(7);

    for (int i = 0; i < 11; ++i)
    {
        Rtc.SetMemory(i, data[i]);
    }
}

void loop () 
{
    RtcDateTime dt = Rtc.GetDateTime();

    if (dt.Second() != prev_second)
    {
        prev_second = dt.Second();
        uint8_t ram[DS1302RamSize];

        for (int i = 0; i < 11; ++i)
        {
            ram[i] = Rtc.GetMemory(i);
        }
        
        Serial.print((const char*)ram);
        Serial.print(' ');
        printDateTime(dt);
    }

    display1.showNumberDec(dt.Hour(), 0x80, 2, 2);
    display2.showNumberDecEx(dt.Minute(), 0x80, 2, 2);
    display2.showNumberDecEx(dt.Second(), 0x80, true, 2, 2);

    delay(100);
}

void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second());
    Serial.print(datestring);
    Serial.println();
}
4-Digit Display
DS1302Breakout
4-Digit Display