changed alarm.time API

This commit is contained in:
Dan Halbert 2020-11-25 15:07:57 -05:00
parent f868cc5dd0
commit 9dbea36eac
17 changed files with 209 additions and 158 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-11-21 12:36-0500\n"
"POT-Creation-Date: 2020-11-25 15:08-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,13 +17,6 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: main.c
msgid ""
"\n"
"\n"
"------ soft reboot ------\n"
msgstr ""
#: main.c
msgid ""
"\n"
@ -849,6 +842,10 @@ msgstr ""
msgid "Expected an Address"
msgstr ""
#: shared-bindings/alarm/__init__.c
msgid "Expected an alarm"
msgstr ""
#: shared-module/_pixelbuf/PixelBuf.c
#, c-format
msgid "Expected tuple of length %d, got %d"
@ -1440,6 +1437,10 @@ msgid ""
"%d bpp given"
msgstr ""
#: ports/esp32s2/common-hal/alarm/__init__.c
msgid "Only one alarm.time alarm can be set."
msgstr ""
#: shared-module/displayio/ColorConverter.c
msgid "Only one color can be transparent at a time"
msgstr ""
@ -1497,6 +1498,10 @@ msgstr ""
msgid "Pin number already reserved by EXTI"
msgstr ""
#: ports/esp32s2/common-hal/alarm/__init__.c
msgid "PinAlarm deep sleep not yet implemented"
msgstr ""
#: shared-bindings/rgbmatrix/RGBMatrix.c
#, c-format
msgid ""
@ -2449,10 +2454,6 @@ msgstr ""
msgid "division by zero"
msgstr ""
#: ports/esp32s2/common-hal/alarm/time/DurationAlarm.c
msgid "duration out of range"
msgstr ""
#: py/objdeque.c
msgid "empty"
msgstr ""
@ -3354,6 +3355,10 @@ msgstr ""
msgid "small int overflow"
msgstr ""
#: main.c
msgid "soft reboot\n"
msgstr ""
#: extmod/ulab/code/numerical/numerical.c
msgid "sort argument must be an ndarray"
msgstr ""

7
main.c
View File

@ -321,7 +321,9 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
ESP_LOGI("main", "common_hal_alarm_enable_deep_sleep_alarms()");
#if CIRCUITPY_ALARM
// Enable pin or time alarms before sleeping.
common_hal_alarm_enable_deep_sleep_alarms();
// If immediate_wake is true, then there's alarm that would trigger immediately,
// so don't sleep.
bool immediate_wake = !common_hal_alarm_enable_deep_sleep_alarms();
#endif
@ -332,6 +334,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
// It's ok to deep sleep if we're not connected to a host, but we need to make sure
// we're giving enough time for USB enumeration to happen.
bool deep_sleep_allowed =
!immediate_wake &&
(ok || supervisor_workflow_get_allow_deep_sleep_on_error()) &&
(!supervisor_workflow_active() || supervisor_workflow_get_allow_deep_sleep_when_connected()) &&
(supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024);
@ -576,7 +579,7 @@ int __attribute__((used)) main(void) {
}
if (exit_code == PYEXEC_FORCED_EXIT) {
if (!first_run) {
serial_write_compressed(translate("\n\n------ soft reboot ------\n"));
serial_write_compressed(translate("soft reboot\n"));
}
first_run = false;
skip_repl = run_code_py(safe_mode);

View File

@ -25,13 +25,15 @@
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "py/objtuple.h"
#include "py/runtime.h"
#include "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/time/__init__.h"
#include "esp_sleep.h"
@ -49,8 +51,8 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_TIMER: {
// Wake up from timer.
alarm_time_duration_alarm_obj_t *timer = m_new_obj(alarm_time_duration_alarm_obj_t);
timer->base.type = &alarm_time_duration_alarm_type;
alarm_time_monotonic_time_alarm_obj_t *timer = m_new_obj(alarm_time_monotonic_time_alarm_obj_t);
timer->base.type = &alarm_time_monotonic_time_alarm_type;
return timer;
}
@ -84,7 +86,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_pin_pin_alarm_type)) {
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
}
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_duration_alarm_type)) {
else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_monotonic_time_alarm_type)) {
if (time_alarm_set) {
mp_raise_ValueError(translate("Only one alarm.time alarm can be set."));
}
@ -95,14 +97,25 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
_deep_sleep_alarms = mp_obj_new_tuple(n_alarms, alarms);
}
void common_hal_alarm_enable_deep_sleep_alarms(void) {
// Return false if we should wake up immediately because a time alarm is in the past
// or otherwise already triggered.
bool common_hal_alarm_enable_deep_sleep_alarms(void) {
for (size_t i = 0; i < _deep_sleep_alarms->len; i++) {
mp_obj_t alarm = _deep_sleep_alarms->items[i];
if (MP_OBJ_IS_TYPE(alarm, &alarm_time_duration_alarm_type)) {
alarm_time_duration_alarm_obj_t *duration_alarm = MP_OBJ_TO_PTR(alarm);
esp_sleep_enable_timer_wakeup(
(uint64_t) (duration_alarm->duration * 1000000.0f));
if (MP_OBJ_IS_TYPE(alarm, &alarm_pin_pin_alarm_type)) {
// TODO: handle pin alarms
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
}
else if (MP_OBJ_IS_TYPE(alarm, &alarm_time_monotonic_time_alarm_type)) {
alarm_time_monotonic_time_alarm_obj_t *monotonic_time_alarm = MP_OBJ_TO_PTR(alarm);
mp_float_t now = uint64_to_float(common_hal_time_monotonic());
// Compute a relative time in the future from now.
mp_float_t duration_secs = now - monotonic_time_alarm->monotonic_time;
if (duration_secs <= 0.0f) {
return false;
}
esp_sleep_enable_timer_wakeup((uint64_t) (duration_secs * 1000000));
}
// TODO: handle pin alarms
}
return true;
}

View File

@ -3,7 +3,6 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2020 @microDev1 (GitHub)
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@ -38,7 +37,7 @@ void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, c
self->pull = pull;
}
const mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self) {
mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self) {
return self->pins;
}

View File

@ -3,7 +3,6 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2020 @microDev1 (GitHub)
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@ -29,23 +28,12 @@
#include "py/runtime.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
void common_hal_alarm_time_duration_alarm_construct(alarm_time_duration_alarm_obj_t *self, mp_float_t duration) {
self->duration = duration;
void common_hal_alarm_time_monotonic_time_alarm_construct(alarm_time_monotonic_time_alarm_obj_t *self, mp_float_t monotonic_time) {
self->monotonic_time = monotonic_time;
}
mp_float_t common_hal_alarm_time_duration_alarm_get_duration(alarm_time_duration_alarm_obj_t *self) {
return self->duration;
}
void common_hal_alarm_time_duration_alarm_enable(alarm_time_duration_alarm_obj_t *self) {
if (esp_sleep_enable_timer_wakeup((uint64_t) (self->duration * 1000000)) == ESP_ERR_INVALID_ARG) {
mp_raise_ValueError(translate("duration out of range"));
}
}
void common_hal_alarm_time_duration_alarm_disable (alarm_time_duration_alarm_obj_t *self) {
(void) self;
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
mp_float_t common_hal_alarm_time_monotonic_time_alarm_get_monotonic_time(alarm_time_monotonic_time_alarm_obj_t *self) {
return self->monotonic_time;
}

View File

@ -3,7 +3,6 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2020 @microDev1 (GitHub)
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@ -30,5 +29,5 @@
typedef struct {
mp_obj_base_t base;
mp_float_t duration; // seconds
} alarm_time_duration_alarm_obj_t;
mp_float_t monotonic_time; // values compatible with time.monotonic_time()
} alarm_time_monotonic_time_alarm_obj_t;

View File

@ -303,7 +303,7 @@ SRC_COMMON_HAL_ALL = \
_pew/__init__.c \
alarm/__init__.c \
alarm/pin/PinAlarm.c \
alarm/time/DurationAlarm.c \
alarm/time/MonotonicTimeAlarm.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
analogio/__init__.c \

View File

@ -679,6 +679,7 @@ mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
extern mp_float_t uint64_to_float(uint64_t ui64);
#endif
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);

View File

@ -333,6 +333,13 @@ mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t
return mp_obj_new_float(lhs_val);
}
// Convert a uint64_t to a 32-bit float without invoking the double-precision math routines,
// which are large.
mp_float_t uint64_to_float(uint64_t ui64) {
// 4294967296 = 2^32
return (mp_float_t) ((uint32_t) (ui64 >> 32) * 4294967296.0f + (uint32_t) (ui64 & 0xffffffff));
}
#pragma GCC diagnostic pop
#endif // MICROPY_PY_BUILTINS_FLOAT

View File

@ -29,7 +29,7 @@
#include "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
//| """Power-saving light and deep sleep. Alarms can be set to wake up from sleep.
//|
@ -60,7 +60,7 @@
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) ||
MP_OBJ_IS_TYPE(objs[i], &alarm_time_duration_alarm_type)) {
MP_OBJ_IS_TYPE(objs[i], &alarm_time_monotonic_time_alarm_type)) {
continue;
}
mp_raise_TypeError_varg(translate("Expected an alarm"));
@ -86,7 +86,9 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_sleep_until_alarms_obj, 1, MP_OBJ_FUN_
//| When awakened, the microcontroller will restart and run ``boot.py`` and ``code.py``
//| from the beginning.
//|
//| The alarm that caused the wake-up is available as `alarm.wake_alarm`.
//| 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.
//| For time-base alarms, currently, an `alarm.time.MonotonicTimeAlarm()` is created.
//|
//| If you call this routine more than once, only the last set of alarms given will be used.
//| """
@ -121,7 +123,7 @@ STATIC const mp_obj_module_t alarm_pin_module = {
STATIC const mp_map_elem_t alarm_time_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
{ MP_ROM_QSTR(MP_QSTR_DurationAlarm), MP_OBJ_FROM_PTR(&alarm_time_duration_alarm_type) },
{ MP_ROM_QSTR(MP_QSTR_MonotonicTimeAlarm), MP_OBJ_FROM_PTR(&alarm_time_monotonic_time_alarm_type) },
};
STATIC MP_DEFINE_CONST_DICT(alarm_time_globals, alarm_time_globals_table);

View File

@ -32,7 +32,7 @@
#include "common-hal/alarm/__init__.h"
extern mp_obj_t common_hal_alarm_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms);
extern void common_hal_alarm_enable_deep_sleep_alarms(void);
extern bool common_hal_alarm_enable_deep_sleep_alarms(void);
extern void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms);
// Used by wake-up code.

View File

@ -31,6 +31,7 @@
#include "py/nlr.h"
#include "py/obj.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
@ -39,7 +40,7 @@
//|
//| def __init__(self, *pins: microcontroller.Pin, value: bool, all_same_value: bool = False, edge: bool = False, pull: bool = False) -> None:
//| """Create an alarm triggered by a `microcontroller.Pin` level. The alarm is not active
//| until it is listed in an `alarm`-enabling function, such as `alarm.sleep_until_alarms()` or
//| until it is passed to an `alarm`-enabling function, such as `alarm.sleep_until_alarms()` or
//| `alarm.set_deep_sleep_alarms()`.
//|
//| :param microcontroller.Pin \*pins: The pins to monitor. On some ports, the choice of pins
@ -88,7 +89,41 @@ STATIC mp_obj_t alarm_pin_pin_alarm_make_new(const mp_obj_type_t *type, mp_uint_
return MP_OBJ_FROM_PTR(self);
}
//| pins: Tuple[microcontroller.pin]
//| """The trigger pins."""
//|
STATIC mp_obj_t alarm_pin_pin_alarm_obj_get_pins(mp_obj_t self_in) {
alarm_pin_pin_alarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_alarm_pin_pin_alarm_get_pins(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(alarm_pin_pin_alarm_get_pins_obj, alarm_pin_pin_alarm_obj_get_pins);
const mp_obj_property_t alarm_pin_pin_alarm_pins_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&alarm_pin_pin_alarm_get_pins_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| value: Tuple[microcontroller.pin]
//| """The value on which to trigger."""
//|
STATIC mp_obj_t alarm_pin_pin_alarm_obj_get_value(mp_obj_t self_in) {
alarm_pin_pin_alarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_alarm_pin_pin_alarm_get_value(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(alarm_pin_pin_alarm_get_value_obj, alarm_pin_pin_alarm_obj_get_value);
const mp_obj_property_t alarm_pin_pin_alarm_value_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&alarm_pin_pin_alarm_get_value_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t alarm_pin_pin_alarm_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_pins), MP_ROM_PTR(&alarm_pin_pin_alarm_pins_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&alarm_pin_pin_alarm_value_obj) },
};
STATIC MP_DEFINE_CONST_DICT(alarm_pin_pin_alarm_locals_dict, alarm_pin_pin_alarm_locals_dict_table);

View File

@ -35,8 +35,8 @@
extern const mp_obj_type_t alarm_pin_pin_alarm_type;
void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, const mp_obj_t pins[], size_t num_pins, bool value, bool all_same_value, bool edge, bool pull);
extern const mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self);
extern bool common_hal_alarm_pin_pin_alarm_get_level(alarm_pin_pin_alarm_obj_t *self);
extern mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self);
extern bool common_hal_alarm_pin_pin_alarm_get_value(alarm_pin_pin_alarm_obj_t *self);
extern bool common_hal_alarm_pin_pin_alarm_get_edge(alarm_pin_pin_alarm_obj_t *self);
extern bool common_hal_alarm_pin_pin_alarm_get_pull(alarm_pin_pin_alarm_obj_t *self);

View File

@ -1,91 +0,0 @@
/*
* 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 "shared-bindings/board/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
#include "py/nlr.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
//| class DurationAlarm:
//| """Trigger an alarm at a specified interval from now."""
//|
//| def __init__(self, secs: float) -> None:
//| """Create an alarm that will be triggered in ``secs`` seconds from the time
//| sleep starts. The alarm is not active until it is listed in an
//| `alarm`-enabling function, such as `alarm.sleep_until_alarms()` or
//| `alarm.set_deep_sleep_alarms()`.
//| """
//| ...
//|
STATIC mp_obj_t alarm_time_duration_alarm_make_new(const mp_obj_type_t *type,
mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
mp_arg_check_num(n_args, kw_args, 1, 1, false);
alarm_time_duration_alarm_obj_t *self = m_new_obj(alarm_time_duration_alarm_obj_t);
self->base.type = &alarm_time_duration_alarm_type;
mp_float_t secs = mp_obj_get_float(args[0]);
common_hal_alarm_time_duration_alarm_construct(self, secs);
return MP_OBJ_FROM_PTR(self);
}
//| def __eq__(self, other: object) -> bool:
//| """Two DurationAlarm objects are equal if their durations differ by less than a millisecond."""
//| ...
//|
STATIC mp_obj_t alarm_time_duration_alarm_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
switch (op) {
case MP_BINARY_OP_EQUAL:
if (MP_OBJ_IS_TYPE(rhs_in, &alarm_time_duration_alarm_type)) {
return mp_obj_new_bool(
abs(common_hal_alarm_time_duration_alarm_get_duration(lhs_in) -
common_hal_alarm_time_duration_alarm_get_duration(rhs_in)) < 0.001f);
}
return mp_const_false;
default:
return MP_OBJ_NULL; // op not supported
}
}
STATIC const mp_rom_map_elem_t alarm_time_duration_alarm_locals_dict_table[] = {
};
STATIC MP_DEFINE_CONST_DICT(alarm_time_duration_alarm_locals_dict, alarm_time_duration_alarm_locals_dict_table);
const mp_obj_type_t alarm_time_duration_alarm_type = {
{ &mp_type_type },
.name = MP_QSTR_DurationAlarm,
.make_new = alarm_time_duration_alarm_make_new,
.binary_op = alarm_time_duration_alarm_binary_op,
.locals_dict = (mp_obj_t)&alarm_time_duration_alarm_locals_dict,
};

View File

@ -0,0 +1,93 @@
/*
* 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 "shared-bindings/board/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
#include "py/nlr.h"
#include "py/obj.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
//| class MonotonicTimeAlarm:
//| """Trigger an alarm when `time.monotonic()` reaches the given value."""
//|
//| def __init__(self, monotonic_time: float) -> None:
//| """Create an alarm that will be triggered when `time.monotonic()` would equal
//| ``monotonic_time``.
//| The alarm is not active until it is passed to an
//| `alarm`-enabling function, such as `alarm.sleep_until_alarms()` or
//| `alarm.set_deep_sleep_alarms()`.
//|
//| If the given time is in the past when sleep occurs, the alarm will be triggered
//| immediately.
//| """
//| ...
//|
STATIC mp_obj_t alarm_time_monotonic_time_alarm_make_new(const mp_obj_type_t *type,
mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
mp_arg_check_num(n_args, kw_args, 1, 1, false);
alarm_time_monotonic_time_alarm_obj_t *self = m_new_obj(alarm_time_monotonic_time_alarm_obj_t);
self->base.type = &alarm_time_monotonic_time_alarm_type;
mp_float_t secs = mp_obj_get_float(args[0]);
common_hal_alarm_time_monotonic_time_alarm_construct(self, secs);
return MP_OBJ_FROM_PTR(self);
}
//| monotonic_time: float
//| """The time at which to trigger, based on the `time.monotonic()` clock."""
//|
STATIC mp_obj_t alarm_time_monotonic_time_alarm_obj_get_monotonic_time(mp_obj_t self_in) {
alarm_time_monotonic_time_alarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_float(common_hal_alarm_time_monotonic_time_alarm_get_monotonic_time(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(alarm_time_monotonic_time_alarm_get_monotonic_time_obj, alarm_time_monotonic_time_alarm_obj_get_monotonic_time);
const mp_obj_property_t alarm_time_monotonic_time_alarm_monotonic_time_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&alarm_time_monotonic_time_alarm_get_monotonic_time_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t alarm_time_monotonic_time_alarm_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_monotonic_time), MP_ROM_PTR(&alarm_time_monotonic_time_alarm_monotonic_time_obj) },
};
STATIC MP_DEFINE_CONST_DICT(alarm_time_monotonic_time_alarm_locals_dict, alarm_time_monotonic_time_alarm_locals_dict_table);
const mp_obj_type_t alarm_time_monotonic_time_alarm_type = {
{ &mp_type_type },
.name = MP_QSTR_TimeAlarm,
.make_new = alarm_time_monotonic_time_alarm_make_new,
.locals_dict = (mp_obj_t)&alarm_time_monotonic_time_alarm_locals_dict,
};

View File

@ -24,19 +24,16 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_DURATION_ALARM_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_DURATION_ALARM_H
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_MONOTONIC_TIME_ALARM_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_MONOTINIC_TIME_ALARM_H
#include "py/obj.h"
#include "common-hal/alarm/time/DurationAlarm.h"
#include "common-hal/alarm/time/MonotonicTimeAlarm.h"
extern const mp_obj_type_t alarm_time_duration_alarm_type;
extern const mp_obj_type_t alarm_time_monotonic_time_alarm_type;
extern void common_hal_alarm_time_duration_alarm_construct(alarm_time_duration_alarm_obj_t *self, mp_float_t duration);
extern mp_float_t common_hal_alarm_time_duration_alarm_get_duration(alarm_time_duration_alarm_obj_t *self);
extern void common_hal_alarm_time_monotonic_time_alarm_construct(alarm_time_monotonic_time_alarm_obj_t *self, mp_float_t monotonic_time);
extern mp_float_t common_hal_alarm_time_monotonic_time_alarm_get_monotonic_time(alarm_time_monotonic_time_alarm_obj_t *self);
extern void common_hal_alarm_time_duration_alarm_enable(alarm_time_duration_alarm_obj_t *self);
extern void common_hal_alarm_time_duration_alarm_disable (alarm_time_duration_alarm_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_DURATION_ALARM_H
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME_MONOTONIC_TIME_ALARM_H

View File

@ -51,9 +51,9 @@
//| ...
//|
STATIC mp_obj_t time_monotonic(void) {
// Returns ms ticks.
uint64_t time64 = common_hal_time_monotonic();
// 4294967296 = 2^32
return mp_obj_new_float(((uint32_t) (time64 >> 32) * 4294967296.0f + (uint32_t) (time64 & 0xffffffff)) / 1000.0f);
return mp_obj_new_float(uint64_to_float(time64) / 1000.0f);
}
MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_obj, time_monotonic);