You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.0 KiB

/*
* Copyright (c) Hisilicon Technologies Co., Ltd. 2020-2022. All rights reserved.
* Description: virtualkeypad input main
* Author: App-Dev Group
* Create: 2020-06-17
*/
#include <thread>
#include "VirtualKeypad.h"
using namespace std;
using namespace android;
static unique_ptr<VirtualKeypad> g_virtualKeypad;
namespace {
const td_u32 KEYPAD_POWER_KEY_NUM = 8;
const int MIN_KEYPAD_VALUE = 0x00;
const int MAX_KEYPAD_VALUE = 0xFF;
void VirtualkeypadThread()
{
td_u32 pre[VirtualKeypad::maxKeypadCh] = {
VirtualKeypad::invalidKey,
VirtualKeypad::invalidKey,
};
td_u32 cur[VirtualKeypad::maxKeypadCh];
td_u32 value = 0;
td_u32 cnt[VirtualKeypad::maxKeypadCh] = {
0,
0,
};
// 10*1000*1000 = 10 ms
struct timespec req = {0, 10 * 1000 * 1000};
while (1) {
nanosleep(&req, nullptr);
/* do gpio_power opt */
if (g_virtualKeypad->gpioPowerInterface->gpioFlag == TD_SUCCESS) {
g_virtualKeypad->gpioPowerInterface->DoGpioPowerOpt();
continue;
}
for (int i = 0; i < VirtualKeypad::maxKeypadCh; i++) {
if (g_virtualKeypad->boardKpd[i].bEnable == 0) {
continue;
}
int ret = g_virtualKeypad->lsadcInterface->LsadcGetValue(g_virtualKeypad->boardKpd[i].u8ChannelID,
value);
if (ret != 0) {
cur[i] = VirtualKeypad::invalidKey;
} else {
cur[i] = g_virtualKeypad->MapKeyValue(i, value);
}
if (cur[i] != pre[i]) {
pre[i] = cur[i];
cnt[i] = 0;
} else {
cnt[i]++;
}
if (cur[i] == pre[i] && cnt[i] >= g_virtualKeypad->gpioPowerInterface->checkCount) {
g_virtualKeypad->ProcessKey(pre[i]);
}
}
}
return;
}
void ThreadProcess()
{
thread keyThread(VirtualkeypadThread);
keyThread.join();
VIRTUALKEY_LOG("VirtualkeypadThread exit! Bye!\n");
}
}
int main()
{
g_virtualKeypad = make_unique<VirtualKeypad>();
/* Open LSADC */
int ret = g_virtualKeypad->lsadcInterface->LsadcInit();
if (ret != TD_SUCCESS) {
VIRTUALKEY_LOG("UAPI_PMOC_Init err, 0x%08x !\n", ret);
return ret;
}
g_virtualKeypad->InitLSADCConfig();
unique_ptr<KeyPars::StKeypadDeviceData> pKpdData = make_unique<KeyPars::StKeypadDeviceData>();
if (pKpdData != nullptr) {
pKpdData->minKeycode = MIN_KEYPAD_VALUE;
pKpdData->maxKeycode = MAX_KEYPAD_VALUE;
} else {
VIRTUALKEY_LOG("Unable to find uInput device\n");
return -1;
}
if (g_virtualKeypad->gpioPowerInterface->keypadInputInterface->SetupUinputDevice(*pKpdData) < 0) {
VIRTUALKEY_LOG("Unable to find uInput device\n");
pKpdData.reset();
return -1;
}
vector<td_u8> powerKeyCode(KEYPAD_POWER_KEY_NUM);
td_u32 powerNum = g_virtualKeypad->GetKeyPadPowerValue(powerKeyCode, KEYPAD_POWER_KEY_NUM);
if (powerNum > 0) {
g_virtualKeypad->lsadcInterface->LsadcPowerKeySet(powerKeyCode, KEYPAD_POWER_KEY_NUM,
powerNum, g_virtualKeypad->channelMask);
}
/* init gpio config */
g_virtualKeypad->gpioPowerInterface->GpioPowerInit();
ThreadProcess();
pKpdData.reset();
/* deinit gpio config */
g_virtualKeypad->gpioPowerInterface->GpioPowerDeinit();
// Destroy the device
int retCheck = g_virtualKeypad->gpioPowerInterface->keypadInputInterface->Close();
if (retCheck < 0) {
return retCheck;
}
g_virtualKeypad->lsadcInterface->LsadcDeinit();
return TD_SUCCESS;
}