您的位置:首页 > 其它

ble学习笔记十四---------ble协议栈之按键事件

2014-10-30 20:53 344 查看
协议栈按键事件

修改工程配置,使能按键功能

添加头文件#include "serial.h"

协议栈中定义宏开关

//自定义宏开关

#if defined( CC2540_MINIDK )||(MT254xboard)

//函数声明,按键响应

static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys );

//init函数中定义

#if defined (MT254xboard)

// Register for all key events - This app will handle all key events

RegisterForKeys( simpleBLEPeripheral_TaskID );

#endif

static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg )

{

switch ( pMsg->event )

{

//

#if defined( CC2540_MINIDK )||(MT254xboard)

case KEY_CHANGE:

simpleBLEPeripheral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );

break;

#endif // #if defined( CC2540_MINIDK )

default:

// do nothing

break;

}

}

//

#if defined( CC2540_MINIDK )||(MT254xboard)

/*********************************************************************

* @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 simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys )

{

//uint8 SK_Keys = 0;

VOID shift; // Intentionally unreferenced parameter

if ( keys & HAL_KEY_UP )

{

SerialPrintf("%s\r\n", "Key Up");

}

if ( keys & HAL_KEY_RIGHT )

{

SerialPrintf("%s\r\n", "Key right");

}

if ( keys & HAL_KEY_CENTER )

{

SerialPrintf("%s\r\n", "Key center");

}

if ( keys & HAL_KEY_LEFT )

{

SerialPrintf("%s\r\n", "Key left");

}

if ( keys & HAL_KEY_DOWN )

{

SerialPrintf("%s\r\n", "Key down");

}

if ( keys & HAL_KEY_SW_6 )

{

SerialPrintf("%s\r\n", "Button down");

}

}

#endif // #if defined( CC2540_MINIDK )

修改五向按键值的电压范围 在hal_led.c文件中

#if !defined ( CC2540_MINIDK )

/**************************************************************************************************

* @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.

*/

uint16 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_10);

if ((adc >= 2) && (adc <= 160)) // right 172

{

ksave0 |= HAL_KEY_RIGHT;

}

else if ((adc >= 161) && (adc <= 180)) // cen 146

{

ksave0 |= HAL_KEY_CENTER;

}

else if ((adc >= 181) && (adc <= 220)) // up 204

{

ksave0 |= HAL_KEY_UP;

}

else if ((adc >= 221) && (adc <= 270)) // left 256

{

ksave0 |= HAL_KEY_LEFT;

}

else if ((adc >= 271) && (adc <= 350)) // down 306

{

ksave0 |= HAL_KEY_DOWN;

}

} while (ksave0 != ksave1);

return ksave0;

}

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: