#include "key.h" extern Menu_table menu; extern BtFrame btframe; bool sent1_key = false; bool sent2_key = false; bool sent3_key = false; bool sent4_key = false; struct keys key[4]={0,0,0,0}; uint8_t KEY_1_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间 { if (key_state == GPIO_PIN_RESET) // 按键按下 { if(!sent1_key) { switch(menu.current) { case 0: { SET_MENU_STATUS(0,0,0,0); break; } case 1: { SET_MENU_STATUS(1,1,0,1); break; } case 2: case 7: { if(menu.up!=3) { menu.up+=1; menu.next-=1; } refresh=true; break; } case 8: case 9: case 0xa: case 0xb: { SET_MENU_STATUS(menu.current,1,0,3); break; } } //PRINT_MENU_STATUS("K1"); sent1_key = true; } return 1; } else // 按键释放 { sent1_key = false; return 0; } } } return 0; } uint8_t KEY_2_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间大于50ms { if (key_state == GPIO_PIN_RESET) // 按键按下 { if (!sent2_key) { switch (menu.current) { case 0: { SET_MENU_STATUS(0,0,0,0); break; } case 1: { SET_MENU_STATUS(1,0,1,1); break; } case 2: case 7: { if(menu.next!=3) { menu.up-=1; menu.next+=1; } refresh=true; break; } case 8: case 9: case 0xa: case 0xb: { SET_MENU_STATUS(menu.current,0,1,3); break; } } //PRINT_MENU_STATUS("K2"); sent2_key = true; } return 1; } else // 按键释放 { sent2_key = false; return 0; } } } return 0; } uint8_t KEY_3_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间大于50ms { if (key_state == GPIO_PIN_RESET) // 按键按下 { if (!sent3_key) { switch (menu.current) { case 0://跳转到1界面 { SET_MENU_STATUS(1,1,0,1); break; } case 1: { if (menu.up==1) { SET_MENU_STATUS(2,3,0,2); }else{ SET_MENU_STATUS(7,3,0,2); } break; } case 2: { SET_MENU_STATUS(6 - menu.up, 3, 0, 3); break; } case 3: { SET_MENU_STATUS(3,3,0,4); break; } case 7: { SET_MENU_STATUS(11 - menu.up, 1, 0, 3); break; } case 8: case 9: case 0xa: case 0xb: { if(menu.up == 1) { SET_MENU_STATUS(menu.current, 1, 0, 4); } else { SET_MENU_STATUS(menu.current, 0, 1, 4); } break; } } //PRINT_MENU_STATUS("K3"); sent3_key = true; } return 1; } else // 按键释放 { sent3_key = false; return 0; } } } return 0; } uint8_t KEY_4_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间大于50ms { if (key_state == GPIO_PIN_RESET) // 按键按下 { if (!sent4_key) {//返回 switch (menu.current) { case 0: case 1: { SET_MENU_STATUS(0,0,0,0); break; } case 2: case 7: { SET_MENU_STATUS(1,1,0,1); break; } /* L*绑定 */ case 3: case 4: case 5: case 6: { SET_MENU_STATUS(2,3,0,2); break; } case 8: case 9: case 0xa: case 0xb: { SET_MENU_STATUS(7,3,0,2); break; } } //PRINT_MENU_STATUS("K4"); sent4_key = true; } return 1; } else // 按键释放 { sent4_key = false; return 0; } } } return 0; } void updatekeystate(void) { KEY_1_Scan(); KEY_2_Scan(); KEY_3_Scan(); KEY_4_Scan(); }