09. 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.
Limitations:
- Only one alarm can be used.
rtc Global Object
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
rtc.setAlarm() | Sets the alarm occurrence time. | ||
rtc.stopAlarm() | Releases the alarm. |
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 | - | - |
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();