-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBtn_SM_Demo.c
238 lines (221 loc) · 8.8 KB
/
Btn_SM_Demo.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/******************************************************************************
* File : Btn_SM_Module.c
* Function : Provide demo of button state machine module.
* description: It is a demo for button state machine module.
* Version : V1.00
* Author : Ian
* Date : 15th Jun 2016
* History : No. When Who Version What
* 1 15/Jun/2016 Ian V1.00 Create
******************************************************************************/
#include "common.h"
#include "KL25_Lpt_Time.h"
#include "Btn_SM_Config.h"
#include "Btn_SM_Module.h"
const uint8* cg_apu8State[] = {"PRESS_EVT | __ ",\
"SHORT_RELEASE_EVT | __ ",\
"LONG_RELEASE_EVT | __ ",\
"PRESSED_EVT | __ ",\
"JUST_LONG_PRESSED_EVT| __ ",\
"SHORT_RELEASED_EVT | __ ",\
"LONG_RELEASED_EVT | __ ",\
"JUST_PRESS | | ",\
"SHORT_RELEASED | | ",\
"LONG_RELEASED | | ",\
"IDLE | | ",\
"SHORT_PRESSED | | ",\
"LONG_PRESSED | | ",\
"NONE_EVT "};
const uint8* cg_apu8Vol[] = {"Vol:[ ]",\
"Vol:[| ]",\
"Vol:[|| ]",\
"Vol:[||| ]",\
"Vol:[|||| ]",\
"Vol:[||||| ]",\
"Vol:[|||||| ]",\
"Vol:[||||||| ]",\
"Vol:[|||||||| ]",\
"Vol:[||||||||| ]",\
"Vol:[|||||||||| ]",\
"Vol:[||||||||||| ]",\
"Vol:[|||||||||||| ]",\
"Vol:[||||||||||||| ]",\
"Vol:[|||||||||||||| ]",\
"Vol:[|||||||||||||||]"};
static T_BTN_RESULT sg_atBtn[MAX_BTN_CH];
/******************************************************************************
* Name : uint8 Btn_St_Get(uint8 u8Ch)
* Function : Provide button state according to the channel number
* Input : uint8 u8Ch 1~255 The number of button channel
* Output: : None
* Return : uint8 BTN_STATE_0 0-State
* BTN_STATE_1 1-State
* BTN_ERROR Failed
* description: None.
* Version : V1.00
* Author : Ian
* Date : 15th Jun 2016
******************************************************************************/
uint8 Btn_St_Get(uint8 u8Ch)
{
uint8 u8Temp;
if(u8Ch == 1) /* If it is button 1 */
{ /* Get the button state */
u8Temp = (!(GPIOA_PDIR & (1 << 5)));
}
else if(u8Ch == 2) /* If it is button 2 */
{ /* Get the button state */
u8Temp = (!(GPIOA_PDIR & (1 << 4)));
}
else if(u8Ch == 3) /* If it is button 3 */
{ /* Get the button state */
u8Temp = (!(GPIOA_PDIR & (1 << 12)));
}
else/* If the button channel number is invalid */
{ /* Return error */
u8Temp = BTN_ERROR;
}
return u8Temp;
}
/******************************************************************************
* Name : uint16 System_Time(void)
* Function : Provide system time in ms
* Input : None
* Output: : None
* Return : uint16 0~65535 The system time in ms
* description: None.
* Version : V1.00
* Author : Ian
* Date : 15th Jun 2016
******************************************************************************/
uint16 System_Time(void)
{
uint16 u16Temp = (uint16)App_GetSystemTime_ms();
return u16Temp;
}
/******************************************************************************
* Name : void Gpio_Init()
* Function : Init GPIOs for as inputs for buttons
* Input : None
* Output: : None
* Return : None
* description: None.
* Version : V1.00
* Author : Ian
* Date : 15th Jun 2016
******************************************************************************/
void Gpio_Init()
{
PORTA_PCR12 = PORT_PCR_MUX(0x1); /* Set the PIN as a GPIO */
PORTA_PCR5 = PORT_PCR_MUX(0x1); /* Set the PIN as a GPIO */
PORTA_PCR4 = PORT_PCR_MUX(0x1); /* Set the PIN as a GPIO */
GPIOA_PDDR &= ~(1 << 12); /* Set the GPIO as input */
GPIOA_PDDR &= ~(1 << 5); /* Set the GPIO as input */
GPIOA_PDDR &= ~(1 << 4); /* Set the GPIO as input */
return;
}
/******************************************************************************
* Name : int main (void)
* Function : A demo shows how to use button state machine module
* Input : None
* Output: : None
* Return : None
* description: There are three buttons:
* Button 3 is used to switch operating mode between "button state
* display" and "Vol control". Long pressed to swtich.
* Button 1 is used to show button state in "button state display"
* mode, and increase vol in "Vol control" mode.
* Button 2 is used to decrease vol in "Vol control" mode only.
* Version : V1.10
* Author : Ian
* Date : 15th Jun 2016
******************************************************************************/
int main (void)
{
uint8 u8Idx,u8Vol = 0,u8FnCode = 1;;
uint16 u16Tm = App_GetSystemTime_ms();
/* Init hardware */
Gpio_Init();
Timer_Init();
/* Esay_Init */
Btn_SM_Easy_Init(System_Time, Btn_St_Get);
while(1)
{
/* Button process and get button event & state */
for(u8Idx = 1; u8Idx <= MAX_BTN_CH; u8Idx++)
{
Btn_Channel_Process(u8Idx, &(sg_atBtn[u8Idx-1]));
}
/* If the button 3 is long pressed */
if(BTN_LONG_PRESSED_EVT == sg_atBtn[2].u8Evt)
{ /* Switch operating mode */
u8FnCode = !u8FnCode;
}
/* If it is the "button state" operation mode */
if(u8FnCode)
{ /* If there is any event with button 1 */
if(sg_atBtn[0].u8Evt != BTN_NONE_EVT)
{ /* Display the event of button 1 */
printf("EVENT:%s\n",cg_apu8State[sg_atBtn[0].u8Evt]);
}
/* If the refresh time is up */
if(App_GetSystemDelay_ms(u16Tm) > 100)
{ /* Display the state of button 1 */
printf("STATE:%s\n",cg_apu8State[sg_atBtn[0].u8State]);
u16Tm = App_GetSystemTime_ms();
}
}
/* If it is the "Vol control" operating mode */
else
{ /* If button 1 is short pressed (event) */
if(sg_atBtn[0].u8Evt == BTN_PRESSED_EVT)
{
if(15 != u8Vol)
{ /* Increase the vol within the range */
u8Vol++;
}
/* Reset the timing */
u16Tm = App_GetSystemTime_ms();
}
/* If button 1 is in long pressed state */
if(sg_atBtn[0].u8State == BTN_HOLDING_ST)
{ /* If re-do time is up */
if(App_GetSystemDelay_ms(u16Tm) > 150)
{
if(15 != u8Vol)
{ /* Increase the vol within the range */
u8Vol++;
}
/* Reset the timing */
u16Tm = App_GetSystemTime_ms();
}
}
/* If button 2 is short pressed (event) */
if(sg_atBtn[1].u8Evt == BTN_PRESSED_EVT)
{
if(0 != u8Vol)
{ /* Decrease the vol within the range */
u8Vol--;
}
/* Reset the timing */
u16Tm = App_GetSystemTime_ms();
}
/* If button 2 is in long pressed state */
if(sg_atBtn[1].u8State == BTN_HOLDING_ST)
{ /* If re-do time is up */
if(App_GetSystemDelay_ms(u16Tm)>150)
{
if(0 != u8Vol)
{ /* Decrease the vol within the range */
u8Vol--;
}
/* Reset the timing */
u16Tm = App_GetSystemTime_ms();
}
}
printf("%s\n", cg_apu8Vol[u8Vol]);
}
}
return 0;
}
/*End of file*/