Skip to content

Commit 7fadcb3

Browse files
committed
pbdrv/ioport: turn off passive devices on stop
This ensures that devices like train motors stop when the program ends. Fixes pybricks/support#226
1 parent b22ad15 commit 7fadcb3

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

lib/pbio/drv/ioport/ioport_lpf2.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <lego_uart.h>
1616

1717
#include <pbdrv/gpio.h>
18+
#include <pbdrv/motor.h>
1819
#include <pbio/error.h>
1920
#include <pbio/iodev.h>
2021
#include <pbio/uartdev.h>
@@ -254,6 +255,15 @@ pbio_error_t pbdrv_ioport_get_iodev(pbio_port_t port, pbio_iodev_t **iodev) {
254255
return PBIO_SUCCESS;
255256
}
256257

258+
// Turns off power to passive (non-uart) devices
259+
void pbio_ioport_reset_passive_devices(void) {
260+
for (int i = 0; i < PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS; i++) {
261+
if (ioport_devs[i].connected_type_id != PBIO_IODEV_TYPE_ID_LPF2_UNKNOWN_UART) {
262+
pbdrv_motor_coast(i + PBDRV_CONFIG_FIRST_IO_PORT);
263+
}
264+
}
265+
}
266+
257267
// This is the device connection manager (dcm). It monitors the ID1 and ID2 pins
258268
// on the port to see when devices are connected or disconnected.
259269
// It is expected for there to be a 2ms delay between calls to this function.

lib/pbio/include/pbdrv/ioport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020

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

23+
void pbio_ioport_reset_passive_devices(void);
24+
2325
#else // PBDRV_CONFIG_IOPORT
2426

2527
static inline pbio_error_t pbdrv_ioport_get_iodev(pbio_port_t port, pbio_iodev_t **iodev) {
2628
*iodev = NULL;
2729
return PBIO_ERROR_NOT_SUPPORTED;
2830
}
2931

32+
static inline void pbio_ioport_reset_passive_devices(void) {
33+
}
34+
3035
#endif // PBDRV_CONFIG_IOPORT
3136

3237
#endif // _PBDRV_IOPORT_H_

lib/pbio/src/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <pbdrv/motor.h>
1717
#include <pbdrv/sound.h>
1818
#include <pbio/config.h>
19+
#include <pbdrv/ioport.h>
1920
#include <pbio/light_matrix.h>
2021
#include <pbio/light.h>
2122
#include "pbio/motor_process.h"
@@ -71,6 +72,9 @@ void pbio_stop_all(void) {
7172
#if PBIO_CONFIG_LIGHT
7273
pbio_light_animation_stop_all();
7374
#endif
75+
#if PBDRV_CONFIG_IOPORT_LPF2
76+
pbio_ioport_reset_passive_devices();
77+
#endif
7478
pbio_motor_process_reset();
7579
pbdrv_sound_stop();
7680
}

0 commit comments

Comments
 (0)