项目中用到Qt进行开发,其中键盘部分电路自己设计,所以qt自带的键盘驱动无法直接使用,需要进行一些简单的移植才可以。
键盘电路是用单片机扫描方式,通过串口与系统的连接。为了修改方便,直接拿qkbdtty_qws.cpp进行修改,同时修改的还有qkbdtty_qws.h和qkbddriverfactory_qws.cpp,这几个文件都在src/gui/embedded下面。
为了串口传送的数据具有一定的完整性,我在串口发送键值时,采用的是一次发送2字节,即键值和键值的反码,接收后方便校验。
编译方法:代码修改完成后,只编译gui的库即可,即到gui目录下make,所作的改动都在qt的libQtGui.so里面,编译完成后拷贝该库到目标板即可。当然,Qt的系统配置里面键盘要配置为tty,一般在profile里面配置。
下面是源码:
qkbddriverfactory_qws.cpp文件修改部分如下:
# ifndef QT_NO_QWS_KBD_TTY
if (driver == QLatin1String("tty") || driver.isEmpty())
return new QWSMyKeyboardHandler(device);
# endif
qkbdtty_qws.cpp源码:
#include "qkbdtty_qws.h"
#if !defined(QT_NO_QWS_KEYBOARD) && !defined(QT_NO_QWS_KBD_TTY)
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <private/qcore_unix_p.h>
#include <qsocketnotifier.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int UART_Open(int fd,char* port)
{
fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY);
//fd = open( port, O_RDWR);
if (FALSE == fd){
perror("Can't Open Serial Port");
return(FALSE);
}
if(fcntl(fd, F_SETFL, 0) < 0){
printf("fcntl failed!\n");
return(FALSE);
} else {
// printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
}
if(0 == isatty(STDIN_FILENO)){
printf("standard input is not a terminal device\n");
return(FALSE);
}
return fd;
}
void UART_Close(int fd)
{
close(fd);
}
int UART_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
int i;
int status;
int speed_arr[] = { B115200,B57600,B38400, B19200, B9600, B4800, B2400, B1200, B300,
B115200,B57600,B38400, B19200, B9600, B4800, B2400, B1200, B300
};