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

协议栈按键事件

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

 

 

添加头文件#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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值