Skip to content

Commit

Permalink
ezTime library update
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthas committed Jun 18, 2022
1 parent 08d0771 commit a8d5d84
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 124 deletions.
59 changes: 59 additions & 0 deletions CWDateTime.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
27 changes: 27 additions & 0 deletions CWDateTime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef CWDateTimeCf_h
#define CWDateTimeCf_h

#include <Arduino.h>

#include <ezTime.h>
#include <WiFi.h>

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
2 changes: 1 addition & 1 deletion Clockface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Clockface.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "Object.h"
// Commons
#include "IClockface.h"
#include "DateTime.h"
#include "CWDateTime.h"

#include "assets.h"
#include "mario.h"
Expand All @@ -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);
};
Expand Down
58 changes: 0 additions & 58 deletions DateTime.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions DateTime.h

This file was deleted.

7 changes: 3 additions & 4 deletions IClockface.h
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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
#endif
38 changes: 14 additions & 24 deletions WiFiConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,30 @@
#define WiFiConnect_h

#include <WiFiManager.h>
#include <EEPROM.h>
#define EEPROM_TZ_ADDR 7

#include "ezTime.h"

WiFiManager wifiManager;
Timezone myTZ;

struct WiFiConnect
{
std::vector<const char *> _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;
}

Expand All @@ -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);
Expand Down
13 changes: 5 additions & 8 deletions mariobros-clock.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
#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);
Expand Down Expand Up @@ -37,7 +35,6 @@ void displaySetup() {
void setup() {

Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);

displaySetup();

Expand All @@ -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() {
Expand Down

0 comments on commit a8d5d84

Please sign in to comment.