09. Sleep
The sleep object is a built-in object that provides power saving features.
Functional overview:
- Supports stop mode.
- Supports standby mode.
- Provides the function of reading and writing data to the backup RAM.
- Provides a function to control wireless communications.
For detailed operation of the power saving function, please refer to here.
The backup RAM is the memory that retains the set value even if the power saving mode is entered, as long as power is supplied. If power supply stops, the set value will be lost.
sleep Global Object
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
sleep.setStop() | Shuts down the network communication and then transitions to stop mode. | 01.00.00+ | |
sleep.setStopSync() | Immediately transitions to stop mode with network communication maintained. | 01.00.00+ | |
sleep.setStandby() | Shuts down the network communication and then transitions to standby mode. | 01.00.00+ | |
sleep.setStandbySync() | Immediately transitions to standby mode with forced network communication shutdown. Immediately transitions to standby mode with network communication maintained. (Previous version) | 02.00.00+ | |
sleep.getEntryResumeControl() | Gets the resumeFlag value that was set immediately before. | 01.00.00+ | |
sleep.setUserRegister() | Saves any value to the backup RAM. | 01.00.00+ | |
sleep.getUserRegister() | Gets the value stored in backup RAM. | 01.00.00+ | |
sleep.setRadio() | Sets enable/disable wireless communication. | 02.00.00+ | |
sleep.setRadioSync() | Sets enable/disable wireless communication. | 02.00.00+ |
Details
sleep.setStop(rtcAlarm[,callback][,netoff])
Shuts down the network communication and then transitions to stop mode.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
rtcAlarm | Object | mandatory | Resume time 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) | *1: If you do not want to use resume by alarm, specify -1 . |
callback(flag) | function | optional | Executes callback processing when an event occurs. flag : {number} Indicates the event content. 0: Immediately before transition to stop mode 1: Immediately after returning from stop mode | |
netoff | boolean | optional | Network shutdown settings true: Enable false: Disable The default value is true. | Version 02.00.00+ |
return | undefined | - | - | When an error occurs, an exception is raised. |
sleep.setStopSync(rtcAlarm)
Immediately transitions to stop mode with network communication maintained.
During the execution of this method, the immediate transition is made.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
rtcAlarm | Object | mandatory | Resume time 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) | *1: If you do not want to use resume by alarm, specify -1 . |
return | undefined | - | - | When an error occurs, an exception is raised. |
sleep.setStandby(rtcAlarm,resumeFlag[,callback])
Shuts down the network communication and then transitions to standby mode.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
rtcAlarm | Object | mandatory | Resume time 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) | *1: If you do not want to use resume by alarm, specify -1 . |
resumeFlag | number | mandatory | Specify the operation at resume. 0: Normal Resume Other than 0: Offline Resume | |
callback() | function | optional | Executes callback processing immediately before transition to standby mode. | |
return | undefined | - | - | When an error occurs, an exception is raised. |
sleep.setStandbySync(rtcAlarm,resumeFlag)
Immediately transitions to standby mode with forced network communication shutdown. During the execution of this method, the immediate transition is made. (Version 02.00.00+)
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
rtcAlarm | Object | mandatory | Resume time 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) | *1: If you do not want to use resume by alarm, specify -1 . |
resumeFlag | number | mandatory | Specify the operation at resume. 0: Normal Resume Other than 0: Offline Resume | |
return | undefined | - | - | When an error occurs, an exception is raised. |
sleep.getEntryResumeControl()
Gets the resumeFlag value that was set immediately before.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | number | - | resumeFlag value | Values other than 0 are rounded to 1. |
sleep.setUserRegister(userValue)
Saves any value to the backup RAM.
It can be used for general purposes.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
userValue | number | mandatory | Any value Range: 0 - 65535 (0x0000 - 0xFFFF) | |
return | undefined | - | - |
sleep.getUserRegister()
Gets the value stored in backup RAM.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | number | - | Read value | If power supply stops and the value is lost, or if the value is not set, it will be 0 . |
sleep.setRadio(ctrl[,callback])
Sets enable/disable wireless communication.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
ctrl | number | mandatory | Wireless Communication Settings 0: Wireless communication OFF 1: Wireless communication ON | If Wireless Communication OFF is specified, services and functions using the network will not be available. To recover, it is necessary to return to the Wireless Communication ON setting. |
callback() | function | optional | Executes callback processing when the specified wireless communication setting is completed. | |
return | undefined | - | - | When an error occurs, an exception is raised. |
sleep.setRadioSync(ctrl)
Sets enable/disable wireless communication.
During the execution of this method, the immediate transition is made.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
ctrl | number | mandatory | Wireless Communication Settings 0: Wireless communication OFF 1: Wireless communication ON | If Wireless Communication OFF is specified, services and functions using the network will not be available. To recover, it is necessary to return to the Wireless Communication ON setting. |
return | undefined | - | - | When an error occurs, an exception is raised. |
Object Usage Examples
Sample 1
This is a sample in stop mode.
Sleep and resume are repeated based on the current time.
var MAX_COUNT = 10;
var count = MAX_COUNT;
while(count-- > 0) {
print('Hello stop-mode');
var set_date = new Date();
print('Now time: ' + set_date.toString());
set_date.setSeconds(set_date.getSeconds() + 30);
var rtc_time = {hour: set_date.getHours(), minute: set_date.getMinutes(),
second: set_date.getSeconds()};
sleep.setStopSync(rtc_time); //Transition to stop mode
print(Date() + ': resume!!!' + 'count=' + count ); //Executed after returning from stop mode
}
Sample 2
This is a sample in standby mode.
Sleep and resume are repeated based on the current time.
var now_count = sleep.getUserRegister();
var entry_resume = sleep.getEntryResumeControl();
print('entry resume cont.=' + entry_resume);
var MAX_COUNT = 5;
var delay_s = 10;
var set_count;
var now_date = new Date();
print('Now time: ' + now_date.toString());
now_date.setSeconds(now_date.getSeconds() + delay_s);
print('Set time: ' + now_date.toString());
var rtc_time = {hour: now_date.getHours(), minute: now_date.getMinutes(),
second: now_date.getSeconds()};
if(entry_resume == 0) {
set_count = MAX_COUNT;
} else {
print(Date() + ': resume!!!');
set_count = --now_count;
}
sleep.setUserRegister(set_count);
if(set_count != 0) {
sleep.setStandby(rtc_time, 1, function() {
print('standby ready');
});
} else {
print('Sleep Finish:');
}