Skip to content

Commit ec338dd

Browse files
committed
Add RTC implementation based on millis for ESP8266
1 parent b3ab0f9 commit ec338dd

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/utility/time/RTCMillis.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifdef ARDUINO_ARCH_ESP8266
19+
20+
/**************************************************************************************
21+
* INCLUDE
22+
**************************************************************************************/
23+
24+
#include <Arduino.h>
25+
#include "RTCMillis.h"
26+
27+
/**************************************************************************************
28+
* CTOR/DTOR
29+
**************************************************************************************/
30+
31+
RTCMillis::RTCMillis()
32+
: _last_rtc_update_tick(0)
33+
, _last_rtc_update_value(0)
34+
{
35+
36+
}
37+
38+
/**************************************************************************************
39+
* PUBLIC MEMBER FUNCTIONS
40+
**************************************************************************************/
41+
42+
void RTCMillis::begin()
43+
{
44+
45+
}
46+
47+
void RTCMillis::set(unsigned long time)
48+
{
49+
_last_rtc_update_tick = millis();
50+
_last_rtc_update_value = time;
51+
}
52+
53+
unsigned long RTCMillis::get()
54+
{
55+
unsigned long current_tick = millis();
56+
unsigned long const elapsed_s = (current_tick - _last_rtc_update_tick) / 1000;
57+
if(elapsed_s) {
58+
set(_last_rtc_update_value + elapsed_s);
59+
}
60+
return _last_rtc_update_value;
61+
}
62+
63+
#endif /* ARDUINO_ARCH_ESP8266 */

src/utility/time/RTCMillis.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_IOT_CLOUD_RTC_MILLIS_H_
19+
#define ARDUINO_IOT_CLOUD_RTC_MILLIS_H_
20+
21+
#ifdef ARDUINO_ARCH_ESP8266
22+
23+
/**************************************************************************************
24+
* INCLUDE
25+
**************************************************************************************/
26+
27+
/**************************************************************************************
28+
* CLASS DECLARATION
29+
**************************************************************************************/
30+
31+
class RTCMillis
32+
{
33+
34+
public:
35+
36+
RTCMillis();
37+
38+
void begin();
39+
void set(unsigned long time);
40+
unsigned long get();
41+
42+
private:
43+
unsigned long _last_rtc_update_tick;
44+
unsigned long _last_rtc_update_value;
45+
46+
};
47+
48+
#endif /* ARDUINO_ARCH_ESP8266 */
49+
50+
#endif /* ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ */

0 commit comments

Comments
 (0)