Skip to content

Commit 423233f

Browse files
markshannondpgeorge
authored andcommitted
Update all modules to use new sleep(): mp_hal_delay_ms.
1 parent 68642ff commit 423233f

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

inc/microbit/mphal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ extern "C" {
3535
void mp_hal_init(void);
3636

3737
unsigned int mp_hal_get_milliseconds(void);
38-
void mp_hal_milli_delay(unsigned int ms);
3938

4039
void mp_hal_set_interrupt_char(int c);
4140
bool mp_hal_stdin_rx_any(void);
@@ -50,6 +49,8 @@ void mp_hal_erase_line_from_cursor(uint n_chars);
5049

5150
void mp_hal_display_string(const char*);
5251

52+
void mp_hal_delay_ms(int ms);
53+
5354
#ifdef __cplusplus
5455
}
5556
#endif

source/microbit/microbitmusic.cpp

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

3030
extern "C" {
3131

32+
#include "mphal.h"
3233
#include "py/runtime.h"
3334
#include "py/objstr.h"
3435
#include "modmicrobit.h"
@@ -152,15 +153,15 @@ STATIC void play_note(microbit_music_obj_t *self, const char *note_str, size_t n
152153
}
153154

154155
// Cut off 10ms from end of note so we hear articulation.
155-
uBit.sleep((ms_per_tick * self->last_duration) - 10);
156+
mp_hal_delay_ms((ms_per_tick * self->last_duration) - 10);
156157

157158
if (note_index >= 10) {
158159
pin->setAnalogValue(128);
159160
}
160161

161162
pin->setAnalogValue(0);
162163
// Add 10ms of silence to the end of note so we hear articulation.
163-
uBit.sleep(10);
164+
mp_hal_delay_ms(10);
164165
}
165166

166167
STATIC mp_obj_t microbit_music_reset(mp_obj_t self_in) {
@@ -241,7 +242,7 @@ STATIC mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args,
241242
const char * note_str = mp_obj_str_get_data(items[i], &note_len);
242243
play_note(self, note_str, note_len, pin, args[2].u_bool);
243244
} else {
244-
uBit.sleep((60000/self->bpm));
245+
mp_hal_delay_ms((60000/self->bpm));
245246
}
246247
// allow CTRL-C to stop the tune
247248
if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
@@ -280,7 +281,7 @@ STATIC mp_obj_t microbit_music_pitch(mp_uint_t n_args, const mp_obj_t *pos_args,
280281

281282
if (length >= 0) {
282283
if (wait) {
283-
uBit.sleep(length);
284+
mp_hal_delay_ms(length);
284285
pin->setAnalogValue(0);
285286
} else {
286287
// FIXME: schedule a callback to stop

source/microbit/modantigravity.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
extern "C" {
3232

33+
#include "mphal.h"
3334
#include "microbitimage.h"
3435
#include "microbitdisplay.h"
3536

@@ -40,7 +41,7 @@ void antigravity(uint8_t interval = 200 /* ms */) {
4041
// move all of the LED's upwards (we can move them in other directions in the future)
4142

4243
for (uint8_t iteration = 0; iteration < 5; iteration++) {
43-
uBit.sleep(interval);
44+
mp_hal_delay_ms(interval);
4445
bool wait = false;
4546
for (uint8_t row = 1; row < 5 - iteration; row++) {
4647
for (uint8_t col = 0; col < 5; col++) {

source/microbit/modlove.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
extern "C" {
3232

33+
#include "mphal.h"
3334
#include "microbitimage.h"
3435
#include "microbitdisplay.h"
3536

@@ -46,11 +47,11 @@ void love(int interval = 80 /* ms */) {
4647
for(int iteration = 0; iteration < 5; iteration++) {
4748
for(int step = 0; step < 7; ++step) {
4849
microbit_display_print(&microbit_display_obj, hearts[step]);
49-
uBit.sleep(interval);
50+
mp_hal_delay_ms(interval);
5051
}
5152
for(int step = 6; step >= 0; --step) {
5253
microbit_display_print(&microbit_display_obj, hearts[step]);
53-
uBit.sleep(interval);
54+
mp_hal_delay_ms(interval);
5455
}
5556
}
5657
microbit_display_clear();

source/microbit/modmicrobit.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
extern "C" {
3030

3131
#include "py/obj.h"
32+
#include "mphal.h"
3233
#include "modmicrobit.h"
3334

3435
STATIC mp_obj_t microbit_reset_(void) {
@@ -39,19 +40,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(microbit_reset_obj, microbit_reset_);
3940

4041
STATIC mp_obj_t microbit_sleep(mp_obj_t ms_in) {
4142
mp_int_t ms = mp_obj_get_int(ms_in);
42-
if (ms <= 0)
43-
return mp_const_none;
44-
unsigned long current = uBit.systemTime();
45-
unsigned long wakeup = current + ms;
46-
if (wakeup < current) {
47-
// Overflow
48-
do {
49-
__WFI();
50-
} while (uBit.systemTime() > current);
51-
}
52-
do {
53-
__WFI();
54-
} while (uBit.systemTime() < wakeup);
43+
mp_hal_delay_ms(ms);
5544
return mp_const_none;
5645
}
5746
MP_DEFINE_CONST_FUN_OBJ_1(microbit_sleep_obj, microbit_sleep);

source/microbit/mphal.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,20 @@ void mp_hal_display_string(const char *str) {
127127
microbit_display_scroll(&microbit_display_obj, str, strlen(str), false);
128128
}
129129

130+
void mp_hal_delay_ms(mp_int_t ms) {
131+
if (ms <= 0)
132+
return;
133+
unsigned long current = uBit.systemTime();
134+
unsigned long wakeup = current + ms;
135+
if (wakeup < current) {
136+
// Overflow
137+
do {
138+
__WFI();
139+
} while (uBit.systemTime() > current);
140+
}
141+
do {
142+
__WFI();
143+
} while (uBit.systemTime() < wakeup);
144+
}
145+
130146
}

0 commit comments

Comments
 (0)