(BLE)CC2541按键程序大致处理流程

来源:互联网 发布:json是转换是什么 编辑:程序博客网 时间:2024/05/21 19:33

1) 向一个全局变量 registeredKeysTaskID中赋值自己的任务 ID,调用了这个函数就能成功注册按键服务

      在osal_init_system()中调用osalInitTasks()初始化系统任务,调用应用程序初始化函数void SimpleBLETest_Init( uint8 task_id );

void SimpleBLETest_Init( uint8 task_id ){ //保存任务id到全局变量     SimpleBLETest_TaskID = task_id;         // 串口初始化 波特率默认是115200, 形参是回调函数  NPI_InitTransport(NpiSerialCallback);  //lcd 显示  HalLcdWriteString( "SimpleBLETest 11", HAL_LCD_LINE_1 );  // Register for all key events - This app will handle all key events  RegisterForKeys( SimpleBLETest_TaskID );  // Setup a delayed profile startup    /*  设置一个任务, 这么做的目的是按照多任务处理的方法来做  SimpleBLETest_ProcessEvent 就是处理 SBP_START_DEVICE_EVT  */  osal_set_event( SimpleBLETest_TaskID, SBP_START_DEVICE_EVT );}


2)  一个按键回调服务注册函数,注册了一个OnBoard_KeyCallback函数

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 */    OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;    //OnboardKeyIntEnable = HAL_KEY_INTERRUPT_DISABLE;    HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);  }}


3) 有按键值,则进入void halProcessKeyInterrupt (void)函数

void halProcessKeyInterrupt (void){  bool valid=FALSE;#if defined ( CC2540_MINIDK )  if( HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) /* Interrupt Flag has been set by SW1 */  {    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* Clear Interrupt Flag */    valid = TRUE;  }  if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)  /* Interrupt Flag has been set by SW2 */  {    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear Interrupt Flag */    valid = TRUE;  }#else  if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)  /* Interrupt Flag has been set */  {    HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */    valid = TRUE;  }  if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  /* Interrupt Flag has been set */  {    HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */    valid = TRUE;  }#endif  if (valid)  {    osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);  }}

4) 进入uint16 Hal_ProcessEvent( uint8 task_id, uint16 events )函数对HAL_KEY_EVENT事件处理,调用HalKeyPoll()轮询按键

uint16 Hal_ProcessEvent( uint8 task_id, uint16 events ){  uint8 *msgPtr;  (void)task_id;  // Intentionally unreferenced parameter  if ( events & SYS_EVENT_MSG )  {    msgPtr = osal_msg_receive(Hal_TaskID);    while (msgPtr)    {      /* Do something here - for now, just deallocate the msg and move on */      /* De-allocate */      osal_msg_deallocate( msgPtr );      /* Next */      msgPtr = osal_msg_receive( Hal_TaskID );    }    return events ^ SYS_EVENT_MSG;  }#if (defined HAL_BUZZER) && (HAL_BUZZER == TRUE)  if (events & HAL_BUZZER_EVENT)  {    HalBuzzerStop();    return events ^ HAL_BUZZER_EVENT;  }#endif#ifdef CC2591_COMPRESSION_WORKAROUND  if ( events & PERIOD_RSSI_RESET_EVT )  {    macRxResetRssi();    return (events ^ PERIOD_RSSI_RESET_EVT);  }#endif  if ( events & HAL_LED_BLINK_EVENT )  {#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)    HalLedUpdate();#endif /* BLINK_LEDS && HAL_LED */    return events ^ HAL_LED_BLINK_EVENT;  }  if (events & HAL_KEY_EVENT)  {#if (defined HAL_KEY) && (HAL_KEY == TRUE)    /* Check for keys */    HalKeyPoll();    /* if interrupt disabled, do next polling */    if (!Hal_KeyIntEnable)    {      osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);    }#endif    return events ^ HAL_KEY_EVENT;  }#if defined POWER_SAVING  if ( events & HAL_SLEEP_TIMER_EVENT )  {    halRestoreSleepLevel();    return events ^ HAL_SLEEP_TIMER_EVENT;  }  if ( events & HAL_PWRMGR_HOLD_EVENT )  {    (void)osal_pwrmgr_task_state(Hal_TaskID, PWRMGR_HOLD);    (void)osal_stop_timerEx(Hal_TaskID, HAL_PWRMGR_CONSERVE_EVENT);    (void)osal_clear_event(Hal_TaskID, HAL_PWRMGR_CONSERVE_EVENT);    return (events & ~(HAL_PWRMGR_HOLD_EVENT | HAL_PWRMGR_CONSERVE_EVENT));  }  if ( events & HAL_PWRMGR_CONSERVE_EVENT )  {    (void)osal_pwrmgr_task_state(Hal_TaskID, PWRMGR_CONSERVE);    return events ^ HAL_PWRMGR_CONSERVE_EVENT;  }#endif  return 0;}

5) 按键轮询函数HalKeyPoll()中若当前有新键值,执行回调函数OnBoard_KeyCallBack()
void HalKeyPoll (void){  uint8 keys = 0;  uint8 notify = 0;#if defined (CC2540_MINIDK)  if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */  {    keys |= HAL_KEY_SW_1;  }  if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */  {    keys |= HAL_KEY_SW_2;  }#else  if ((HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active high */    {    keys |= HAL_KEY_SW_6;  }  if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active HIGH */  {    keys = halGetJoyKeyInput();  }#endif  /* If interrupts are not enabled, previous key status and current key status   * are compared to find out if a key has changed status.   */  if (!Hal_KeyIntEnable)  {    if (keys == halKeySavedKeys)    {      /* Exit - since no keys have changed */      return;    }    else    {      notify = 1;    }  }  else  {    /* Key interrupt handled here */    if (keys)    {      notify = 1;    }  }  /* Store the current keys for comparation next time */  halKeySavedKeys = keys;  /* Invoke Callback if new keys were depressed */  if (notify && (pHalKeyProcessFunction))  {    (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);  }}

6) Keys表示具体哪个键被按下,将该按键消息上传到应用层处理
void OnBoard_KeyCallback ( uint8 keys, uint8 state ){  uint8 shift;  (void)state;  // 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    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 any key is currently pressed down and interrupt     is still enabled, disable interrupt and switch to polling */  if( keys != 0 )  {    if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE )    {      OnboardKeyIntEnable = HAL_KEY_INTERRUPT_DISABLE;      HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);    }  }  /* If no key is currently pressed down and interrupt     is disabled, enable interrupt and turn off polling */  else  {    if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_DISABLE )    {      OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;      HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);    }  }}

7) 应用程序的事件处理函数uint16 SimpleBLETest_ProcessEvent( uint8 task_id, uint16 events )接受底层发来的消息
uint16 SimpleBLETest_ProcessEvent( uint8 task_id, uint16 events ){  VOID task_id; // OSAL required parameter that isn't used in this function  // SYS_EVENT_MSG 这是系统事件比如按键事件蓝牙读写事件处理,都会置这个事件  if ( events & SYS_EVENT_MSG )  {    uint8 *pMsg;    if ( (pMsg = osal_msg_receive( SimpleBLETest_TaskID )) != NULL )    {      simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );      // Release the OSAL message      VOID osal_msg_deallocate( pMsg );    }    // return unprocessed events    return (events ^ SYS_EVENT_MSG);  }  // 这个是我们应用程序自定义的事件,SBP_START_DEVICE_EVT 的值被定义为 0x0001,   // 实际上我们可以定义 16个事件, 第一的时候是以位来定义的  // 这个 SBP_PERIODIC_EVT 就是在SimpleBLETest_Init初始化函数最后一行设置的事件  if ( events & SBP_START_DEVICE_EVT )  {    HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);      // 点亮led1                   // 返回这个, 告诉osal,这个实践你已经处理了    return ( events ^ SBP_START_DEVICE_EVT );     }  // Discard unknown events  return 0;}

8) 应用程序的事件处理函数调用simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg )处理消息
static void simpleBLECentral_ProcessOSALMsg( osal_event_hdr_t *pMsg ){  switch ( pMsg->event )  {    case KEY_CHANGE:   //HalLedSet(HAL_LED_2, HAL_LED_MODE_TOGGLE);      simpleBLECentral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );      break;    case GATT_MSG_EVENT:      //simpleBLECentralProcessGATTMsg( (gattMsgEvent_t *) pMsg );      break;  }}

9)按键消息具体处理方法
static void simpleBLECentral_HandleKeys( uint8 shift, uint8 keys ){  (void)shift;  // Intentionally unreferenced parameter  HalLcdWriteStringValue( "key = 0x", keys, 16, HAL_LCD_LINE_2 );  // smartRF开发板上的S1 对应我们源码上的HAL_KEY_SW_6  if ( keys & HAL_KEY_SW_6 )  {    HalLcdWriteString( "HAL_KEY_SW_6", HAL_LCD_LINE_3 );  }  if ( keys & HAL_KEY_UP )  {      HalLcdWriteString( "HAL_KEY_UP", HAL_LCD_LINE_3 );  }  if ( keys & HAL_KEY_LEFT )  {    HalLcdWriteString( "HAL_KEY_LEFT", HAL_LCD_LINE_3 );  }  if ( keys & HAL_KEY_RIGHT )  {    HalLcdWriteString( "HAL_KEY_RIGHT", HAL_LCD_LINE_3 );  }    if ( keys & HAL_KEY_CENTER )  {    HalLcdWriteString( "HAL_KEY_CENTER", HAL_LCD_LINE_3 );  }    if ( keys & HAL_KEY_DOWN )  {    HalLcdWriteString( "HAL_KEY_DOWN", HAL_LCD_LINE_3 );  }}


0 0
原创粉丝点击