Skip to content

Commit

Permalink
Add support for local timezones (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Apr 16, 2022
1 parent c973b9e commit 7045383
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
6 changes: 6 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ lib_deps =
adafruit/RTClib@^1.14.1
https://github.com/matthewturner/Eventually.git
featherfly/SoftwareSerial@^1.0
https://github.com/MHotchin/Timezone.git
paulstoffregen/Time@^1.6.1

[env:native]
platform = native
Expand All @@ -30,6 +32,8 @@ lib_deps =
adafruit/RTClib@^1.14.1
https://github.com/matthewturner/Eventually.git
featherfly/SoftwareSerial@^1.0
https://github.com/MHotchin/Timezone.git
paulstoffregen/Time@^1.6.1

[env:debug]
platform = native
Expand All @@ -40,3 +44,5 @@ lib_deps =
adafruit/RTClib@^1.14.1
https://github.com/matthewturner/Eventually.git
featherfly/SoftwareSerial@^1.0
https://github.com/MHotchin/Timezone.git
paulstoffregen/Time@^1.6.1
36 changes: 31 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void setup()
setupDisplaySchedule();
setupBrightnessSchedule();
setupMinimalModeSchedule();
setupTimezones();
setupTest();

commandListener = new EvtTimeListener(0, true, (EvtAction)processCommands);
Expand Down Expand Up @@ -79,7 +80,8 @@ bool processCommands()
showTemporarily();
break;
case Commands::SET:
Serial.println(F("Command: SET"));
Serial.print(F("Command: SET "));
Serial.println(command.Data);
clock.adjust(DateTime(command.Data));
showTemporarily();
break;
Expand All @@ -99,9 +101,10 @@ bool update()
{
Serial.println(F("Updating..."));

DateTime now = DateTime();
if (CLOCK_IS_ENABLED)
{
now = clock.now();
now = toLocal(clock.now());
}

if (displaySchedule.valueFor(now.hour()))
Expand Down Expand Up @@ -155,10 +158,12 @@ bool showTemporarily()
updateListener->disable();
returnToNormalListener->enable();

DateTime now = DateTime();
if (CLOCK_IS_ENABLED)
{
now = clock.now();
now = toLocal(clock.now());
}

display.setPart(1, now.hour(), Flags::NONE);
display.setPart(0, now.minute(), Flags::LEADING_ZERO);
display.setSeparator(true);
Expand Down Expand Up @@ -204,11 +209,24 @@ void render()
FastLED.show();
}

DateTime toLocal(DateTime utc)
{
time_t local = timezone->toLocal(utc.unixtime());
return DateTime(local);
}

void reportStatus()
{
bluetoothSerial.println(F("{"));
bluetoothSerial.print(F(" \"time\": "));
bluetoothSerial.println(clock.now().unixtime());
if(CLOCK_IS_ENABLED)
{
bluetoothSerial.println(clock.now().unixtime());
}
else
{
bluetoothSerial.println(0);
}
bluetoothSerial.println(F("}"));
}

Expand Down Expand Up @@ -258,7 +276,6 @@ void setupRealtimeClock()
if (!CLOCK_IS_ENABLED)
{
Serial.println(F("Clock is disabled"));
now = DateTime(2014, 1, 21, 3, 0, 0);
return;
}

Expand Down Expand Up @@ -316,4 +333,13 @@ void setupTest()
render();
delay(500);
display.clear();
}

void setupTimezones()
{
Serial.println(F("Setup timezones..."));

timezone = new Timezone(
TimeChangeRule("BST", Last, Sun, Mar, 1, 60),
TimeChangeRule("GMT", Last, Sun, Oct, 2, 0));
}
5 changes: 4 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Display.h"
#include "Schedule.h"
#include <RTClib.h>
#include <Timezone.h>
#include <Eventually.h>
#include <LowPower.h>
#include <SoftwareSerial.h>
Expand Down Expand Up @@ -34,7 +35,7 @@ Display display;
Display pendingDisplay;
RTC_DS3231 clock;
EvtManager mgr;
DateTime now;
Timezone *timezone;
Schedule<CRGB::HTMLColorCode> colorSchedule(CRGB::Red);
Schedule<bool> displaySchedule(false);
Schedule<bool> separatorSchedule(false);
Expand Down Expand Up @@ -62,12 +63,14 @@ bool showTemporarily();
bool returnToNormal();
bool processCommands();
void reportStatus();
DateTime toLocal(DateTime utc);

void setupColorSchedule();
void setupDisplaySchedule();
void setupBrightnessSchedule();
void setupRealtimeClock();
void setupMinimalModeSchedule();
void setupTimezones();
void setupTest();

#endif

0 comments on commit 7045383

Please sign in to comment.