Skip to content

Commit 817d136

Browse files
committed
pbio/main: Introduce deinit.
Now we have exit points to match the entries for drv, pbio, and sys. We can already use it to call port deinit, but we'll be closing other processes too.
1 parent df33e3e commit 817d136

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

bricks/virtualhub/mp_port.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "pbio_os_config.h"
1818

19+
#include <pbdrv/core.h>
20+
1921
#include <pbio/main.h>
2022
#include <pbio/os.h>
2123
#include <pbsys/core.h>
@@ -70,8 +72,8 @@ bool pbsys_main_stdin_event(uint8_t c) {
7072
// MICROPY_PORT_INIT_FUNC
7173
void pb_virtualhub_port_init(void) {
7274

75+
pbdrv_init();
7376
pbio_init(true);
74-
7577
pbsys_init();
7678

7779
pbsys_status_set(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING);

lib/pbio/include/pbio/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pbio/config.h"
1010

1111
void pbio_init(bool start_processes);
12+
void pbio_deinit(void);
1213
void pbio_stop_all(bool reset);
1314

1415
#endif // _PBIO_MAIN_H_

lib/pbio/src/main.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,9 @@
88

99
#include <stdbool.h>
1010

11-
#include <contiki.h>
12-
13-
#include <pbdrv/button.h>
14-
#include <pbdrv/config.h>
15-
#include <pbdrv/core.h>
1611
#include <pbdrv/sound.h>
17-
#include <pbio/config.h>
18-
#include <pbio/dcmotor.h>
12+
1913
#include <pbio/imu.h>
20-
#include <pbio/light_matrix.h>
21-
#include <pbio/light.h>
22-
#include <pbio/main.h>
2314
#include <pbio/motor_process.h>
2415
#include <pbio/port_interface.h>
2516

@@ -35,7 +26,6 @@
3526
* tests that test one driver at a time.
3627
*/
3728
void pbio_init(bool start_processes) {
38-
pbdrv_init();
3929

4030
pbio_imu_init();
4131

@@ -51,14 +41,23 @@ void pbio_init(bool start_processes) {
5141
}
5242

5343
/**
54-
* Stops all user-level background processes. Drivers and OS-level processes
55-
* continue running.
44+
* Deinitialize pbio modules that are not needed after soft-poweroff.
45+
*/
46+
void pbio_deinit(void) {
47+
// Power off sensors and motors, including the ones that are always powered.
48+
pbio_port_power_off();
49+
}
50+
51+
/**
52+
* Stops all user-level background processes. Called when the user application
53+
* completes to get these modules back into their default state. Drivers and
54+
* OS-level processes continue running.
5655
*
5756
* @param [in] reset Whether to reset all user-level processes to a clean
5857
* state (true), or whether to only stop active outputs
59-
* like sound or motors (false). The latter is useful
60-
* to preserve the state for debugging, without sound
61-
* or movement getting in the way or out of control.
58+
* like sound or motors (false). The latter is useful to
59+
* preserve the state for debugging, without sound or
60+
* movement getting in the way, or out of control.
6261
*/
6362
void pbio_stop_all(bool reset) {
6463
#if PBIO_CONFIG_LIGHT

lib/pbio/sys/core.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ void pbsys_init(void) {
6565

6666
void pbsys_deinit(void) {
6767

68+
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING);
69+
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
70+
6871
pbsys_storage_deinit();
6972
pbsys_hub_light_matrix_deinit();
7073

lib/pbio/sys/main.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pbio_error_t pbsys_main_program_request_start(pbio_pybricks_user_program_id_t id
8181
*/
8282
int main(int argc, char **argv) {
8383

84+
pbdrv_init();
8485
pbio_init(true);
8586
pbsys_init();
8687

@@ -131,17 +132,16 @@ int main(int argc, char **argv) {
131132
}
132133
}
133134

134-
// Power off sensors and motors, including the ones that are always powered.
135-
// This also makes it easier to see that users can let go of the button.
136-
pbio_port_power_off();
137-
138-
// Stop system processes and save user data before we shutdown.
139-
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING);
140-
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
135+
// Stop system processes and selected drivers in reverse order. This will
136+
// also save user data to flash,
141137
pbsys_deinit();
142138

143139
// Now lower-level processes may shutdown and/or power off.
140+
pbio_deinit();
144141
pbdrv_deinit();
142+
143+
// REVISIT: We should use the deinit hooks above to gracefully request exit
144+
// instead of having the drivers rely on system statuses.
145145
pbsys_status_set(PBIO_PYBRICKS_STATUS_SHUTDOWN);
146146

147147
// The power could be held on due to someone pressing the center button

lib/pbio/test/test-pbio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include "../drv/motor_driver/motor_driver_virtual_simulation.h"
1515

16+
#include <pbdrv/core.h>
17+
1618
#include <pbio/main.h>
1719

1820
#include <contiki.h>
@@ -52,6 +54,7 @@ static void pbio_test_run_thread(void *env, bool start_pbio_processes) {
5254
pbdrv_motor_driver_disable_process();
5355
}
5456

57+
pbdrv_init();
5558
pbio_init(start_pbio_processes);
5659

5760
PT_INIT(&pt);
@@ -111,6 +114,7 @@ void pbio_test_run_thread_with_pbio_os_processes(void *env) {
111114
// when not needed.
112115
pbdrv_motor_driver_disable_process();
113116

117+
pbdrv_init();
114118
pbio_init(true);
115119

116120
clock_gettime(CLOCK_MONOTONIC, &start_time);

0 commit comments

Comments
 (0)