summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2020-11-25 15:07:57 -0500
committerDan Halbert <halbert@halwitz.org>2020-11-25 15:09:27 -0500
commit9dbea36eac9a78cf324bba8d32a5cb2c9940d411 (patch)
tree7d54888455af9db4599a9928b709859296924a01 /shared-bindings
parentf868cc5dd02319957500180541281f41ec0d0573 (diff)
changed alarm.time API
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/alarm/__init__.c10
-rw-r--r--shared-bindings/alarm/__init__.h2
-rw-r--r--shared-bindings/alarm/pin/PinAlarm.c37
-rw-r--r--shared-bindings/alarm/pin/PinAlarm.h4
-rw-r--r--shared-bindings/alarm/time/DurationAlarm.c91
-rw-r--r--shared-bindings/alarm/time/MonotonicTimeAlarm.c93
-rw-r--r--shared-bindings/alarm/time/MonotonicTimeAlarm.h (renamed from shared-bindings/alarm/time/DurationAlarm.h)17
-rw-r--r--shared-bindings/time/__init__.c4
8 files changed, 147 insertions, 111 deletions
diff --git a/shared-bindings/alarm/__init__.c b/shared-bindings/alarm/__init__.c
index b6b86c835..a3ecdd2ba 100644
--- a/shared-bindings/alarm/__init__.c
+++ b/shared-bindings/alarm/__init__.c
@@ -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);
diff --git a/shared-bindings/alarm/__init__.h b/shared-bindings/alarm/__init__.h
index d8d6812c9..0f084c78e 100644
--- a/shared-bindings/alarm/__init__.h
+++ b/shared-bindings/alarm/__init__.h
@@ -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.
diff --git a/shared-bindings/alarm/pin/PinAlarm.c b/shared-bindings/alarm/pin/PinAlarm.c
index bb48b93c4..ff7b19ca1 100644
--- a/shared-bindings/alarm/pin/PinAlarm.c
+++ b/shared-bindings/alarm/pin/PinAlarm.c
@@ -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);
diff --git a/shared-bindings/alarm/pin/PinAlarm.h b/shared-bindings/alarm/pin/PinAlarm.h
index bbf3018b5..cb6946812 100644
--- a/shared-bindings/alarm/pin/PinAlarm.h
+++ b/shared-bindings/alarm/pin/PinAlarm.h
@@ -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);
diff --git a/shared-bindings/alarm/time/DurationAlarm.c b/shared-bindings/alarm/time/DurationAlarm.c
deleted file mode 100644
index 6831aba5d..000000000
--- a/shared-bindings/alarm/time/DurationAlarm.c
+++ /dev/null
@@ -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,
-};
diff --git a/shared-bindings/alarm/time/MonotonicTimeAlarm.c b/shared-bindings/alarm/time/MonotonicTimeAlarm.c
new file mode 100644
index 000000000..6ee411e88
--- /dev/null
+++ b/shared-bindings/alarm/time/MonotonicTimeAlarm.c
@@ -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,
+};
diff --git a/shared-bindings/alarm/time/DurationAlarm.h b/shared-bindings/alarm/time/MonotonicTimeAlarm.h
index 87f5d9390..6eb2738ab 100644
--- a/shared-bindings/alarm/time/DurationAlarm.h
+++ b/shared-bindings/alarm/time/MonotonicTimeAlarm.h
@@ -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
diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c
index 44f82c62e..804d5ecd8 100644
--- a/shared-bindings/time/__init__.c
+++ b/shared-bindings/time/__init__.c
@@ -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);