1 /******************************************************************************* 2 Filename: board_key.h 3 Revised: $Date: 2014-02-28 14:18:14 -0800 (Fri, 28 Feb 2014) $ 4 Revision: $Revision: 37461 $ 5 6 Description: This file contains the SRF06EB Key Service definitions 7 and prototypes. 8 ******************************************************************************** 9 适用于按键单击检测及处理,不能检测及处理连按2次按键(双击) 10 ******************************************************************************** 11 使用说明: 12 1.在需要使用board_key.c的应用程序中包含board_key.h头文件。 13 #include "board_key.h" 14 2.在需要使用board_key.c的应用程序中定义按键事件,宏名根据实际应用修改。 15 #define SBP_KEY_CHANGE_EVT 0x0010 16 3.在需要使用board_key.c的应用程序中声明按键回调函数及按键处理函数。 17 static void SimpleBLEPeripheral_keyChangeHandler(uint8 keys); // 按键回调 18 static void SimpleBLEPeripheral_handleKeys(uint8_t shift, uint8_t keys); // 按键处理 19 4.在需要使用board_key.c的应用程序中定义按键回调函数及按键处理函数,消息结构体 20 可能需要根据实际应用修改。 21 void SimpleBLEPeripheral_keyChangeHandler(uint8 keys) 22 { 23 SimpleBLEPeripheral_enqueueMsg(SBP_KEY_CHANGE_EVT, keys); // 在swi中发按键消息 24 } 25 static void SimpleBLEPeripheral_handleKeys(uint8_t shift, uint8_t keys) 26 { 27 (void)shift; // Intentionally unreferenced parameter 28 29 if (keys & Board_KEY_SELECT) 30 { 31 Board_blinkLed( Board_LED2, 250, 1 ); // 闪烁周期500ms,闪烁1次 32 return; 33 } 34 if (keys & Board_KEY_UP) 35 { 36 return; 37 } 38 if (keys & Board_KEY_RIGHT) 39 { 40 return; 41 } 42 if (keys & Board_KEY_SELECT) 43 { 44 return; 45 } 46 if (keys & Board_KEY_DOWN) 47 { 48 return; 49 } 50 } 51 5.在应用程序中初始化按键,同时传入app层按建回调函数(发键值消息给task线程) 52 Board_initKeys(SimpleBLEPeripheral_keyChangeHandler); 53 6.在应用程序中的消息处理函数 54 (如:static void SimpleBLEPeripheral_processAppMsg(sbpEvt_t *pMsg)) 55 中添加如下代码: 56 case SBP_KEY_CHANGE_EVT: 57 SimpleBLEPeripheral_handleKeys(0, pMsg->hdr.state); // 在task线程处理按键 58 break; 59 ******************************************************************************** 60 原理说明: 61 1.按键ISR中读取键值并保存键值 // hwi线程 62 2.按键回调函数发送键值消息 // swi线程 63 3.按键处理函数处理按键 // task线程 64 *******************************************************************************/ 65 66 #ifndef BOARD_KEY_H 67 #define BOARD_KEY_H 68 69 #ifdef __cplusplus 70 extern "C" { 71 #endif 72 73 /********************************************************************* 74 * INCLUDES 75 */ 76 77 /********************************************************************* 78 * EXTERNAL VARIABLES 79 */ 80 81 /********************************************************************* 82 * CONSTANTS 83 */ 84 #define KEY_SELECT 0x0001 85 #define KEY_UP 0x0002 86 #define KEY_DOWN 0x0004 87 #define KEY_LEFT 0x0008 88 #define KEY_RIGHT 0x0010 89 90 // Debounce timeout in milliseconds 91 #define KEY_DEBOUNCE_TIMEOUT 200 92 93 /********************************************************************* 94 * TYPEDEFS 95 */ 96 typedef void (*keysPressedCB_t)(uint8 keysPressed); 97 98 /********************************************************************* 99 * MACROS 100 */ 101 102 /********************************************************************* 103 * API FUNCTIONS 104 */ 105 106 /********************************************************************* 107 * @fn Board_initKeys 108 * 109 * @brief Enable interrupts for keys on GPIOs. 110 * 111 * @param appKeyCB - application key pressed callback 112 * 113 * @return none 114 */ 115 void Board_initKeys(keysPressedCB_t appKeyCB); 116 117 /********************************************************************* 118 *********************************************************************/ 119 120 #ifdef __cplusplus 121 } 122 #endif 123 124 #endif /* BOARD_KEY_H */
1 /******************************************************************************* 2 Filename: board_key.c 3 Revised: $Date: 2014-03-10 07:29:12 -0700 (Mon, 10 Mar 2014) $ 4 Revision: $Revision: 37597 $ 5 6 Description: This file contains the interface to the SRF06EB Key Service. 7 *******************************************************************************/ 8 9 /********************************************************************* 10 * INCLUDES 11 */ 12 #include <stdbool.h> 13 #include <ti/sysbios/knl/Clock.h> 14 #include <ti/sysbios/family/arm/m3/Hwi.h> 15 #include <ti/sysbios/knl/Semaphore.h> 16 #include <ti/sysbios/knl/Queue.h> 17 18 #include <ti/drivers/pin/PINCC26XX.h> 19 20 #ifdef USE_ICALL 21 #include <ICall.h> 22 #endif 23 24 #include <inc/hw_ints.h> 25 #include "bcomdef.h" 26 27 #include "util.h" 28 #include "board_key.h" 29 #include "Board.h" 30 31 /********************************************************************* 32 * TYPEDEFS 33 */ 34 35 /********************************************************************* 36 * LOCAL FUNCTIONS 37 */ 38 static void Board_keyChangeHandler(UArg a0); 39 static void Board_keyCallback(PIN_Handle hPin, PIN_Id pinId); 40 41 /******************************************************************************* 42 * EXTERNAL VARIABLES 43 */ 44 45 /********************************************************************* 46 * LOCAL VARIABLES 47 */ 48 49 // Value of keys Pressed 50 static uint8_t keysPressed; 51 52 // Key debounce clock 53 static Clock_Struct keyChangeClock; 54 55 // Pointer to application callback 56 keysPressedCB_t appKeyChangeHandler = NULL; 57 58 // Memory for the GPIO module to construct a Hwi 59 Hwi_Struct callbackHwiKeys; 60 61 // PIN configuration structure to set all KEY pins as inputs with pullups enabled 62 PIN_Config keyPinsCfg[] = 63 { 64 Board_KEY_SELECT | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP, 65 Board_KEY_UP | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP, 66 Board_KEY_DOWN | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP, 67 Board_KEY_LEFT | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP, 68 Board_KEY_RIGHT | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP, 69 PIN_TERMINATE 70 }; 71 72 PIN_State keyPins; 73 PIN_Handle hKeyPins; 74 75 /********************************************************************* 76 * PUBLIC FUNCTIONS 77 */ 78 /********************************************************************* 79 * @fn Board_initKeys 80 * 81 * @brief Enable interrupts for keys on GPIOs. 82 * 83 * @brief task 84 * 85 * @param appKeyCB - application key pressed callback 86 * 87 * @return none 88 */ 89 void Board_initKeys(keysPressedCB_t appKeyCB) 90 { 91 // Initialize KEY pins. Enable int after callback registered 92 hKeyPins = PIN_open(&keyPins, keyPinsCfg); 93 PIN_registerIntCb(hKeyPins, Board_keyCallback); 94 95 PIN_setConfig(hKeyPins, PIN_BM_IRQ, Board_KEY_SELECT | PIN_IRQ_NEGEDGE); 96 PIN_setConfig(hKeyPins, PIN_BM_IRQ, Board_KEY_UP | PIN_IRQ_NEGEDGE); 97 PIN_setConfig(hKeyPins, PIN_BM_IRQ, Board_KEY_DOWN | PIN_IRQ_NEGEDGE); 98 PIN_setConfig(hKeyPins, PIN_BM_IRQ, Board_KEY_LEFT | PIN_IRQ_NEGEDGE); 99 PIN_setConfig(hKeyPins, PIN_BM_IRQ, Board_KEY_RIGHT | PIN_IRQ_NEGEDGE); 100 101 #ifdef POWER_SAVING 102 //Enable wakeup 103 PIN_setConfig(hKeyPins, PINCC26XX_BM_WAKEUP, Board_KEY_SELECT | PINCC26XX_WAKEUP_NEGEDGE); 104 PIN_setConfig(hKeyPins, PINCC26XX_BM_WAKEUP, Board_KEY_UP | PINCC26XX_WAKEUP_NEGEDGE); 105 PIN_setConfig(hKeyPins, PINCC26XX_BM_WAKEUP, Board_KEY_DOWN | PINCC26XX_WAKEUP_NEGEDGE); 106 PIN_setConfig(hKeyPins, PINCC26XX_BM_WAKEUP, Board_KEY_LEFT | PINCC26XX_WAKEUP_NEGEDGE); 107 PIN_setConfig(hKeyPins, PINCC26XX_BM_WAKEUP, Board_KEY_RIGHT | PINCC26XX_WAKEUP_NEGEDGE); 108 #endif 109 110 // Setup keycallback for keys 111 Util_constructClock(&keyChangeClock, Board_keyChangeHandler, 112 KEY_DEBOUNCE_TIMEOUT, 0, false, 0); 113 114 // Set the application callback 115 appKeyChangeHandler = appKeyCB; 116 } 117 118 /********************************************************************* 119 * @fn Board_keyCallback 120 * 121 * @brief Interrupt handler for Keys 122 * 123 * @brief hwi 124 * 125 * @param none 126 * 127 * @return none 128 */ 129 static void Board_keyCallback(PIN_Handle hPin, PIN_Id pinId) 130 { 131 keysPressed = 0; 132 133 if ( PIN_getInputValue(Board_KEY_SELECT) == 0 ) 134 { 135 keysPressed |= KEY_SELECT; 136 } 137 138 if ( PIN_getInputValue(Board_KEY_UP) == 0 ) 139 { 140 keysPressed |= KEY_UP; 141 } 142 143 if ( PIN_getInputValue(Board_KEY_DOWN) == 0 ) 144 { 145 keysPressed |= KEY_DOWN; 146 } 147 148 if ( PIN_getInputValue(Board_KEY_LEFT) == 0 ) 149 { 150 keysPressed |= KEY_LEFT; 151 } 152 153 if ( PIN_getInputValue(Board_KEY_RIGHT) == 0 ) 154 { 155 keysPressed |= KEY_RIGHT; 156 } 157 158 Util_startClock(&keyChangeClock); 159 } 160 161 /********************************************************************* 162 * @fn Board_keyChangeHandler 163 * 164 * @brief Handler for key change 165 * 166 * @brief swi 167 * 168 * @param UArg a0 - ignored 169 * 170 * @return none 171 */ 172 static void Board_keyChangeHandler(UArg a0) 173 { 174 if (appKeyChangeHandler != NULL) 175 { 176 // Notify the application 177 (*appKeyChangeHandler)(keysPressed); 178 } 179 } 180 /********************************************************************* 181 *********************************************************************/
时间: 2024-10-08 10:00:36