CC2652R TI-RTOS平臺實現key down/key up 按鍵長按效果
阿新 • • 發佈:2018-12-24
由於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);
}