09. Sleep
The sleep object is a built-in object that provides power saving features.
Functional overview:
- Supports the Spresense
Cold Sleep
andDeep Sleep
modes as the 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. The backup RAM is only available when using the Cold Sleep
mode. In Deep Sleep
mode, the set value is not retained.
sleep Global Object
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
sleep.setStandby() | Shuts down the network communication and then transitions to standby mode. | 02.00.00+ | |
sleep.setStandbySync() | Immediately transitions to standby mode with forced network communication shutdown. | 02.00.00+ | |
sleep.getEntryResumeControl() | Gets the resumeFlag value that was set immediately before. | 02.00.00+ | |
sleep.setUserRegister() | Saves any value to the backup RAM. | 02.00.00+ | |
sleep.getUserRegister() | Gets the value stored in backup RAM. | 02.00.00+ | |
sleep.setRadio() | Sets enable/disable wireless communication. | 02.00.00+ | |
sleep.setRadioSync() | Sets enable/disable wireless communication. | 02.00.00+ |
Details
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 ( Cold Sleep )1: Offline Resume ( Cold Sleep )Other than: Normal Resume ( Deep Sleep ) | (*** ) is the sleep mode of the Spresense used. |
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.
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 ( Cold Sleep )1: Offline Resume ( Cold Sleep )Other than: Normal Resume ( Deep Sleep ) | (*** ) is the sleep mode of the Spresense used. |
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 | If the power supply stops and the value disappears, or after a resume from Deep Sleep mode, it becomes 0 . |
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 - 4294967295 (0x00000000 - 0xFFFFFFFF) | |
return | undefined | - | - |
sleep.getUserRegister()
Gets the value stored in backup RAM.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | number | - | Read value | If the power supply stops and the value disappears, or after a resume from Deep Sleep mode, it becomes 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 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:');
}