CC2540重写按键

时间:2023-03-08 16:54:59

TI写的按键比较啰嗦关键还没什么功能,所以重写了,阿莫的开发板,

头文件H:

#ifndef HAL_KEY_H
#define HAL_KEY_H #ifdef __cplusplus
extern "C"
{
#endif /**************************************************************************************************
* INCLUDES
**************************************************************************************************/
#include "hal_board.h" /**************************************************************************************************
* MACROS
**************************************************************************************************/ /**************************************************************************************************
* CONSTANTS
**************************************************************************************************/
#define defHalKeyCheckTimePriod 20 /* Key state - shift or nornal */
#define HAL_KEY_STATE_NORMAL 0x00
#define HAL_KEY_STATE_SHIFT 0x01 #define HAL_KEY_SW_1 0x20 // Joystick up
/*
#define HAL_KEY_SW_2 0x02 // Joystick right
#define HAL_KEY_SW_5 0x04 // Joystick center
#define HAL_KEY_SW_4 0x08 // Joystick left
#define HAL_KEY_SW_3 0x10 // Joystick down*/ #define HAL_KEY_UP 0x01 // Joystick up
#define HAL_KEY_RIGHT 0x02 // Joystick right
#define HAL_KEY_CENTER 0x04 // Joystick center
#define HAL_KEY_LEFT 0x08 // Joystick left
#define HAL_KEY_DOWN 0x10 // Joystick down #define HAL_KEY_SHIFT_KEEP 0x01 // Joystick right
#define HAL_KEY_SHIFT_DOWN 0x02 // Joystick down
#define HAL_KEY_SHIFT_UP 0x03 // Joystick up /**************************************************************************************************
* TYPEDEFS
**************************************************************************************************/
typedef void (*halKeyCBack_t) (uint8 keys, uint8 state); /**************************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************************/
extern bool Hal_KeyIntEnable; /**************************************************************************************************
* FUNCTIONS - API
**************************************************************************************************/ /*
* Initialize the Key Service
*/
extern void HalKeyInit( void ); /*
* Configure the Key Service
*/
void HalKeyConfig (halKeyCBack_t cback); /*
* Read the Key status
*/
extern uint8 HalKeyRead( void); /*
* Enter sleep mode, store important values
*/
extern void HalKeyEnterSleep ( void ); /*
* Exit sleep mode, retore values
*/
extern uint8 HalKeyExitSleep ( void ); /*
* This is for internal used by hal_driver
*/
extern void HalKeyPoll ( void ); /*
* This is for internal used by hal_sleep
*/
extern bool HalKeyPressed( void ); extern uint8 hal_key_keys(void); extern uint8 hal_key_int_keys(void); /**************************************************************************************************
**************************************************************************************************/ #ifdef __cplusplus
}
#endif #endif

  以下是C文件

/**************************************************************************************************
* INCLUDES
**************************************************************************************************/
#include "hal_mcu.h"
#include "hal_defs.h"
#include "hal_types.h"
#include "hal_drivers.h"
#include "hal_adc.h"
#include "hal_key.h"
#include "osal.h" /* SW_1 is at P0.0 */
#define HAL_KEY_SW_1_PORT P0
#define HAL_KEY_SW_1_BIT BV(1)
#define HAL_KEY_SW_1_SEL P0SEL
#define HAL_KEY_SW_1_DIR P0DIR
#define HAL_KEY_SW_1_EDGEBIT BV(0) #define HAL_KEY_SW_1_IEN IEN1 /* CPU interrupt mask register */
#define HAL_KEY_SW_1_ICTL P0IEN /* Port Interrupt Control register */
#define HAL_KEY_SW_1_ICTLBIT BV(0) /* P0IEN - P0.0 enable/disable bit */
#define HAL_KEY_SW_1_IENBIT BV(5) /* Mask bit for all of Port_0 */ #define HAL_KEY_SW_1_PXIFG P0IFG /* Interrupt flag at source */ /* Joy stick move at P2.0 */
#define HAL_KEY_JOY_MOVE_PORT P2
#define HAL_KEY_JOY_MOVE_BIT BV(0)
#define HAL_KEY_JOY_MOVE_SEL P2SEL
#define HAL_KEY_JOY_MOVE_DIR P2DIR
#define HAL_KEY_SW_2_IENBIT BV(5) /* Mask bit for all of Port_0 */ /* edge interrupt */
#define HAL_KEY_JOY_MOVE_EDGEBIT BV(3)
#define HAL_KEY_JOY_MOVE_EDGE HAL_KEY_FALLING_EDGE /* Joy move interrupts */
#define HAL_KEY_JOY_MOVE_IEN IEN2 /* CPU interrupt mask register */
#define HAL_KEY_JOY_MOVE_IENBIT BV(1) /* Mask bit for all of Port_2 */
#define HAL_KEY_JOY_MOVE_ICTL P2IEN /* Port Interrupt Control register */
#define HAL_KEY_JOY_MOVE_ICTLBIT BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */
#define HAL_KEY_JOY_MOVE_PXIFG P2IFG /* Interrupt flag at source */ #define HAL_KEY_JOY_CHN HAL_ADC_CHANNEL_6 uint8 halGetJoyKeyInput(void); struct Key_State_t {
uint8 key;
uint8 keep;
uint8 shift;
uint32 runtime;
halKeyCBack_t pHalKeyProcessFunction;
};
struct Key_State_t hal_key_state;
/**************************************************************************************************
* @fn HalKeyInit
*
* @brief Initilize Key Service
*
* @param none
*
* @return None
**************************************************************************************************/
void HalKeyInit( void )
{
osal_memset(&hal_key_state,0,sizeof(hal_key_state)); HAL_KEY_SW_1_SEL &= ~(HAL_KEY_SW_1_BIT); /* Set pin function to GPIO */
HAL_KEY_SW_1_DIR &= ~(HAL_KEY_SW_1_BIT); /* Set pin direction to Input */ HAL_KEY_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */
HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input */
/* Initialize callback function */
hal_key_state.pHalKeyProcessFunction = NULL;
} /**************************************************************************************************
* @fn HalKeyConfig
*
* @brief Configure the Key serivce
*
* @param interruptEnable - TRUE/FALSE, enable/disable interrupt
* cback - pointer to the CallBack function
*
* @return None
**************************************************************************************************/
void HalKeyConfig (halKeyCBack_t cback)
{
hal_key_state.pHalKeyProcessFunction = cback;
osal_start_reload_timer( Hal_TaskID, HAL_KEY_EVENT, 20);
}
/**************************************************************************************************
* @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_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT)) /* Key is active low */
{
keys |= HAL_KEY_SW_1;
}
if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active low */
{
keys |= halGetJoyKeyInput();
}
return keys;
}
/**************************************************************************************************
* @fn HalKeyPoll
*
* @brief Called by hal_driver to poll the keys
*
* @param None
*
* @return None
**************************************************************************************************/
void HalKeyPoll (void)
{
uint8 keys = 0;
if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT)) /* Key is active low */
{
keys |= HAL_KEY_SW_1;
}
if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active HIGH */
{
keys = halGetJoyKeyInput();
}
if(keys)
{
hal_key_state.key = keys;
if(hal_key_state.keep==0x00)
{
hal_key_state.keep = 0x01;
hal_key_state.shift = HAL_KEY_SHIFT_DOWN;
hal_key_state.runtime = 0;
}
else
{
hal_key_state.shift = HAL_KEY_SHIFT_KEEP;
hal_key_state.runtime+=defHalKeyCheckTimePriod;
}
}
else if(hal_key_state.keep)
{
hal_key_state.keep = 0x00;
hal_key_state.shift = HAL_KEY_SHIFT_UP;
}
/* Invoke Callback if new keys were depressed */
if (hal_key_state.shift && (hal_key_state.pHalKeyProcessFunction))
{
hal_key_state.pHalKeyProcessFunction(keys, hal_key_state.shift);
hal_key_state.shift = 0;
}
}
/**************************************************************************************************
* @fn halGetJoyKeyInput
*
* @brief Map the ADC value to its corresponding key.
*
* @param None
*
* @return keys - current joy key status
**************************************************************************************************/
uint8 halGetJoyKeyInput(void)
{
/* The joystick control is encoded as an analog voltage.
* Read the JOY_LEVEL analog value and map it to joy movement.
*/
uint8 adc;
uint8 ksave0 = 0;
uint8 ksave1; /* Keep on reading the ADC until two consecutive key decisions are the same. */
do
{
ksave1 = ksave0; /* save previouse key reading */ adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8); if ((adc >= 2) && (adc <= 38))
{
ksave0 |= HAL_KEY_UP;
}
else if ((adc >= 74) && (adc <= 88))
{
ksave0 |= HAL_KEY_RIGHT;
}
else if ((adc >= 60) && (adc <= 73))
{
ksave0 |= HAL_KEY_LEFT;
}
else if ((adc >= 39) && (adc <= 59))
{
ksave0 |= HAL_KEY_DOWN;
}
else if ((adc >= 89) && (adc <= 100))
{
ksave0 |= HAL_KEY_CENTER;
}
} while (ksave0 != ksave1); return ksave0;
}
/**************************************************************************************************
* @fn HalKeyEnterSleep
*
* @brief - Get called to enter sleep mode
*
* @param
*
* @return
**************************************************************************************************/
void HalKeyEnterSleep ( void )
{
} /**************************************************************************************************
* @fn HalKeyExitSleep
*
* @brief - Get called when sleep is over
*
* @param
*
* @return - return saved keys
**************************************************************************************************/
uint8 HalKeyExitSleep ( void )
{
/* Wake up and read keys */
return ( HalKeyRead () );
} /**************************************************************************************************
**************************************************************************************************/

  hal_driver.c要修改成下面的:

  if (events & HAL_KEY_EVENT)
{
#if (defined HAL_KEY) && (HAL_KEY == TRUE)
/* Check for keys */
HalKeyPoll();
#endif
return events ^ HAL_KEY_EVENT;
}

  onBoard.c要修改:

void InitBoard( uint8 level )
{
if ( level == OB_COLD )
{
// Interrupts off
osal_int_disable( INTS_ALL );
// Turn all LEDs off
HalLedSet( HAL_LED_ALL, HAL_LED_MODE_OFF );
// Check for Brown-Out reset
// ChkReset();
}
else // !OB_COLD
{
/* Initialize Key stuff */
HalKeyConfig( OnBoard_KeyCallback);
}
}
void OnBoard_KeyCallback ( uint8 keys, uint8 shift )
{
// shift key (S1) is used to generate key interrupt
// applications should not use S1 when key interrupt is enabled
//shift = (OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE) ? false : ((keys & HAL_KEY_SW_6) ? true : false); if ( OnBoard_SendKeys( keys, shift ) != SUCCESS )
{
// Process SW1 here
//把不需要的全删
}
}

  good luck!