circuitpython/shared-bindings/alarm/__init__.c

163 lines
6.5 KiB
C
Raw Normal View History

/*
* 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"
#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-18 20:44:34 -05:00
//| """Power-saving light and deep sleep. Alarms can be set to wake up from sleep.
//|
//| The `alarm` module provides sleep related functionality. There are two supported levels of
//| sleep, light and deep.
//|
//| Light sleep leaves the CPU and RAM powered so that CircuitPython can resume where it left off
//| after being woken up. CircuitPython automatically goes into a light sleep when `time.sleep()` is
//| called. To light sleep until a non-time alarm use `alarm.sleep_until_alarms()`. Any active
2020-11-18 20:44:34 -05:00
//| peripherals, such as I2C, are left on.
//|
//| Deep sleep shuts down power to nearly all of the chip including the CPU and RAM. This can save
2020-11-25 17:52:06 -05:00
//| a more significant amount of power, but CircuitPython must start ``code.py`` from the beginning when
//| awakened.
2020-11-22 19:10:09 -05:00
//|
//| An error includes an uncaught exception, or sys.exit() called with a non-zero argument
//|
//| To set alarms for deep sleep use `alarm.set_deep_sleep_alarms()` they will apply to next deep sleep only."""
2020-11-18 20:44:34 -05:00
//|
//| wake_alarm: Alarm
//| """The most recent alarm to wake us up from a sleep (light or deep.)"""
//|
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"));
}
}
//| def 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
//| is returned, and is also available as `alarm.wake_alarm`.
//| """
2020-11-18 20:44:34 -05:00
//| ...
//|
2020-11-22 19:10:09 -05:00
STATIC mp_obj_t alarm_sleep_until_alarms(size_t n_args, const mp_obj_t *args) {
validate_objs_are_alarms(n_args, args);
common_hal_alarm_sleep_until_alarms(n_args, args);
2020-11-19 17:47:12 -05:00
return mp_const_none;
2020-11-18 20:44:34 -05:00
}
2020-11-22 19:10:09 -05:00
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_sleep_until_alarms_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_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-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-25 15:07:57 -05:00
//| An alarm equivalent to the one that caused the wake-up is available as `alarm.wake_alarm`.
//| 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-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-25 17:52:06 -05:00
common_hal_exit_and_deep_sleep_until_alarms(n_args, args);
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
//| """The `alarm.pin` module contains alarm attributes and classes related to pins.
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
};
//| """The `alarm.time` module contains alarm attributes and classes related to time-keeping.
//| """
//|
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-11-22 19:10:09 -05:00
{ MP_ROM_QSTR(MP_QSTR_sleep_until_alarms), MP_OBJ_FROM_PTR(&alarm_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,
};