forked from thoralt/esp8266wordclock
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ntp.h
81 lines (71 loc) · 2.13 KB
/
ntp.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
81
// ESP8266 Wordclock
// Copyright (C) 2016 Thoralt Franz, https://github.com/thoralt
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See ntp.cpp for description.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// This code is based on (heavily modified):
// https://github.com/sandeepmistry/esp8266-Arduino/blob/master/esp8266com/esp8266/libraries/ESP8266WiFi/examples/NTPClient
#ifndef _NTP_H_
#define _NTP_H_
#include <stdint.h>
#include <Ticker.h>
#include <WiFiUdp.h>
// type definition for NTP callback
typedef void (*TNtpCallback)(uint8_t, uint8_t, uint8_t, uint8_t);
class NtpClass
{
public:
// public methods
NtpClass();
void begin(IPAddress ip, TNtpCallback callback, int timezone, bool DST);
void setServer(IPAddress address);
IPAddress getServer();
void setTimeZone(int timeZone);
// public members
bool syncInProgress = false;
private:
enum class NtpState
{
idle, startRequest, waitingForReply, waitingForReload
};
int lastSunday(int year, int month, int lastDayInMonth);
static void tickerFunctionWrapper(NtpClass *obj);
int dayOfWeek(int y, int m, int d);
void decodeTime(long long t);
void tickerFunction();
bool isDSTactive();
void sendPacket();
void parse();
IPAddress timeServer;
Ticker ticker;
WiFiUDP udp;
NtpState state = NtpState::idle;
TNtpCallback _callback = NULL;
int timer = 0;
int h = 0;
int m = 0;
int s = 0;
int year = 0;
int month = 0;
int day = 0;
int weekday = 0;
int yearday = 0;
int ms = 0;
int tz = 0;
bool useDST = false;
};
extern NtpClass NTP;
#endif