Skip to content

Commit a1cf040

Browse files
committed
pbio/port: Add explicit poweroff hook.
Partially implements #265
1 parent 1677902 commit a1cf040

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

lib/pbio/include/pbio/port_interface.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
* capabilities for a port.
3131
*/
3232
typedef enum {
33+
/**
34+
* No mode specified.
35+
*/
36+
PBIO_PORT_MODE_NONE = 0,
3337
/**
3438
* The port is in LEGO Powered Up mode, auto-detecting official active and
3539
* passive components. Runs LEGO UART Messaging protocol when a LUMP device
@@ -53,6 +57,8 @@ typedef enum {
5357

5458
void pbio_port_init(void);
5559

60+
bool pbio_port_poweroff_is_ready(void);
61+
5662
void pbio_port_stop_user_actions(bool reset);
5763

5864
pbio_error_t pbio_port_get_port(pbio_port_id_t id, pbio_port_t **port);
@@ -82,6 +88,9 @@ pbio_error_t pbio_port_set_mode(pbio_port_t *port, pbio_port_mode_t mode);
8288
static inline void pbio_port_init(void) {
8389
}
8490

91+
static inline bool pbio_port_poweroff_is_ready(void) {
92+
}
93+
8594
static inline void pbio_port_stop_user_actions(bool reset) {
8695
}
8796

lib/pbio/src/port.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,30 @@ void pbio_port_stop_user_actions(bool reset) {
478478
}
479479
}
480480

481+
/**
482+
* Prepares the system for power off by turning off all sensors and motors.
483+
*
484+
* @return Whether the system is ready to power off.
485+
*/
486+
487+
bool pbio_port_poweroff_is_ready(void) {
488+
// Stops motors if active, ignoring sensors that need permanent power.
489+
pbio_port_stop_user_actions(true);
490+
491+
// We also want to power off sensors that need power now and stop their
492+
// processes from triggering any further actions.
493+
for (uint8_t i = 0; i < PBIO_CONFIG_PORT_NUM_PORTS; i++) {
494+
pbio_port_t *port = &ports[i];
495+
pbio_port_set_mode(port, PBIO_PORT_MODE_NONE);
496+
}
497+
498+
// Use ioport platform-specific needs for power off.
499+
pbdrv_ioport_enable_vcc(PBDRV_IOPORT_VCC_SHUTDOWN);
500+
501+
// REVISIT: Test button quirk here.
502+
return true;
503+
}
504+
481505
void pbio_port_process_poll(void *port) {
482506
if (!port) {
483507
return;
@@ -512,6 +536,10 @@ pbio_error_t pbio_port_set_mode(pbio_port_t *port, pbio_port_mode_t mode) {
512536
port->mode = mode;
513537

514538
switch (mode) {
539+
case PBIO_PORT_MODE_NONE:
540+
pbdrv_ioport_p5p6_set_mode(port->pdata->pins, port->uart_dev, PBDRV_IOPORT_P5P6_MODE_GPIO_ADC);
541+
pbio_port_p1p2_set_power(port, PBIO_PORT_POWER_REQUIREMENTS_NONE);
542+
return PBIO_SUCCESS;
515543
case PBIO_PORT_MODE_LEGO_PUP:
516544
// Physical modes for this mode will be set by the process so this
517545
// is all we need to do here.

lib/pbio/sys/main.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
#include <stdint.h>
99

10-
#include <pbdrv/ioport.h>
1110
#include <pbdrv/reset.h>
1211
#include <pbdrv/usb.h>
1312
#include <pbio/main.h>
13+
#include <pbio/port_interface.h>
1414
#include <pbio/protocol.h>
1515
#include <pbsys/core.h>
1616
#include <pbsys/main.h>
@@ -129,8 +129,6 @@ int main(int argc, char **argv) {
129129
// Now lower-level processes may shutdown and/or power off.
130130
pbsys_status_set(PBIO_PYBRICKS_STATUS_SHUTDOWN);
131131

132-
pbdrv_ioport_enable_vcc(PBDRV_IOPORT_VCC_SHUTDOWN);
133-
134132
// The power could be held on due to someone pressing the center button
135133
// or USB being plugged in, so we have this loop to keep pumping events
136134
// to turn off most of the peripherals and keep the battery charger running.
@@ -140,6 +138,12 @@ int main(int argc, char **argv) {
140138
while (pbio_do_one_event()) {
141139
}
142140

141+
// Prepare ports for power off but don't turn off power yet if
142+
// ports are not ready.
143+
if (!pbio_port_poweroff_is_ready()) {
144+
continue;
145+
}
146+
143147
#if PBSYS_CONFIG_BATTERY_CHARGER
144148
// On hubs with USB battery chargers, we can't turn off power while
145149
// USB is connected, otherwise it disables the op-amp that provides

0 commit comments

Comments
 (0)