-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.c
228 lines (183 loc) · 6.19 KB
/
example.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/***************************************************************************
* Copyright(C)2014-2015 by FlyLu <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include ".\key.h"
#define PRINT_RESET_FSM() do {s_tState = START;} while(0)
fsm_rt_t print(uint8_t *pchStr)
{
static uint8_t *s_pchStr;
static enum {
START = 0,
CHECK,
PRINT,
} s_tState = START;
switch (s_tState) {
case START:
s_pchStr = pchStr;
s_tState = CHECK;
//break;
case CHECK:
if (0 != *s_pchStr) {
s_tState = PRINT;
} else {
PRINT_RESET_FSM();
return fsm_rt_cpl;
}
case PRINT:
if (GENERAL_OUTPUT(*s_pchStr)) {
s_pchStr ++;
s_tState = CHECK;
}
break;
}
return fsm_rt_on_going;
}
#define KEY_1 1
#define KEY_2 2
uint8_t get_key_scaned_value()
{
if (IS_KEY1_DOWN()) {
return KEY_1;
} else if (IS_KEY2_DOWN() ) {
return KEY_2;
} else {
return KEY_NULL;
}
}
#define KEY_APP_RESET_FSM() do {s_tState = START;} while(0)
fsm_rt_t key_app(void)
{
static key_t s_tKey;
static enum {
START = 0,
CHECK_KEY,
KEY_1_UP,
KEY_1_DOWN,
KEY_1_PRESSED,
KEY_1_LONG_PRESSED,
KEY_1_REPEATE,
KEY_2_UP,
KEY_2_DOWN,
KEY_2_PRESSED,
KEY_2_LONG_PRESSED,
KEY_2_REPEATE,
} s_tState = START;
switch (s_tState) {
case START:
s_tState = CHECK_KEY;
//break;
case CHECK_KEY:
if (get_key(&s_tKey)) {
if (KEY_1 == s_tKey.chKeyValue) {
if (KEY_UP == s_tKey.tEvent) {
s_tState = KEY_1_UP;
} else if (KEY_DOWN == s_tKey.tEvent) {
s_tState = KEY_1_DOWN;
} else if (KEY_PRESSED == s_tKey.tEvent) {
s_tState = KEY_1_PRESSED;
} else if (KEY_LONG_PRESSED == s_tKey.tEvent) {
s_tState = KEY_1_LONG_PRESSED;
} else if (KEY_REPEAT == s_tKey.tEvent) {
s_tState = KEY_1_REPEATE;
}
} else if (KEY_2 == s_tKey.chKeyValue) {
if (KEY_UP == s_tKey.tEvent) {
s_tState = KEY_2_UP;
} else if (KEY_DOWN == s_tKey.tEvent) {
s_tState = KEY_2_DOWN;
} else if (KEY_PRESSED == s_tKey.tEvent) {
s_tState = KEY_2_PRESSED;
} else if (KEY_LONG_PRESSED == s_tKey.tEvent) {
s_tState = KEY_2_LONG_PRESSED;
} else if (KEY_REPEAT == s_tKey.tEvent) {
s_tState = KEY_2_REPEATE;
}
}
}
break;
case KEY_1_UP:
if (fsm_rt_cpl == print("KEY1 UP\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_1_DOWN:
if (fsm_rt_cpl == print("KEY1 DOWN\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_1_PRESSED:
if (fsm_rt_cpl == print("KEY1 PRESSED\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_1_LONG_PRESSED:
if (fsm_rt_cpl == print("KEY1 LONG PRESSED\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_1_REPEATE:
if (fsm_rt_cpl == print("KEY1 REPEAT\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_2_UP:
if (fsm_rt_cpl == print("KEY2 UP\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_2_DOWN:
if (fsm_rt_cpl == print("KEY2 DOWN\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_2_PRESSED:
if (fsm_rt_cpl == print("KEY2 PRESSED\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_2_LONG_PRESSED:
if (fsm_rt_cpl == print("KEY2 LONG PRESSED\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
case KEY_2_REPEATE:
if (fsm_rt_cpl == print("KEY2 REPEAT\r\n")) {
KEY_APP_RESET_FSM();
return fsm_rt_cpl;
}
break;
}
return fsm_rt_on_going;
}
int main(void)
{
key_init();
for(;;) {
key_task();
key_app();
}
}