zynq_7010/zynq_7010_code/Keyboard.cpp

192 lines
5.5 KiB
C++

#include "Keyboard.h"
#include <stdio.h>
#include <sys/time.h>
#include "utilsfunc.h"
#define KEYCODE_POWER 116
#define KEYCODE_START 60
#define KEYCODE_STOP 115
namespace huagao
{
Keyboard::Keyboard(std::function<void(const int)> buttonevent)
{
m_event = buttonevent;
mInputFd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK);
}
Keyboard::~Keyboard()
{
b_run = false;
if (pollthread.joinable())
pollthread.join();
if (mInputFd > 0)
{
::close(mInputFd);
}
::close(mEpId);
}
void Keyboard::init()
{
mEpId = epoll_create(1);
int err;
int len;
int i;
unsigned char byte;
int bit;
struct input_id id;
unsigned int evbit[2];
std::string ev_name[] = {
"EV_SYN",
"EV_KEY",
"EV_REL",
"EV_ABS",
"EV_MSC",
"EV_SW",
"NULL",
"NULL",
"NULL",
"NULL",
"NULL",
"NULL",
"NULL",
"NULL",
"NULL",
"NULL",
"NULL",
"EV_LED",
"EV_LND",
"NULL",
"EV_REP",
"EV_FF",
"EV_PWR",
};
if (mInputFd == -1)
{
LOG("open kd device or mouse fail! \n");
}
else
{
ev.data.fd = mInputFd;
ev.events = EPOLLIN | EPOLLET;
int ret = epoll_ctl(mEpId, EPOLL_CTL_ADD, mInputFd, &ev);
if (ret == -1)
{
LOG("add kd device in epoll fail! \n");
}
}
err = ioctl(mInputFd, EVIOCGID, &id);
if (err == 0)
{
LOG("bustype = 0x%x\n", id.bustype);
LOG("vendor = 0x%x\n", id.vendor);
LOG("product = 0x%x\n", id.product);
LOG("version = 0x%x\n", id.version);
}
len = ioctl(mInputFd, EVIOCGBIT(0, sizeof(evbit)), &evbit);
if (len > 0 && len <= sizeof(evbit))
{
LOG("support ev type : ");
for (i = 0; i < len; i++)
{
byte = ((unsigned char *)evbit)[i];
for (bit = 0; bit < 8; bit++)
{
if (byte & (1 << bit))
LOG("%s ", ev_name[i * 8 + bit].c_str());
}
}
LOG("\n");
}
b_run = true;
pollthread = std::thread(&Keyboard::poll, this);
}
void Keyboard::poll()
{
int re = 0;
while (b_run)
{
re = epoll_wait(mEpId, events, MAX_EVENTS, 1000);
for (int i = 0; i < re; ++i)
{
if (events[i].events & EPOLLIN)
{
LOG("poll\n");
doInput(events + i);
}
}
re = 0;
}
}
long time_pressed_sec;
long time_pressed_usec;
void Keyboard::doInput(const epoll_event *ev)
{
ssize_t resize = 0;
ssize_t n = 0;
struct input_event input_ev;
while ((resize = read(ev->data.fd, buffer + n, sizeof(struct input_event) - n)) > 0)
{
n += resize;
if (n == sizeof(input_event))
{
memcpy((void *)(&input_ev), buffer, sizeof(input_event));
if (input_ev.type != 1) // 非按键不响应
{
n = 0;
continue;
}
LOG("keyboard type=%d; code=%d; value=%d, sec=%ld, usec=%ld\n", (int)input_ev.type,
(int)input_ev.code, (int)input_ev.value, input_ev.time.tv_sec, input_ev.time.tv_usec);
if (input_ev.value == 0 && input_ev.type == 1) //记录按下的时间状态
{
time_pressed_sec = (long)input_ev.time.tv_sec;
time_pressed_usec = (long)input_ev.time.tv_usec;
n = 0;
continue;
}
LOG("keyboard press tiem sec=%ld, usec=%ld\n", time_pressed_sec, time_pressed_usec);
double delay = (input_ev.time.tv_usec - time_pressed_usec) / 1000;
delay = delay + (input_ev.time.tv_sec - time_pressed_sec) * 1000;
LOG("press delay:%lf\n", delay);
switch (input_ev.code)
{
case KEYCODE_POWER:
if (delay >= 3000 && delay <= 200000)
{
printf("设备关机中。。。\r\n");
system("poweroff");
}
else if (get_devs_status(SLEEP_STATUS_PATH) == SCANNER_ERR_SLEEP)
{
m_event(3);
printf("按键唤醒设备中。。。\r\n");
}
break;
case KEYCODE_START:
printf("KEYCODE_START\r\n");
m_event(0);
break;
case KEYCODE_STOP:
printf("按键停止扫描!!!\n");
m_event(1);
break;
default:
break;
}
n = 0;
}
}
}
} // namespace huagao