Key.c 15.7 KB
Newer Older
hu's avatar
hu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14

#include "Key.h"

/*------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------*/

/*-----按键服务函数调用周期。单位ms--------*/
#define KEY_CALL_CYCLE 10U

/*-----------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/
typedef struct
{
hu's avatar
hu committed
15 16
    uint16_t u16KeyShortPressTime;     /*¶Ì°´¼ì²âʱ¼ä*/
    uint16_t u16KeyLongPressTime;      /*³¤°´¼ì²âʱ¼ä*/
17
    uint16_t u16KeySuperLongPressTime; /*³¬³¤°´¼ì²âʱ¼ä*/
hu's avatar
hu committed
18 19 20
    uint16_t u16KeyLoosenTime;         /*ËÉ¿ª¼ì²âʱ¼ä*/
    uint16_t u16KeyValidOFFTime;       /*ÓÐЧOFF¼ì²âʱ¼ä*/
    uint16_t u16KeyValidONTime;        /*ÓÐЧON¼ì²âʱ¼ä*/
hu's avatar
hu committed
21 22 23 24 25

} Key_Parameter_st_t;

typedef struct
{
26 27 28 29 30 31 32 33
    Key_Real_Status_en_t enKeyTypeLast;
    uint16_t u16KeyInvalidTimeCount;
    uint16_t u16KeyValidTimeCount;
    uint16_t u16KeyFirstIGNTimeRecord;
    Key_IGN_en_t enKeyIGNFirstStatus;
    Key_IGN_en_t enKeyIGNStatusLast;
    uint8_t u8KeyIGNChangeFlag;
    uint16_t u16KeyIGNTimeCount;
hu's avatar
hu committed
34 35 36 37

} Key_Ctr_st_t;
/*------------------------------------------------------------------------------------------*/

hu's avatar
hu committed
38
#pragma ghs section bss = ".myNonInitArea"
hu's avatar
hu committed
39
static Key_Parameter_st_t stKeyParameter;
hu's avatar
hu committed
40
#pragma ghs section bss = default
hu's avatar
hu committed
41 42

static Key_Ctr_st_t stKeyCtrVariate[KEY_NUM_MAX];
hu's avatar
hu committed
43 44 45 46
// static Key_IGN_en_t enKeyIGNFirstStatus = KEY_IG_INVALID;
// static Key_IGN_en_t enKeyIGNStatusLast = KEY_IG_INVALID;
// static uint8_t u8KeyIGNChangeFlag = 0U;
// static uint16_t u16KeyIGNTimeCount;
hu's avatar
hu committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60

/*-----------------------------------------------------------------------------------------*/
static void Key_Process(Key_Num_en_t enKeyIndex);
void Key_Service(void);
void Key_Init(void);
Key_Set_en_t Key_Parameter_Set_Short_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_Long_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_Super_Long_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_OFF_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_ON_Press_Time(uint16_t u16Time);

/*-----------------------------------------------------*/
static void Key_Process(Key_Num_en_t enKeyIndex)
{
61 62 63 64 65
    if (enKeyIndex < KEY_NUM_MAX)
    {
        /*Key_Real_Status_en_t enKeyRealType = (*stKeyAttribute[enKeyIndex].penKeyRealStatus);*/
        Key_Real_Status_en_t enKeyRealType = KEY_CAN_NONE;
        Key_IGN_en_t enKeyIGNRealStatus = Key_Get_IGN_Status();
hu's avatar
hu committed
66

67 68 69 70 71 72 73 74 75 76 77 78
        if (stKeyAttribute[enKeyIndex].pfnKeyReadStatusCallBack != 0U)
        {
            enKeyRealType = stKeyAttribute[enKeyIndex].pfnKeyReadStatusCallBack();
        }
        /*CAN°´¼ü*/
        if (stKeyAttribute[enKeyIndex].enKeyType == KEY_NO_DEBOUNCE_RISE)
        {
            if (enKeyRealType <= KEY_CAN_LONG_PRESS_NOT_RELEASE)
            {
                if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
                {
                    if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_NONE) &&
79
                            (enKeyRealType == KEY_CAN_SHORT_PRESS))
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SHORT_PRESS);
                    }
                    else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_NONE) &&
                             (enKeyRealType == KEY_CAN_LONG_PRESS))
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_LONG_PRESS);
                    }
                    else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_NONE) &&
                             (enKeyRealType == KEY_CAN_LONG_PRESS_NOT_RELEASE))
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SUPER_LONG_PRESS);
                    }
                    else
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_NONE);
                    }
                }
hu's avatar
hu committed
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112
                stKeyCtrVariate[enKeyIndex].enKeyTypeLast = enKeyRealType;
            }
            else
            {
                stKeyCtrVariate[enKeyIndex].enKeyTypeLast = KEY_CAN_NONE;
            }
        }
        else if (stKeyAttribute[enKeyIndex].enKeyType == KEY_NO_DEBOUNCE_FALL)
        {
            if (enKeyRealType <= KEY_CAN_LONG_PRESS_NOT_RELEASE)
            {
                if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
                {
                    if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_SHORT_PRESS) &&
113
                            (enKeyRealType == KEY_CAN_NONE))
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SHORT_PRESS);
                    }
                    else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_LONG_PRESS) &&
                             (enKeyRealType == KEY_CAN_NONE))
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_LONG_PRESS);
                    }
                    else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_LONG_PRESS_NOT_RELEASE) &&
                             (enKeyRealType == KEY_CAN_NONE))
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SUPER_LONG_PRESS);
                    }
                    else
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_NONE);
                    }
                }
hu's avatar
hu committed
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
                stKeyCtrVariate[enKeyIndex].enKeyTypeLast = enKeyRealType;
            }
            else
            {
                stKeyCtrVariate[enKeyIndex].enKeyTypeLast = KEY_CAN_NONE;
            }
        }
        /*Ó²Ïß°´¼ü*/
        else if (stKeyAttribute[enKeyIndex].enKeyType == KEY_NEED_DEBOUNCE_NORMAL)
        {
            if (enKeyRealType == KEY_LINE_LOOSEN) /*空闲*/
            {
                if (stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount <= stKeyParameter.u16KeyLoosenTime)
                {
                    stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount += KEY_CALL_CYCLE;
                }
                else
                {
                    stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
                    stKeyCtrVariate[enKeyIndex].enKeyTypeLast = KEY_LINE_LOOSEN;
                    stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
                    stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus = KEY_IG_INVALID;
                    stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast = KEY_IG_INVALID;
                    stKeyCtrVariate[enKeyIndex].u8KeyIGNChangeFlag = 0U;
                    stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount = 0U;
                }
                if ((stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount >= stKeyParameter.u16KeyLoosenTime) &&
160 161
                        (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeyShortPressTime) &&
                        (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount < stKeyParameter.u16KeyLongPressTime))
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
                {
                    stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
                    /* call back */
                    if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
                    {
                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SHORT_PRESS);
                    }
                }
            }
            else if (enKeyRealType == KEY_LINE_PRESS) /*按键按下*/
            {
                if (stKeyCtrVariate[enKeyIndex].enKeyTypeLast != enKeyRealType)
                {
                    stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
                    stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
                    stKeyCtrVariate[enKeyIndex].enKeyTypeLast = enKeyRealType;
                    stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount = 0U;
hu's avatar
hu committed
179

180
                    stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus = enKeyIGNRealStatus;
hu's avatar
hu committed
181

182 183 184 185 186 187 188 189 190 191 192
                    stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast = enKeyIGNRealStatus;
                }
                else
                {
                    if (stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast != enKeyIGNRealStatus)
                    {
                        stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast = enKeyIGNRealStatus;
                        if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus != enKeyIGNRealStatus)
                        {
                            stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount;
                        }
hu's avatar
hu committed
193

194 195 196 197 198 199 200 201 202
                        stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount = 0U;
                    }
                    else
                    {
                        if (stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount < 0xFFF0U)
                        {
                            stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount += KEY_CALL_CYCLE;
                        }
                    }
hu's avatar
hu committed
203

204 205 206 207 208 209 210 211
                    if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount < 0xFFF0U)
                    {
                        stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount += KEY_CALL_CYCLE;
                    }
                    if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus != enKeyIGNRealStatus)
                    {
                        stKeyCtrVariate[enKeyIndex].u8KeyIGNChangeFlag = 1U;
                    }
hu's avatar
hu committed
212

213 214
                    if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeyShortPressTime)
                    {
hu's avatar
hu committed
215
                        // stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount = 0U;
216
                    }
hu's avatar
hu committed
217 218
                    stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount = 0U;

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                    if (stKeyCtrVariate[enKeyIndex].u8KeyIGNChangeFlag == 0U)
                    {
                        if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeySuperLongPressTime)
                        {
                            if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
                            {
                                stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SUPER_LONG_PRESS);
                            }
                        }
                        else if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeyLongPressTime)
                        {
                            if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
                            {
                                stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_LONG_PRESS);
                            }
                        }
                    }
                    else
                    {
                        if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus == KEY_IG_OFF)
                        {
                            if (enKeyIGNRealStatus == KEY_IG_ON)
                            {
                                if ((stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord >= stKeyParameter.u16KeyValidOFFTime) &&
243
                                        (stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount >= stKeyParameter.u16KeyValidONTime))
244 245 246 247 248 249 250 251 252 253 254 255 256 257
                                {
                                    if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
                                    {
                                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_OFF_TO_ON);
                                    }
                                    stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
                                }
                            }
                        }
                        else if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus == KEY_IG_ON)
                        {
                            if (enKeyIGNRealStatus == KEY_IG_OFF)
                            {
                                if ((stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord >= stKeyParameter.u16KeyValidONTime) &&
258
                                        (stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount >= stKeyParameter.u16KeyValidOFFTime))
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
                                {
                                    if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
                                    {
                                        stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_ON_TO_OFF);
                                    }
                                    stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                /*ERROR*/
                stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount = 0U;
                stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
                stKeyCtrVariate[enKeyIndex].enKeyTypeLast = (Key_Real_Status_en_t)0U;
            }
        }
        else
        {
        }
    }
hu's avatar
hu committed
283 284 285 286
}
/*10ms调用一次*/
void Key_Service(void)
{
287 288 289 290 291
    uint8_t i = 0U;
    for (i = 0U; i < KEY_NUM_MAX; i++)
    {
        Key_Process((Key_Num_en_t)i); /*Key_Num_en_t*/
    }
hu's avatar
hu committed
292 293 294 295
}
/*首次上电时调用一次即可*/
void Key_Init(void)
{
296 297 298 299 300 301 302 303 304 305 306 307 308
    uint8_t i = 0U;
    for (i = 0U; i < KEY_NUM_MAX; i++)
    {
        stKeyCtrVariate[i].enKeyTypeLast = KEY_CAN_NONE;
        stKeyCtrVariate[i].u16KeyInvalidTimeCount = 0U;
        stKeyCtrVariate[i].u16KeyValidTimeCount = 0U;
        stKeyCtrVariate[i].u16KeyFirstIGNTimeRecord = 0U;
        stKeyCtrVariate[i].enKeyIGNFirstStatus = KEY_IG_INVALID;
        stKeyCtrVariate[i].enKeyIGNStatusLast = KEY_IG_INVALID;
        stKeyCtrVariate[i].u8KeyIGNChangeFlag = 0U;
        stKeyCtrVariate[i].u16KeyIGNTimeCount = 0U;
    }
    /*按键动作时间*/
309
    /*342 4.1 短按/长按操作的判断标准(判定时间):短按;40ms以上,未到800ms,长按;800ms以上*/
hu's avatar
hu committed
310
    stKeyParameter.u16KeyLoosenTime = 100U;
311
    stKeyParameter.u16KeyShortPressTime = 40U;
312
    stKeyParameter.u16KeyLongPressTime = 1000U;
313
    stKeyParameter.u16KeySuperLongPressTime = 10000U;
hu's avatar
hu committed
314
    stKeyParameter.u16KeyValidOFFTime = 200U; /*按键动作时间*/
315
    stKeyParameter.u16KeyValidONTime = 1000U;
hu's avatar
hu committed
316 317 318 319
}
/*对于硬线按键而言,判断短按的时间参数*/
Key_Set_en_t Key_Parameter_Set_Short_Press_Time(uint16_t u16Time)
{
320 321 322 323 324 325 326 327 328 329 330 331 332
    Key_Set_en_t enKeySetResult = KEY_SET_OK;
    if (u16Time < stKeyParameter.u16KeyLoosenTime)
    {
        enKeySetResult = KEY_SET_ERROR;
    }
    else if (u16Time > stKeyParameter.u16KeyLongPressTime)
    {
        enKeySetResult = KEY_SET_ERROR;
    }
    else
    {
        stKeyParameter.u16KeyShortPressTime = u16Time;
    }
hu's avatar
hu committed
333

334
    return enKeySetResult;
hu's avatar
hu committed
335 336 337 338
}
/*对于硬线按键而言,判断长按的时间参数*/
Key_Set_en_t Key_Parameter_Set_Long_Press_Time(uint16_t u16Time)
{
339 340 341 342 343 344 345 346 347 348 349 350 351 352
    Key_Set_en_t enKeySetResult = KEY_SET_OK;
    if (u16Time < stKeyParameter.u16KeyShortPressTime)
    {
        enKeySetResult = KEY_SET_ERROR;
    }
    else if (u16Time > stKeyParameter.u16KeySuperLongPressTime)
    {
        enKeySetResult = KEY_SET_ERROR;
    }
    else
    {
        stKeyParameter.u16KeyLongPressTime = u16Time;
    }
    return enKeySetResult;
hu's avatar
hu committed
353 354 355 356
}
/*对于硬线按键而言,判断超长按的时间参数*/
Key_Set_en_t Key_Parameter_Set_Super_Long_Press_Time(uint16_t u16Time)
{
357 358 359 360 361 362 363 364 365 366
    Key_Set_en_t enKeySetResult = KEY_SET_OK;
    if (u16Time < stKeyParameter.u16KeyLongPressTime)
    {
        enKeySetResult = KEY_SET_ERROR;
    }
    else
    {
        stKeyParameter.u16KeySuperLongPressTime = u16Time;
    }
    return enKeySetResult;
hu's avatar
hu committed
367
}