首页 > 其他分享 >在TMOS系统中添加按键检测功能

在TMOS系统中添加按键检测功能

时间:2022-12-12 16:59:23浏览次数:61  
标签:TMOS HAL keys SW 添加 KEY 按键 PUSH

目录

TMOS系统中自带有按键检测轮询功能,默认100ms检测一次,在debug时很有帮助。不过在需要低功耗休眠的前提下,检测按键需要频繁唤醒,影响功耗,故多数BLE的工程都没有用到这个功能。代码贴在了最后。

启用按键查询,先要在工程配置中添加HAL_KEY,以及确保KEY.c添加进编译。

 

然后添加按键处理函数,函数名自拟,但是形参须为(uint8_t keys),调用HalKeyConfig函数注册一下。

这样默认代码中配置好了PB22和PB4两个按键,如上图添加,评估板上用PB4作为按键,就已经可以跑起来。如果需要更多按键,可以如下图添加处理,下图中添加到了总计5个按键,可以添加B组引脚,也可以添加A组引脚。

在KEY.c中需要做如下修改。

 

 在KEY.h中需要做如下修改。

打印如下,支持同时检测多个按键按下。

 

/*KEY.c中的修改后代码*/

/**************************************************************************************************
 * @fn      HAL_KeyInit
 *
 * @brief   Initilize Key Service
 *
 * @param   none
 *
 * @return  None
 **************************************************************************************************/
void HAL_KeyInit(void)
{
    /* Initialize previous key to 0 */
    halKeySavedKeys = 0;
    /* Initialize callback function */
    pHalKeyProcessFunction = NULL;
    KEY1_DIR;
    KEY1_PU;
    KEY2_DIR;
    KEY2_PU;
    KEY3_DIR;       //JW
    KEY3_PU;
    KEY4_DIR;       //JW
    KEY4_PU;
    KEY5_DIR;       //JW
    KEY5_PU;
}

/**************************************************************************************************
 * @fn      HalKeyConfig
 *
 * @brief   Configure the Key serivce
 *
 * @param   cback - pointer to the CallBack function
 *
 * @return  None
 **************************************************************************************************/
void HalKeyConfig(halKeyCBack_t cback)
{
    /* Register the callback fucntion */
    pHalKeyProcessFunction = cback;
    tmos_start_task(halTaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE); /* Kick off polling */
}

/**************************************************************************************************
 * @fn      HalKeyRead
 *
 * @brief   Read the current value of a key
 *
 * @param   None
 *
 * @return  keys - current keys status
 **************************************************************************************************/
uint8_t HalKeyRead(void)
{
    uint8_t keys = 0;

    if(HAL_PUSH_BUTTON1())
    { //读按键1
        keys |= HAL_KEY_SW_1;
    }
    if(HAL_PUSH_BUTTON2())
    { //读按键2
        keys |= HAL_KEY_SW_2;
    }
    if(HAL_PUSH_BUTTON3())
    { //读按键3
        keys |= HAL_KEY_SW_3;
    }
    if(HAL_PUSH_BUTTON4())
    { //读按键4
        keys |= HAL_KEY_SW_4;
    }
    if(HAL_PUSH_BUTTON5())          //JW
    { //读按键5
        keys |= HAL_KEY_SW_5;
    }
    return keys;
}

/**************************************************************************************************
 * @fn      HAL_KeyPoll
 *
 * @brief   Called by hal_driver to poll the keys
 *
 * @param   None
 *
 * @return  None
 **************************************************************************************************/
void HAL_KeyPoll(void)
{
    uint8_t keys = 0;
    if(HAL_PUSH_BUTTON1())
    {
        keys |= HAL_KEY_SW_1;
    }
    if(HAL_PUSH_BUTTON2())
    {
        keys |= HAL_KEY_SW_2;
    }
    if(HAL_PUSH_BUTTON3())
    {
        keys |= HAL_KEY_SW_3;
    }
    if(HAL_PUSH_BUTTON4())
    {
        keys |= HAL_KEY_SW_4;
    }
    if(HAL_PUSH_BUTTON5())          //JW
    {
        keys |= HAL_KEY_SW_5;
    }
    if(keys == halKeySavedKeys)
    { /* Exit - since no keys have changed */
        return;
    }
    halKeySavedKeys = keys; /* Store the current keys for comparation next time */
    /* Invoke Callback if new keys were depressed */
    if(keys && (pHalKeyProcessFunction))
    {
        (pHalKeyProcessFunction)(keys);
    }
}
/*KEY.h中的修改后代码*/

/**************************************************************************************************
 *                                              MACROS
 **************************************************************************************************/
#define HAL_KEY_POLLING_VALUE    100

/* Switches (keys) */
#define HAL_KEY_SW_1             0x01  // key1
#define HAL_KEY_SW_2             0x02  // key2
#define HAL_KEY_SW_3             0x04  // key3
#define HAL_KEY_SW_4             0x08  // key4
#define HAL_KEY_SW_5             0x10  // key5      //JW

/* 按键定义 */

/* 1 - KEY */
#define KEY1_BV                  BV(22)
#define KEY2_BV                  BV(4)
#define KEY3_BV                  BV(8)      //JW
#define KEY4_AV                  (1 << 10)      //JW
#define KEY5_AV                  (1 << 15)      //JW

#define KEY1_PU                  (R32_PB_PU |= KEY1_BV)
#define KEY2_PU                  (R32_PB_PU |= KEY2_BV)
#define KEY3_PU                  (R32_PB_PU |= KEY3_BV)     //JW
#define KEY4_PU                  (R32_PA_PU |= KEY4_AV)     //JW
#define KEY5_PU                  (R32_PA_PU |= KEY5_AV)     //JW

#define KEY1_DIR                 (R32_PB_DIR &= ~KEY1_BV)
#define KEY2_DIR                 (R32_PB_DIR &= ~KEY2_BV)
#define KEY3_DIR                 (R32_PB_DIR &= ~KEY3_BV)       //JW
#define KEY4_DIR                 (R32_PA_DIR &= ~KEY4_AV)       //JW
#define KEY5_DIR                 (R32_PA_DIR &= ~KEY5_AV)       //JW

#define KEY1_IN                  (ACTIVE_LOW(R32_PB_PIN & KEY1_BV))
#define KEY2_IN                  (ACTIVE_LOW(R32_PB_PIN & KEY2_BV))
#define KEY3_IN                  (ACTIVE_LOW(R32_PB_PIN & KEY3_BV))     //JW
#define KEY4_IN                  (ACTIVE_LOW(R32_PA_PIN & KEY4_AV))     //JW
#define KEY5_IN                  (ACTIVE_LOW(R32_PA_PIN & KEY5_AV))     //JW

#define HAL_PUSH_BUTTON1()       (KEY1_IN) //添加自定义按键
#define HAL_PUSH_BUTTON2()       (KEY2_IN)
#define HAL_PUSH_BUTTON3()       (KEY3_IN)      //JW
#define HAL_PUSH_BUTTON4()       (KEY4_IN)      //JW
#define HAL_PUSH_BUTTON5()       (KEY5_IN)      //JW

标签:TMOS,HAL,keys,SW,添加,KEY,按键,PUSH
From: https://www.cnblogs.com/JayWellsBlog/p/16976473.html

相关文章