forked from 530china/BitsButton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_callback.c
More file actions
151 lines (128 loc) · 3.4 KB
/
example_callback.c
File metadata and controls
151 lines (128 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "embedded_button.h"
enum button_id_e {
btn1_id = 0,
btn2_id,
};
struct button_obj_t button1;
struct button_obj_t button2;
uint8_t read_button_pin(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case btn1_id:
return get_button1_value(); //Require self implementation
break;
case btn2_id:
return get_button2_value(); //Require self implementation
break;
default:
return 0;
break;
}
return 0;
}
void single_click_handle(void* btn)
{
//do something...
printf("/****single click****/\r\n");
}
void double_click_handle(void* btn)
{
//do something...
printf("/****double click****/\r\n");
}
void long_press_handle(void* btn)
{
//do something...
printf("/****long press****/\r\n");
}
void single_click_then_long_press_handle(void* btn)
{
//do something...
printf("/****single click and long press****/\r\n");
}
void quintuple_click_handle(void* btn)
{
//do something...
if(check_is_repeat_click_mode(btn))
{// To implement the logic where any key press that occurs five or more times in succession is treated as a five-hit press.
printf("/****quintuple click****/\r\n");
}
}
void repeat_click_handle(void* btn)
{
//do something...
if(check_is_repeat_click_mode(btn))
{
printf("/****repeat click****/\r\n");
if(get_button_key_value(&button2) == 0b1010)
{
printf("/****button2 double click****/\r\n");
}
if(get_button_key_value(&button2) == 0b101010)
{
printf("/****button2 triple click****/\r\n");
}
if(get_button_key_value(&button2) == 0b10101010)
{
printf("/****button2 quadruple click****/\r\n");
}
}
}
void filter_ending_with_long_press_handle(void* btn)
{
printf("/****an event ending with a long press on a key****/\r\n");
}
const key_value_match_map_t button1_map[] =
{
{
.tar_result = SINGLE_CLICK_KV,
.kv_func_cb = single_click_handle
},
{
.tar_result = DOUBLE_CLICK_KV,
.kv_func_cb = double_click_handle
},
{
.tar_result = LONG_PRESEE_START,
.kv_func_cb = long_press_handle
},
{
.tar_result = SINGLE_CLICK_THEN_LONG_PRESS_KV,
.kv_func_cb = single_click_then_long_press_handle
},
{
.operand = 0b1010101010,
.operator = KV_MATCH_OPERATOR_BITWISE_AND,
.tar_result = 0b1010101010,
.kv_func_cb = quintuple_click_handle
}
};
const key_value_match_map_t button2_map[] =
{
{
.operand = 0b1010,
.operator = KV_MATCH_OPERATOR_BITWISE_AND,
.tar_result = 0b1010,
.kv_func_cb = repeat_click_handle
},
{
.operand = 0b1111,
.operator = KV_MATCH_OPERATOR_BITWISE_AND,
.tar_result = 0b1110,
.kv_func_cb = filter_ending_with_long_press_handle
}
};
int main()
{
button_init(&button1, read_button_pin, 0, btn1_id, button1_map, ARRAY_SIZE(button1_map));
button_start(&button1);
button_init(&button2, read_button_pin, 0, btn2_id, button2_map, ARRAY_SIZE(button2_map));
button_start(&button2);
//make the timer invoking the button_ticks() interval 5ms.
//This function is implemented by yourself.
__timer_start(button_ticks, 0, 5);
while(1)
{}
}