From cf35e07c2fad900019d0720e0c6a0cf9e20e55dc Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 28 Sep 2023 13:57:51 +0700 Subject: [PATCH] pre --- .../lib/ESPMegaPRO/ESPMegaPRO.cpp | 41 ++++++++++--------- Template Project/lib/ESPMegaPRO/ESPMegaPRO.h | 18 ++++++-- Template Project/platformio.ini | 2 +- Template Project/src/fram_demo.cpp.disabled | 0 ...c_scanner.cpp.disabled => i2c_scanner.cpp} | 3 +- Template Project/src/rtc_demo.cpp | 12 ------ Template Project/src/rtc_demo.cpp.disabled | 30 ++++++++++++++ 7 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 Template Project/src/fram_demo.cpp.disabled rename Template Project/src/{i2c_scanner.cpp.disabled => i2c_scanner.cpp} (98%) delete mode 100644 Template Project/src/rtc_demo.cpp create mode 100644 Template Project/src/rtc_demo.cpp.disabled diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp index 6675142..57d9457 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp @@ -100,7 +100,8 @@ void IRAM_ATTR refreshInputBankB() inputBufferB = inputBankB.read8(); } -rtctime_t ESPMega_getTime() { +rtctime_t ESPMega_getTime() +{ tmElements_t timeElement; RTC.read(timeElement); rtctime_t time; @@ -109,18 +110,19 @@ rtctime_t ESPMega_getTime() { time.seconds = timeElement.Second; time.day = timeElement.Day; time.month = timeElement.Month; - time.year = timeElement.Year+1970; + time.year = timeElement.Year + 1970; return time; } -void ESPMega_setTime(int hours,int minutes, int seconds, int day, int month, int year) { +void ESPMega_setTime(int hours, int minutes, int seconds, int day, int month, int year) +{ tmElements_t timeElement; timeElement.Hour = hours; timeElement.Minute = minutes; timeElement.Second = seconds; timeElement.Day = day; timeElement.Month = month; - timeElement.Year = year-1970; + timeElement.Year = year - 1970; RTC.write(timeElement); } @@ -154,24 +156,23 @@ void ESPMega_dacWrite(int id, int value) } } -/* void ESPMega_rtcNTPUpdate() +bool ESPMega_updateTimeFromNTP() { - ntpTimeClient.update(); - if (ntpTimeClient.updated()) + struct tm timeinfo; + if (getLocalTime(&timeinfo)) { - int day = ntpTimeClient.getDay(); - int month = ntpTimeClient.getMonth(); - int year = ntpTimeClient.getYear(); - int hours = ntpTimeClient.getHours(); - int minutes = ntpTimeClient.getMinutes(); - int seconds = ntpTimeClient.getSeconds(); - ESPMega_RTC.setYear(year); - ESPMega_RTC.setMonth(month); - ESPMega_RTC.setDay(day); - ESPMega_RTC.setHour(hours); - ESPMega_RTC.setMinute(minutes); - ESPMega_RTC.setSecond(seconds); + rtctime_t rtctime = ESPMega_getTime(); + if (rtctime.hours != timeinfo.tm_hour || rtctime.minutes != timeinfo.tm_min || + rtctime.seconds != timeinfo.tm_sec || rtctime.day != timeinfo.tm_mday || + rtctime.month != timeinfo.tm_mon + 1 || rtctime.year != timeinfo.tm_year + 1900) + { + ESPMega_setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, + timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900); + + } + return true; } -} */ + return false; +} #endif \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h index d6e67bc..e552741 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h @@ -5,12 +5,13 @@ #include #include #include -#ifdef ANALOG_CARD_ENABLE -#include -#include #include #include #include +#include +#ifdef ANALOG_CARD_ENABLE +#include +#include #endif #define INPUT_BANK_A_ADDRESS 0x21 @@ -23,12 +24,13 @@ #define DAC1_ADDRESS 0x61 #define DAC2_ADDRESS 0x62 #define DAC3_ADDRESS 0x63 -#define EEPROM_ADDRESS 0x70 +#define EEPROM_ADDRESS 0x5A //#define USE_INTERRUPT #define INPUT_BANK_A_INTERRUPT 36 #define INPUT_BANK_B_INTERRUPT 39 extern I2C_eeprom ESPMega_EEPROM; +#define ESPMega_configNTP configTime struct rtctime_t { uint8_t hours; uint8_t minutes; @@ -99,6 +101,14 @@ rtctime_t ESPMega_getTime(); */ void ESPMega_setTime(int hours,int minutes, int seconds, int day, int month, int year); +/** + * Update the onboard RTC's time + * by using time from the NTP server + * configured with ESPMega_configNTP(); + * @return true when updated successfully. +*/ +bool ESPMega_updateTimeFromNTP(); + #ifdef ANALOG_CARD_ENABLE /** * Read one of the ESPMega Analog Card's Analog Input pins (A0-A7) diff --git a/Template Project/platformio.ini b/Template Project/platformio.ini index d1ddcbe..71dcb86 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -22,5 +22,5 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 robtillaart/MCP4725@^0.3.7 robtillaart/I2C_EEPROM@^1.7.3 paulstoffregen/Time@^1.6.1 - paulstoffregen/DS1307RTC + paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033 monitor_speed = 115200 \ No newline at end of file diff --git a/Template Project/src/fram_demo.cpp.disabled b/Template Project/src/fram_demo.cpp.disabled new file mode 100644 index 0000000..e69de29 diff --git a/Template Project/src/i2c_scanner.cpp.disabled b/Template Project/src/i2c_scanner.cpp similarity index 98% rename from Template Project/src/i2c_scanner.cpp.disabled rename to Template Project/src/i2c_scanner.cpp index 7f1f5e7..be6d04e 100644 --- a/Template Project/src/i2c_scanner.cpp.disabled +++ b/Template Project/src/i2c_scanner.cpp @@ -33,8 +33,9 @@ void setup() { + Wire.setClock(50000); Wire.begin(14,33); - + Serial.begin(115200); // Leonardo: wait for serial monitor Serial.println("\nI2C Scanner"); } diff --git a/Template Project/src/rtc_demo.cpp b/Template Project/src/rtc_demo.cpp deleted file mode 100644 index c0146a0..0000000 --- a/Template Project/src/rtc_demo.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -void setup() { - ESPMega_begin(); - Serial.begin(115200); - ESPMega_setTime(16,35,0,16,9,2023); -} - -void loop() { - delay(1000); - rtctime_t tm = ESPMega_getTime(); - Serial.printf("%02d:%02d:%02d %02d/%02d/%04d\n", tm.hours,tm.minutes,tm.seconds,tm.day,tm.month,tm.year); -} \ No newline at end of file diff --git a/Template Project/src/rtc_demo.cpp.disabled b/Template Project/src/rtc_demo.cpp.disabled new file mode 100644 index 0000000..9e8990e --- /dev/null +++ b/Template Project/src/rtc_demo.cpp.disabled @@ -0,0 +1,30 @@ +#include +#include + +uint8_t utc_offset = 7; + +IPAddress IP(192, 168, 0, 241); +IPAddress SUBNET(255, 255, 255, 0); +IPAddress GATEWAY(192, 168, 0, 1); +IPAddress DNS(10, 192, 1, 1); + +void setup() +{ + ESPMega_begin(); + Serial.begin(115200); + ETH.begin(); + ETH.config(IP, GATEWAY, SUBNET, DNS, DNS); + delay(5000); + char ntp[19]; + DNS.toString().toCharArray(ntp, 19); + ESPMega_configNTP(utc_offset * 3600, 0, ntp); + ESPMega_updateTimeFromNTP(); + //ESPMega_setTime(0,10,52,9,11,2008); +} + +void loop() +{ + rtctime_t time = ESPMega_getTime(); + Serial.printf("RTC: %02d:%02d:%02d %02d/%02d/%04d\n", time.hours, time.minutes, time.seconds, time.day, time.month, time.year); + delay(1000); +} \ No newline at end of file