Skip to content

Commit 9dbea36

Browse files
committed
changed alarm.time API
1 parent f868cc5 commit 9dbea36

File tree

17 files changed

+209
-158
lines changed

17 files changed

+209
-158
lines changed

locale/circuitpython.pot

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-11-21 12:36-0500\n"
11+
"POT-Creation-Date: 2020-11-25 15:08-0500\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -17,13 +17,6 @@ msgstr ""
1717
"Content-Type: text/plain; charset=CHARSET\n"
1818
"Content-Transfer-Encoding: 8bit\n"
1919

20-
#: main.c
21-
msgid ""
22-
"\n"
23-
"\n"
24-
"------ soft reboot ------\n"
25-
msgstr ""
26-
2720
#: main.c
2821
msgid ""
2922
"\n"
@@ -849,6 +842,10 @@ msgstr ""
849842
msgid "Expected an Address"
850843
msgstr ""
851844

845+
#: shared-bindings/alarm/__init__.c
846+
msgid "Expected an alarm"
847+
msgstr ""
848+
852849
#: shared-module/_pixelbuf/PixelBuf.c
853850
#, c-format
854851
msgid "Expected tuple of length %d, got %d"
@@ -1440,6 +1437,10 @@ msgid ""
14401437
"%d bpp given"
14411438
msgstr ""
14421439

1440+
#: ports/esp32s2/common-hal/alarm/__init__.c
1441+
msgid "Only one alarm.time alarm can be set."
1442+
msgstr ""
1443+
14431444
#: shared-module/displayio/ColorConverter.c
14441445
msgid "Only one color can be transparent at a time"
14451446
msgstr ""
@@ -1497,6 +1498,10 @@ msgstr ""
14971498
msgid "Pin number already reserved by EXTI"
14981499
msgstr ""
14991500

1501+
#: ports/esp32s2/common-hal/alarm/__init__.c
1502+
msgid "PinAlarm deep sleep not yet implemented"
1503+
msgstr ""
1504+
15001505
#: shared-bindings/rgbmatrix/RGBMatrix.c
15011506
#, c-format
15021507
msgid ""
@@ -2449,10 +2454,6 @@ msgstr ""
24492454
msgid "division by zero"
24502455
msgstr ""
24512456

2452-
#: ports/esp32s2/common-hal/alarm/time/DurationAlarm.c
2453-
msgid "duration out of range"
2454-
msgstr ""
2455-
24562457
#: py/objdeque.c
24572458
msgid "empty"
24582459
msgstr ""
@@ -3354,6 +3355,10 @@ msgstr ""
33543355
msgid "small int overflow"
33553356
msgstr ""
33563357

3358+
#: main.c
3359+
msgid "soft reboot\n"
3360+
msgstr ""
3361+
33573362
#: extmod/ulab/code/numerical/numerical.c
33583363
msgid "sort argument must be an ndarray"
33593364
msgstr ""

main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
321321
ESP_LOGI("main", "common_hal_alarm_enable_deep_sleep_alarms()");
322322
#if CIRCUITPY_ALARM
323323
// Enable pin or time alarms before sleeping.
324-
common_hal_alarm_enable_deep_sleep_alarms();
324+
// If immediate_wake is true, then there's alarm that would trigger immediately,
325+
// so don't sleep.
326+
bool immediate_wake = !common_hal_alarm_enable_deep_sleep_alarms();
325327
#endif
326328

327329

@@ -332,6 +334,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
332334
// It's ok to deep sleep if we're not connected to a host, but we need to make sure
333335
// we're giving enough time for USB enumeration to happen.
334336
bool deep_sleep_allowed =
337+
!immediate_wake &&
335338
(ok || supervisor_workflow_get_allow_deep_sleep_on_error()) &&
336339
(!supervisor_workflow_active() || supervisor_workflow_get_allow_deep_sleep_when_connected()) &&
337340
(supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024);
@@ -576,7 +579,7 @@ int __attribute__((used)) main(void) {
576579
}
577580
if (exit_code == PYEXEC_FORCED_EXIT) {
578581
if (!first_run) {
579-
serial_write_compressed(translate("\n\n------ soft reboot ------\n"));
582+
serial_write_compressed(translate("soft reboot\n"));
580583
}
581584
first_run = false;
582585
skip_repl = run_code_py(safe_mode);

ports/esp32s2/common-hal/alarm/__init__.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include "py/obj.h"
2829
#include "py/objtuple.h"
2930
#include "py/runtime.h"
3031

3132
#include "shared-bindings/alarm/__init__.h"
3233
#include "shared-bindings/alarm/pin/PinAlarm.h"
33-
#include "shared-bindings/alarm/time/DurationAlarm.h"
34+
#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
3435
#include "shared-bindings/microcontroller/__init__.h"
36+
#include "shared-bindings/time/__init__.h"
3537

3638
#include "esp_sleep.h"
3739

@@ -49,8 +51,8 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
4951
switch (esp_sleep_get_wakeup_cause()) {
5052
case ESP_SLEEP_WAKEUP_TIMER: {
5153
// Wake up from timer.
52-
alarm_time_duration_alarm_obj_t *timer = m_new_obj(alarm_time_duration_alarm_obj_t);
53-
timer->base.type = &alarm_time_duration_alarm_type;
54+
alarm_time_monotonic_time_alarm_obj_t *timer = m_new_obj(alarm_time_monotonic_time_alarm_obj_t);
55+
timer->base.type = &alarm_time_monotonic_time_alarm_type;
5456
return timer;
5557
}
5658

@@ -84,7 +86,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
8486
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_pin_pin_alarm_type)) {
8587
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
8688
}
87-
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_duration_alarm_type)) {
89+
else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_monotonic_time_alarm_type)) {
8890
if (time_alarm_set) {
8991
mp_raise_ValueError(translate("Only one alarm.time alarm can be set."));
9092
}
@@ -95,14 +97,25 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
9597
_deep_sleep_alarms = mp_obj_new_tuple(n_alarms, alarms);
9698
}
9799

98-
void common_hal_alarm_enable_deep_sleep_alarms(void) {
100+
// Return false if we should wake up immediately because a time alarm is in the past
101+
// or otherwise already triggered.
102+
bool common_hal_alarm_enable_deep_sleep_alarms(void) {
99103
for (size_t i = 0; i < _deep_sleep_alarms->len; i++) {
100104
mp_obj_t alarm = _deep_sleep_alarms->items[i];
101-
if (MP_OBJ_IS_TYPE(alarm, &alarm_time_duration_alarm_type)) {
102-
alarm_time_duration_alarm_obj_t *duration_alarm = MP_OBJ_TO_PTR(alarm);
103-
esp_sleep_enable_timer_wakeup(
104-
(uint64_t) (duration_alarm->duration * 1000000.0f));
105+
if (MP_OBJ_IS_TYPE(alarm, &alarm_pin_pin_alarm_type)) {
106+
// TODO: handle pin alarms
107+
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
108+
}
109+
else if (MP_OBJ_IS_TYPE(alarm, &alarm_time_monotonic_time_alarm_type)) {
110+
alarm_time_monotonic_time_alarm_obj_t *monotonic_time_alarm = MP_OBJ_TO_PTR(alarm);
111+
mp_float_t now = uint64_to_float(common_hal_time_monotonic());
112+
// Compute a relative time in the future from now.
113+
mp_float_t duration_secs = now - monotonic_time_alarm->monotonic_time;
114+
if (duration_secs <= 0.0f) {
115+
return false;
116+
}
117+
esp_sleep_enable_timer_wakeup((uint64_t) (duration_secs * 1000000));
105118
}
106-
// TODO: handle pin alarms
107119
}
120+
return true;
108121
}

ports/esp32s2/common-hal/alarm/pin/PinAlarm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2020 @microDev1 (GitHub)
76
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
87
*
98
* 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
3837
self->pull = pull;
3938
}
4039

41-
const mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self) {
40+
mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self) {
4241
return self->pins;
4342
}
4443

ports/esp32s2/common-hal/alarm/time/DurationAlarm.c renamed to ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2020 @microDev1 (GitHub)
76
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
87
*
98
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -29,23 +28,12 @@
2928

3029
#include "py/runtime.h"
3130

32-
#include "shared-bindings/alarm/time/DurationAlarm.h"
31+
#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
3332

34-
void common_hal_alarm_time_duration_alarm_construct(alarm_time_duration_alarm_obj_t *self, mp_float_t duration) {
35-
self->duration = duration;
33+
void common_hal_alarm_time_monotonic_time_alarm_construct(alarm_time_monotonic_time_alarm_obj_t *self, mp_float_t monotonic_time) {
34+
self->monotonic_time = monotonic_time;
3635
}
3736

38-
mp_float_t common_hal_alarm_time_duration_alarm_get_duration(alarm_time_duration_alarm_obj_t *self) {
39-
return self->duration;
40-
}
41-
42-
void common_hal_alarm_time_duration_alarm_enable(alarm_time_duration_alarm_obj_t *self) {
43-
if (esp_sleep_enable_timer_wakeup((uint64_t) (self->duration * 1000000)) == ESP_ERR_INVALID_ARG) {
44-
mp_raise_ValueError(translate("duration out of range"));
45-
}
46-
}
47-
48-
void common_hal_alarm_time_duration_alarm_disable (alarm_time_duration_alarm_obj_t *self) {
49-
(void) self;
50-
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
37+
mp_float_t common_hal_alarm_time_monotonic_time_alarm_get_monotonic_time(alarm_time_monotonic_time_alarm_obj_t *self) {
38+
return self->monotonic_time;
5139
}

ports/esp32s2/common-hal/alarm/time/DurationAlarm.h renamed to ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2020 @microDev1 (GitHub)
76
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
87
*
98
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -30,5 +29,5 @@
3029

3130
typedef struct {
3231
mp_obj_base_t base;
33-
mp_float_t duration; // seconds
34-
} alarm_time_duration_alarm_obj_t;
32+
mp_float_t monotonic_time; // values compatible with time.monotonic_time()
33+
} alarm_time_monotonic_time_alarm_obj_t;

py/circuitpy_defns.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ SRC_COMMON_HAL_ALL = \
303303
_pew/__init__.c \
304304
alarm/__init__.c \
305305
alarm/pin/PinAlarm.c \
306-
alarm/time/DurationAlarm.c \
306+
alarm/time/MonotonicTimeAlarm.c \
307307
analogio/AnalogIn.c \
308308
analogio/AnalogOut.c \
309309
analogio/__init__.c \

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
679679
#if MICROPY_PY_BUILTINS_FLOAT
680680
mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
681681
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
682+
extern mp_float_t uint64_to_float(uint64_t ui64);
682683
#endif
683684
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
684685
mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);

py/objfloat.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
333333
return mp_obj_new_float(lhs_val);
334334
}
335335

336+
// Convert a uint64_t to a 32-bit float without invoking the double-precision math routines,
337+
// which are large.
338+
mp_float_t uint64_to_float(uint64_t ui64) {
339+
// 4294967296 = 2^32
340+
return (mp_float_t) ((uint32_t) (ui64 >> 32) * 4294967296.0f + (uint32_t) (ui64 & 0xffffffff));
341+
}
342+
336343
#pragma GCC diagnostic pop
337344

338345
#endif // MICROPY_PY_BUILTINS_FLOAT

shared-bindings/alarm/__init__.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#include "shared-bindings/alarm/__init__.h"
3131
#include "shared-bindings/alarm/pin/PinAlarm.h"
32-
#include "shared-bindings/alarm/time/DurationAlarm.h"
32+
#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
3333

3434
//| """Power-saving light and deep sleep. Alarms can be set to wake up from sleep.
3535
//|
@@ -60,7 +60,7 @@
6060
void validate_objs_are_alarms(size_t n_args, const mp_obj_t *objs) {
6161
for (size_t i = 0; i < n_args; i++) {
6262
if (MP_OBJ_IS_TYPE(objs[i], &alarm_pin_pin_alarm_type) ||
63-
MP_OBJ_IS_TYPE(objs[i], &alarm_time_duration_alarm_type)) {
63+
MP_OBJ_IS_TYPE(objs[i], &alarm_time_monotonic_time_alarm_type)) {
6464
continue;
6565
}
6666
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_
8686
//| When awakened, the microcontroller will restart and run ``boot.py`` and ``code.py``
8787
//| from the beginning.
8888
//|
89-
//| The alarm that caused the wake-up is available as `alarm.wake_alarm`.
89+
//| An alarm equivalent to the one that caused the wake-up is available as `alarm.wake_alarm`.
90+
//| Its type and/or attributes may not correspond exactly to the original alarm.
91+
//| For time-base alarms, currently, an `alarm.time.MonotonicTimeAlarm()` is created.
9092
//|
9193
//| If you call this routine more than once, only the last set of alarms given will be used.
9294
//| """
@@ -121,7 +123,7 @@ STATIC const mp_obj_module_t alarm_pin_module = {
121123
STATIC const mp_map_elem_t alarm_time_globals_table[] = {
122124
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
123125

124-
{ MP_ROM_QSTR(MP_QSTR_DurationAlarm), MP_OBJ_FROM_PTR(&alarm_time_duration_alarm_type) },
126+
{ MP_ROM_QSTR(MP_QSTR_MonotonicTimeAlarm), MP_OBJ_FROM_PTR(&alarm_time_monotonic_time_alarm_type) },
125127
};
126128

127129
STATIC MP_DEFINE_CONST_DICT(alarm_time_globals, alarm_time_globals_table);

0 commit comments

Comments
 (0)