Skip to content

Commit 7d56fe8

Browse files
committed
pbio/drv/button_gpio: Wait for release and delay on power down.
Fixes pybricks/support#2176
1 parent aca95a4 commit 7d56fe8

File tree

6 files changed

+32
-3
lines changed

6 files changed

+32
-3
lines changed

lib/pbio/drv/button/button.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
*/
1616
void pbdrv_button_init(void);
1717

18+
/**
19+
* De-initializes the low level button driver.
20+
*/
21+
void pbdrv_button_deinit(void);
22+
1823
#else
1924

2025
#define pbdrv_button_init()
26+
#define pbdrv_button_deinit()
2127

2228
#endif
2329

lib/pbio/drv/button/button_gpio.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <pbdrv/gpio.h>
1111
#include <pbio/button.h>
12+
#include <pbio/busy_count.h>
1213
#include <pbio/config.h>
1314
#include <pbio/error.h>
1415
#include <pbio/os.h>
@@ -49,7 +50,9 @@ pbio_error_t pbdrv_button_process_thread(pbio_os_state_t *state, void *context)
4950

5051
pbio_os_timer_set(&timer, 10);
5152

52-
for (;;) {
53+
// Loop until cancellation is requested on power off and buttons are released.
54+
while (!pbdrv_button_process.request || pbdrv_button_state) {
55+
5356
PBIO_OS_AWAIT_UNTIL(state, pbio_os_timer_is_expired(&timer));
5457

5558
next = pbdrv_button_gpio_read();
@@ -64,8 +67,11 @@ pbio_error_t pbdrv_button_process_thread(pbio_os_state_t *state, void *context)
6467
pbio_os_timer_extend(&timer);
6568
}
6669

67-
// Unreachable
68-
PBIO_OS_ASYNC_END(PBIO_ERROR_FAILED);
70+
// Wait a while after release to prevent accidental power on.
71+
PBIO_OS_AWAIT_MS(state, &timer, 100);
72+
pbio_busy_count_down();
73+
74+
PBIO_OS_ASYNC_END(PBIO_ERROR_CANCELED);
6975
}
7076

7177
#endif // PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
@@ -90,4 +96,11 @@ void pbdrv_button_init(void) {
9096
#endif
9197
}
9298

99+
void pbdrv_button_deinit(void) {
100+
#if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
101+
pbio_busy_count_up();
102+
pbio_os_process_make_request(&pbdrv_button_process, PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL);
103+
#endif
104+
}
105+
93106
#endif // PBDRV_CONFIG_BUTTON_GPIO

lib/pbio/drv/button/button_nxt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
void pbdrv_button_init(void) {
1515
}
1616

17+
void pbdrv_button_deinit(void) {
18+
}
19+
1720
pbio_button_flags_t pbdrv_button_get_pressed(void) {
1821

1922
nx_avr_button_t button = nx_avr_get_button();

lib/pbio/drv/button/button_resistor_ladder.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ void pbdrv_button_init(void) {
4848
pbio_os_process_start(&pbdrv_button_init_process, pbdrv_button_init_process_thread, NULL);
4949
}
5050

51+
void pbdrv_button_deinit(void) {
52+
}
53+
5154
pbio_button_flags_t pbdrv_button_get_pressed(void) {
5255
pbdrv_resistor_ladder_ch_flags_t flags;
5356
pbio_error_t err;

lib/pbio/drv/button/button_test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ void pbio_test_button_set_pressed(pbio_button_flags_t flags) {
1919
void pbdrv_button_init(void) {
2020
}
2121

22+
void pbdrv_button_deinit(void) {
23+
}
24+
2225
pbio_button_flags_t pbdrv_button_get_pressed(void) {
2326
return pbio_test_button_flags;
2427
}

lib/pbio/drv/core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ void pbdrv_deinit(void) {
8989

9090
pbdrv_imu_deinit();
9191
pbdrv_bluetooth_deinit();
92+
pbdrv_button_deinit();
9293

9394
while (pbio_busy_count_busy()) {
9495
pbio_os_run_processes_once();

0 commit comments

Comments
 (0)