2020-11-21 23:29:52 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
2020-11-27 16:02:17 -05:00
|
|
|
#include "py/reload.h"
|
2020-11-21 23:29:52 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/alarm/__init__.h"
|
|
|
|
#include "shared-bindings/alarm/pin/PinAlarm.h"
|
2020-11-25 17:52:06 -05:00
|
|
|
#include "shared-bindings/alarm/time/TimeAlarm.h"
|
2020-11-27 16:02:17 -05:00
|
|
|
#include "shared-bindings/supervisor/Runtime.h"
|
2020-11-26 22:06:37 -05:00
|
|
|
#include "shared-bindings/time/__init__.h"
|
2020-11-27 16:02:17 -05:00
|
|
|
#include "supervisor/shared/autoreload.h"
|
2020-11-26 22:06:37 -05:00
|
|
|
#include "supervisor/shared/workflow.h"
|
|
|
|
|
2020-11-27 16:02:17 -05:00
|
|
|
//| """Alarms and sleep
|
2020-11-18 20:44:34 -05:00
|
|
|
//|
|
2020-11-27 16:02:17 -05:00
|
|
|
//| Provides alarms that trigger based on time intervals or on external events, such as pin
|
|
|
|
//| changes.
|
2020-12-01 20:01:14 -05:00
|
|
|
//| The program can simply wait for these alarms, or go to sleep and be awoken when they trigger.
|
2020-11-18 20:44:34 -05:00
|
|
|
//|
|
2020-11-27 16:02:17 -05:00
|
|
|
//| There are two supported levels of sleep: light sleep and deep sleep.
|
2020-11-18 20:44:34 -05:00
|
|
|
//|
|
2020-12-01 20:01:14 -05:00
|
|
|
//| Light sleep keeps sufficient state so the program can resume after sleeping.
|
|
|
|
//| It does not shut down WiFi, BLE, or other communications, or ongoing activities such
|
|
|
|
//| as audio playback. It reduces power consumption to the extent possible that leaves
|
|
|
|
//| these continuing activities running. In some cases there may be no decrease in power consumption.
|
2020-11-27 16:02:17 -05:00
|
|
|
//|
|
|
|
|
//| Deep sleep shuts down power to nearly all of the microcontroller including the CPU and RAM. This can save
|
|
|
|
//| a more significant amount of power, but CircuitPython must restart ``code.py`` from the beginning when
|
2020-11-25 17:52:06 -05:00
|
|
|
//| awakened.
|
2020-12-01 20:01:14 -05:00
|
|
|
//|
|
|
|
|
//| For both light sleep and deep sleep, if CircuitPython is connected to a host computer,
|
|
|
|
//| maintaining the connection takes priority and power consumption may not be reduced.
|
2020-11-26 22:06:37 -05:00
|
|
|
//| """
|
2020-11-25 17:52:06 -05:00
|
|
|
|
2020-11-18 20:44:34 -05:00
|
|
|
//|
|
|
|
|
//| wake_alarm: Alarm
|
2020-11-27 16:02:17 -05:00
|
|
|
//| """The most recently triggered alarm. If CircuitPython was sleeping, the alarm the woke it from sleep."""
|
2020-11-18 20:44:34 -05:00
|
|
|
//|
|
2020-11-27 16:02:17 -05:00
|
|
|
|
|
|
|
// wake_alarm is implemented as a dictionary entry, so there's no code here.
|
|
|
|
|
2020-11-22 19:10:09 -05:00
|
|
|
void validate_objs_are_alarms(size_t n_args, const mp_obj_t *objs) {
|
|
|
|
for (size_t i = 0; i < n_args; i++) {
|
|
|
|
if (MP_OBJ_IS_TYPE(objs[i], &alarm_pin_pin_alarm_type) ||
|
2020-11-25 17:52:06 -05:00
|
|
|
MP_OBJ_IS_TYPE(objs[i], &alarm_time_time_alarm_type)) {
|
2020-11-22 19:10:09 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mp_raise_TypeError_varg(translate("Expected an alarm"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:01:14 -05:00
|
|
|
//| def light_sleep_until_alarms(*alarms: Alarm) -> Alarm:
|
2020-11-25 17:52:06 -05:00
|
|
|
//| """Go into a light sleep until awakened one of the alarms. The alarm causing the wake-up
|
2020-11-27 16:02:17 -05:00
|
|
|
//| is returned, and is also available as `alarm.wake_alarm`.
|
|
|
|
//|
|
|
|
|
//| If no alarms are specified, return immediately.
|
|
|
|
//|
|
2020-12-01 20:01:14 -05:00
|
|
|
//| **If CircuitPython is connected to a host computer, the connection will be maintained,
|
|
|
|
//| and the microcontroller may not actually go into a light sleep.**
|
2020-11-27 16:02:17 -05:00
|
|
|
//| This allows the user to interrupt an existing program with ctrl-C,
|
2020-12-01 20:01:14 -05:00
|
|
|
//| and to edit the files in CIRCUITPY, which would not be possible in true light sleep.
|
|
|
|
//| Thus, to use light sleep and save significant power,
|
|
|
|
// it may be necessary to disconnect from the host.
|
2020-11-21 23:29:52 -05:00
|
|
|
//| """
|
2020-11-18 20:44:34 -05:00
|
|
|
//| ...
|
|
|
|
//|
|
2020-12-01 20:01:14 -05:00
|
|
|
STATIC mp_obj_t alarm_light_sleep_until_alarms(size_t n_args, const mp_obj_t *args) {
|
2020-12-02 21:05:29 -05:00
|
|
|
if (n_args == 0) {
|
|
|
|
return mp_const_none;
|
2020-12-01 20:01:14 -05:00
|
|
|
}
|
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
validate_objs_are_alarms(n_args, args);
|
|
|
|
|
|
|
|
return common_hal_alarm_light_sleep_until_alarms(n_args, args);
|
2020-11-18 20:44:34 -05:00
|
|
|
}
|
2020-12-01 20:01:14 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_light_sleep_until_alarms_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_light_sleep_until_alarms);
|
2020-11-18 20:44:34 -05:00
|
|
|
|
2020-11-25 17:52:06 -05:00
|
|
|
//| def exit_and_deep_sleep_until_alarms(*alarms: Alarm) -> None:
|
|
|
|
//| """Exit the program and go into a deep sleep, until awakened by one of the alarms.
|
2020-11-27 16:02:17 -05:00
|
|
|
//| This function does not return.
|
2020-11-22 19:10:09 -05:00
|
|
|
//|
|
2020-11-25 17:52:06 -05:00
|
|
|
//| When awakened, the microcontroller will restart and will run ``boot.py`` and ``code.py``
|
2020-11-22 19:10:09 -05:00
|
|
|
//| from the beginning.
|
|
|
|
//|
|
2020-11-27 16:02:17 -05:00
|
|
|
//| After restart, an alarm *equivalent* to the one that caused the wake-up
|
|
|
|
//| will be available as `alarm.wake_alarm`.
|
2020-11-25 15:07:57 -05:00
|
|
|
//| Its type and/or attributes may not correspond exactly to the original alarm.
|
2020-11-25 17:52:06 -05:00
|
|
|
//| For time-base alarms, currently, an `alarm.time.TimeAlarm()` is created.
|
2020-11-22 19:10:09 -05:00
|
|
|
//|
|
2020-11-25 17:52:06 -05:00
|
|
|
//| If no alarms are specified, the microcontroller will deep sleep until reset.
|
2020-11-26 22:06:37 -05:00
|
|
|
//|
|
2020-12-06 12:00:00 -05:00
|
|
|
//| **If CircuitPython is connected to a host computer, the connection will be maintained,
|
|
|
|
//| and the system will not go into deep sleep.**
|
2020-11-27 16:02:17 -05:00
|
|
|
//| This allows the user to interrupt an existing program with ctrl-C,
|
|
|
|
//| and to edit the files in CIRCUITPY, which would not be possible in true deep sleep.
|
2020-12-01 20:01:14 -05:00
|
|
|
//| Thus, to use deep sleep and save significant power, you will need to disconnect from the host.
|
2020-11-27 16:02:17 -05:00
|
|
|
//|
|
|
|
|
//| Here is skeletal example that deep-sleeps and restarts every 60 seconds:
|
|
|
|
//|
|
|
|
|
//| .. code-block:: python
|
|
|
|
//|
|
|
|
|
//| import alarm
|
|
|
|
//| import time
|
|
|
|
//|
|
|
|
|
//| print("Waking up")
|
|
|
|
//|
|
|
|
|
//| # Set an alarm for 60 seconds from now.
|
|
|
|
//| time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60)
|
|
|
|
//|
|
|
|
|
//| # Deep sleep until the alarm goes off. Then restart the program.
|
|
|
|
//| alarm.exit_and_deep_sleep_until_alarms(time_alarm)
|
2020-11-18 20:44:34 -05:00
|
|
|
//| """
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-11-25 17:52:06 -05:00
|
|
|
STATIC mp_obj_t alarm_exit_and_deep_sleep_until_alarms(size_t n_args, const mp_obj_t *args) {
|
2020-11-22 19:10:09 -05:00
|
|
|
validate_objs_are_alarms(n_args, args);
|
2020-11-26 22:06:37 -05:00
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
// Validate the alarms and set them.
|
|
|
|
common_hal_alarm_set_deep_sleep_alarms(n_args, args);
|
2020-12-01 20:01:14 -05:00
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
// Raise an exception, which will be processed in main.c.
|
|
|
|
mp_raise_arg1(&mp_type_DeepSleepRequest, NULL);
|
2020-11-26 22:06:37 -05:00
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
// Doesn't get here.
|
2020-11-19 17:47:12 -05:00
|
|
|
return mp_const_none;
|
2020-11-18 20:44:34 -05:00
|
|
|
}
|
2020-11-25 17:52:06 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_exit_and_deep_sleep_until_alarms_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_exit_and_deep_sleep_until_alarms);
|
2020-11-18 20:44:34 -05:00
|
|
|
|
2020-11-20 23:33:39 -05:00
|
|
|
STATIC const mp_map_elem_t alarm_pin_globals_table[] = {
|
2020-11-18 20:44:34 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pin) },
|
|
|
|
|
2020-11-20 23:33:39 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PinAlarm), MP_OBJ_FROM_PTR(&alarm_pin_pin_alarm_type) },
|
2020-11-18 20:44:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(alarm_pin_globals, alarm_pin_globals_table);
|
|
|
|
|
2020-11-20 23:33:39 -05:00
|
|
|
STATIC const mp_obj_module_t alarm_pin_module = {
|
2020-11-18 20:44:34 -05:00
|
|
|
.base = { &mp_type_module },
|
2020-11-20 23:33:39 -05:00
|
|
|
.globals = (mp_obj_dict_t*)&alarm_pin_globals,
|
2020-11-18 20:44:34 -05:00
|
|
|
};
|
|
|
|
|
2020-11-20 23:33:39 -05:00
|
|
|
STATIC const mp_map_elem_t alarm_time_globals_table[] = {
|
2020-11-18 20:44:34 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
|
|
|
|
|
2020-11-25 17:52:06 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_TimeAlarm), MP_OBJ_FROM_PTR(&alarm_time_time_alarm_type) },
|
2020-11-18 20:44:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(alarm_time_globals, alarm_time_globals_table);
|
|
|
|
|
2020-11-20 23:33:39 -05:00
|
|
|
STATIC const mp_obj_module_t alarm_time_module = {
|
2020-11-18 20:44:34 -05:00
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&alarm_time_globals,
|
|
|
|
};
|
|
|
|
|
2020-11-20 23:33:39 -05:00
|
|
|
STATIC mp_map_elem_t alarm_module_globals_table[] = {
|
2020-11-18 20:44:34 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) },
|
|
|
|
|
2020-11-22 19:10:09 -05:00
|
|
|
// wake_alarm is a mutable attribute.
|
2020-11-18 20:44:34 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_wake_alarm), mp_const_none },
|
|
|
|
|
2020-12-01 20:01:14 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_light_sleep_until_alarms), MP_OBJ_FROM_PTR(&alarm_light_sleep_until_alarms_obj) },
|
2020-11-25 17:52:06 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_exit_and_deep_sleep_until_alarms),
|
|
|
|
MP_OBJ_FROM_PTR(&alarm_exit_and_deep_sleep_until_alarms_obj) },
|
2020-11-18 20:44:34 -05:00
|
|
|
|
2020-11-20 23:33:39 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pin), MP_OBJ_FROM_PTR(&alarm_pin_module) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_time), MP_OBJ_FROM_PTR(&alarm_time_module) }
|
2020-11-18 20:44:34 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
|
|
|
|
|
|
|
|
void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) {
|
|
|
|
// Equivalent of:
|
|
|
|
// alarm.wake_alarm = alarm
|
|
|
|
mp_map_elem_t *elem =
|
2020-11-20 23:33:39 -05:00
|
|
|
mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP);
|
2020-11-18 20:44:34 -05:00
|
|
|
if (elem) {
|
|
|
|
elem->value = alarm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_module_t alarm_module = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&alarm_module_globals,
|
|
|
|
};
|