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}
0 commit comments