1+ #include <rtthread.h>
2+ #include <rtdevice.h>
13#include "multi_button.h"
24
3- struct Button btn1 ;
4- struct Button btn2 ;
5+ static struct button btn ;
56
6- int read_button1_GPIO ()
7- {
8- return HAL_GPIO_ReadPin (B1_GPIO_Port , B1_Pin );
9- }
7+ #define BUTTON_PIN (10)
108
11- int read_button2_GPIO ()
9+ static uint8_t button_read_pin ( void )
1210{
13- return HAL_GPIO_ReadPin ( B2_GPIO_Port , B2_Pin );
11+ return rt_pin_read ( BUTTON_PIN );
1412}
1513
16- int main ( )
14+ void button_callback ( void * btn )
1715{
18- button_init (& btn1 , read_button1_GPIO , 0 );
19- button_init (& btn2 , read_button2_GPIO , 0 );
20-
21- button_attach (& btn1 , PRESS_DOWN , BTN1_PRESS_DOWN_Handler );
22- button_attach (& btn1 , PRESS_UP , BTN1_PRESS_UP_Handler );
23- button_attach (& btn1 , PRESS_REPEAT , BTN1_PRESS_REPEAT_Handler );
24- button_attach (& btn1 , SINGLE_CLICK , BTN1_SINGLE_Click_Handler );
25- button_attach (& btn1 , DOUBLE_CLICK , BTN1_DOUBLE_Click_Handler );
26- button_attach (& btn1 , LONG_RRESS_START , BTN1_LONG_RRESS_START_Handler );
27- button_attach (& btn2 , LONG_PRESS_HOLD , BTN1_LONG_PRESS_HOLD_Handler );
28-
29- button_attach (& btn2 , PRESS_DOWN , BTN2_PRESS_DOWN_Handler );
30- button_attach (& btn2 , PRESS_UP , BTN2_PRESS_UP_Handler );
31- button_attach (& btn2 , PRESS_REPEAT , BTN2_PRESS_REPEAT_Handler );
32- button_attach (& btn2 , SINGLE_CLICK , BTN2_SINGLE_Click_Handler );
33- button_attach (& btn2 , DOUBLE_CLICK , BTN2_DOUBLE_Click_Handler );
34- button_attach (& btn2 , LONG_RRESS_START , BTN2_LONG_RRESS_START_Handler );
35- button_attach (& btn2 , LONG_PRESS_HOLD , BTN2_LONG_PRESS_HOLD_Handler );
36-
37- button_start (& btn1 );
38- button_start (& btn2 );
39-
40- //make the timer invoking the button_ticks() interval 5ms.
41- //This function is implemented by yourself.
42- __timer_start (button_ticks , 0 , 5 );
16+ uint32_t btn_event_val ;
17+
18+ btn_event_val = get_button_event ((struct button * )btn );
19+
20+ switch (btn_event_val )
21+ {
22+ case PRESS_DOWN :
23+ rt_kprintf ("button press down\n" );
24+ break ;
4325
44- while (1 )
45- {}
26+ case PRESS_UP :
27+ rt_kprintf ("button press up\n" );
28+ break ;
29+
30+ case PRESS_REPEAT :
31+ rt_kprintf ("button press repeat\n" );
32+ break ;
33+
34+ case SINGLE_CLICK :
35+ rt_kprintf ("button single click\n" );
36+ break ;
37+
38+ case DOUBLE_CLICK :
39+ rt_kprintf ("button double click\n" );
40+ break ;
41+
42+ case LONG_RRESS_START :
43+ rt_kprintf ("button long press start\n" );
44+ break ;
45+
46+ case LONG_PRESS_HOLD :
47+ rt_kprintf ("button long press hold\n" );
48+ break ;
49+ }
4650}
4751
48- void BTN1_PRESS_DOWN_Handler (void * btn )
52+ void btn_thread_entry (void * p )
4953{
50- //do something...
54+ while (1 )
55+ {
56+ /* 5ms */
57+ rt_thread_delay (RT_TICK_PER_SECOND /200 );
58+ button_ticks ();
59+ }
5160}
5261
53- void BTN1_PRESS_UP_Handler (void * btn )
62+ int multi_button_test (void )
5463{
55- //do something...
56- }
64+ rt_thread_t thread = RT_NULL ;
65+
66+ /* Create background ticks thread */
67+ thread = rt_thread_create ("btn" , btn_thread_entry , RT_NULL , 1024 , 10 , 10 );
68+ if (thread == RT_NULL )
69+ {
70+ return RT_ERROR ;
71+ }
72+ rt_thread_startup (thread );
73+
74+ /* low level drive */
75+ rt_pin_mode (BUTTON_PIN , PIN_MODE_INPUT );
76+ button_init (& btn , button_read_pin , PIN_LOW );
77+ button_attach (& btn , PRESS_DOWN , button_callback );
78+ button_attach (& btn , PRESS_UP , button_callback );
79+ button_attach (& btn , PRESS_REPEAT , button_callback );
80+ button_attach (& btn , SINGLE_CLICK , button_callback );
81+ button_attach (& btn , DOUBLE_CLICK , button_callback );
82+ button_attach (& btn , LONG_RRESS_START , button_callback );
83+ button_attach (& btn , LONG_PRESS_HOLD , button_callback );
84+ button_start (& btn );
5785
58- ...
86+ return RT_EOK ;
87+ }
88+ INIT_APP_EXPORT (multi_button_test );
0 commit comments