Skip to content

Commit 9b10fc1

Browse files
committed
micropython: Update event loop hooks.
This is a drop-in replacement for our original pb_event_poll_hook, but prepares for the new MicroPython 2.0 API. Fixes pybricks/support#2412.
1 parent 02b2206 commit 9b10fc1

File tree

9 files changed

+31
-31
lines changed

9 files changed

+31
-31
lines changed

bricks/_common/micropython.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@
4141
#include "py/stackctrl.h"
4242
#include "py/stream.h"
4343

44-
// Implementation for MICROPY_EVENT_POLL_HOOK
45-
void pb_event_poll_hook(void) {
46-
47-
while (pbio_os_run_processes_once()) {
48-
}
49-
50-
mp_handle_pending(true);
51-
52-
pbio_os_run_processes_and_wait_for_event();
53-
}
54-
5544
// callback for when stop button is pressed in IDE or on hub
5645
void pbsys_main_stop_program(bool force_stop) {
5746
if (force_stop) {

bricks/_common/mpconfigport.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,18 @@ typedef long mp_off_t;
167167
} \
168168
} while (0)
169169

170-
#define MICROPY_EVENT_POLL_HOOK \
170+
#define MICROPY_INTERNAL_EVENT_HOOK \
171171
do { \
172-
extern void pb_event_poll_hook(void); \
173-
pb_event_poll_hook(); \
172+
PYBRICKS_VM_HOOK_LOOP_EXTRA \
173+
extern bool pbio_os_run_processes_once(void); \
174+
while (pbio_os_run_processes_once()) { \
175+
} \
176+
} while (0);
177+
178+
#define MICROPY_INTERNAL_WFE(TIMEOUT_MS) \
179+
do { \
180+
extern void pbio_os_run_processes_and_wait_for_event(void); \
181+
pbio_os_run_processes_and_wait_for_event(); \
174182
} while (0);
175183

176184
// We need to provide a declaration/definition of alloca()

bricks/_common/mphalport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void mp_hal_delay_ms(mp_uint_t Delay) {
2727
// This macro will execute the necessary idle behaviour. It may
2828
// raise an exception, switch threads or enter sleep mode (waiting for
2929
// (at least) the SysTick interrupt).
30-
MICROPY_EVENT_POLL_HOOK
30+
mp_event_wait_indefinite();
3131
} while (pbdrv_clock_get_ms() - start < Delay);
3232
}
3333

@@ -48,7 +48,7 @@ int mp_hal_stdin_rx_chr(void) {
4848

4949
// wait for rx interrupt
5050
while (size = 1, pbsys_host_stdin_read(&c, &size) != PBIO_SUCCESS) {
51-
MICROPY_EVENT_POLL_HOOK
51+
mp_event_wait_indefinite();
5252
}
5353

5454
return c;
@@ -75,7 +75,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
7575

7676
// Allow long prints to be interrupted.
7777
if (remaining) {
78-
MICROPY_EVENT_POLL_HOOK
78+
mp_event_wait_indefinite();
7979
}
8080
}
8181

bricks/nxt/mphalport.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void mp_hal_delay_ms(mp_uint_t Delay) {
2828
// This macro will execute the necessary idle behaviour. It may
2929
// raise an exception, switch threads or enter sleep mode (waiting for
3030
// (at least) the SysTick interrupt).
31-
MICROPY_EVENT_POLL_HOOK
31+
mp_event_wait_indefinite();
3232
} while (pbdrv_clock_get_ms() - start < Delay);
3333
}
3434

@@ -53,7 +53,7 @@ extern bool nx_bt_is_ready(void);
5353
int mp_hal_stdin_rx_chr(void) {
5454

5555
while (!nx_bt_is_ready()) {
56-
MICROPY_EVENT_POLL_HOOK
56+
mp_event_wait_indefinite();
5757
}
5858

5959
uint8_t rx_char;
@@ -63,7 +63,7 @@ int mp_hal_stdin_rx_chr(void) {
6363

6464
// wait for data to be read
6565
while (nx_bt_stream_data_read() != sizeof(rx_char)) {
66-
MICROPY_EVENT_POLL_HOOK
66+
mp_event_wait_indefinite();
6767
}
6868

6969
return rx_char;
@@ -73,7 +73,7 @@ int mp_hal_stdin_rx_chr(void) {
7373
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
7474

7575
while (!nx_bt_is_ready()) {
76-
MICROPY_EVENT_POLL_HOOK
76+
mp_event_wait_indefinite();
7777
}
7878

7979
// Nothing to do if disconnected or empty data
@@ -83,7 +83,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
8383

8484
nx_bt_stream_write((uint8_t *)str, len);
8585
while (!nx_bt_stream_data_written()) {
86-
MICROPY_EVENT_POLL_HOOK;
86+
mp_event_wait_indefinite();
8787
}
8888

8989
return len;

pybricks/common/pb_type_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void *pb_type_device_get_data_blocking(mp_obj_t self_in, uint8_t mode) {
5050
pb_assert(pbio_port_lump_set_mode(sensor->lump_dev, mode));
5151
pbio_error_t err;
5252
while ((err = pbio_port_lump_is_ready(sensor->lump_dev)) == PBIO_ERROR_AGAIN) {
53-
MICROPY_EVENT_POLL_HOOK
53+
mp_event_wait_indefinite();
5454
}
5555
pb_assert(err);
5656
void *data = NULL;

pybricks/common/pb_type_system.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ static MP_DEFINE_CONST_FUN_OBJ_1(pb_type_System_set_stop_button_obj, pb_type_Sys
9999

100100
static mp_obj_t pb_type_System_shutdown(void) {
101101

102-
// Start shutdown.
102+
// Request shutdown. This will make the program_stop module call
103+
// pbsys_main_stop_program, which schedules mp_sched_vm_abort to stop
104+
// the MicroPython application (gracefully as far as pbsys is concerned).
105+
// Then, pbsys will proceed to shutdown instead of running another program.
103106
pbsys_status_set(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST);
104107

105-
// Keep running MicroPython until we are stopped.
108+
// Keep running MicroPython until mp_sched_vm_abort is eventually handled.
106109
for (;;) {
107-
MICROPY_EVENT_POLL_HOOK;
110+
mp_event_wait_indefinite();
108111
}
109112

110113
return mp_const_none;

pybricks/hubs/pb_type_movehub.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@
7878
static void motion_spi_wait(void) {
7979
do {
8080
while (!(SPI1->SR & SPI_SR_RXNE)) {
81-
MICROPY_EVENT_POLL_HOOK;
81+
mp_event_wait_indefinite();
8282
}
83-
MICROPY_EVENT_POLL_HOOK;
83+
mp_event_wait_indefinite();
8484
} while (SPI1->SR & SPI_SR_BSY);
8585
}
8686

@@ -96,7 +96,7 @@ static void motion_spi_read(uint8_t reg, uint8_t *value) {
9696
while (SPI1->SR & SPI_SR_RXNE) {
9797
uint8_t dummy = BYTE_ACCESS(SPI1->DR);
9898
(void)dummy;
99-
MICROPY_EVENT_POLL_HOOK;
99+
mp_event_wait_indefinite();
100100
}
101101
BYTE_ACCESS(SPI1->DR) = 0;
102102

pybricks/tools/pb_module_tools.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static pbio_button_flags_t pb_module_tools_hub_menu_wait_for_press(bool press) {
220220

221221
pbio_button_flags_t btn;
222222
while ((bool)(btn = pbdrv_button_get_pressed()) == !press) {
223-
MICROPY_EVENT_POLL_HOOK;
223+
mp_event_wait_indefinite();
224224
}
225225
return btn;
226226
}

pybricks/tools/pb_type_async.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ mp_obj_t pb_type_async_wait_or_await(pb_type_async_t *config, pb_type_async_t **
142142
// Otherwise wait for completion here without allocating the iterable.
143143
pbio_error_t err;
144144
while ((err = config->iter_once(&config->state, config->parent_obj)) == PBIO_ERROR_AGAIN) {
145-
MICROPY_EVENT_POLL_HOOK;
145+
mp_event_wait_indefinite();
146146
}
147147
pb_assert(err);
148148
return config->return_map ? config->return_map(config->parent_obj) : mp_const_none;

0 commit comments

Comments
 (0)