From a8d5d8485dc3fb24f9673338b30965acd2b7c286 Mon Sep 17 00:00:00 2001 From: Jonathas Barbosa Date: Sat, 18 Jun 2022 18:19:46 -0300 Subject: [PATCH] ezTime library update --- CWDateTime.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++ CWDateTime.h | 27 +++++++++++++++++++++ Clockface.cpp | 2 +- Clockface.h | 6 ++--- DateTime.cpp | 58 -------------------------------------------- DateTime.h | 26 -------------------- IClockface.h | 7 +++--- WiFiConnect.h | 38 +++++++++++------------------ mariobros-clock.ino | 13 ++++------ 9 files changed, 112 insertions(+), 124 deletions(-) create mode 100644 CWDateTime.cpp create mode 100644 CWDateTime.h delete mode 100644 DateTime.cpp delete mode 100644 DateTime.h mode change 100755 => 100644 IClockface.h diff --git a/CWDateTime.cpp b/CWDateTime.cpp new file mode 100644 index 0000000..077e8e2 --- /dev/null +++ b/CWDateTime.cpp @@ -0,0 +1,59 @@ + +#include "CWDateTime.h" + +void CWDateTime::begin() +{ + myTZ.setCache(0); + waitForSync(); +} + +void CWDateTime::setTimezone(const char *timeZone) +{ + myTZ.setCache(0); + myTZ.setLocation(timeZone); + waitForSync(); +} + +String CWDateTime::getTimezone() +{ + return myTZ.getTimezoneName(0); +} + + +void CWDateTime::update() +{ +} + +String CWDateTime::getFormattedTime() +{ + return myTZ.dateTime(); +} + +char *CWDateTime::getHour(const char *format) +{ + static char buffer[3] = {'\0'}; + snprintf(buffer, sizeof(buffer), format, myTZ.dateTime("H")); + return buffer; +} + +char *CWDateTime::getMinute(const char *format) +{ + static char buffer[3] = {'\0'}; + strncpy(buffer, myTZ.dateTime("i").c_str(), sizeof(buffer)); + return buffer; +} + +int CWDateTime::getHour() +{ + return myTZ.dateTime("H").toInt(); +} + +int CWDateTime::getMinute() +{ + return myTZ.dateTime("i").toInt(); +} + +int CWDateTime::getSecond() +{ + return myTZ.dateTime("s").toInt(); +} diff --git a/CWDateTime.h b/CWDateTime.h new file mode 100644 index 0000000..4d4a97a --- /dev/null +++ b/CWDateTime.h @@ -0,0 +1,27 @@ +#ifndef CWDateTimeCf_h +#define CWDateTimeCf_h + +#include + +#include +#include + +class CWDateTime +{ +private: + Timezone myTZ; + +public: + void begin(); + void setTimezone(const char *timeZone); + String getTimezone(); + void update(); + String getFormattedTime(); + + char *getHour(const char *format); + char *getMinute(const char *format); + int getHour(); + int getMinute(); + int getSecond(); +}; +#endif diff --git a/Clockface.cpp b/Clockface.cpp index 61d43ca..0ee4bf2 100644 --- a/Clockface.cpp +++ b/Clockface.cpp @@ -29,7 +29,7 @@ Clockface::Clockface(Adafruit_GFX* display) { Locator::provide(&eventBus); } -void Clockface::setup(DateTime *dateTime) { +void Clockface::setup(CWDateTime *dateTime) { _dateTime = dateTime; Locator::getDisplay()->setFont(&Super_Mario_Bros__24pt7b); diff --git a/Clockface.h b/Clockface.h index 5946ceb..7cdebbc 100644 --- a/Clockface.h +++ b/Clockface.h @@ -10,7 +10,7 @@ #include "Object.h" // Commons #include "IClockface.h" -#include "DateTime.h" +#include "CWDateTime.h" #include "assets.h" #include "mario.h" @@ -19,12 +19,12 @@ class Clockface: public IClockface { private: Adafruit_GFX* _display; - DateTime* _dateTime; + CWDateTime* _dateTime; void updateTime(); public: Clockface(Adafruit_GFX* display); - void setup(DateTime *dateTime); + void setup(CWDateTime *dateTime); void update(); void externalEvent(int type); }; diff --git a/DateTime.cpp b/DateTime.cpp deleted file mode 100644 index 6ec4bc7..0000000 --- a/DateTime.cpp +++ /dev/null @@ -1,58 +0,0 @@ - -#include "DateTime.h" -#include - -#define EEPROM_TZ_ADDR 7 - -void DateTime::begin() -{ - signed char tz = EEPROM.read(EEPROM_TZ_ADDR); - if (tz < -12 || tz > 14) { - tz = 0; - } - - Serial.print("Current Timezone: "); - Serial.println(tz); - - this->ntp = new NTPClient(udp, tz * 3600); - - ntp->begin(); - ntp->forceUpdate(); - setTime(ntp->getEpochTime()); -} - -void DateTime::update() -{ - ntp->update(); -} - -String DateTime::getFormattedTime() -{ - return ntp->getFormattedTime(); -} - -char* DateTime::getHour(const char *format) -{ - static char buffer[3] = {'\0'}; - snprintf(buffer, sizeof(buffer), format, ntp->getHours()); - return buffer; -} - -char* DateTime::getMinute(const char *format) -{ - static char buffer[3] = {'\0'}; - snprintf(buffer, sizeof(buffer), format, ntp->getMinutes()); - return buffer; -} - -int DateTime::getHour() { - return ntp->getHours(); -} - -int DateTime::getMinute() { - return ntp->getMinutes(); -} - -int DateTime::getSecond() { - return ntp->getSeconds(); -} diff --git a/DateTime.h b/DateTime.h deleted file mode 100644 index 688796d..0000000 --- a/DateTime.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef DateTimeCf_h -#define DateTimeCf_h - -#include -#include -#include - -class DateTime -{ - private: - WiFiUDP udp; - NTPClient* ntp; - - public: - void begin(); - void update(); - String getFormattedTime(); - - char* getHour(const char *format); - char* getMinute(const char *format); - - int getHour(); - int getMinute(); - int getSecond(); -}; -#endif diff --git a/IClockface.h b/IClockface.h old mode 100755 new mode 100644 index 6052aeb..41ba888 --- a/IClockface.h +++ b/IClockface.h @@ -1,14 +1,13 @@ #ifndef IClockface_h #define IClockface_h -#include "DateTime.h" +#include "CWDateTime.h" class IClockface { - //virtual void setup(DateTime *dateTime) = 0; - virtual void setup(DateTime *dateTime) = 0; + virtual void setup(CWDateTime *dateTime) = 0; virtual void update() = 0; }; -#endif \ No newline at end of file +#endif diff --git a/WiFiConnect.h b/WiFiConnect.h index 9a6985c..4b5a01c 100644 --- a/WiFiConnect.h +++ b/WiFiConnect.h @@ -2,40 +2,30 @@ #define WiFiConnect_h #include -#include -#define EEPROM_TZ_ADDR 7 - +#include "ezTime.h" WiFiManager wifiManager; +Timezone myTZ; struct WiFiConnect { std::vector _menu = {"wifi","exit"}; - char timezone[4]="0"; + char timezone[40]="0"; bool shouldSaveConfig = false; void saveTimezone(const char* value) { - signed char tz = atoi(value); - if (tz < -12 || tz > 14) { - tz = 0; - } - - Serial.print("Save Timezone: "); - Serial.println(tz); - - EEPROM.write(EEPROM_TZ_ADDR, tz); - delay(10); - if (!EEPROM.commit()) { - Serial.print("Error saving EEPROM."); - } + Serial.print("Save: "); + Serial.println(value); + myTZ.setCache(0); + myTZ.setLocation(value); shouldSaveConfig = false; } - signed char loadTimezone() { - signed char tz = EEPROM.read(EEPROM_TZ_ADDR); - if (tz < -12 || tz > 14) { - tz = 0; - } + String loadTimezone() { + myTZ.setCache(0); + String tz = myTZ.getTimezoneName(); + Serial.print("Load: "); + Serial.println(tz); return tz; } @@ -45,8 +35,8 @@ struct WiFiConnect //wifiManager.resetSettings(); wifiManager.setSaveConfigCallback([&](){ shouldSaveConfig = true; }); - sprintf(timezone, "%d", loadTimezone()); - WiFiManagerParameter timezoneParam("tz", "Inform your timezone (GMT)", timezone, 3); + sprintf(timezone, "%s", loadTimezone()); + WiFiManagerParameter timezoneParam("tz", "Inform your timezone (e.g. America/Lima)", timezone, 36); wifiManager.setTitle("Clockwise Wifi Setup"); wifiManager.setMenu(_menu); diff --git a/mariobros-clock.ino b/mariobros-clock.ino index a1f446e..b1cb0a9 100644 --- a/mariobros-clock.ino +++ b/mariobros-clock.ino @@ -1,13 +1,11 @@ #include #include "Clockface.h" #include "WiFiConnect.h" -#include "DateTime.h" - -#define EEPROM_SIZE 512 +#include "CWDateTime.h" MatrixPanel_I2S_DMA *dma_display = nullptr; WiFiConnect wifi; -DateTime dateTime; +CWDateTime cwDateTime; Clockface *clockface; uint16_t myBLACK = dma_display->color565(0, 0, 0); @@ -37,7 +35,6 @@ void displaySetup() { void setup() { Serial.begin(115200); - EEPROM.begin(EEPROM_SIZE); displaySetup(); @@ -50,11 +47,11 @@ void setup() { dma_display->setTextColor(myBLUE); dma_display->setCursor(0, 32); dma_display->print("connecting..."); - + wifi.connect(); - dateTime.begin(); + cwDateTime.begin(); - clockface->setup(&dateTime); + clockface->setup(&cwDateTime); } void loop() {