forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WidgetRTC.h
62 lines (49 loc) · 1.36 KB
/
WidgetRTC.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
/**
* @file WidgetRTC.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Mar 2016
* @brief
*
*/
#ifndef WidgetRTC_h
#define WidgetRTC_h
#include <Blynk/BlynkApi.h>
class WidgetRTC
{
public:
WidgetRTC() {}
void setVPin(int vPin) { mPin = vPin; }
void onWrite(BlynkReq& request, const BlynkParam& param);
void begin();
static uint8_t mPin;
};
#ifndef WidgetRTC_h_NO_IMPL
#include <TimeLib.h>
namespace WidgetRTC_impl {
// This is called by Time library when it needs time sync
static
time_t requestTimeSync()
{
Blynk.syncVirtual(WidgetRTC::mPin); // Request RTC widget update from the server
return 0; // Tell the Time library that we'll set it later
}
}
inline
void WidgetRTC::begin()
{
setSyncProvider(WidgetRTC_impl::requestTimeSync);
}
inline
void WidgetRTC::onWrite(BlynkReq& request, const BlynkParam& param)
{
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
unsigned long blynkTime = param.asLong();
if ( blynkTime >= DEFAULT_TIME) { // Check the integer is a valid time (greater than Jan 1 2013)
setTime(blynkTime); // Sync Time library clock to the value received from Blynk
BLYNK_LOG1(BLYNK_F("Time sync: OK"));
}
}
#endif
#endif