Skip to content

Commit

Permalink
pbdrv/ioport: turn off passive devices on stop
Browse files Browse the repository at this point in the history
This ensures that devices like train motors stop when the program ends. Fixes pybricks/support#226
  • Loading branch information
laurensvalk committed Jan 22, 2021
1 parent b22ad15 commit dfbb4be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/pbio/drv/ioport/ioport_lpf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <lego_uart.h>

#include <pbdrv/gpio.h>
#include <pbdrv/motor.h>
#include <pbio/error.h>
#include <pbio/iodev.h>
#include <pbio/uartdev.h>
Expand Down Expand Up @@ -254,6 +255,15 @@ pbio_error_t pbdrv_ioport_get_iodev(pbio_port_t port, pbio_iodev_t **iodev) {
return PBIO_SUCCESS;
}

// Turns off power to passive (non-uart) devices
void pbio_ioport_reset_passive_devices(void) {
for (int i = 0; i < PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS; i++) {
if (ioport_devs[i].connected_type_id != PBIO_IODEV_TYPE_ID_LPF2_UNKNOWN_UART) {
pbdrv_motor_coast(i + PBDRV_CONFIG_FIRST_IO_PORT);
}
}
}

// This is the device connection manager (dcm). It monitors the ID1 and ID2 pins
// on the port to see when devices are connected or disconnected.
// It is expected for there to be a 2ms delay between calls to this function.
Expand Down
5 changes: 5 additions & 0 deletions lib/pbio/include/pbdrv/ioport.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@

pbio_error_t pbdrv_ioport_get_iodev(pbio_port_t port, pbio_iodev_t **iodev);

void pbio_ioport_reset_passive_devices(void);

#else // PBDRV_CONFIG_IOPORT

static inline pbio_error_t pbdrv_ioport_get_iodev(pbio_port_t port, pbio_iodev_t **iodev) {
*iodev = NULL;
return PBIO_ERROR_NOT_SUPPORTED;
}

static inline void pbio_ioport_reset_passive_devices(void) {
}

#endif // PBDRV_CONFIG_IOPORT

#endif // _PBDRV_IOPORT_H_
Expand Down
2 changes: 2 additions & 0 deletions lib/pbio/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <pbdrv/motor.h>
#include <pbdrv/sound.h>
#include <pbio/config.h>
#include <pbdrv/ioport.h>
#include <pbio/light_matrix.h>
#include <pbio/light.h>
#include "pbio/motor_process.h"
Expand Down Expand Up @@ -71,6 +72,7 @@ void pbio_stop_all(void) {
#if PBIO_CONFIG_LIGHT
pbio_light_animation_stop_all();
#endif
pbio_ioport_reset_passive_devices();
pbio_motor_process_reset();
pbdrv_sound_stop();
}
Expand Down

0 comments on commit dfbb4be

Please sign in to comment.