-
Notifications
You must be signed in to change notification settings - Fork 22
/
Clock.h
80 lines (63 loc) · 2.94 KB
/
Clock.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef CLOCK_H
#define CLOCK_H
#include <stdint.h>
#include <TimeLib.h>
// For NTP
#include <WiFi.h>
#include <NTPClient.h>
// For the DS3231 RTC
#include <DS1307RTC.h>
#include "StoredConfig.h"
class Clock {
public:
Clock() : loop_time(0), local_time(0), time_valid(false), config(NULL) {}
// The global WiFi from WiFi.h must already be .begin()'d before calling Clock::begin()
void begin(StoredConfig::Config::Clock *config_);
void loop();
// Calls NTPClient::getEpochTime() or RTC::get() as appropriate
// This has to be static to pass to TimeLib::setSyncProvider.
static time_t syncProvider();
// Same as syncProvider, but forces NTP update and sync with RTC
void forceNTPUpdate();
// Set preferred hour format. true = 12hr, false = 24hr
void setTwelveHour(bool twelve_hour_) { config->twelve_hour = twelve_hour_; }
bool getTwelveHour() { return config->twelve_hour; }
void toggleTwelveHour() { config->twelve_hour = !config->twelve_hour; }
// Internal time is kept in UTC. This affects the displayed time.
void setTimeZoneOffset(time_t offset) { config->time_zone_offset = offset; }
time_t getTimeZoneOffset() { return config->time_zone_offset; }
void adjustTimeZoneOffset(time_t adj) { config->time_zone_offset += adj; }
// Proxy C functions from TimeLib.h
// I really wish it were a class we could just subclass, but here we are.
uint16_t getYear() { return year(local_time); }
uint8_t getMonth() { return month(local_time); }
uint8_t getDay() { return day(local_time); }
uint8_t getHour() { return config->twelve_hour ? hourFormat12(local_time) : hour(local_time); }
uint8_t getHour12() { return hourFormat12(local_time); }
uint8_t getHour24() { return hour(local_time); }
uint8_t getMinute() { return minute(local_time); }
uint8_t getSecond() { return second(local_time); }
bool isAm() { return isAM(local_time); }
bool isPm() { return isPM(local_time); }
// Helper functions for making a clock.
uint8_t getHoursTens() { return getHour()/10; }
uint8_t getHoursOnes() { return getHour()%10; }
uint8_t getHours12Tens() { return getHour12()/10; }
uint8_t getHours12Ones() { return getHour12()%10; }
uint8_t getHours24Tens() { return getHour24()/10; }
uint8_t getHours24Ones() { return getHour24()%10; }
uint8_t getMinutesTens() { return getMinute()/10; }
uint8_t getMinutesOnes() { return getMinute()%10; }
uint8_t getSecondsTens() { return getSecond()/10; }
uint8_t getSecondsOnes() { return getSecond()%10; }
private:
time_t loop_time, local_time;
bool time_valid;
StoredConfig::Config::Clock *config;
// Static variables needed for syncProvider()
static WiFiUDP ntpUDP;
static NTPClient ntpTimeClient;
static uint32_t millis_last_ntp;
const static uint32_t refresh_ntp_every_ms = 3600000; // Get new NTP every hour, use RTC in between.
};
#endif // CLOCK_H