diff --git a/include/Program.h b/include/Program.h index 8f39a37..2c81ba2 100644 --- a/include/Program.h +++ b/include/Program.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "DiscordAPI.h" @@ -44,6 +45,7 @@ private: LedLib* ledLib; WiFiUDP* ntpUDP; NTPClient* timeClient; + Timezone* timezone; /** * @brief Send time to discord diff --git a/platformio.ini b/platformio.ini index 325b3ea..b490b50 100644 --- a/platformio.ini +++ b/platformio.ini @@ -54,6 +54,8 @@ lib_deps = ; erropix/ESP32 AnalogWrite@0.2 adafruit/Adafruit NeoPixel@^1.11.0 arduino-libraries/NTPClient@^3.2.1 + paulstoffregen/Time @ ^1.6.1 + jchristensen/Timezone@^1.2.5 ; Checker settings check_tool = clangtidy, cppcheck diff --git a/src/Program.cpp b/src/Program.cpp index 7f68b52..3ebf40b 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -1,5 +1,7 @@ #include "Program.h" - +#include +#include +#include Program::Program() { @@ -30,7 +32,14 @@ Program::Program() { this->ntpUDP = new WiFiUDP(); this->timeClient = new NTPClient(*this->ntpUDP, "pool.ntp.org", 3600*1);//*2 = gnt+2 - this->timeClient->update(); + if (this->timeClient->update()) { + setTime(this->timeClient->getEpochTime()); + } + + // setup Timezone + TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time + TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time + this->timezone = new Timezone(CEST, CET); // Startup Rotary this->encoder = new SwitchableEncodeur(ENCODER_DT, ENCODER_CLK, ENCODER_SWITCH, 3); @@ -63,24 +72,26 @@ void Program::loop() { break; case MainMenu::GET_START_HOUR:{ // get NTP Hour - //TODO: géré heure d'été et d'hiver - this->timeClient->update(); - int hour = this->timeClient->getHours(); - int min = this->timeClient->getMinutes(); - int day = this->timeClient->getDay(); - if(hour >= 12){ - hour -= 12; + if (this->timeClient->update()) { + setTime(this->timeClient->getEpochTime()); + } + time_t localNow = this->timezone->toLocal(now()); + int _hour = hour(localNow); + int _min = this->timeClient->getMinutes(); + int _day = this->timeClient->getDay(); + if(_hour >= 12){ + _hour -= 12; this->pm = true; }else{ this->pm = false; } - this->nLedStart = hour*2; - if(min >= 30){ + this->nLedStart = _hour*2; + if(_min >= 30){ this->nLedStart++; } this->menu=MainMenu::SELECT_END_HOUR; this->resetMillis = millis(); - if(day == 2 || day == 5){ + if(_day == 2 || _day == 5){ this->encoder->setValue(21-this->nLedStart); }else{ this->encoder->setValue(1);