2020-11-25 15:07:57 -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/nlr.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
2020-11-26 22:06:37 -05:00
|
|
|
|
|
|
|
#include "shared-bindings/alarm/time/TimeAlarm.h"
|
2021-11-10 09:42:21 -05:00
|
|
|
#include "shared-bindings/rtc/__init__.h"
|
|
|
|
#include "shared-bindings/time/__init__.h"
|
2020-11-26 22:06:37 -05:00
|
|
|
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2020-11-26 22:06:37 -05:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
|
|
|
mp_obj_t MP_WEAK rtc_get_time_source_time(void) {
|
|
|
|
mp_raise_RuntimeError(translate("RTC is not supported on this board"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-25 17:52:06 -05:00
|
|
|
//| class TimeAlarm:
|
2020-11-26 22:06:37 -05:00
|
|
|
//| """Trigger an alarm when the specified time is reached."""
|
2020-11-25 15:07:57 -05:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def __init__(
|
|
|
|
//| self, monotonic_time: Optional[float] = None, epoch_time: Optional[int] = None
|
|
|
|
//| ) -> None:
|
2020-11-25 15:07:57 -05:00
|
|
|
//| """Create an alarm that will be triggered when `time.monotonic()` would equal
|
2020-11-26 22:06:37 -05:00
|
|
|
//| ``monotonic_time``, or when `time.time()` would equal ``epoch_time``.
|
|
|
|
//| Only one of the two arguments can be given.
|
2020-11-25 15:07:57 -05:00
|
|
|
//| The alarm is not active until it is passed to an
|
2020-12-01 20:13:46 -05:00
|
|
|
//| `alarm`-enabling function, such as `alarm.light_sleep_until_alarms()` or
|
2020-11-26 22:06:37 -05:00
|
|
|
//| `alarm.exit_and_deep_sleep_until_alarms()`.
|
2020-11-25 15:07:57 -05:00
|
|
|
//|
|
|
|
|
//| If the given time is in the past when sleep occurs, the alarm will be triggered
|
|
|
|
//| immediately.
|
|
|
|
//| """
|
|
|
|
//| ...
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC mp_obj_t alarm_time_timealarm_make_new(const mp_obj_type_t *type,
|
2021-10-15 14:43:12 -04:00
|
|
|
size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2020-12-22 19:13:02 -05:00
|
|
|
alarm_time_timealarm_obj_t *self = m_new_obj(alarm_time_timealarm_obj_t);
|
|
|
|
self->base.type = &alarm_time_timealarm_type;
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2020-11-26 22:06:37 -05:00
|
|
|
enum { ARG_monotonic_time, ARG_epoch_time };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_monotonic_time, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_epoch_time, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-11-26 22:06:37 -05:00
|
|
|
|
|
|
|
bool have_monotonic = args[ARG_monotonic_time].u_obj != mp_const_none;
|
|
|
|
bool have_epoch = args[ARG_epoch_time].u_obj != mp_const_none;
|
|
|
|
|
|
|
|
if (!(have_monotonic ^ have_epoch)) {
|
|
|
|
mp_raise_ValueError(translate("Supply one of monotonic_time or epoch_time"));
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_float_t monotonic_time = 0; // To avoid compiler warning.
|
|
|
|
if (have_monotonic) {
|
|
|
|
monotonic_time = mp_obj_get_float(args[ARG_monotonic_time].u_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_float_t monotonic_time_now = common_hal_time_monotonic_ms() / 1000.0;
|
|
|
|
|
|
|
|
if (have_epoch) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
2020-11-26 22:06:37 -05:00
|
|
|
mp_raise_ValueError(translate("epoch_time not supported on this board"));
|
2021-03-15 09:57:36 -04:00
|
|
|
#else
|
2020-11-26 22:06:37 -05:00
|
|
|
mp_uint_t epoch_time_secs = mp_obj_int_get_checked(args[ARG_epoch_time].u_obj);
|
|
|
|
|
|
|
|
timeutils_struct_time_t tm;
|
|
|
|
struct_time_to_tm(rtc_get_time_source_time(), &tm);
|
|
|
|
mp_uint_t epoch_secs_now = timeutils_seconds_since_epoch(tm.tm_year, tm.tm_mon, tm.tm_mday,
|
2021-03-15 09:57:36 -04:00
|
|
|
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
2020-11-26 22:06:37 -05:00
|
|
|
// How far in the future (in secs) is the requested time?
|
|
|
|
mp_int_t epoch_diff = epoch_time_secs - epoch_secs_now;
|
|
|
|
// Convert it to a future monotonic time.
|
|
|
|
monotonic_time = monotonic_time_now + epoch_diff;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2020-11-26 22:06:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (monotonic_time < monotonic_time_now) {
|
|
|
|
mp_raise_ValueError(translate("Time is in the past."));
|
|
|
|
}
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
common_hal_alarm_time_timealarm_construct(self, monotonic_time);
|
2020-11-25 15:07:57 -05:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
//| monotonic_time: float
|
2020-11-26 22:06:37 -05:00
|
|
|
//| """When this time is reached, the alarm will trigger, based on the `time.monotonic()` clock.
|
|
|
|
//| The time may be given as ``epoch_time`` in the constructor, but it is returned
|
|
|
|
//| by this property only as a `time.monotonic()` time.
|
|
|
|
//| """
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC mp_obj_t alarm_time_timealarm_obj_get_monotonic_time(mp_obj_t self_in) {
|
|
|
|
alarm_time_timealarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_float(common_hal_alarm_time_timealarm_get_monotonic_time(self));
|
2020-11-25 15:07:57 -05:00
|
|
|
}
|
2020-12-22 19:13:02 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(alarm_time_timealarm_get_monotonic_time_obj, alarm_time_timealarm_obj_get_monotonic_time);
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(alarm_time_timealarm_monotonic_time_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&alarm_time_timealarm_get_monotonic_time_obj);
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC const mp_rom_map_elem_t alarm_time_timealarm_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_monotonic_time), MP_ROM_PTR(&alarm_time_timealarm_monotonic_time_obj) },
|
2020-11-25 15:07:57 -05:00
|
|
|
};
|
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(alarm_time_timealarm_locals_dict, alarm_time_timealarm_locals_dict_table);
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
const mp_obj_type_t alarm_time_timealarm_type = {
|
2020-11-25 15:07:57 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_TimeAlarm,
|
2020-12-22 19:13:02 -05:00
|
|
|
.make_new = alarm_time_timealarm_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&alarm_time_timealarm_locals_dict,
|
2020-11-25 15:07:57 -05:00
|
|
|
};
|