CC2652R TI-RTOS平台实现key down/key up 按键长按效果

版权声明:谁想转载随便转载吧,反正多写这一句“未经允许不得转载” 然并卵!!! https://blog.csdn.net/code_style/article/details/85133381

由于TI的hal_key.c的实现比较简陋,上层只能收到key down消息,没办法收到key up消息,也就是只能收到按键按下消息,没办法收到按键弹起的消息,所以对于需要实现长按某个按键实现指定功能来说,基本上没办法实现。

实现按键长按的原理非常简单,大概是这样的,在按键按下的时候,增加一个timer来计时,在按键弹起的时候,如果timer没超时,则删掉。然后在timer超时的回调函数里面实现指定的功能即可,这样只要按键按下不弹起,timer在指定的时间肯定会超时,就可以实现按键长按的效果了。

  • 直接上hal_key.c和hal_key.h的代码,下面的例子是实现Board_BUTTON4按键的长按效果,其他按键基本上按照这个修改都可以实现长按效果,上层应用要配合回调函数修改相应的参数。

hal_key.h

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

 @file board_key.h

 @brief This file contains the Key Press Service definitions and prototypes.

 Group: WCS LPC
 Target Device: CC2652

 ******************************************************************************
 
 Copyright (c) 2016-2018, Texas Instruments Incorporated
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 *  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

 *  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

 *  Neither the name of Texas Instruments Incorporated nor the names of
    its contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 ******************************************************************************
 Release Name: simplelink_cc26x2_sdk_2_10_00_44_s
 Release Date: 2018-04-09 12:59:57
 *****************************************************************************/
#ifndef BOARD_KEY_H
#define BOARD_KEY_H

/******************************************************************************
 Includes
 *****************************************************************************/

#include "Board.h"

#ifdef __cplusplus
extern "C"
{
#endif

/*!
 \defgroup BoardKey Keypress Functions
 <BR>
 This module handles the definition, initilization and key bounce for key
 presses.
 To use this module, call Board_Key_initialize() with a callback function as
 the parameter, then when a key or keys are pressed, the callback function
 will be called (after a debounce period).  Single or multiple key presses are
 detected and passed to the callback function.
 <BR>
 */

/******************************************************************************
 Constants
 *****************************************************************************/

/*!
 * \ingroup BoardKey
 * @{
 */

/*! Select Key ID */
#define KEY_SELECT            0x01
/*! Up Key ID */
#define KEY_UP                0x02
/*! Down Key ID */
#define KEY_DOWN              0x04
/*! Left Key ID */
#define KEY_LEFT              0x08
/*! Right Key ID */
#define KEY_RIGHT             0x10

#define KEY_PRESSED           0x10
#define KEY_RELEASED          0x20

/*! Debounce timeout in milliseconds */
#define KEY_DEBOUNCE_TIMEOUT  50

/******************************************************************************
 Typedefs
 *****************************************************************************/

/*! Key Press Callback function typedef */
typedef void (*Board_Key_keysPressedCB_t)(uint8_t keysPressed,uint8_t keysStatus);

/******************************************************************************
 API Functions
 *****************************************************************************/

/*!
 * @brief   Enable interrupts for keys on GPIOs.
 *
 * @param   appKeyCB - application key pressed callback
 *
 * @return  current state of all keys, check for bit masked
 *          KEY_SELECT, KEY_UP, etc.
 */
uint8_t Board_Key_initialize(Board_Key_keysPressedCB_t appKeyCB);

/*! @} end group BoardKey */

#ifdef __cplusplus
}
#endif

#endif /* BOARD_KEY_H */

hal_key.c

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

 @file board_key.c

 @brief This file contains the interface to the Launchpad Key Service

 <BR>
 NOTE:  The launchpad only has 2 buttons, so only KEY_LEFT and KEY_RIGHT are
        enabled.
 <BR>
 Group: WCS LPC
 Target Device: CC2652

 ******************************************************************************
 
 Copyright (c) 2016-2018, Texas Instruments Incorporated
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 *  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

 *  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

 *  Neither the name of Texas Instruments Incorporated nor the names of
    its contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 ******************************************************************************
 Release Name: simplelink_cc26x2_sdk_2_10_00_44_s
 Release Date: 2018-04-09 12:59:57
 *****************************************************************************/

/******************************************************************************
 Includes
 *****************************************************************************/

#include <stdbool.h>
#include <ti/drivers/pin/PINCC26XX.h>
#include <chipinfo.h>
#include "util_timer.h"
#include "board_key.h"

/******************************************************************************
 Local Variables
 *****************************************************************************/

/* Value of keys Pressed */
static uint8_t keysPressed;
static uint8_t keysStatus;

/* Key debounce clock */
static Clock_Struct keyChangeClock;

/* Pointer to application callback */
static Board_Key_keysPressedCB_t appKeyChangeHandler = NULL;

/*
 Create the MSA KEY pin table. This will override the key attributes in
 BoardGpioInitTable[].
 */
static PIN_Config keyPinTable[] = {
      Board_BUTTON0 | PIN_INPUT_EN | PIN_PULLDOWN | PIN_IRQ_POSEDGE|PINCC26XX_WAKEUP_POSEDGE,      //left
      Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLDOWN | PIN_IRQ_POSEDGE|PINCC26XX_WAKEUP_POSEDGE,      //right
      Board_BUTTON2 | PIN_INPUT_EN | PIN_PULLDOWN | PIN_IRQ_POSEDGE|PINCC26XX_WAKEUP_POSEDGE,      //up
      Board_BUTTON3 | PIN_INPUT_EN | PIN_PULLDOWN | PIN_IRQ_POSEDGE|PINCC26XX_WAKEUP_POSEDGE,      //down
      Board_BUTTON4 | PIN_INPUT_EN | PIN_NOPULL | PIN_IRQ_BOTHEDGES|PINCC26XX_WAKEUP_POSEDGE,      //select
      PIN_TERMINATE /* Terminate list */
    };

#if defined(CC13X2R1_LAUNCHXL) && !defined(NO_CC1312R1_SUPPORT)
static PIN_Config keyPinTable_CC1312R1[] = {
      Board_PIN_BUTTON0_CC1312R1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
      Board_PIN_BUTTON1_CC1312R1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
      PIN_TERMINATE /* Terminate list */
    };
#endif

/* KEY pin state */
static PIN_State keyPinState;

/* KEY Pin Handle */
static PIN_Handle keyPinHandle;

/******************************************************************************
 Local Functions
 *****************************************************************************/
static void board_key_changeHandler(UArg a0);
static void board_key_keyFxn(PIN_Handle keyPinHandle, PIN_Id keyPinId);
static uint8_t board_key_getValues(void);

/******************************************************************************
 Public Functions
 *****************************************************************************/

/*!
 Enable interrupts for keys on PIN.

 Public function defined in board_key.h
 */
uint8_t Board_Key_initialize(Board_Key_keysPressedCB_t appKeyCB)
{
    uint8_t keyState;

    /* Setup KEY ISR */
#if defined(CC13X2R1_LAUNCHXL) && !defined(NO_CC1312R1_SUPPORT)
#ifndef TIMAC_AGAMA_FPGA
    if (ChipInfo_GetChipType() == CHIP_TYPE_CC1312)
    {
        keyButton0 = Board_PIN_BUTTON0_CC1312R1;
        keyButton1 = Board_PIN_BUTTON1_CC1312R1;
        keyPinHandle = PIN_open(&keyPinState, keyPinTable_CC1312R1);
    }
    else
#endif
    {
        keyButton0 = Board_PIN_BUTTON0;
        keyButton1 = Board_PIN_BUTTON1;
        keyPinHandle = PIN_open(&keyPinState, keyPinTable);
    }
#else

    keyPinHandle = PIN_open(&keyPinState, keyPinTable);
#endif
    /* Get current key state */
    keyState = board_key_getValues();

    /* Register Callbacks */
    PIN_registerIntCb(keyPinHandle, board_key_keyFxn);

    /* Setup keycallback for keys */
    Timer_construct(&keyChangeClock, board_key_changeHandler,
                    (KEY_DEBOUNCE_TIMEOUT),
                    0, false, 0);

    /* Set the application callback */
    appKeyChangeHandler = appKeyCB;

    return(keyState);
}

/*!
 * @brief       Interrupt handler for a Key press
 *
 * @param       keyPinHandle - PIN Handle
 * @param       keyPinId - PIN ID
 */
static void board_key_keyFxn(PIN_Handle keyPinHandle, PIN_Id keyPinId)
{
    (void)keyPinHandle;
    
    if(keyPinId == Board_BUTTON0)
    {
        keysPressed |= KEY_LEFT;
    }
    else if(keyPinId == Board_BUTTON1)
    {
        keysPressed |= KEY_RIGHT;
    }
    else if(keyPinId == Board_BUTTON2)
    {
        keysPressed |= KEY_UP;
    }
    else if(keyPinId == Board_BUTTON3)
    {
        keysPressed |= KEY_DOWN;
    }
    else if(keyPinId == Board_BUTTON4)
    {       
        if(PIN_getInputValue(keyPinId) == false)
        {
          keysStatus=KEY_PRESSED;
        }else if(PIN_getInputValue(keyPinId) == true){
          keysStatus=KEY_RELEASED;
        }

        keysPressed |= KEY_SELECT;
    }
    
    if(Timer_isActive(&keyChangeClock) != true)
    {
        Timer_start(&keyChangeClock);
    }
}

/*!
 * @brief       Handler for key change
 *
 * @param       UArg a0 - ignored
 */
static void board_key_changeHandler(UArg a0)
{
    if(appKeyChangeHandler != NULL)
    {
        /* Notify the application */
        (*appKeyChangeHandler)(keysPressed,keysStatus);

        /* Clear keys */
        keysPressed = 0;
        keysStatus = 0;
    }
}

/*!
 * @brief       Get the current value for all the keys
 *
 * @return      bit mapped representation of all keys
 */
static uint8_t board_key_getValues(void)
{
    uint8_t keys = 0;

    if(PIN_getInputValue(Board_BUTTON0) == false)
    {
        keys |= KEY_LEFT;
    }

    if(PIN_getInputValue(Board_BUTTON1) == false)
    {
        keys |= KEY_RIGHT;
    }

    return(keys);
}


猜你喜欢

转载自blog.csdn.net/code_style/article/details/85133381