30
30
#define TICKS_PERIOD 0x80000000
31
31
#define TICKS_DIFF (t1 , t0 ) ((int32_t)(((t1 - t0 + TICKS_PERIOD / 2) & (TICKS_PERIOD - 1)) - TICKS_PERIOD / 2))
32
32
33
+ static bool microbit_soft_timer_paused = false;
34
+
33
35
STATIC int microbit_soft_timer_lt (mp_pairheap_t * n1 , mp_pairheap_t * n2 ) {
34
36
microbit_soft_timer_entry_t * e1 = (microbit_soft_timer_entry_t * )n1 ;
35
37
microbit_soft_timer_entry_t * e2 = (microbit_soft_timer_entry_t * )n2 ;
@@ -38,10 +40,14 @@ STATIC int microbit_soft_timer_lt(mp_pairheap_t *n1, mp_pairheap_t *n2) {
38
40
39
41
void microbit_soft_timer_deinit (void ) {
40
42
MP_STATE_PORT (soft_timer_heap ) = NULL ;
43
+ microbit_soft_timer_paused = false;
41
44
}
42
45
43
46
// This function can be executed at interrupt priority.
44
47
void microbit_soft_timer_handler (void ) {
48
+ if (microbit_soft_timer_paused ) {
49
+ return ;
50
+ }
45
51
uint32_t ticks_ms = mp_hal_ticks_ms ();
46
52
microbit_soft_timer_entry_t * heap = MP_STATE_PORT (soft_timer_heap );
47
53
while (heap != NULL && TICKS_DIFF (heap -> expiry_ms , ticks_ms ) <= 0 ) {
@@ -67,3 +73,19 @@ void microbit_soft_timer_insert(microbit_soft_timer_entry_t *entry, uint32_t ini
67
73
MP_STATE_PORT (soft_timer_heap ) = (microbit_soft_timer_entry_t * )mp_pairheap_push (microbit_soft_timer_lt , & MP_STATE_PORT (soft_timer_heap )-> pairheap , & entry -> pairheap );
68
74
MICROPY_END_ATOMIC_SECTION (atomic_state );
69
75
}
76
+
77
+ void microbit_soft_timer_set_pause (bool paused ) {
78
+ microbit_soft_timer_paused = paused ;
79
+ }
80
+
81
+ uint32_t microbit_soft_timer_get_ms_to_next_expiry (void ) {
82
+ microbit_soft_timer_entry_t * heap = MP_STATE_PORT (soft_timer_heap );
83
+ if (heap == NULL ) {
84
+ return UINT32_MAX ;
85
+ }
86
+ int32_t dt = TICKS_DIFF (heap -> expiry_ms , mp_hal_ticks_ms ());
87
+ if (dt <= 0 ) {
88
+ return 0 ;
89
+ }
90
+ return dt ;
91
+ }
0 commit comments