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 , 200 );
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
0 commit comments