10. RTC
The rtc object is a built-in object that provides the RTC alarm function.
Functional overview:
- Provides a function to raise an alarm event at a specified time.
- Provides a function related to system time.
Limitations:
- Only one alarm can be used.
rtc Global Object
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
rtc.setAlarm() | Sets the alarm occurrence time. | 00.00.15+ | |
rtc.stopAlarm() | Releases the alarm. | 00.00.15+ | |
rtc.getInitTimeCorrVal() | Gets the correction value after time synchronization for timestamp values acquired when the system time was not yet set. | 02.00.00+ |
Details
rtc.setAlarm(rtcAlarm[,callback])
Sets the alarm occurrence time.
Executes callback processing when an alarm occurs.
The alarm continues until it is stopped.
This method does not allow multiple simultaneous uses.
If you want to set another time, you need to release it once.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
rtcAlarm | Object | mandatory | Alarm occurrence time setting hour : {number} Alarm occurrence time [hour] Range: 0 - 23 (*1) minute : {number} Alarm occurrence time [minutes] Range: 0 - 59 (*1) second : {number} Alarm occurrence time [second] Range: 0 - 59 (*1) timeout : {number} Timeout value of the first alarm occurrence time [s] Range: 1 - 10 (*2) | *1: Setting it to -1 means "don't care" . |
callback() | function | mandatory | Executes callback processing when an alarm occurs. | |
return | undefined | - | - | When an error occurs, an exception is raised. |
*2: If there is no margin between the current time and the first alarm occurrence time, the alarm settings may not be in time due to internal delays. This option uses a timer to complement this situation. If the alarm does not occur before the timeout, executes callback processing instead of the alarm. Set this option when the margin until the alarm occurrence time is less than 10 seconds. If not set, it will be invalid.
rtc.stopAlarm()
Releases the alarm.
Name | Type/Object | M/O | Summary | Note |
---|---|---|---|---|
return | undefined | - | - |
rtc.getInitTimeCorrVal()
Gets the correction value after time synchronization for timestamp values acquired when the system time was not yet set.
It is also possible to determine if the system time has been set (time synchronized).
This function can be utilized when using Express Mode.
Timestamp values when the system time had not yet been set can be converted to the present time by adding this correction value after time synchronization (after the correction value becomes non-zero).
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | number | - | Timestamp Correction Value 0: System time not set Other than 0: Correction Value [ms] | If 0 , It indicates that the system time is not set.If not 0 , it indicates that time synchronization has been completed and the correct time is retained. |
Use as follows:
var timestamp = Date.now();
print("Timestamp before time-sync: " + timestamp + " (" + new Date(timestamp) + ")");
while(!rtc.getInitTimeCorrVal()); //Waiting for time synchronization
var utime = Date.now();
print("Time synchronized: " + utime + " (" + new Date(utime) + ")");
var corVal = rtc.getInitTimeCorrVal();
print("Correction value: " + corVal);
timestamp += corVal;
print("Corrected timestamp: " + timestamp + " (" + new Date(timestamp) + ")");
Timestamp before time-sync: 946684812645 (Sat Jan 01 2000 00:00:12 GMT+00:00)
Time synchronized: 1654050009000 (Wed Jun 01 2022 02:20:09 GMT+00:00)
Correction value: 707365142297
Corrected timestamp: 1654049954942 (Wed Jun 01 2022 02:19:14 GMT+00:00)
Object Usage Examples
Sample1
This is a sample that generates an alarm.
Gets the current time and raises an RTC alarm 20 seconds after that time.
var MAX_COUNT = 5;
var count = MAX_COUNT;
var set_date;
var rtc_time;
var perAlarm = function() {
rtc.stopAlarm();
count--;
print('alarm: count = ' + count);
set_date = new Date();
print('Now time: ' + set_date.toString());
if(count > 0) {
set_date.setSeconds(set_date.getSeconds() + 20);
rtc_time = {
hour: set_date.getHours(),
minute: set_date.getMinutes(),
second: set_date.getSeconds()
};
rtc.setAlarm(rtc_time, perAlarm);
}
}
perAlarm();