// https://programmersqtcpp.blogspot.com/2022/04/arduino-lcd-con-sprintf.html
#include <LiquidCrystal.h>
#include "RTClib.h"
#include "MTScrollText.h"
RTC_DS1307 rtc;
DateTime dateTime;
struct RtcDate {
uint8_t dayOfWeek;
uint8_t dayOfMonth;
uint8_t monthOfYear;
uint8_t year;
};
struct RtcTime {
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
};
//RtcDate oldDate = {0};
RtcDate newDate = {0};
//RtcTime oldTime = {0};
RtcTime newTime = {0};
uint8_t gDayOfMonth = 0;
uint8_t gOldDayOfMonth = 0;
uint8_t second = 0;
uint8_t oldSecond = 0;
const char it_ddummy_P[] PROGMEM = "Lun";
const char it_dlun_P[] PROGMEM = "Lun";
const char it_dmar_P[] PROGMEM = "Mar";
const char it_dmer_P[] PROGMEM = "Mer";
const char it_dgio_P[] PROGMEM = "Gio";
const char it_dven_P[] PROGMEM = "Ven";
const char it_dsab_P[] PROGMEM = "Sab";
const char it_ddom_P[] PROGMEM = "Dom";
PGM_P const it_daysOfWeek[8] PROGMEM = {
it_ddummy_P, it_dlun_P, it_dmar_P, it_dmer_P,
it_dgio_P, it_dven_P, it_dsab_P,
it_ddom_P
};
void mday_P(const char *buff, const uint8_t n) {
PGM_P it = (PGM_P)pgm_read_word(&(it_daysOfWeek[n]));
strcpy_P(buff, it);
//Serial.println((char)pgm_read_byte(it));
}
const char it_mgen_P[] PROGMEM = "Gen";
const char it_mfeb_P[] PROGMEM = "Feb";
const char it_mmar_P[] PROGMEM = "Mar";
const char it_mapr_P[] PROGMEM = "Apr";
const char it_mmag_P[] PROGMEM = "Mag";
const char it_mgiu_P[] PROGMEM = "Giu";
const char it_mlug_P[] PROGMEM = "Lug";
const char it_mago_P[] PROGMEM = "Ago";
const char it_mset_P[] PROGMEM = "Set";
const char it_mott_P[] PROGMEM = "Ott";
const char it_mnov_P[] PROGMEM = "Nov";
const char it_mdic_P[] PROGMEM = "Dic";
PGM_P const it_month[] PROGMEM = {
it_mgen_P, it_mfeb_P, it_mmar_P,
it_mapr_P, it_mmag_P, it_mgiu_P,
it_mlug_P, it_mago_P, it_mset_P,
it_mott_P, it_mnov_P, it_mdic_P
};
void month_P(const char *buff, const uint8_t n) {
PGM_P it = (PGM_P)pgm_read_word(&(it_month[n]));
strcpy_P(buff, it);
}
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
uint32_t millisOldTime = 0;
uint8_t speedScroll = 30; // un carattere ogni 30ms
// myScrollD( 20 ) occupa 16 colonne sul display
MTScrollText myScroll( 16 );
/*
"MauroTec Software Lun 31 Gen 2022"
" 00:00:00 WokWi Simulator ";
#define LEN_TEXT 59
char textBuff[LEN_TEXT + 1] = {0};
Invece di contare manualmente il numero di caratteri
di cui è composta la stringa da visualizzare (textBuff)
uso strlen che viene risolto al momento della compilazione
e ricavo in lenText la lughezza per creare textBuff.
Nota che la string literal "MauroTec ... non si trova in
RAM e quindi non ci sono controidicazioni.
*/
#define LEN_TEXT lenText
const int lenText = strlen("MauroTec Software Lun 31 Gen 2022"
" 00:00:00 WokWi Simulator ");
char textBuff[lenText + 1] = {0};
void setup()
{
String s = "s è String";
String s1 = "now s è più lunga di prima";
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
Serial.flush();
abort();
}
lcd.begin(16, 2);
String s2 = s + s1;
// inizializza e stabilisce la lunghezza di textBuff
memset(textBuff, ' ', LEN_TEXT);
myScroll.setText(textBuff);
//printRam();
millisOldTime = millis();
} // end void setup()
const char *dateToStr(const char *dateBuff) {
uint8_t n_dayOfTheMonth = dateTime.day();
uint8_t n_day = dateTime.dayOfTheWeek();
uint8_t n_month = dateTime.month() - 1;
uint16_t n_year = dateTime.year();
char strMonth[4] = {0};
month_P(strMonth, n_month);
char strDay[4] = {0};
// mistero: sel al posto di n_day scrivo 6
// strDay vale "Dom". Con la costante 6 lavora
// con la variabile n_day = 6 non lavora.
// Bug di wokwi???
mday_P(strDay, n_day);
//Serial.println(strDay);
// 17+'\0' char 1+3+1+2+1+3+1+4+1
sprintf(dateBuff, "%s %02u %s %u"
, strDay
, n_dayOfTheMonth
, strMonth
, n_year
);
return dateBuff;
}
const char *timeToStr(const char *buff) {
sprintf(buff, "%02u:%02u:%02u"
, dateTime.hour()
, dateTime.minute()
, dateTime.second()
);
return buff;
}
void loadRtcDate(RtcDate &d) {
d.dayOfWeek = dateTime.dayOfTheWeek();
d.dayOfMonth = dateTime.day();
d.monthOfYear = dateTime.month();
d.year = dateTime.year();
}
void loadRtcTime(RtcTime &t) {
t.hours = dateTime.hour();
t.minutes = dateTime.minute();
t.seconds = dateTime.second();
}
char dateBuff[16] = {0};
char timeBuff[9] = {0};
void loop() {
dateTime = rtc.now();
second = dateTime.second();
gDayOfMonth = dateTime.month();
loadRtcDate(newDate);
if (gDayOfMonth != gOldDayOfMonth) {
gOldDayOfMonth = gDayOfMonth;
dateToStr(dateBuff);
}
loadRtcTime(newTime);
if (second != oldSecond) {
oldSecond = second;
timeToStr(timeBuff);
}
sprintf(textBuff, "%s %s %s %s ",
"MauroTec Software",
dateBuff,
timeBuff,
"WokWi Simulator"
);
if (millis() - millisOldTime >= speedScroll) {
millisOldTime = millis();
char *scrollBuff = myScroll.doScroll();
lcd.setCursor(0, 1);
lcd.print(scrollBuff);
}
} // end void loop()