Skip to content

Commit 4c253e3

Browse files
committed
pbio/drv/button_resistor_ladder: Wait until ready.
Higher level UI code becomes cleaner if buttons just work. If buttons aren't working, we shouldn't be booting.
1 parent 1e29752 commit 4c253e3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lib/pbio/drv/button/button_resistor_ladder.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,45 @@
77

88
#if PBDRV_CONFIG_BUTTON_RESISTOR_LADDER
99

10+
#include <stddef.h>
11+
1012
#include <pbdrv/resistor_ladder.h>
1113
#include <pbio/button.h>
1214
#include <pbio/error.h>
15+
#include <pbio/os.h>
16+
17+
#include "../core.h"
18+
19+
static pbio_os_process_t pbdrv_button_init_process;
20+
21+
/**
22+
* Wait for the buttons to get ready before giving control to system code.
23+
*
24+
* The resistor ladders give unknown values for 10s of milliseconds after boot.
25+
*
26+
* This ensures that system level functions such as the UI don't need hacks to
27+
* wait for buttons to start working.
28+
*/
29+
pbio_error_t pbdrv_button_init_process_thread(pbio_os_state_t *state, void *context) {
30+
31+
static pbio_os_timer_t timer;
32+
pbdrv_resistor_ladder_ch_flags_t flags;
33+
34+
PBIO_OS_ASYNC_BEGIN(state);
35+
36+
while (pbdrv_resistor_ladder_get(0, &flags) != PBIO_SUCCESS || pbdrv_resistor_ladder_get(1, &flags) != PBIO_SUCCESS) {
37+
PBIO_OS_AWAIT_MS(state, &timer, 10);
38+
}
39+
PBIO_OS_AWAIT_MS(state, &timer, 30);
40+
41+
pbdrv_init_busy_down();
42+
43+
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
44+
}
1345

1446
void pbdrv_button_init(void) {
47+
pbdrv_init_busy_up();
48+
pbio_os_process_start(&pbdrv_button_init_process, pbdrv_button_init_process_thread, NULL);
1549
}
1650

1751
pbio_button_flags_t pbdrv_button_get_pressed(void) {

0 commit comments

Comments
 (0)