/* * Copyright (c) Hisilicon Technologies Co., Ltd. 2020-2022. All rights reserved. * Description: virtualkeypad input * Author: App-Dev Group * Create: 2020-06-16 */ #include #include #include #include "VirtualKeypad.h" using namespace std; using namespace android; void VirtualKeypad::SetChannelMask(td_u32& u32ChannelMask) { for (int i = 0; i < maxKeypadCh; i++) { if (boardKpd[i].bEnable == 0 && boardKpd[i].u8ChannelID < channelIdMaxValue) { continue; } gpioPowerInterface->lsadcEnable = 1; u32ChannelMask |= 0x1 << static_cast(boardKpd[i].u8ChannelID); } channelMask = u32ChannelMask; } td_u32 VirtualKeypad::MapKeyValue(int keyPadTabIdx, td_u32 origValue) const { if (keyPadTabIdx >= maxKeypadCh) { return invalidKey; } if (boardKpd[keyPadTabIdx].u8KeyLevelNum > KEY_MAX_NUM) { return invalidKey; } for (int i = 0; i < boardKpd[keyPadTabIdx].u8KeyLevelNum; i++) { if ((origValue <= boardKpd[keyPadTabIdx].u8KeyThreshold[i]) && (origValue >= (boardKpd[keyPadTabIdx].u8KeyThreshold[i] - boardKpd[keyPadTabIdx].u8KeyRange[i]))) { return boardKpd[keyPadTabIdx].u32KeyCode[i]; } } return invalidKey; // something wrong, need check } int VirtualKeypad::TimeAfter(const struct timeval& bef, const struct timeval& cur, unsigned int ms) const { unsigned long long a, b; // 1s equals 1000ms a = static_cast((cur.tv_sec) * 1000) + static_cast((cur.tv_usec) / 1000); // 1ms equals 1000us b = static_cast((bef.tv_sec) * 1000) + static_cast((bef.tv_usec) / 1000); if ((a > b) && (a - b >= ms)) { return 1; } return 0; } void VirtualKeypad::ProcessKey(td_u32 key) { static struct timeval time, trep, tlast; static td_u32 klast = invalidKey; static int hold = 0; gettimeofday(&time, nullptr); if (klast != invalidKey && TimeAfter(tlast, time, keyOffTime) == 1) { gpioPowerInterface->keypadInputInterface->WriteEventToDevice(klast, 0); klast = invalidKey; } if (key == invalidKey) { return; } tlast = time; if (klast != key) { trep = time; gpioPowerInterface->keypadInputInterface->WriteEventToDevice(key, 1); klast = key; hold = 1; } else { if (hold == 1 && TimeAfter(trep, time, repeatHoldTime) == 1) { trep = time; hold = 0; } else if (TimeAfter(trep, time, repeatInterval) == 1) { trep = time; } } } void VirtualKeypad::InitLSADCConfig() { LSADC_Config stLSADCConfig; if (memset_s(&stLSADCConfig, sizeof(LSADC_Config), 0x00, sizeof(LSADC_Config)) != EOK) { VIRTUALKEY_LOG("main memset_s failed !"); } lsadcInterface->LsadcGetConfig(stLSADCConfig); boardKpd.resize(maxChNum); gpioPowerInterface->keyPars->GetBoardKeyPad(boardKpd, maxKeypadCh); td_u32 channelMaskRet = LSADC_CHAN_MASK(&stLSADCConfig); SetChannelMask(channelMaskRet); LsadcInterface::LsadcConfig config = { channelMaskRet, 0xFC, 0x0, 0x1, 0, 0x0, 0xff, 0x1, 0x0, 600 }; lsadcInterface->LsadcStructConfig(stLSADCConfig, config); lsadcInterface->LsadcSetConfig(stLSADCConfig); } td_u32 VirtualKeypad::GetKeyPadPowerValue(vector& powerKeyCode, td_u32 length) const { td_u32 keycodeNum = 0; if (powerKeyCode.empty()) { return keycodeNum; } for (int i = 0; i < maxKeypadCh; i++) { if (boardKpd[i].bEnable == 0) { continue; } if (boardKpd[i].u8KeyLevelNum > keyLevelMaxNum) { continue; } for (int j = 0; j < boardKpd[i].u8KeyLevelNum; j++) { if (keycodeNum > length) { break; } if (boardKpd[i].u32KeyCode[j] == KEY_POWER) { powerKeyCode.at(i) = boardKpd[i].u8KeyThreshold[j]; keycodeNum++; } } } return keycodeNum; }