|
1 | 1 | // SPDX-License-Identifier: MIT |
2 | | -// Copyright (c) 2018-2020 The Pybricks Authors |
| 2 | +// Copyright (c) 2018-2022 The Pybricks Authors |
3 | 3 |
|
4 | 4 | #include <pbdrv/config.h> |
5 | 5 |
|
6 | 6 | #if PBDRV_CONFIG_BUTTON_GPIO |
7 | 7 |
|
| 8 | +#include <contiki.h> |
| 9 | + |
8 | 10 | #include <pbdrv/gpio.h> |
9 | 11 | #include <pbio/button.h> |
10 | 12 | #include <pbio/config.h> |
11 | 13 | #include <pbio/error.h> |
12 | 14 |
|
13 | 15 | #include "button_gpio.h" |
14 | 16 |
|
| 17 | +#if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE |
| 18 | + |
| 19 | +PROCESS(pbdrv_button_process, "button"); |
| 20 | + |
| 21 | +static pbio_button_flags_t pbdrv_button_state; |
| 22 | + |
| 23 | +#endif // PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE |
| 24 | + |
15 | 25 | void pbdrv_button_init(void) { |
16 | 26 | for (int i = 0; i < PBDRV_CONFIG_BUTTON_GPIO_NUM_BUTTON; i++) { |
17 | 27 | const pbdrv_button_gpio_platform_t *platform = &pbdrv_button_gpio_platform[i]; |
18 | 28 | pbdrv_gpio_set_pull(&platform->gpio, platform->pull); |
19 | 29 | pbdrv_gpio_input(&platform->gpio); |
20 | 30 | } |
| 31 | + |
| 32 | + #if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE |
| 33 | + process_start(&pbdrv_button_process); |
| 34 | + #endif |
21 | 35 | } |
22 | 36 |
|
23 | | -pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed) { |
24 | | - *pressed = 0; |
| 37 | +/** |
| 38 | + * Reads the current button state. |
| 39 | + * @returns Flags that reflect the current button state. |
| 40 | + */ |
| 41 | +static pbio_button_flags_t pbdrv_button_gpio_read(void) { |
| 42 | + pbio_button_flags_t flags = 0; |
25 | 43 |
|
26 | 44 | for (int i = 0; i < PBDRV_CONFIG_BUTTON_GPIO_NUM_BUTTON; i++) { |
27 | 45 | const pbdrv_button_gpio_platform_t *platform = &pbdrv_button_gpio_platform[i]; |
| 46 | + |
28 | 47 | if (!!pbdrv_gpio_input(&platform->gpio) ^ platform->active_low) { |
29 | | - *pressed |= platform->button; |
| 48 | + flags |= platform->button; |
30 | 49 | } |
31 | 50 | } |
32 | 51 |
|
| 52 | + return flags; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed) { |
| 57 | + #if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE |
| 58 | + *pressed = pbdrv_button_state; |
| 59 | + #else |
| 60 | + *pressed = pbdrv_button_gpio_read(); |
| 61 | + #endif |
| 62 | + |
33 | 63 | return PBIO_SUCCESS; |
34 | 64 | } |
35 | 65 |
|
| 66 | +#if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE |
| 67 | + |
| 68 | +PROCESS_THREAD(pbdrv_button_process, ev, data) { |
| 69 | + static struct etimer timer; |
| 70 | + static pbio_button_flags_t prev, next; |
| 71 | + |
| 72 | + PROCESS_BEGIN(); |
| 73 | + |
| 74 | + etimer_set(&timer, 10); |
| 75 | + |
| 76 | + for (;;) { |
| 77 | + PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); |
| 78 | + |
| 79 | + next = pbdrv_button_gpio_read(); |
| 80 | + |
| 81 | + // when we get the same state twice in a row, consider it "good" |
| 82 | + if (next == prev) { |
| 83 | + pbdrv_button_state = next; |
| 84 | + } |
| 85 | + |
| 86 | + prev = next; |
| 87 | + |
| 88 | + etimer_reset(&timer); |
| 89 | + } |
| 90 | + |
| 91 | + PROCESS_END(); |
| 92 | +} |
| 93 | + |
| 94 | +#endif // PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE |
| 95 | + |
36 | 96 | #endif // PBDRV_CONFIG_BUTTON_GPIO |
0 commit comments