55
66#if PBDRV_CONFIG_BUTTON_GPIO
77
8- #include <contiki .h>
8+ #include <stddef .h>
99
1010#include <pbdrv/gpio.h>
1111#include <pbio/button.h>
1212#include <pbio/config.h>
1313#include <pbio/error.h>
14+ #include <pbio/os.h>
1415
1516#include "button_gpio.h"
1617
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-
25- void pbdrv_button_init (void ) {
26- for (int i = 0 ; i < PBDRV_CONFIG_BUTTON_GPIO_NUM_BUTTON ; i ++ ) {
27- const pbdrv_button_gpio_platform_t * platform = & pbdrv_button_gpio_platform [i ];
28- pbdrv_gpio_set_pull (& platform -> gpio , platform -> pull );
29- pbdrv_gpio_input (& platform -> gpio );
30- }
31-
32- #if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
33- process_start (& pbdrv_button_process );
34- #endif
35- }
36-
3718/**
3819 * Reads the current button state.
3920 * @returns Flags that reflect the current button state.
@@ -52,26 +33,24 @@ static pbio_button_flags_t pbdrv_button_gpio_read(void) {
5233 return flags ;
5334}
5435
55- pbio_button_flags_t pbdrv_button_get_pressed (void ) {
56- #if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
57- return pbdrv_button_state ;
58- #else
59- return pbdrv_button_gpio_read ();
60- #endif
61- }
62-
6336#if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
6437
65- PROCESS_THREAD (pbdrv_button_process , ev , data ) {
66- static struct etimer timer ;
38+ static pbio_button_flags_t pbdrv_button_state ;
39+
40+ static pbio_os_process_t pbdrv_button_process ;
41+
42+ pbio_error_t pbdrv_button_process_thread (pbio_os_state_t * state , void * context ) {
43+
44+ static pbio_os_timer_t timer ;
45+
6746 static pbio_button_flags_t prev , next ;
6847
69- PROCESS_BEGIN ( );
48+ PBIO_OS_ASYNC_BEGIN ( state );
7049
71- etimer_set (& timer , 10 );
50+ pbio_os_timer_set (& timer , 10 );
7251
7352 for (;;) {
74- PROCESS_WAIT_EVENT_UNTIL ( ev == PROCESS_EVENT_TIMER && etimer_expired (& timer ));
53+ PBIO_OS_AWAIT_UNTIL ( state , pbio_os_timer_is_expired (& timer ));
7554
7655 next = pbdrv_button_gpio_read ();
7756
@@ -82,12 +61,33 @@ PROCESS_THREAD(pbdrv_button_process, ev, data) {
8261
8362 prev = next ;
8463
85- etimer_reset (& timer );
64+ pbio_os_timer_extend (& timer );
8665 }
8766
88- PROCESS_END ();
67+ // Unreachable
68+ PBIO_OS_ASYNC_END (PBIO_ERROR_FAILED );
8969}
9070
9171#endif // PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
9272
73+ pbio_button_flags_t pbdrv_button_get_pressed (void ) {
74+ #if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
75+ return pbdrv_button_state ;
76+ #else
77+ return pbdrv_button_gpio_read ();
78+ #endif
79+ }
80+
81+ void pbdrv_button_init (void ) {
82+ for (int i = 0 ; i < PBDRV_CONFIG_BUTTON_GPIO_NUM_BUTTON ; i ++ ) {
83+ const pbdrv_button_gpio_platform_t * platform = & pbdrv_button_gpio_platform [i ];
84+ pbdrv_gpio_set_pull (& platform -> gpio , platform -> pull );
85+ pbdrv_gpio_input (& platform -> gpio );
86+ }
87+
88+ #if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
89+ pbio_os_process_start (& pbdrv_button_process , pbdrv_button_process_thread , NULL );
90+ #endif
91+ }
92+
9393#endif // PBDRV_CONFIG_BUTTON_GPIO
0 commit comments