/*
Use of MAX72XX, DS1307 and DTH22 components to
print some information on the display.
for more examples:
https://github.com/MajicDesigns/MD_Parola/tree/main/examples
https://github.com/MajicDesigns/MD_MAX72XX/tree/main/examples
*/
// Header file includes
#include <TimeLib.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Wire.h>
#include "Font7Seg.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 the number of displays connected
#define CLK_PIN 13 // CLK or SCK
#define DATA_PIN 11 // DATA or MOSI
#define CS_PIN 10 // CS or SS
#define SPEED_TIME 75 // Speed of the transition
#define PAUSE_TIME 0
#define MAX_MESG 20
// These are for the clock
#define DS1307_ADDRESS 0x68
// These are for the temperature
#
// Global variables
uint8_t wday, mday, monthL, yearL;
uint8_t hours, minutes, seconds;
char szTime[9]; // mm:ss\0
char szMesg[MAX_MESG + 1] = "";
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
auto md_max = P.getGraphicObject();
uint8_t clear = 0x00;
void beginDS1307()
{
// Read the values (date and time) of the DS1307 module
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(clear);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 0x07);
seconds = bcdToDec(Wire.read());
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0xff);
wday = bcdToDec(Wire.read());
mday = bcdToDec(Wire.read());
monthL = bcdToDec(Wire.read());
yearL = bcdToDec(Wire.read());
}
uint8_t decToBcd(uint8_t value)
{
return ((value / 10 * 16) + (value % 10));
}
uint8_t bcdToDec(uint8_t value)
{
return ((value / 16 * 10) + (value % 16));
}
// Code for reading clock time
void getTime(char *psz, bool f = true)
{
sprintf(psz, "%02d%c%02d", hours, (f ? ':' : ' '), minutes);
}
void setup(void)
{
Wire.begin();
Serial.begin(9600);
P.begin(2);
P.setInvert(false);
P.setZone(0, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.setZone(1, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.displayZoneText(1, szTime, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
P.displayZoneText(0, szMesg, PA_CENTER, SPEED_TIME, 0, PA_PRINT, PA_NO_EFFECT);
}
void loop(void)
{
static uint32_t lastTime = 0; // Memory (ms)
static uint32_t lastDSUpdate = 0; // Memory (ms)
static bool flasher = false; // Seconds passing flasher
delay(1);
if (!lastDSUpdate || (millis() - lastDSUpdate) > 500) {
lastDSUpdate = millis();
beginDS1307();
setTime(hours, minutes, seconds, mday, monthL, yearL);
}
else hours = hour(), minutes = minute(), seconds = second();
if (millis() - lastTime <= 1000)return;
getTime(szMesg, flasher);
if (P.displayAnimate())
//if (P.getZoneStatus(0))
{
//Serial.println(seconds);
P.setFont(0, numeric7Seg);
P.setTextEffect(0, PA_PRINT, PA_NO_EFFECT);
// P.setPause(0, 0);
lastTime = millis();
//getTime(szMesg, flasher);
//if (P.isAnimationAdvanced()) {
seconds <= 30 ? md_max->setPoint(7, seconds == 0 ? 1 : seconds, true) :
md_max->setPoint(7, seconds == 31 ? 30 : 61 - seconds, true);
//}
flasher = !flasher;
P.displayReset(0);
}
}