#include <input_manager.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
static int StdinDevInit(void)//改变串口为不阻塞
{
struct termios tTTYState;
//get the terminal state
tcgetattr(STDIN_FILENO, &tTTYState);
//turn off canonical mode
tTTYState.c_lflag &= ~ICANON;
//minimum of number input read.
tTTYState.c_cc[VMIN] = 1; /* 有一个数据时就立刻返回 */
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &tTTYState);
return 0;
}
static int StdinDevExit(void)//把原来状态改回来
{
struct termios tTTYState;
//get the terminal state
tcgetattr(STDIN_FILENO, &tTTYState);
//turn on canonical mode
tTTYState.c_lflag |= ICANON;
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &tTTYState);
return 0;
}
static int StdinGetInputEvent(PT_InputEvent ptInputEvent)
{
struct timeval tTV;
fd_set tFDs;
char c;
tTV.tv_usec=0;
tTV.tv_sec=0;
FD_ZERO(&tFDs);//清空集合
FD_SET(STDIN_FILENO, &tFDs); //STDIN_FILENO is 0标准输入
select(STDIN_FILENO+1, &tFDs, NULL, NULL, &tTV);//开始监听
if (FD_ISSET(STDIN_FILENO, &tFDs))//文件有数据
{
ptInputEvent->iType = INPUT_TYPE_STDIN;
gettimeofday(&ptInputEvent->tTime, NULL);
c = fgetc(stdin);
if (c == 'u')
{
ptInputEvent->iVal = INPUT_VALUE_UP;
}
else if (c == 'n')
{
ptInputEvent->iVal = INPUT_VALUE_DOWN;
}
else if (c == 'q')
{
ptInputEvent->iVal = INPUT_VALUE_EXIT;
}
else
{
ptInputEvent->iVal = INPUT_VALUE_UNKNOWN;
}
return 0;
}
else
{
return -1;
}
}
static T_InputOpr g_tStdinOpr = {
.name = "stdin",
.DeviceInit = StdinDevInit,
.DeviceExit = StdinDevExit,
.GetInputEvent = StdinGetInputEvent,
};
int StdinInit(void)
{
return RegisterInputOpr(&g_tStdinOpr);
}
串口select不阻赛输入学习笔记
最新推荐文章于 2023-07-04 17:18:14 发布