Skip to content

Commit c901eaa

Browse files
committed
[update] The example Adapts to RT-Thread and Fix the bug. | 例子适配RT-Thread并修复bug.
1 parent 29990a6 commit c901eaa

File tree

5 files changed

+285
-156
lines changed

5 files changed

+285
-156
lines changed

SConscript

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ inc = inc + [cwd]
1313
# add MultiButton basic code
1414
src = src + ['./multi_button.c']
1515

16+
# add MultiButton Test code
17+
if GetDepend('MULTIBUTTON_USING_EXAMPLE_ASYNC'):
18+
src = src + ['./examples/event_async.c']
19+
if GetDepend('MULTIBUTTON_USING_EXAMPLE_INQUIRE'):
20+
src = src + ['./examples/event_inquire.c']
21+
1622
# add group to IDE project
1723
group = DefineGroup('MultiButton', src, depend = ['PKG_USING_MULTIBUTTON'], CPPPATH = inc)
1824

examples/event_async.c

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,88 @@
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);

examples/event_inquire.c

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,79 @@
1+
#include <rtthread.h>
2+
#include <rtdevice.h>
13
#include "multi_button.h"
24

3-
struct Button btn1;
5+
static struct button btn;
46

5-
int read_button1_GPIO()
7+
#define BUTTON_PIN (10)
8+
9+
static uint8_t button_read_pin(void)
610
{
7-
return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
11+
return rt_pin_read(BUTTON_PIN);
812
}
913

10-
11-
int main()
14+
void btn_test_thread_entry(void *p)
1215
{
13-
static uint8_t btn1_event_val;
16+
uint32_t btn_event_val;
17+
18+
while(1)
19+
{
20+
if(btn_event_val != get_button_event(&btn))
21+
{
22+
btn_event_val = get_button_event(&btn);
1423

15-
button_init(&btn1, read_button1_GPIO, 0);
16-
button_start(&btn1);
24+
switch(btn_event_val)
25+
{
26+
case PRESS_DOWN:
27+
rt_kprintf("button press down\n");
28+
break;
1729

18-
//make the timer invoking the button_ticks() interval 5ms.
19-
//This function is implemented by yourself.
20-
__timer_start(button_ticks, 0, 5);
30+
case PRESS_UP:
31+
rt_kprintf("button press up\n");
32+
break;
2133

22-
while(1)
23-
{
24-
if(btn1_event_val != get_button_event(&btn1)) {
25-
btn1_event_val = get_button_event(&btn1);
26-
27-
if(btn1_event_val == PRESS_DOWN) {
28-
//do something
29-
} else if(btn1_event_val == PRESS_UP) {
30-
//do something
31-
} else if(btn1_event_val == LONG_PRESS_HOLD) {
32-
//do something
34+
case PRESS_REPEAT:
35+
rt_kprintf("button press repeat\n");
36+
break;
37+
38+
case SINGLE_CLICK:
39+
rt_kprintf("button single click\n");
40+
break;
41+
42+
case DOUBLE_CLICK:
43+
rt_kprintf("button double click\n");
44+
break;
45+
46+
case LONG_RRESS_START:
47+
rt_kprintf("button long press start\n");
48+
break;
49+
50+
case LONG_PRESS_HOLD:
51+
rt_kprintf("button long press hold\n");
52+
break;
3353
}
3454
}
55+
56+
button_ticks();
57+
rt_thread_delay(RT_TICK_PER_SECOND/200);
3558
}
3659
}
3760

61+
int multi_button_test(void)
62+
{
63+
rt_thread_t thread = RT_NULL;
64+
65+
thread = rt_thread_create("btn_test", btn_test_thread_entry, RT_NULL, 1024, 15, 10);
66+
if(thread == RT_NULL)
67+
{
68+
return RT_ERROR;
69+
}
70+
rt_thread_startup(thread);
71+
72+
/* low level drive */
73+
rt_pin_mode (BUTTON_PIN, PIN_MODE_INPUT);
74+
button_init (&btn, button_read_pin, PIN_LOW);
75+
button_start(&btn);
76+
77+
return RT_EOK;
78+
}
79+
INIT_APP_EXPORT(multi_button_test);

0 commit comments

Comments
 (0)