实现功能
短按进入NB模式,长按3s左右进入BLE模式,长按5s关机。
实现步骤
首先按键作为一个输入设备,我们应该把IO口设置成输入上拉模式,其次还涉及到消抖处理,防止误触发。由于该按键需实现长短按,于是把IO口设置成双边沿触发中断,具体实现如下:
//配置IO位输入上拉模式
PIN_Config keyPinsCfg[] =
{
Board_WakeUpKey | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP,
PIN_TERMINATE
};
//注册按键回调函数
Board_initKeys(SimpleBLEPeripheral_keyChangeHandler);
//按键初始化
void Board_initKeys(keysPressedCB_t appKeyCB)
{
// Initialize KEY pins. Enable int after callback registered
KeyHandle = PIN_open(&KeyState, keyPinsCfg);
PIN_registerIntCb(KeyHandle, Board_keyCallback); //注册中断,中断触发进入Board_keyCallback函数
//定义中断触发条件
PIN_setConfig(KeyHandle, PIN_BM_IRQ, Board_WakeUpKey | PIN_IRQ_BOTHEDGES); //电源按键
#ifdef POWER_SAVING //低功耗状态唤醒
//Enable wakeup
PIN_setConfig(KeyHandle, PINCC26XX_BM_WAKEUP, Board_WakeUpKey | PINCC26XX_WAKEUP_NEGEDGE);
#endif //POWER_SAVING
// Setup keycallback for keys 消抖
Util_constructClock(&keyChangeClock, Board_keyChangeHandler,
KEY_DEBOUNCE_TIMEOUT, 0, false, 0); //手动定时器
// Set the application callback
//appKeyCB 入队列的函数指针
appKeyChangeHandler = appKeyCB; //通知APP层,有按键触发
}
|