Skip to content

Commit af2787f

Browse files
committed
pbio/port: Reset power to port on disconnect.
We used to turn power off only at the end of active sensor processes as well as at the end of a program. In general, we want to turn power off whenever something is disconnected, which is what we do here. The goal here is to be able to turn off 9V to a sensor if a user has switched it on and then unplugs the device.
1 parent 8f2b930 commit af2787f

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/pbio/src/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ static pbio_error_t pbio_port_process_lego_dcm_thread(pbio_os_state_t *state, vo
125125
for (;;) {
126126
// Run passive device connection manager until smart device is detected.
127127
pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_GPIO_ADC);
128+
pbio_port_p1p2_set_power(port, PBIO_PORT_POWER_REQUIREMENTS_NONE);
128129
PBIO_OS_AWAIT(state, &port->child1, err = pbio_port_dcm_thread(&port->child1, &port->timer, port->connection_manager, port->pdata->pins));
129130

130131
// Active device detected. Check type to decide next steps.
@@ -145,7 +146,6 @@ static pbio_error_t pbio_port_process_lego_dcm_thread(pbio_os_state_t *state, vo
145146
pbio_port_lump_data_recv_thread(&port->child1, port->lump_dev, port->uart_dev),
146147
pbio_port_lump_data_send_thread(&port->child2, port->lump_dev, port->uart_dev, &port->timer)
147148
);
148-
pbio_port_p1p2_set_power(port, PBIO_PORT_POWER_REQUIREMENTS_NONE);
149149
}
150150
}
151151

lib/pbio/src/port_dcm_pup.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ static const lego_device_type_id_t legodev_pup_type_id_lookup[3][3] = {
7474
* Thread that detects the device type. It monitors the ID1 and ID2 pins
7575
* on the port to see when devices are connected or disconnected.
7676
*
77+
* It ends once a LEGO UART device is connected or nothing is connected.
78+
*
7779
* @param [in] state The process thread state.
7880
* @param [in] timer The timer to use for timing.
7981
* @param [in] dcm The device connection manager.
@@ -86,8 +88,10 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
8688
dcm->prev_type_id = LEGO_DEVICE_TYPE_ID_NONE;
8789
dcm->dev_id_match_count = 0;
8890

89-
// Keep running until a UART device is definitively found.
90-
while (dcm->dev_id_match_count < AFFIRMATIVE_MATCH_COUNT || dcm->prev_type_id != LEGO_DEVICE_TYPE_ID_LPF2_UNKNOWN_UART) {
91+
// Repeat until we definitively have a UART device or no device is
92+
// detected. So it stays here as long as a passive motor device is
93+
// attached.
94+
while (dcm->dev_id_match_count < AFFIRMATIVE_MATCH_COUNT || (dcm->prev_type_id != LEGO_DEVICE_TYPE_ID_LPF2_UNKNOWN_UART && dcm->prev_type_id != LEGO_DEVICE_TYPE_ID_NONE)) {
9195

9296
dcm->type_id = LEGO_DEVICE_TYPE_ID_NONE;
9397
dcm->dev_id1_group = DEV_ID_GROUP_OPEN;

0 commit comments

Comments
 (0)