123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- #include "key.h"
- static KEY_T s_tBtn[KEY_COUNT];
- static KEY_FIFO_T s_tKey;
- static void bsp_DetectKey(uint8_t i);
- static uint8_t IsKeyDown1(void)
- {
- if ((GPIO_PORT_K1->IDR & GPIO_PIN_K1) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown2(void)
- {
- if ((GPIO_PORT_K2->IDR & GPIO_PIN_K2) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown3(void)
- {
- if ((GPIO_PORT_K3->IDR & GPIO_PIN_K3) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown4(void)
- {
- if ((GPIO_PORT_K4->IDR & GPIO_PIN_K4) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown5(void)
- {
- if ((GPIO_PORT_K5->IDR & GPIO_PIN_K5) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown6(void)
- {
- if ((GPIO_PORT_K6->IDR & GPIO_PIN_K6) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown7(void)
- {
- if ((GPIO_PORT_K7->IDR & GPIO_PIN_K7) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown8(void)
- {
- if ((GPIO_PORT_K8->IDR & GPIO_PIN_K8) == 0)
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown9(void)
- {
- if (IsKeyDown1() && IsKeyDown2())
- return 1;
- else
- return 0;
- }
- static uint8_t IsKeyDown10(void)
- {
- if (IsKeyDown1() && IsKeyDown2())
- return 1;
- else
- return 0;
- }
- void key_put(uint8_t _KeyCode)
- {
- s_tKey.Buf[s_tKey.Write] = _KeyCode;
- if (++s_tKey.Write >= KEY_FIFO_SIZE)
- {
- s_tKey.Write = 0;
- }
- }
- uint8_t key_get(void)
- {
- uint8_t ret;
- if (s_tKey.Read == s_tKey.Write)
- {
- return KEY_NONE;
- }
- else
- {
- ret = s_tKey.Buf[s_tKey.Read];
- if (++s_tKey.Read >= KEY_FIFO_SIZE)
- {
- s_tKey.Read = 0;
- }
- return ret;
- }
- }
- uint8_t key_get_fifo(void)
- {
- uint8_t ret;
- if (s_tKey.Read2 == s_tKey.Write)
- {
- return KEY_NONE;
- }
- else
- {
- ret = s_tKey.Buf[s_tKey.Read2];
- if (++s_tKey.Read2 >= KEY_FIFO_SIZE)
- {
- s_tKey.Read2 = 0;
- }
- return ret;
- }
- }
- uint8_t key_state(KEY_ID_E _ucKeyID)
- {
- return s_tBtn[_ucKeyID].State;
- }
- void key_set(uint8_t _ucKeyID, uint16_t _LongTime, uint8_t _RepeatSpeed)
- {
- s_tBtn[_ucKeyID].LongTime = _LongTime;
- s_tBtn[_ucKeyID].RepeatSpeed = _RepeatSpeed;
- s_tBtn[_ucKeyID].RepeatCount = 0;
- }
- void key_clear_fifo(void)
- {
- s_tKey.Read = s_tKey.Write;
- }
- void key_init(void)
- {
- uint8_t i;
-
- s_tKey.Read = 0;
- s_tKey.Write = 0;
- s_tKey.Read2 = 0;
-
- for (i = 0; i < KEY_COUNT; i++)
- {
- s_tBtn[i].LongTime = KEY_LONG_TIME;
- s_tBtn[i].Count = KEY_FILTER_TIME / 2;
- s_tBtn[i].State = 0;
-
-
-
- s_tBtn[i].RepeatSpeed = 0;
- s_tBtn[i].RepeatCount = 0;
- }
-
-
- s_tBtn[KID_JOY_U].LongTime = 100;
- s_tBtn[KID_JOY_U].RepeatSpeed = 5;
- s_tBtn[KID_JOY_D].LongTime = 100;
- s_tBtn[KID_JOY_D].RepeatSpeed = 5;
- s_tBtn[KID_JOY_L].LongTime = 100;
- s_tBtn[KID_JOY_L].RepeatSpeed = 5;
- s_tBtn[KID_JOY_R].LongTime = 100;
- s_tBtn[KID_JOY_R].RepeatSpeed = 5;
-
- s_tBtn[0].IsKeyDownFunc = IsKeyDown1;
- s_tBtn[1].IsKeyDownFunc = IsKeyDown2;
- s_tBtn[2].IsKeyDownFunc = IsKeyDown3;
- s_tBtn[3].IsKeyDownFunc = IsKeyDown4;
- s_tBtn[4].IsKeyDownFunc = IsKeyDown5;
- s_tBtn[5].IsKeyDownFunc = IsKeyDown6;
- s_tBtn[6].IsKeyDownFunc = IsKeyDown7;
- s_tBtn[7].IsKeyDownFunc = IsKeyDown8;
-
- s_tBtn[8].IsKeyDownFunc = IsKeyDown9;
- s_tBtn[9].IsKeyDownFunc = IsKeyDown10;
- }
- static void bsp_DetectKey(uint8_t i)
- {
- KEY_T *pBtn;
-
- pBtn = &s_tBtn[i];
- if (pBtn->IsKeyDownFunc())
- {
- if (pBtn->Count < KEY_FILTER_TIME)
- {
- pBtn->Count = KEY_FILTER_TIME;
- }
- else if (pBtn->Count < 2 * KEY_FILTER_TIME)
- {
- pBtn->Count++;
- }
- else
- {
- if (pBtn->State == 0)
- {
- pBtn->State = 1;
-
- key_put((uint8_t)(3 * i + 1));
- }
- if (pBtn->LongTime > 0)
- {
- if (pBtn->LongCount < pBtn->LongTime)
- {
-
- if (++pBtn->LongCount == pBtn->LongTime)
- {
-
- key_put((uint8_t)(3 * i + 3));
- }
- }
- else
- {
- if (pBtn->RepeatSpeed > 0)
- {
- if (++pBtn->RepeatCount >= pBtn->RepeatSpeed)
- {
- pBtn->RepeatCount = 0;
-
- key_put((uint8_t)(3 * i + 1));
- }
- }
- }
- }
- }
- }
- else
- {
- if (pBtn->Count > KEY_FILTER_TIME)
- {
- pBtn->Count = KEY_FILTER_TIME;
- }
- else if (pBtn->Count != 0)
- {
- pBtn->Count--;
- }
- else
- {
- if (pBtn->State == 1)
- {
- pBtn->State = 0;
-
- key_put((uint8_t)(3 * i + 2));
- }
- }
- pBtn->LongCount = 0;
- pBtn->RepeatCount = 0;
- }
- }
- void key_scan(void)
- {
- uint8_t i;
- for (i = 0; i < KEY_COUNT; i++)
- {
- bsp_DetectKey(i);
- }
- }
|