123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- //==================================================================
- //
- // Filename: key.c
- // Description: key process.
- //
- //------------------------------------------------------------------
- //
- // version: v2.0
- // data: 10:08am, 07.Mar.2013, written by Sirius
- // Description: Change the way to process for CAN & line.
- //
- //------------------------------------------------------------------
- //==================================================================
- /*------------------------------------------------------------------------------
- * I N C L U D E
- *------------------------------------------------------------------------------*/
- #include "stm32f0xx.h"
- #include "general.h"
- #include "main.h"
- #include "mapp_msg.h"
- #include "mapp_arm.h"
- #include "mapp_key.h"
- #include "mapp_gpio.h"
- #include "mapp_pwr.h"
- /*------------------------------------------------------------------------------
- * D E F I N E
- *------------------------------------------------------------------------------*/
- /*--- sampling couter ---*/
- #define SCAN_ACK_COUNT (3) //防抖次数
- #define ANOTHER_MACRO_COUNT (0) //防抖次数
- #define KEY_COUNT (8) //按键数量
- //#define CLOCK_PERIOD_MS 10 // 假设时钟周期为10ms
- /*--- Key status timer ---*/
- #define K_TIMER_LONG_PRESS (50) //长按按键的时间阈值 /* Continious timer since key pressed 35*50 =1.75s */
- #define K_TIMER_RPT_INTERVAL (OS_TIMER_200MS) //按键重复触发的时间间隔 /* Interval timer between twice long press */
- /*------------------------------------------------------------------------------
- * S T R U C T
- *------------------------------------------------------------------------------*/
- /*----------------------
- ** Key struct
- -----------------------*/
- /*------------------------------------------------------------------------------
- * S T A T I C - V A R I A B L E S
- *------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- * G L O B A L
- *------------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------------
- * P R O T O T Y P E
- *------------------------------------------------------------------------------*/
- //static void s_KeyDealwithTask(void);
- static VU16 s_keytime[KEY_COUNT] = {0};
- //uint8_t u8cnt4 = 0; // 定义计数器
- /*----------------------------------------------------------------------
- // Function name : ADC_Init
- // Input parameter :
- // Output parameter :
- // Use Function :
- // Reserve date : 10:25am, 07.Sep.2013, written by zwj
- ----------------------------------------------------------------------*/
- void AdcCfgInit(void)
- {
- }
- /*----------------------------------------------------------------------
- // Function name : KeyVarInit
- // Input parameter :
- // Output parameter :
- // Use Function : Init key module variables.
- // Reserve date : 11:37am, 07.Mar.2013, written by Sirius
- ----------------------------------------------------------------------*/
- static U08 last_KeyVal[KEY_COUNT] = {0};
- void KeyVarInit(void)
- {
- // Initialize last_KeyVal with current key states to prevent startup triggers
- static U08 init_done = 0;
-
- // Only initialize once
- if (init_done) return;
- init_done = 1;
-
- last_KeyVal[0] = !KEY_PA10_CALL_DET;
- last_KeyVal[1] = gtArm.PB0status;
- last_KeyVal[2] = !KEY_PA7_EMEGENCY_DET;
- last_KeyVal[3] = !KEY_PA12_REINFORCE_DET;
- last_KeyVal[4] = !KEY_PA11_NURSE_DET;
- last_KeyVal[5] = !KEY_PB1_CHANGE_DET;
- last_KeyVal[6] = !KEY_PA9_CANCEL_DET;
- last_KeyVal[7] = KEY_PA5_ESC_DET;
- // Set initial state to prevent startup triggers
- gtArm.KeyState = 0;
- gtArm.KeyStateBack = 0;
- }
- void KeyPB0Check(void) //按键PB0
- {
-
- static U08 u8cnt = 0; // 静态变量,用于计算按键状态持续的时间
- static U08 u8cnt2 = 0; // 静态变量,用于辅助判断按键状态的稳定性
- static U16 u16cnt2 = 0; // 静态变量,暂时没有使用
- static U16 u16cnt = 0;
- static U16 u8cnt3 = 0; // 计算高电平次数
- u8cnt++; // 每次检测按键,计数器递增
- u8cnt2++;
- u16cnt++;
- u16cnt2++;
-
-
- if (!KEY_PB0_HANDLE_DET) { //按下后,一直低电平
- if ( u8cnt2 >=10) { //多次检测没有检测到高电平,认为是按下,初始设5次
- u8cnt2 =0;
- gtArm.PB0status = 1; //按下或者检测到脉冲,一直是低电平
- //OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(6, 3));//接上手柄一直发送
- }
- u16cnt = 0;
- u8cnt3++;
-
- }else { //没有按下,有高电平
- //u16cnt = 0;
- //u8cnt3++;
- u8cnt2 = 0;
- gtArm.PB0status = 0; //没有按下,有高电平
-
- }
- if ( u16cnt >12000&>Arm.olcheck_enable) { //60s检测一次,60s未检测到方波,发04状态
- u16cnt = 0; // 重置计数
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(6, 4));
- }
- // if ( u16cnt >200) { //1s检测一次,1s未检测到方波,将u8cnt3清零
- // u8cnt3 = 0; // 重置计数
- // }
- if(u8cnt3 == 30000&>Arm.olcheck_enable){
- u8cnt3 = 0;
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(6, 3));
- }
- }
- void KeyPA8Check(void) //按键PB0
- {
-
- static U08 u8cnt = 0; // 静态变量,用于计算按键状态持续的时间
- static U08 u8cnt2 = 0; // 静态变量,用于辅助判断按键状态的稳定性
- static U16 u16cnt2 = 0; // 静态变量,暂时没有使用
- static U16 u16cnt = 0;
- static U16 u8cnt3 = 0; // 计算高电平次数
- u8cnt++; // 每次检测按键,计数器递增
- u8cnt2++;
- u16cnt++;
- u16cnt2++;
-
-
- if (!KEY_PA8_WAVE_DET) { //按下后,一直低电平
- if ( u8cnt2 >=15) { //多次检测没有检测到高电平,认为是按下,初始设5次,没有方波
- u8cnt2 =0;
- //u8cnt3++;
- //gtArm.PB8status = 1; //按下或者检测到脉冲,一直是低电平
- // OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(14, 0));
- }
- }
-
- else { //没有按下,有高电平,有方波
- u16cnt = 0;
- u8cnt2 = 0;
- u8cnt3++;
- //gtArm.PA8status = 0; //没有按下,有高电平
- //OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(14, 1));
- }
- if ( u16cnt >1200&>Arm.olcheck_enable) { //60s检测一次,10s未检测到方波,发02状态
- u16cnt = 0; // 重置计数
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(7, 4));
- }
- // if ( u16cnt >200) { //1s检测一次,1s未检测到方波,将u8cnt3清零
- // u8cnt3 = 0; // 重置计数
- // }
- if(u8cnt3 == 30000&>Arm.olcheck_enable){ //上电后检测到方波发送03状态
- u8cnt3 = 0;
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(7, 3));
- }
- }
- #if 0
- if (!KEY_PB0_HANDLE_DET) {
- if (u8cnt2 > 5) {
- u8cnt2 = 0;
- //gtArm.PB0status = 1;
-
- }
- u8cnt = 0;
- } else {
- u8cnt2 = 0;
- //gtArm.PB0status = 0;
- }
-
- if (u8cnt > 80) {
- if ((u8cnt % 80) == 0) {
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(6, KEY_CONTINUE));
- }
- } else {
- }
- #endif
-
- /*----------------------------------------------------------------------
- // Function uses : KeyTask
- // Input parameter :
- // Output parameter :
- // Use Function : execute every 35ms.
- // Reserve date : 16:22pm, 27.Nov.2012, written by Sirius
- ----------------------------------------------------------------------*/
- void KeyTask(void)
- {
- static U08 u8cnt[KEY_COUNT] = {0};
- static U08 trascnt1[KEY_COUNT] = {0};
- static U08 trascnt2[KEY_COUNT] = {0};
- static U08 keybz1 =0;
- static U08 keybz2 =0;
- static U08 keystate =0;
- U08 index = 0;
- U08 u8KeyVal[KEY_COUNT] = {0};
- //获取按键状态
- u8KeyVal[0] = !KEY_PA10_CALL_DET; //5
- //u8KeyVal[1] = !KEY_PB0_HANDLE_DET; //6 240506
- u8KeyVal[1] = gtArm.PB0status;
- u8KeyVal[2] = !KEY_PA7_EMEGENCY_DET; //7
- //u8KeyVal[2] = !gtArm.PA8status;
- u8KeyVal[3] = !KEY_PA12_REINFORCE_DET; //8
- u8KeyVal[4] = !KEY_PA11_NURSE_DET; //9
- u8KeyVal[5] = !KEY_PB1_CHANGE_DET; //a
- u8KeyVal[6] = !KEY_PA9_CANCEL_DET; //b
- u8KeyVal[7] = KEY_PA5_ESC_DET; //c
-
-
- if (u8KeyVal[5] && (u8KeyVal[1] )) { //同时按下是拔针 dwd240513
- keybz1 ++;
- if ((keybz1 >3) && (keybz2==0)) {
- keybz2 =1;
- keybz1 =0;
- }
- if (keybz2 ==1 ) {
- keybz2 = 2;
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(0x0d,KEY_PRESSED)); //拔针 dwd231225
- }
- return;
- } else {
- keybz1 =0;
-
- if (keybz2 ==2 ) {
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(0x0d,KEY_RELEASED)); //拔针 dwd231225
- keybz2 =0;
- return;
- }
- keybz2 =0;
-
- }
-
- for (index = 0;index <KEY_COUNT;index ++) {
- if (u8KeyVal[index] == 0) { //检查当前按键是,0没有按下
- gtArm.KeyState &= ~(1 <<index);
- trascnt1[index] =0; //第index个元素重置为0
- trascnt2[index] ++; //第index个元素加1
- keystate = KEY_RELEASED; //将按键状态,设置值为释放
- s_keytime[index] =0; //按键按下时长的第index个元素加1
- if (trascnt2[index] > SCAN_ACK_COUNT) { //防抖
- trascnt2[index] =0;
- if ( last_KeyVal[index] != u8KeyVal[index] ) {
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(index+5,KEY_RELEASED));
- last_KeyVal[index] = u8KeyVal[index];
- // if (last_KeyVal[index] == KEY_CONTINUE) {
- // OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(index+5,KEY_CONTINUE));
- // } else {
- // }
- }
- }
- } else {
- gtArm.KeyState |= (1 <<index);
- trascnt1[index]++;
- trascnt2[index] =0;
- s_keytime[index] ++;
-
- if (trascnt1[index] > SCAN_ACK_COUNT) { //防抖
- trascnt1[index] =0;
- if (s_keytime[index] > K_TIMER_LONG_PRESS ) { //长按
- s_keytime[index] = 0;
- if ( keystate != KEY_CONTINUE) {
- keystate = KEY_CONTINUE;
- if (index !=1 && index != 2) {
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(index+5,KEY_CONTINUE));
- }
- }
- } else if ( last_KeyVal[index] != u8KeyVal[index] ) {
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(index+5,KEY_PRESSED));
- last_KeyVal[index] = u8KeyVal[index];
- }
-
- }
- }
- }
- }
- #if 0
- for (index = 0; index < KEY_COUNT; index++) {
- if (u8KeyVal[index] != last_KeyVal[index]) {
- u8cnt[index]++;
- if (u8cnt[index] > 5) {
- u8cnt[index] = 0;
- last_KeyVal[index] = u8KeyVal[index];
- if (u8KeyVal[index] == 0) {
- // 发送按键按下的消息
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(index+5, KEY_PRESSED));
- } else {
- if (index == 1 && u8KeyVal[5] == 0) { // 如果是PB0按键抬起,并且PB1没按下
- // 不发送弹起的消息
- } else {
- // 发送按键弹起的消息
- OSQPost(ArmMsgQueue, ARM_MSG_KEY, TO_WORD(index+5, KEY_RELEASED));
- }
- }
- }
- } else {
- u8cnt[index] = 0;
- }
- }
- #endif
- /*=============================== END OF FILE ===============================*/
|