Ho risolto in parte il problema dell'anno (non so neanche io come ed il perchè ho dovuto mettere quelle strane sottrazioni di anni: tmpYear = year() - 1999; e tm.Year = tmpYear - 2018;) ma almeno ora mi memorizza l'anno da me scelto in modo corretto.
Ho però ancora un problema quando l'anno scende sotto il 2010 che sul display scrive cose strane (cioè ha problemi con lo zero) e soprattutto supera il fatidico 2065; Non riesco quindi a limitare di selezionare un range "sensato" in modo da impedire all'encoder di scendere sotto un certo anno (che sarebbe il 1970, giusto?) e sopra il 2065, per non far casini con il salvataggio.
Questo è il nuovo codice:
void SetDay() // Modifico il giorno
{
//Serial.println ("MODIFICO IL GIORNO");
if (LCDML_BUTTON_checkUp()) // incremento i minuti
{
tmpDay = tmpDay < 31 ? tmpDay + 1 : tmpDay;
lcd.setCursor(x, y);
if ( tmpDay < 10 ) lcd.print("0");
lcd.print(tmpDay);
LCDML_BUTTON_resetUp();
}
else if (LCDML_BUTTON_checkDown()) // decremento i minuti
{
tmpDay = tmpDay > 1 ? tmpDay - 1 : tmpDay;
lcd.setCursor(x, y);
if (tmpDay < 10 ) lcd.print("0");
lcd.print(tmpDay);
LCDML_BUTTON_resetDown();
}
else if (LCDML_BUTTON_checkEnter()) // termino
scelta = 1;
LCDML_BUTTON_resetEnter();
}
void SetMonth() // Modifico il mese
{
// Serial.println ("MODIFICO IL MESE");
if (LCDML_BUTTON_checkUp()) // incremento i minuti
{
tmpMonth = tmpMonth < 12 ? tmpMonth + 1 : tmpMonth;
lcd.setCursor(x, y);
if ( tmpMonth < 10 ) lcd.print("0");
lcd.print(tmpMonth);
LCDML_BUTTON_resetUp();
}
else if (LCDML_BUTTON_checkDown()) // decremento i minuti
{
tmpMonth = tmpMonth > 1 ? tmpMonth - 1 : tmpMonth;
lcd.setCursor(x, y);
if ( tmpMonth < 10 ) lcd.print("0");
lcd.print(tmpMonth);
LCDML_BUTTON_resetDown();
}
else if (LCDML_BUTTON_checkEnter()) // termino
scelta = 2;
LCDML_BUTTON_resetEnter();
}
void SetYear() // Modifico l'anno
{
if (LCDML_BUTTON_checkUp()) // incremento i minuti
{
//tmpYear = tmpYear < tmpYear-2018 ? tmpYear + 1 : tmpYear;
tmpYear++;
lcd.setCursor(x, y);
//if ( tmpYear < 10 ) lcd.print("0");
lcd.print(tmpYear);
LCDML_BUTTON_resetUp();
}
else if (LCDML_BUTTON_checkDown()) // decremento i minuti
{
// tmpYear = tmpYear > 0 ? tmpYear - 1 : tmpYear;
tmpYear = tmpYear - 1;
lcd.setCursor(x, y);
//if ( tmpYear < 10 ) lcd.print("0");
lcd.print(tmpYear);
LCDML_BUTTON_resetDown();
}
else if (LCDML_BUTTON_checkEnter()) // termino
scelta = 3;
LCDML_BUTTON_resetEnter();
}
void SaveDateTime() // Memorizzo Data ed ora
{
tm.Hour = tmpHours; //set the tm structure to 23h31m30s on 13Feb2009
tm.Minute = tmpMinutes;
tm.Second = 0;
tm.Day = tmpDay;
tm.Month = tmpMonth;
tm.Year = tmpYear - 2018;
SyncRTC(); // Sincronizzo il RTC per recuperare la data corretta
RTC.write(tm); //set the RTC from the tm structure
Serial.println("MEMORIZZO ED ESCO");
SyncRTC(); // Sincronizzo il RTC per aggiornare ora e data sul display
}
void SyncRTC() {
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Errore sincronizzazione con RTC");
else
Serial.println("Orario sincronizzato con RTC");
digitalClockDisplay(); // Stampo l'ora corrente sul monitor seriale
}
// *********************************************************************
uint8_t g_func_timer_set_date = 0; // time counter (global variable)
unsigned long g_timer_Date = 0; // timer variable (globale variable)
void LCDML_DISP_setup(LCDML_FUNC_set_date)
// *********************************************************************
{
// setup function
lcd.setCursor(0, 1);
lcd.print(F("Esco tra sec.")); // print some content on first row
g_func_timer_set_date = 60; // reset and set timer
LCDML_DISP_triggerMenu(100); // set trigger for this function to 100 millisecounds
tmpDay = day();
tmpMonth = month();
tmpYear = year() - 1999;
x = 3; y = 0;
Show_Short_Day();
}
void LCDML_DISP_loop(LCDML_FUNC_set_date)
{
// loop function, can be run in a loop when LCDML_DISP_triggerMenu(xx) is set
// the quit button works in every DISP function without any checks; it starts the loop_end function
g_lcdml_initscreen = millis(); // reset initscreen timer
tmElements_t tm;
switch (scelta) {
case 0:
x = 3; y = 0;
SetDay(); // Modifico il giorno
break;
case 1:
x = 6; y = 0;
SetMonth(); // Modifico il mese
break;
case 2:
x = 11; y = 0;
SetYear(); // Modifico l'anno
break;
case 3:
SaveDateTime(); // Memorizzo Data ed ora ora corretta
scelta = 0;
stato =0;
// end function for callback
LCDML_DISP_funcend();
break;
}
// this timer checks every 1000 millisecounds if it is called
if ((millis() - g_timer_Date) >= 1000) {
g_timer_Date = millis();
g_func_timer_set_date--; // increment the value every secound
lcd.setCursor(9, 1);
if ( g_func_timer_set_date < 10 ) lcd.print("00");
// set cursor pos
lcd.print(g_func_timer_set_date); // print the time counter value
}
// reset the initscreen timer
LCDML_DISP_resetIsTimer();
// this function can only be ended when quit button is pressed or the time is over
// check if the function ends normaly
if (g_func_timer_set_date <= 0)
{
// end function for callback
LCDML_DISP_funcend();
}
}
void LCDML_DISP_loop_end(LCDML_FUNC_set_date)
{
// this functions is ever called when a DISP function is quit
// you can here reset some global vars or do nothing
LCDML.goRoot(); // go to root element (first element of this menu with id=0)
}