3030#define TICKS_PERIOD 0x80000000
3131#define TICKS_DIFF (t1 , t0 ) ((int32_t)(((t1 - t0 + TICKS_PERIOD / 2) & (TICKS_PERIOD - 1)) - TICKS_PERIOD / 2))
3232
33+ static bool microbit_soft_timer_paused = false;
34+
3335STATIC int microbit_soft_timer_lt (mp_pairheap_t * n1 , mp_pairheap_t * n2 ) {
3436 microbit_soft_timer_entry_t * e1 = (microbit_soft_timer_entry_t * )n1 ;
3537 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) {
3840
3941void microbit_soft_timer_deinit (void ) {
4042 MP_STATE_PORT (soft_timer_heap ) = NULL ;
43+ microbit_soft_timer_paused = false;
4144}
4245
4346// This function can be executed at interrupt priority.
4447void microbit_soft_timer_handler (void ) {
48+ if (microbit_soft_timer_paused ) {
49+ return ;
50+ }
4551 uint32_t ticks_ms = mp_hal_ticks_ms ();
4652 microbit_soft_timer_entry_t * heap = MP_STATE_PORT (soft_timer_heap );
4753 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
6773 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 );
6874 MICROPY_END_ATOMIC_SECTION (atomic_state );
6975}
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