Skip to content

Commit 6ee3094

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 c85bfeb commit 6ee3094

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

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

0 commit comments

Comments
 (0)