From f402d31be58553c78fbb371e1353e2e02ef4926e Mon Sep 17 00:00:00 2001 From: Markus Ransberger Date: Sun, 3 Sep 2023 14:54:14 +0200 Subject: [PATCH] NTPClientPlus rework, minor improvements. --- include/ntp_client_plus.h | 90 +++--- src/connectivity/ntp_client_plus.cpp | 397 ++++++++++++--------------- src/wordclock_esp8266.cpp | 47 ++-- 3 files changed, 247 insertions(+), 287 deletions(-) diff --git a/include/ntp_client_plus.h b/include/ntp_client_plus.h index e5e81d7..b60486e 100644 --- a/include/ntp_client_plus.h +++ b/include/ntp_client_plus.h @@ -16,7 +16,7 @@ typedef enum NTP_UPDATE_SUCCESS = 0, NTP_UPDATE_DIFFTOOHIGH = 1, NTP_UPDATE_TIME_INVALID = 2 -} ntp_return_values; +} NtpReturnValue; /** * @brief Own NTP Client library for Arduino with code from: @@ -27,64 +27,64 @@ typedef enum class NTPClientPlus { public: - NTPClientPlus(UDP &udp, const char *poolServerName, int utcx, bool _swChange); - void setupNTPClient(); - int updateNTP(); + NTPClientPlus(UDP &udp, const char *pool_server_name, int utcx, bool sw_change); + bool is_leap_year(unsigned int year); + bool update_sw_change(); + int get_hours_12() const; + int get_hours_24() const; + int get_minutes() const; + int get_month(int dayOfYear); + int get_seconds() const; + int update_ntp(); + long get_time_offset(); + String get_formatted_date(); + String get_formatted_time() const; + unsigned int get_day_of_week(); + unsigned int get_year(); + unsigned long get_epoch_time() const; + unsigned long get_secs_since_1900() const; + void calc_date(); void end(); - void setTimeOffset(int timeOffset); - void setPoolServerName(const char *poolServerName); - unsigned long getSecsSince1900() const; - unsigned long getEpochTime() const; - int getHours24() const; - int getHours12() const; - int getMinutes() const; - int getSeconds() const; - String getFormattedTime() const; - String getFormattedDate(); - void calcDate(); - unsigned int getDayOfWeek(); - unsigned int getYear(); - bool isLeapYear(unsigned int year); - int getMonth(int dayOfYear); - long getTimeOffset(); - bool updateSWChange(); + void set_pool_server_name(const char *pool_server_name); + void set_time_offset(int time_offset); + void setup_ntp_client(); private: UDP *_udp; - bool _udpSetup = false; + bool _udp_setup = false; - bool _swChange = 1; - const char *_poolServerName = "pool.ntp.org"; // Default time server + bool _sw_change = 1; + const char *_pool_server_name = "pool.ntp.org"; // Default time server int _utcx = 0; - IPAddress _poolServerIP; - long _timeOffset = 0; + IPAddress _pool_server_ip; + long _time_offset = 0; unsigned int _port = NTP_DEFAULT_LOCAL_PORT; - unsigned long _updateInterval = 60000; // In ms + unsigned long _update_interval = 60000; // In ms - unsigned long _currentEpoc = 0; // In s - unsigned long _lastUpdate = 0; // In ms - unsigned long _secsSince1900 = 0; // seconds since 1. Januar 1900, 00:00:00 - unsigned long _lastSecsSince1900 = 0; - unsigned int _dateYear = 0; - unsigned int _dateMonth = 0; - unsigned int _dateDay = 0; - unsigned int _dayOfWeek = 0; + unsigned int _date_day = 0; + unsigned int _date_month = 0; + unsigned int _date_year = 0; + unsigned int _day_of_week = 0; + unsigned long _current_epoc = 0; // In s + unsigned long _last_secs_since_1900 = 0; + unsigned long _last_update = 0; // In ms + unsigned long _secs_since_1900 = 0; // seconds since 1. Januar 1900, 00:00:00 - unsigned char _packetBuffer[NTP_PACKET_SIZE] = {0}; - void sendNTPPacket(); - void setSummertime(bool summertime); + unsigned char _packet_buffer[NTP_PACKET_SIZE] = {0}; + void send_ntp_packet(); + void set_summertime(bool summertime); - static const unsigned long secondperday = 86400; - static const unsigned long secondperhour = 3600; - static const unsigned long secondperminute = 60; - static const unsigned long minuteperhour = 60; - static const unsigned long millisecondpersecond = 1000; + static const unsigned long milliseconds_per_second = 1000; + static const unsigned long minutes_per_hour = 60; + static const unsigned long seconds_per_day = 86400; + static const unsigned long seconds_per_hour = 3600; + static const unsigned long seconds_per_minute = 60; // number of days in months - unsigned int daysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + unsigned int _days_per_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; }; -void wait(unsigned long time); +void wait(unsigned long time_ms); #endif /* NTPCLIENTPLUS_H */ diff --git a/src/connectivity/ntp_client_plus.cpp b/src/connectivity/ntp_client_plus.cpp index 6df0928..a0632ae 100644 --- a/src/connectivity/ntp_client_plus.cpp +++ b/src/connectivity/ntp_client_plus.cpp @@ -5,29 +5,29 @@ * @brief Construct a new NTPClientPlus::NTPClientPlus object * * @param udp UDP client - * @param poolServerName time server name + * @param pool_server_name time server name * @param utcx UTC offset (in 1h) - * @param _swChange should summer/winter time be considered + * @param sw_change should summer/winter time be considered */ -NTPClientPlus::NTPClientPlus(UDP &udp, const char *poolServerName, int utcx, bool _swChange) +NTPClientPlus::NTPClientPlus(UDP &udp, const char *pool_server_name, int utcx, bool sw_change) { this->_udp = &udp; this->_utcx = utcx; - this->_timeOffset = this->secondperhour * this->_utcx; - this->_poolServerName = poolServerName; - this->_swChange = _swChange; + this->_time_offset = this->seconds_per_hour * this->_utcx; + this->_pool_server_name = pool_server_name; + this->_sw_change = sw_change; } /** * @brief Starts the underlying UDP client, get first NTP timestamp and calc date * */ -void NTPClientPlus::setupNTPClient() +void NTPClientPlus::setup_ntp_client() { this->_udp->begin(this->_port); - this->_udpSetup = true; - this->updateNTP(); - this->calcDate(); + this->_udp_setup = true; + this->update_ntp(); + this->calc_date(); } /** @@ -38,16 +38,15 @@ void NTPClientPlus::setupNTPClient() * @return NTP_UPDATE_DIFFTOOHIGH too much difference to previous received time (try again) * @return NTP_UPDATE_TIME_INVALID time value is invalid */ -int NTPClientPlus::updateNTP() +int NTPClientPlus::update_ntp() { - // flush any existing packets while (this->_udp->parsePacket() != 0) { this->_udp->flush(); } - this->sendNTPPacket(); + this->send_ntp_packet(); // Wait till data is there or timeout... uint8_t conn_tries = 0; @@ -64,35 +63,35 @@ int NTPClientPlus::updateNTP() return NTP_UPDATE_TIMEOUT; } - this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); + this->_udp->read(this->_packet_buffer, NTP_PACKET_SIZE); - unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); - unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); + unsigned long high_word = word(this->_packet_buffer[40], this->_packet_buffer[41]); + unsigned long low_word = word(this->_packet_buffer[42], this->_packet_buffer[43]); // combine the four bytes (two words) into a long integer // this is NTP time (seconds since Jan 1 1900): - unsigned long tempSecsSince1900 = highWord << 16 | lowWord; + unsigned long temp_secs_since_1900 = high_word << 16 | low_word; - if (tempSecsSince1900 < UNIX_TIMESTAMP_1900) // NTP time is not valid + if (temp_secs_since_1900 < UNIX_TIMESTAMP_1900) // NTP time is not valid { return NTP_UPDATE_TIME_INVALID; } // check if time off last ntp update is roughly in the same range: 100sec apart (validation check) - if (this->_lastSecsSince1900 == 0 || tempSecsSince1900 - this->_lastSecsSince1900 < 100000) + if (this->_last_secs_since_1900 == 0 || temp_secs_since_1900 - this->_last_secs_since_1900 < 100000) { // Only update time then account for delay in reading the time - this->_lastUpdate = (system_get_time() / 1000) - (NTP_RECEIVE_WAIT_TIME_MS * (conn_tries + 1)); - this->_secsSince1900 = tempSecsSince1900; - this->_currentEpoc = this->_secsSince1900 - UNIX_TIMESTAMP_1900; + this->_last_update = (system_get_time() / 1000) - (NTP_RECEIVE_WAIT_TIME_MS * (conn_tries + 1)); + this->_secs_since_1900 = temp_secs_since_1900; + this->_current_epoc = this->_secs_since_1900 - UNIX_TIMESTAMP_1900; // Remember time of last update - this->_lastSecsSince1900 = tempSecsSince1900; + this->_last_secs_since_1900 = temp_secs_since_1900; return NTP_UPDATE_SUCCESS; // return 0 after successful update } else { // Remember time of last update - this->_lastSecsSince1900 = tempSecsSince1900; + this->_last_secs_since_1900 = temp_secs_since_1900; return NTP_UPDATE_DIFFTOOHIGH; } } @@ -104,32 +103,32 @@ int NTPClientPlus::updateNTP() void NTPClientPlus::end() { this->_udp->stop(); - this->_udpSetup = false; + this->_udp_setup = false; } /** * @brief Setter TimeOffset * - * @param timeOffset offset from UTC in seconds + * @param time_offset offset from UTC in seconds */ -void NTPClientPlus::setTimeOffset(int timeOffset) +void NTPClientPlus::set_time_offset(int time_offset) { - this->_timeOffset = timeOffset; + this->_time_offset = time_offset; } -long NTPClientPlus::getTimeOffset() +long NTPClientPlus::get_time_offset() { - return this->_timeOffset; + return this->_time_offset; } /** * @brief Set time server name * - * @param poolServerName + * @param pool_server_name */ -void NTPClientPlus::setPoolServerName(const char *poolServerName) +void NTPClientPlus::set_pool_server_name(const char *pool_server_name) { - this->_poolServerName = poolServerName; + this->_pool_server_name = pool_server_name; } /** @@ -137,11 +136,11 @@ void NTPClientPlus::setPoolServerName(const char *poolServerName) * * @return unsigned long seconds since 1. Jan. 1900 */ -unsigned long NTPClientPlus::getSecsSince1900() const +unsigned long NTPClientPlus::get_secs_since_1900() const { - return this->_timeOffset + // User offset - this->_secsSince1900 + // seconds returned by the NTP server - (((system_get_time() / 1000) - this->_lastUpdate) / 1000); // Time since last update + return this->_time_offset + // User offset + this->_secs_since_1900 + // seconds returned by the NTP server + (((system_get_time() / 1000) - this->_last_update) / 1000); // Time since last update } /** @@ -149,9 +148,9 @@ unsigned long NTPClientPlus::getSecsSince1900() const * * @return unsigned long UNIX Epoch time since 1. Jan. 1970 in seconds */ -unsigned long NTPClientPlus::getEpochTime() const +unsigned long NTPClientPlus::get_epoch_time() const { - return this->getSecsSince1900() - UNIX_TIMESTAMP_1900; + return this->get_secs_since_1900() - UNIX_TIMESTAMP_1900; } /** @@ -159,9 +158,9 @@ unsigned long NTPClientPlus::getEpochTime() const * * @return int */ -int NTPClientPlus::getHours24() const +int NTPClientPlus::get_hours_24() const { - int hours = ((this->getEpochTime() % 86400L) / 3600); + int hours = ((this->get_epoch_time() % 86400L) / 3600); return hours; } @@ -170,14 +169,9 @@ int NTPClientPlus::getHours24() const * * @return int */ -int NTPClientPlus::getHours12() const +int NTPClientPlus::get_hours_12() const { - int hours = this->getHours24(); - if (hours >= 12) - { - hours = hours - 12; - } - return hours; + return this->get_hours_24() % 12; } /** @@ -185,9 +179,9 @@ int NTPClientPlus::getHours12() const * * @return int */ -int NTPClientPlus::getMinutes() const +int NTPClientPlus::get_minutes() const { - return ((this->getEpochTime() % 3600) / 60); + return ((this->get_epoch_time() % 3600) / 60); } /** @@ -195,9 +189,9 @@ int NTPClientPlus::getMinutes() const * * @return int */ -int NTPClientPlus::getSeconds() const +int NTPClientPlus::get_seconds() const { - return (this->getEpochTime() % 60); + return this->get_epoch_time() % 60; } /** @@ -205,19 +199,19 @@ int NTPClientPlus::getSeconds() const * * @return String time formatted like `hh:mm:ss` */ -String NTPClientPlus::getFormattedTime() const +String NTPClientPlus::get_formatted_time() const { - unsigned long rawTime = this->getEpochTime(); - unsigned long hours = (rawTime % 86400L) / 3600; - String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); + unsigned long raw_time = this->get_epoch_time(); + unsigned long hours = (raw_time % 86400L) / 3600; + String hours_str = hours < 10 ? "0" + String(hours) : String(hours); - unsigned long minutes = (rawTime % 3600) / 60; - String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); + unsigned long minutes = (raw_time % 3600) / 60; + String minute_str = minutes < 10 ? "0" + String(minutes) : String(minutes); - unsigned long seconds = rawTime % 60; - String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); + unsigned long seconds = raw_time % 60; + String second_str = seconds < 10 ? "0" + String(seconds) : String(seconds); - return hoursStr + ":" + minuteStr + ":" + secondStr; + return hours_str + ":" + minute_str + ":" + second_str; } /** @@ -225,12 +219,12 @@ String NTPClientPlus::getFormattedTime() const * * @return String date formatted like `dd.mm.yyyy` */ -String NTPClientPlus::getFormattedDate() +String NTPClientPlus::get_formatted_date() { - this->calcDate(); - unsigned int dateDay = this->_dateDay; - unsigned int dateMonth = this->_dateMonth; - unsigned int dateYear = this->_dateYear; + this->calc_date(); + unsigned int dateDay = this->_date_day; + unsigned int dateMonth = this->_date_month; + unsigned int dateYear = this->_date_year; String dayStr = dateDay < 10 ? "0" + String(dateDay) : String(dateDay); String monthStr = dateMonth < 10 ? "0" + String(dateMonth) : String(dateMonth); @@ -243,66 +237,64 @@ String NTPClientPlus::getFormattedDate() * @brief Calc date from seconds since 1900 * */ -void NTPClientPlus::calcDate() +void NTPClientPlus::calc_date() { - // Start: Calc date - // get days since 1900 - unsigned long days1900 = this->getSecsSince1900() / secondperday; + unsigned long days1900 = this->get_secs_since_1900() / seconds_per_day; // calc current year - this->_dateYear = this->getYear(); + this->_date_year = this->get_year(); // calc how many leap days since 1.Jan 1900 - int leapDays = 0; - for (unsigned int i = 1900; i < this->_dateYear; i++) + int leap_days = 0; + for (unsigned int i = 1900; i < this->_date_year; i++) { // check if leap year - if (this->isLeapYear(i)) + if (this->is_leap_year(i)) { - leapDays++; + leap_days++; } } - leapDays = leapDays - 1; + leap_days = leap_days - 1; // check if current year is leap year - if (this->isLeapYear(this->_dateYear)) + if (this->is_leap_year(this->_date_year)) { - daysInMonth[2] = 29; + _days_per_month[2] = 29; } else { - daysInMonth[2] = 28; + _days_per_month[2] = 28; } - unsigned int dayOfYear = (days1900 - ((this->_dateYear - 1900) * 365) - leapDays); + unsigned int day_of_year = (days1900 - ((this->_date_year - 1900) * 365) - leap_days); // calc current month - this->_dateMonth = this->getMonth(dayOfYear); + this->_date_month = this->get_month(day_of_year); - this->_dateDay = 0; + this->_date_day = 0; // calc day of month - for (unsigned int i = 0; i < this->_dateMonth; i++) + for (unsigned int i = 0; i < this->_date_month; i++) { - this->_dateDay = this->_dateDay + daysInMonth[i]; + this->_date_day = this->_date_day + _days_per_month[i]; } - this->_dateDay = dayOfYear - this->_dateDay; + this->_date_day = day_of_year - this->_date_day; // calc day of week: // Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, Sunday = 7 // 1. Januar 1900 was a monday - this->_dayOfWeek = 1; + this->_day_of_week = 1; for (unsigned int i = 0; i < days1900; i++) { - if (this->_dayOfWeek < 7) + if (this->_day_of_week < 7) { - this->_dayOfWeek = this->_dayOfWeek + 1; + this->_day_of_week = this->_day_of_week + 1; } else { - this->_dayOfWeek = 1; + this->_day_of_week = 1; } } @@ -310,7 +302,7 @@ void NTPClientPlus::calcDate() // calc if summer time active - this->updateSWChange(); + this->update_sw_change(); } /** @@ -318,9 +310,9 @@ void NTPClientPlus::calcDate() * * @return unsigned int */ -unsigned int NTPClientPlus::getDayOfWeek() +unsigned int NTPClientPlus::get_day_of_week() { - return this->_dayOfWeek; + return this->_day_of_week; } /** @@ -328,40 +320,36 @@ unsigned int NTPClientPlus::getDayOfWeek() * * @return unsigned int */ -unsigned int NTPClientPlus::getYear() +unsigned int NTPClientPlus::get_year() { - - unsigned long sec1900 = this->getSecsSince1900(); + unsigned long secs_since_1900 = this->get_secs_since_1900(); // NTP starts at 1. Jan 1900 unsigned int result = 1900; - unsigned int dayInYear = 0; + unsigned int days_in_year = 0; unsigned int days = 0; - unsigned int days1900 = 0; + unsigned int days_since_1900 = 0; unsigned int for_i = 0; - bool leapYear = LOW; + bool leap_year = false; - days1900 = sec1900 / this->secondperday; + days_since_1900 = secs_since_1900 / this->seconds_per_day; - for (for_i = 0; for_i < days1900; for_i++) + for (for_i = 0; for_i < days_since_1900; for_i++) { + leap_year = this->is_leap_year(result); - leapYear = this->isLeapYear(result); - - if (leapYear) + if (leap_year) { - dayInYear = 366; + days_in_year = 366; } - else { - dayInYear = 365; + days_in_year = 365; } - days++; - if (days >= dayInYear) + if (days >= days_in_year) { result++; days = 0; @@ -378,33 +366,29 @@ unsigned int NTPClientPlus::getYear() * @return true * @return false */ -bool NTPClientPlus::isLeapYear(unsigned int year) +bool NTPClientPlus::is_leap_year(unsigned int year) { - bool result = LOW; + bool result = false; // check for leap year if ((year % 4) == 0) { - - result = HIGH; + result = true; if ((year % 100) == 0) { - - result = LOW; + result = false; if ((year % 400) == 0) { - - result = HIGH; + result = true; } } } - else { - result = LOW; + result = false; } return result; @@ -413,106 +397,102 @@ bool NTPClientPlus::isLeapYear(unsigned int year) /** * @brief Get Month of given day of year * - * @param dayOfYear + * @param day_of_year * @return int */ -int NTPClientPlus::getMonth(int dayOfYear) +int NTPClientPlus::get_month(int day_of_year) { - - bool leapYear = this->isLeapYear(this->getYear()); + bool leap_year = this->is_leap_year(this->get_year()); // Month beginnings - int monthMin[13] = {0, 1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; + int month_min[13] = {0, 1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; // Month endings - int monthMax[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; - + int month_max[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; int month = 0; - int y = 0; // Calculation of the beginning and end of each month in the leap year - if (leapYear == HIGH) + if (leap_year == true) { - for (y = 3; y < 13; y++) { - monthMin[y] = monthMin[y] + 1; + ++month_min[y]; } for (y = 2; y < 13; y++) { - monthMax[y] = monthMax[y] + 1; + ++month_max[y]; } } // January - if (dayOfYear >= monthMin[1] && dayOfYear <= monthMax[1]) + if (day_of_year >= month_min[1] && day_of_year <= month_max[1]) { month = 1; } // February - if (dayOfYear >= monthMin[2] && dayOfYear <= monthMax[2]) + if (day_of_year >= month_min[2] && day_of_year <= month_max[2]) { month = 2; } // March - if (dayOfYear >= monthMin[3] && dayOfYear <= monthMax[3]) + if (day_of_year >= month_min[3] && day_of_year <= month_max[3]) { month = 3; } // April - if (dayOfYear >= monthMin[4] && dayOfYear <= monthMax[4]) + if (day_of_year >= month_min[4] && day_of_year <= month_max[4]) { month = 4; } // May - if (dayOfYear >= monthMin[5] && dayOfYear <= monthMax[5]) + if (day_of_year >= month_min[5] && day_of_year <= month_max[5]) { month = 5; } // June - if (dayOfYear >= monthMin[6] && dayOfYear <= monthMax[6]) + if (day_of_year >= month_min[6] && day_of_year <= month_max[6]) { month = 6; } // July - if (dayOfYear >= monthMin[7] && dayOfYear <= monthMax[7]) + if (day_of_year >= month_min[7] && day_of_year <= month_max[7]) { month = 7; } // August - if (dayOfYear >= monthMin[8] && dayOfYear <= monthMax[8]) + if (day_of_year >= month_min[8] && day_of_year <= month_max[8]) { month = 8; } // September - if (dayOfYear >= monthMin[9] && dayOfYear <= monthMax[9]) + if (day_of_year >= month_min[9] && day_of_year <= month_max[9]) { month = 9; } // October - if (dayOfYear >= monthMin[10] && dayOfYear <= monthMax[10]) + if (day_of_year >= month_min[10] && day_of_year <= month_max[10]) { month = 10; } // November - if (dayOfYear >= monthMin[11] && dayOfYear <= monthMax[11]) + if (day_of_year >= month_min[11] && day_of_year <= month_max[11]) { month = 11; } // December - if (dayOfYear >= monthMin[12] && dayOfYear <= monthMax[12]) + if (day_of_year >= month_min[12] && day_of_year <= month_max[12]) { month = 12; } @@ -524,32 +504,32 @@ int NTPClientPlus::getMonth(int dayOfYear) * @brief (private) Send NTP Packet to NTP server * */ -void NTPClientPlus::sendNTPPacket() +void NTPClientPlus::send_ntp_packet() { // set all bytes in the buffer to 0 - memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); + memset(this->_packet_buffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request - this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode - this->_packetBuffer[1] = 0; // Stratum, or type of clock - this->_packetBuffer[2] = 6; // Polling Interval - this->_packetBuffer[3] = 0xEC; // Peer Clock Precision + this->_packet_buffer[0] = 0b11100011; // LI, Version, Mode + this->_packet_buffer[1] = 0; // Stratum, or type of clock + this->_packet_buffer[2] = 6; // Polling Interval + this->_packet_buffer[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay & Root Dispersion - this->_packetBuffer[12] = 49; - this->_packetBuffer[13] = 0x4E; - this->_packetBuffer[14] = 49; - this->_packetBuffer[15] = 52; + this->_packet_buffer[12] = 49; + this->_packet_buffer[13] = 0x4E; + this->_packet_buffer[14] = 49; + this->_packet_buffer[15] = 52; // all NTP fields have been given values, now // you can send a packet requesting a timestamp: - if (this->_poolServerName) + if (this->_pool_server_name) { - this->_udp->beginPacket(this->_poolServerName, 123); + this->_udp->beginPacket(this->_pool_server_name, 123); } else { - this->_udp->beginPacket(this->_poolServerIP, 123); + this->_udp->beginPacket(this->_pool_server_ip, 123); } - this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); + this->_udp->write(this->_packet_buffer, NTP_PACKET_SIZE); this->_udp->endPacket(); } @@ -558,15 +538,15 @@ void NTPClientPlus::sendNTPPacket() * * @param summertime */ -void NTPClientPlus::setSummertime(bool summertime) +void NTPClientPlus::set_summertime(bool summertime) { if (summertime) { - this->_timeOffset = this->secondperhour * (this->_utcx + 1); + this->_time_offset = this->seconds_per_hour * (this->_utcx + 1); } else { - this->_timeOffset = this->secondperhour * (this->_utcx); + this->_time_offset = this->seconds_per_hour * (this->_utcx); } } @@ -575,63 +555,56 @@ void NTPClientPlus::setSummertime(bool summertime) * * @returns bool summertime active */ -bool NTPClientPlus::updateSWChange() +bool NTPClientPlus::update_sw_change() { - unsigned int dayOfWeek = this->_dayOfWeek; - unsigned int dateDay = this->_dateDay; - unsigned int dateMonth = this->_dateMonth; + unsigned int day_of_week = this->_day_of_week; + unsigned int date_day = this->_date_day; + unsigned int date_month = this->_date_month; - bool summertimeActive = false; + bool summertime_active = false; - if (this->_swChange) + if (this->_sw_change) { // Start: Set summer-/ winter time - // current month is march - if (dateMonth == 3) + if (date_month == 3) { // it is last week in march - if ((this->daysInMonth[3] - dateDay) < 7) + if ((this->_days_per_month[3] - date_day) < 7) { - // Example year 2020: March 31 days; Restart March 26, 2020 (Thursday = weekday = 4); 5 days remaining; Last Sunday March 29, 2020 // Calculation: 31 - 26 = 5; 5 + 4 = 9; - // Result: Last day in March is a Tuesday. There follows another Sunday in October => set winter time + // Result: Last day in March is a Tuesday. There folfalses another Sunday in October => set winter time // Example year 2021: March 31 days; Restart March 30, 2021 (Tuesday = weekday = 2); 1 days remaining; Last Sunday March 28, 2021 // Calculation: 31 - 30 = 1; 1 + 2 = 3; // Result: Last day in March is a Wednesday. Changeover to summer time already done => set summer time - // There follows within the last week in March one more Sunday => set winter time - if (((this->daysInMonth[3] - dateDay) + dayOfWeek) >= 7) + // There folfalses within the last week in March one more Sunday => set winter time + if (((this->_days_per_month[3] - date_day) + day_of_week) >= 7) { - this->setSummertime(0); - summertimeActive = false; + this->set_summertime(0); + summertime_active = false; } - - // last sunday in march already over -> summer time - else + else // last sunday in march already over -> summer time { - this->setSummertime(1); - summertimeActive = true; + this->set_summertime(1); + summertime_active = true; } } - - // restart in first three weeks of march -> winter time - else + else // restart in first three weeks of march -> winter time { - this->setSummertime(0); - summertimeActive = false; + this->set_summertime(0); + summertime_active = false; } } // current month is october - else if (dateMonth == 10) + else if (date_month == 10) { - // restart last week of october - if ((this->daysInMonth[10] - dateDay) < 7) + if ((this->_days_per_month[10] - date_day) < 7) { // Example year 2020: October 31 days; restart October 26, 2020 (Monday = weekday = 1); 5 days remaining; last Sunday October 25, 2020 @@ -640,53 +613,45 @@ bool NTPClientPlus::updateSWChange() // Example year 2021: October 31 days; Restart 26. October 2021 (Tuesday = weekday = 2); 5 days remaining; Last Sunday 31. October 2021 // Calculation: 31 - 26 = 5; 5 + 2 = 7; - // Result: Last day in October is a Sunday. There follows another Sunday in October => set summer time + // Result: Last day in October is a Sunday. There folfalses another Sunday in October => set summer time - // There follows within the last week in October one more Sunday => summer time - if (((this->daysInMonth[10] - dateDay) + dayOfWeek) >= 7) + // There folfalses within the last week in October one more Sunday => summer time + if (((this->_days_per_month[10] - date_day) + day_of_week) >= 7) { - this->setSummertime(1); - summertimeActive = true; + this->set_summertime(1); + summertime_active = true; } - - // last sunday in october already over -> winter time - else + else // last sunday in october already over -> winter time { - this->setSummertime(0); - summertimeActive = false; + this->set_summertime(0); + summertime_active = false; } } - - // restart in first three weeks of october -> summer time - else + else // restart in first three weeks of october -> summer time { - this->setSummertime(1); - summertimeActive = true; + this->set_summertime(1); + summertime_active = true; } } - - // restart in summer time - else if (dateMonth > 3 && dateMonth < 10) + else if (date_month > 3 && date_month < 10) // restart in summer time { - this->setSummertime(1); - summertimeActive = true; + this->set_summertime(1); + summertime_active = true; } - - // restart in winter time - else if (dateMonth < 3 || dateMonth > 10) + else if (date_month < 3 || date_month > 10) // restart in winter time { - this->setSummertime(0); - summertimeActive = false; + this->set_summertime(0); + summertime_active = false; } } - return summertimeActive; + return summertime_active; } -void wait(unsigned long time) +void wait(unsigned long time_ms) { - unsigned long start = (system_get_time() / 1000); - while (((system_get_time() / 1000) - start) < time) + unsigned long start = (system_get_time() / 1000); // in ms + while (((system_get_time() / 1000) - start) < time_ms) { yield(); }; diff --git a/src/wordclock_esp8266.cpp b/src/wordclock_esp8266.cpp index aad01d1..c450711 100644 --- a/src/wordclock_esp8266.cpp +++ b/src/wordclock_esp8266.cpp @@ -221,14 +221,14 @@ void setup() } // setup NTP - ntp_client.setupNTPClient(); + ntp_client.setup_ntp_client(); logger.log_string("NTP running"); - logger.log_string("Time: " + ntp_client.getFormattedTime()); - logger.log_string("TimeOffset (seconds): " + String(ntp_client.getTimeOffset())); + logger.log_string("Time: " + ntp_client.get_formatted_time()); + logger.log_string("TimeOffset (seconds): " + String(ntp_client.get_time_offset())); // show the current time for short time in words - int hours = ntp_client.getHours24(); - int minutes = ntp_client.getMinutes(); + int hours = ntp_client.get_hours_24(); + int minutes = ntp_client.get_minutes(); String timeMessage = time_to_string(hours, minutes); show_string_on_clock(timeMessage, main_color_clock); draw_minute_indicator(minutes, main_color_clock); @@ -358,8 +358,8 @@ void handle_current_state() { case ST_CLOCK: // state clock { - int hours = ntp_client.getHours24(); - int minutes = ntp_client.getMinutes(); + int hours = ntp_client.get_hours_24(); + int minutes = ntp_client.get_minutes(); (void)show_string_on_clock(time_to_string((uint8_t)hours, (uint8_t)minutes), main_color_clock); draw_minute_indicator((uint8_t)minutes, main_color_clock); @@ -367,8 +367,8 @@ void handle_current_state() } case ST_DICLOCK: // state diclock { - int hours = ntp_client.getHours24(); - int minutes = ntp_client.getMinutes(); + int hours = ntp_client.get_hours_24(); + int minutes = ntp_client.get_minutes(); show_digital_clock((uint8_t)hours, (uint8_t)minutes, main_color_clock); break; } @@ -455,9 +455,8 @@ void update_matrix() */ void send_heartbeat() { - logger.log_string("Heartbeat, state: " + state_names[current_state] + ", FreeHeap: " + ESP.getFreeHeap() + - ", HeapFrag: " + ESP.getHeapFragmentation() + ", MaxFreeBlock: " + ESP.getMaxFreeBlockSize() + - "\nCounter: " + heartbeat_counter + ", Hours: " + (float)(heartbeat_counter) / 3600.0 + "\n"); + logger.log_string("Heartbeat, current state: " + state_names[current_state] + ", counter: " + + heartbeat_counter + ", on-time: " + (float)(heartbeat_counter) / 3600.0 + "h\n"); heartbeat_counter++; } @@ -483,8 +482,8 @@ void check_wifi_status() void check_night_mode() { // check if nightmode need to be activated - int hours = ntp_client.getHours24(); - int minutes = ntp_client.getMinutes(); + int hours = ntp_client.get_hours_24(); + int minutes = ntp_client.get_minutes(); if (hours == night_mode_times.start_hour && minutes == night_mode_times.start_min) { @@ -504,18 +503,14 @@ void check_night_mode() void ntp_time_update(uint32 *last_ntp_update_us) { // NTP time update - int ntp_retval = ntp_client.updateNTP(); + int ntp_retval = ntp_client.update_ntp(); switch (ntp_retval) { case NTP_UPDATE_SUCCESS: { - ntp_client.calcDate(); - logger.log_string("NTP-Update successful"); - logger.log_string("Time: " + ntp_client.getFormattedTime()); - logger.log_string("Date: " + ntp_client.getFormattedDate()); - logger.log_string("TimeOffset (seconds): " + String(ntp_client.getTimeOffset())); - logger.log_string("Summertime: " + String(ntp_client.updateSWChange())); + ntp_client.calc_date(); + logger.log_string("NTP-Update successful, Time: " + ntp_client.get_formatted_time()); *last_ntp_update_us = system_get_time(); watchdog_counter = 30; break; @@ -530,10 +525,10 @@ void ntp_time_update(uint32 *last_ntp_update_us) case NTP_UPDATE_DIFFTOOHIGH: { logger.log_string("NTP-Update not successful. Reason: Too large time difference"); - logger.log_string("Time: " + ntp_client.getFormattedTime()); - logger.log_string("Date: " + ntp_client.getFormattedDate()); - logger.log_string("TimeOffset (seconds): " + String(ntp_client.getTimeOffset())); - logger.log_string("Summertime: " + String(ntp_client.updateSWChange())); + logger.log_string("Time: " + ntp_client.get_formatted_time()); + logger.log_string("Date: " + ntp_client.get_formatted_date()); + logger.log_string("TimeOffset (seconds): " + String(ntp_client.get_time_offset())); + logger.log_string("Summertime: " + String(ntp_client.update_sw_change())); *last_ntp_update_us += 10000000; watchdog_counter--; break; @@ -548,7 +543,7 @@ void ntp_time_update(uint32 *last_ntp_update_us) } } - logger.log_string("Watchdog Counter: " + String(watchdog_counter)); + logger.log_string("Watchdog counter: " + String(watchdog_counter)); if (watchdog_counter <= 0) { logger.log_string("Trigger restart due to watchdog...");