/********************************** (C) COPYRIGHT ******************************* * File Name : KEY.c * Author : WCH * Version : V1.0 * Date : 2014/05/12 * Description : *******************************************************************************/ /******************************************************************************/ /* 头文件包含 */ #include "CH57x_common.h" #include "HAL.h" /************************************************************************************************** * GLOBAL VARIABLES **************************************************************************************************/ uint8 Hal_KeyIntEnable; /* interrupt enable/disable flag */ // Registered keys task ID, initialized to NOT USED. static uint8 registeredKeysTaskID = TASK_NO_TASK; static uint8 halKeySavedKeys; /* 保留按键最后的状态,用于查询是否有键值变化 */ static uint8 KeyConfigFlag; /* 按键是否配置标志位 */ /************************************************************************************************** * FUNCTIONS - Local **************************************************************************************************/ static halKeyCBack_t pHalKeyProcessFunction; /* callback function */ /************************************************************************************************** * @fn HAL_KeyInit * * @brief Initilize Key Service * * @param none * * @return None **************************************************************************************************/ void HAL_KeyInit( void ) { /* Initialize previous key to 0 */ halKeySavedKeys = 0; /* Initialize callback function */ pHalKeyProcessFunction = NULL; /* Start with key is not configured */ KeyConfigFlag = FALSE; KEY1_DIR; KEY1_PU; KEY2_DIR; KEY2_PU; KEY3_DIR; KEY3_PU; KEY4_DIR; KEY4_PU; KEY5_DIR; KEY5_PU; KEY6_DIR; KEY6_PU; KEY7_DIR; KEY7_PU; KEY8_DIR; KEY8_PU; KEY9_DIR; KEY9_PU; KEY10_DIR; KEY10_PU; KEY11_DIR; KEY11_PU; KEY12_DIR; KEY12_PU; HalKeyConfig( HAL_KEY_INTERRUPT_ENABLE, HalKeyCallback ); } void HAL_KEY_RegisterForKeys( tmosTaskID id ) { registeredKeysTaskID = id; } /************************************************************************************************** * @fn HalKeyConfig * * @brief Configure the Key serivce * * @param interruptEnable - TRUE/FALSE, enable/disable interrupt * cback - pointer to the CallBack function * * @return None **************************************************************************************************/ void HalKeyConfig (uint8 interruptEnable, halKeyCBack_t cback) { /* Enable/Disable Interrupt or */ Hal_KeyIntEnable = interruptEnable; /* Register the callback fucntion */ pHalKeyProcessFunction = cback; /* Determine if interrupt is enable or not */ if (Hal_KeyIntEnable){ /* Do this only after the hal_key is configured - to work with sleep stuff */ if (KeyConfigFlag == TRUE){ tmos_stop_task( halTaskID, HAL_KEY_EVENT ); /* Cancel polling if active */ } } else{ /* Interrupts NOT enabled */ tmos_start_task( halTaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE); /* Kick off polling */ } /* Key now is configured */ KeyConfigFlag = TRUE; } /********************************************************************* * @fn OnBoard_SendKeys * * @brief Send "Key Pressed" message to application. * * @param keys - keys that were pressed * state - shifted * * @return status *********************************************************************/ uint8 OnBoard_SendKeys( uint8 keys, uint8 state ) { keyChange_t *msgPtr; if ( registeredKeysTaskID != TASK_NO_TASK ){ // Send the address to the task msgPtr = (keyChange_t *)tmos_msg_allocate( sizeof(keyChange_t) ); if ( msgPtr ){ msgPtr->hdr.event = KEY_CHANGE; msgPtr->state = state; msgPtr->keys = keys; tmos_msg_send( registeredKeysTaskID, (uint8 *)msgPtr ); } return ( SUCCESS ); } else{ return ( FAILURE ); } } /********************************************************************* * @fn OnBoard_KeyCallback * * @brief Callback service for keys * * @param keys - keys that were pressed * state - shifted * * @return void *********************************************************************/ void HalKeyCallback ( uint8 keys, uint8 state ) { (void)state; if ( OnBoard_SendKeys( keys, state ) != SUCCESS ){ // Process SW1 here if ( keys & HAL_KEY_SW_1 ){ // Switch 1 } // Process SW2 here if ( keys & HAL_KEY_SW_2 ){ // Switch 2 } // Process SW3 here if ( keys & HAL_KEY_SW_3 ){ // Switch 3 } // Process SW4 here if ( keys & HAL_KEY_SW_4 ){ // Switch 4 } // Process SW5 here if ( keys & HAL_KEY_SW_5 ) // Switch 5 { } // Process SW6 here if ( keys & HAL_KEY_SW_6 ) // Switch 6 { } if ( keys & HAL_KEY_SW_7 ) // Switch 7 { } if ( keys & HAL_KEY_SW_8 ) // Switch 8 { } if ( keys & HAL_KEY_SW_9 ) // Switch 9 { } if ( keys & HAL_KEY_SW_10 ) // Switch 9 { } if ( keys & HAL_KEY_SW_11 ) // Switch 9 { } if ( keys & HAL_KEY_SW_12 ) // Switch 9 { } } } /************************************************************************************************** * @fn HalKeyRead * * @brief Read the current value of a key * * @param None * * @return keys - current keys status **************************************************************************************************/ uint8 HalKeyRead ( void ) { uint8 keys = 0; if (HAL_PUSH_BUTTON1()){ //读按键1 keys |= HAL_KEY_SW_1; } if (HAL_PUSH_BUTTON2()){ //读按键1 keys |= HAL_KEY_SW_2; } if (HAL_PUSH_BUTTON3()){ //读按键1 keys |= HAL_KEY_SW_3; } if (HAL_PUSH_BUTTON4()){ //读按键1 keys |= HAL_KEY_SW_4; } if (HAL_PUSH_BUTTON5()){ //读按键1 keys |= HAL_KEY_SW_5; } if (HAL_PUSH_BUTTON6()){ //读按键1 keys |= HAL_KEY_SW_6; } if (HAL_PUSH_BUTTON7()){ //读按键1 keys |= HAL_KEY_SW_7; } if (HAL_PUSH_BUTTON8()){ //读按键1 keys |= HAL_KEY_SW_8; } if (HAL_PUSH_BUTTON9()){ //读按键1 keys |= HAL_KEY_SW_9; } if (HAL_PUSH_BUTTON10()){ //读按键1 keys |= HAL_KEY_SW_10; } if (HAL_PUSH_BUTTON11()){ //读按键1 keys |= HAL_KEY_SW_11; } if (HAL_PUSH_BUTTON12()){ //读按键1 keys |= HAL_KEY_SW_12; } return keys; } /************************************************************************************************** * @fn HAL_KeyPoll * * @brief Called by hal_driver to poll the keys * * @param None * * @return None **************************************************************************************************/ void HAL_KeyPoll (void) { uint8 keys = 0; if( HAL_PUSH_BUTTON1() ){ keys |= HAL_KEY_SW_1; } if( HAL_PUSH_BUTTON2() ){ keys |= HAL_KEY_SW_2; } if( HAL_PUSH_BUTTON3() ){ keys |= HAL_KEY_SW_3; } if( HAL_PUSH_BUTTON4() ){ keys |= HAL_KEY_SW_4; } if( HAL_PUSH_BUTTON5() ){ keys |= HAL_KEY_SW_5; } if( HAL_PUSH_BUTTON6() ){ keys |= HAL_KEY_SW_6; } if( HAL_PUSH_BUTTON7() ){ keys |= HAL_KEY_SW_7; } if( HAL_PUSH_BUTTON8() ){ keys |= HAL_KEY_SW_8; } if( HAL_PUSH_BUTTON9() ){ keys |= HAL_KEY_SW_9; } if( HAL_PUSH_BUTTON10() ){ keys |= HAL_KEY_SW_10; } if( HAL_PUSH_BUTTON11() ){ keys |= HAL_KEY_SW_11; } if( HAL_PUSH_BUTTON12() ){ keys |= HAL_KEY_SW_12; } if (!Hal_KeyIntEnable){ /* 中断未使能 */ if(keys == halKeySavedKeys){ /* Exit - since no keys have changed */ return; } halKeySavedKeys = keys; /* Store the current keys for comparation next time */ } /* Invoke Callback if new keys were depressed */ if (keys && (pHalKeyProcessFunction)){ (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL); } } /******************************** endfile @ key ******************************/
KEY.H
/********************************** (C) COPYRIGHT ******************************* * File Name : KEY.h * Author : WCH * Version : V1.0 * Date : 2016/04/12 * Description : *******************************************************************************/ /******************************************************************************/ #ifndef __KEY_H #define __KEY_H #ifdef __cplusplus extern "C" { #endif /************************************************************************************************** * MACROS **************************************************************************************************/ #define KEY_CHANGE 0xC0 // Key message #define HAL_KEY_RISING_EDGE 0 #define HAL_KEY_FALLING_EDGE 1 #define HAL_KEY_DEBOUNCE_VALUE 25 #define HAL_KEY_POLLING_VALUE 100 /* Interrupt option - Enable or disable */ #define HAL_KEY_INTERRUPT_DISABLE 0x00 #define HAL_KEY_INTERRUPT_ENABLE 0x01 /* Key state - shift or nornal */ #define HAL_KEY_STATE_NORMAL 0x00 #define HAL_KEY_STATE_SHIFT 0x01 /* Switches (keys) */ #define HAL_KEY_SW_1 0x0001 #define HAL_KEY_SW_2 0x0002 #define HAL_KEY_SW_3 0x0004 #define HAL_KEY_SW_4 0x0008 #define HAL_KEY_SW_5 0x0010 #define HAL_KEY_SW_6 0x0020 #define HAL_KEY_SW_7 0x0040 #define HAL_KEY_SW_8 0x0080 #define HAL_KEY_SW_9 0x0100 #define HAL_KEY_SW_10 0x0200 #define HAL_KEY_SW_11 0x0400 #define HAL_KEY_SW_12 0x0800 /* 按键定义 */ /* 1 - KEY */ #define KEY1_BV BV(23) //引脚定义 #define KEY2_BV GPIO_Pin_1 #define KEY3_BV GPIO_Pin_2 #define KEY4_BV GPIO_Pin_3 #define KEY5_BV GPIO_Pin_4 #define KEY6_BV GPIO_Pin_5 #define KEY7_BV GPIO_Pin_6 #define KEY8_BV GPIO_Pin_7 #define KEY9_BV GPIO_Pin_8 #define KEY10_BV GPIO_Pin_9 #define KEY11_BV GPIO_Pin_10 #define KEY12_BV GPIO_Pin_11 #define KEY1_PU (R32_PB_PU |= KEY1_BV) //上拉电阻使能 #define KEY2_PU (R32_PB_PU |= KEY2_BV) #define KEY3_PU (R32_PB_PU |= KEY3_BV) #define KEY4_PU (R32_PB_PU |= KEY4_BV) #define KEY5_PU (R32_PB_PU |= KEY5_BV) #define KEY6_PU (R32_PB_PU |= KEY6_BV) #define KEY7_PU (R32_PB_PU |= KEY7_BV) #define KEY8_PU (R32_PB_PU |= KEY8_BV) #define KEY9_PU (R32_PB_PU |= KEY9_BV) #define KEY10_PU (R32_PB_PU |= KEY10_BV) #define KEY11_PU (R32_PB_PU |= KEY11_BV) #define KEY12_PU (R32_PB_PU |= KEY12_BV) #define KEY1_DIR (R32_PB_DIR &= ~KEY1_BV) //设置成输入模式 #define KEY2_DIR (R32_PB_DIR &= ~KEY2_BV) #define KEY3_DIR (R32_PB_DIR &= ~KEY3_BV) #define KEY4_DIR (R32_PB_DIR &= ~KEY4_BV) #define KEY5_DIR (R32_PB_DIR &= ~KEY5_BV) #define KEY6_DIR (R32_PB_DIR &= ~KEY6_BV) #define KEY7_DIR (R32_PB_DIR &= ~KEY7_BV) #define KEY8_DIR (R32_PB_DIR &= ~KEY8_BV) #define KEY9_DIR (R32_PB_DIR &= ~KEY9_BV) #define KEY10_DIR (R32_PB_DIR &= ~KEY10_BV) #define KEY11_DIR (R32_PB_DIR &= ~KEY11_BV) #define KEY12_DIR (R32_PB_DIR &= ~KEY12_BV) #define KEY1_IN (ACTIVE_LOW(R32_PB_PIN&KEY1_BV)) //没有按键按下时1111(由于上拉了),如第第一个按下,1110 然后&上第一个按键的键码0001 结果变成0000 后再取反变成1111 #define KEY2_IN (ACTIVE_LOW(R32_PB_PIN&KEY2_BV)) #define KEY3_IN (ACTIVE_LOW(R32_PB_PIN&KEY3_BV)) #define KEY4_IN (ACTIVE_LOW(R32_PB_PIN&KEY4_BV)) #define KEY5_IN (ACTIVE_LOW(R32_PB_PIN&KEY5_BV)) #define KEY6_IN (ACTIVE_LOW(R32_PB_PIN&KEY6_BV)) #define KEY7_IN (ACTIVE_LOW(R32_PB_PIN&KEY7_BV)) #define KEY8_IN (ACTIVE_LOW(R32_PB_PIN&KEY8_BV)) #define KEY9_IN (ACTIVE_LOW(R32_PB_PIN&KEY9_BV)) #define KEY10_IN (ACTIVE_LOW(R32_PB_PIN&KEY10_BV)) #define KEY11_IN (ACTIVE_LOW(R32_PB_PIN&KEY11_BV)) #define KEY12_IN (ACTIVE_LOW(R32_PB_PIN&KEY12_BV)) #define HAL_PUSH_BUTTON1() ( KEY1_IN ) //添加自定义按键 #define HAL_PUSH_BUTTON2() ( KEY2_IN ) #define HAL_PUSH_BUTTON3() ( KEY3_IN ) #define HAL_PUSH_BUTTON4() ( KEY4_IN ) #define HAL_PUSH_BUTTON5() ( KEY5_IN ) //添加自定义按键 #define HAL_PUSH_BUTTON6() ( KEY6_IN ) #define HAL_PUSH_BUTTON7() ( KEY7_IN ) #define HAL_PUSH_BUTTON8() ( KEY8_IN ) #define HAL_PUSH_BUTTON9() ( KEY9_IN ) //添加自定义按键 #define HAL_PUSH_BUTTON10() ( KEY10_IN ) #define HAL_PUSH_BUTTON11() ( KEY11_IN ) #define HAL_PUSH_BUTTON12() ( KEY12_IN ) /************************************************************************************************** * TYPEDEFS **************************************************************************************************/ typedef void (*halKeyCBack_t) (uint8 keys, uint8 state); typedef struct { tmos_event_hdr_t hdr; uint8 state; // shift uint8 keys; // keys } keyChange_t; /************************************************************************************************** * GLOBAL VARIABLES **************************************************************************************************/ extern uint8 Hal_KeyIntEnable; /********************************************************************* * FUNCTIONS */ /* * Initialize the Key Service */ void HAL_KeyInit( void ); /* * This is for internal used by hal_driver */ void HAL_KeyPoll( void ); /* * Register the Key Service */ void HAL_KEY_RegisterForKeys( tmosTaskID id ); /* * Configure the Key Service */ void HalKeyConfig( uint8 interruptEnable, const halKeyCBack_t cback); /* * Read the Key callback */ void HalKeyCallback ( uint8 keys, uint8 state ); /* * Read the Key status */ uint8 HalKeyRead( void); /************************************************************************************************** **************************************************************************************************/ #ifdef __cplusplus } #endif #endif
peripheral.C
这个只是相关key的代码
HAL_KEY_RegisterForKeys(Peripheral_TaskID); /********************************************************************* * @fn Peripheral_ProcessTMOSMsg * * @brief Process an incoming task message. * * @param pMsg - message to process * * @return none */ static void Peripheral_ProcessTMOSMsg( tmos_event_hdr_t *pMsg ) { switch ( pMsg->event ){ case KEY_CHANGE: PRINT("KEY changed \n"); Peripheral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys ); break; case GATT_MSG_EVENT: // Process GATT message Peripheral_ProcessGATTMsg( (gattMsgEvent_t *)pMsg ); break; default: break; } } /********************************************************************* * @fn simpleBLEPeripheral_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void Peripheral_HandleKeys( uint8 shift, uint16 keys ) { uint8 char1 = 1; uint8 char2 = 2; uint8 char3 = 3; uint8 char4 = 4; uint8 char5 = 5; uint8 char6 = 6; uint8 char7 = 7; uint8 char8 = 8; uint8 char9 = 9; uint8 char10 = 16; //假如此处的值为10,那么手机端显示0A,10进制的10是16进制的0A,10进制的16是16进制的10 uint8 char11 = 17; //同上 uint8 char12 = 18; //同上 uint8 char21 = 153; //同上 if ( keys & HAL_KEY_SW_1 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char1); button_values = 1; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_2 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char2); button_values = 2; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_3 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char3); button_values = 3; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_4 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char4); button_values = 4; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_5 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char5); button_values = 5; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_6 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char6); button_values = 6; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_7 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char7); button_values = 7; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_8 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char8); button_values = 8; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_9 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char9); button_values = 9; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_10) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char10); button_values = 16; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_11 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char11); button_values = 17; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if ( keys & HAL_KEY_SW_12 ) { SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char12); button_values = 18; HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); } if (keys == 0x00){ SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &char21); } }
热门产品 :
CH182: 以太网PHY收发器芯片