⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tpdrv.c

📁 linux console 界面下的触摸屏的驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
				g_MouseStat.LastX = TempX;
				g_MouseStat.LastY = TempY;
			}
		}
	}
	
	return RetValue;
}

/*===================================================================
*	k_ConvertPS2XYData
*
*		TP偐傜偺嵗昗僨乕僞傪偐傜PS2僷働僢僩傪嶌傞丅
*		儃僞儞墴壓忬懺愝掕偼峴傢側偄
*		
*	堷悢
*		X	嵟怴偺X嵗昗僨乕僞	
*		Y	嵟怴偺Y嵗昗僨乕僞
*	栠傝抣
*		PS2僷働僢僩姰惉悢	
*--------------------------------------------------------------------*/
static int k_ConvertPS2XYData(int X, int Y)
{
	int RetValue;
	int DiffTempX, DiffTempY;
	int XSign = 0x04;	/* 儃僞儞偺4價僢僩栚偼昁偢棫偭偰偄傞偨傔棫偰偰偍偔 */
	int YSign = 0x04;
	
	if(g_MouseStat.LastX <= X)
	{
		DiffTempX = X - g_MouseStat.LastX;
	}
	else
	{
		XSign |= 0x10;	/* X幉偺晧偺曽岦傊偺曄壔 */
		DiffTempX = g_MouseStat.LastX - X;
	}

	if(g_MouseStat.LastY <= Y)
	{
		YSign |= 0x20;	/* Y幉偺晧偺曽岦傊偺曄壔 */
		DiffTempY = Y - g_MouseStat.LastY;
	}
	else
	{
		DiffTempY = g_MouseStat.LastY - Y;
	}

	RetValue = k_AccelCalc(DiffTempX, DiffTempY, XSign, YSign);

	return RetValue;
}
/*===================================================================*
*	k_AccelCalc
*
*	堷悢
*      X Y 偵偼堏摦検傪愨懳抣偱擖傟傞偙偲丄
*      XSign YSign偵偼晞崋傪擖傟傞
*	栠傝抣
*		PS2僷働僢僩姰惉悢	
*	愢柧
*		壛懍搙寁嶼
*--------------------------------------------------------------------*/
static int k_AccelCalc(int X, int Y, char XSign, char YSign)
{
	int RetValue;
	int XNum = 0;
	int YNum = 0;
	int ii;	
	while(X)
	{
		if(g_MAXMOVE < X)
		{
			g_ps2Packet[XNum].X = 255;
			X -= 255;			
		}
		else if(g_TPProf.Threashold < X)		{
			g_ps2Packet[XNum].X = (X - g_TPProf.Threashold + g_TPProf.Threashold*g_TPProf.Scalling)/g_TPProf.Scalling;
			if(0 < (X - g_TPProf.Threashold + g_TPProf.Threashold*g_TPProf.Scalling)%g_TPProf.Scalling)
			{
				XNum++;
				g_ps2Packet[XNum].X = (X - g_TPProf.Threashold + g_TPProf.Threashold*g_TPProf.Scalling)%g_TPProf.Scalling;			
			}
			X = 0;		}		else		{			g_ps2Packet[XNum].X = X;			X = 0;		}			
		XNum++;
	}	
	for(ii = 0; ii < XNum; ii++)
	{
		if(XSign & 0x10)
		{
			g_ps2Packet[ii].X *= -1;
			g_ps2Packet[ii].Botton |= XSign;
		}			
	}
	
	while(Y)
	{
		if(g_MAXMOVE < Y)
		{
			g_ps2Packet[YNum].Y = 255;
			Y -= 255;			
		}
		else if(g_TPProf.Threashold < Y)
		{
			g_ps2Packet[YNum].Y = (Y - g_TPProf.Threashold + g_TPProf.Threashold*g_TPProf.Scalling)/g_TPProf.Scalling;
			if(0 < (Y - g_TPProf.Threashold + g_TPProf.Threashold*g_TPProf.Scalling)%g_TPProf.Scalling)
			{
				YNum++;
				g_ps2Packet[YNum].Y = (Y - g_TPProf.Threashold + g_TPProf.Threashold*g_TPProf.Scalling)%g_TPProf.Scalling;			
			}
			Y = 0;
		}		else		{			g_ps2Packet[YNum].Y = Y;			Y = 0;		}			
		YNum++;
	}	
	for(ii = 0; ii < YNum; ii++)
	{
		if(YSign & 0x20)
		{
			g_ps2Packet[ii].Y *= -1;
			g_ps2Packet[ii].Botton |= YSign;
		}			
	}
	if(YNum < XNum)
		RetValue = XNum;
	else
		RetValue = YNum;

	return RetValue;}
//================================================
//  Rs_Open
//================================================
static int k_RS_Open(void)
{
	int				FncRet;
	struct termios	NewTio;
	char   PortDev[16];
	
	sprintf(PortDev, "/dev/%s", g_TPProf.Port);
	g_SERISH = open(PortDev, O_RDWR | O_NOCTTY); 
	if (g_SERISH < 0) 
		goto l_ErrorOpen;		

	bzero(&NewTio, sizeof(NewTio));

	NewTio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
	NewTio.c_iflag = IGNBRK | IGNPAR;
 
	NewTio.c_oflag = 0;

	// read function wait for 1byte recv or 1s time past
	NewTio.c_cc[VTIME]    = 50;
	NewTio.c_cc[VMIN]     = 0;
	
	FncRet = tcflush(g_SERISH, TCIFLUSH);
	if(FncRet < 0)
		goto l_ErrorFlush;
	FncRet = tcsetattr(g_SERISH, TCSANOW, &NewTio);
	if(FncRet < 0)
		goto l_ErrorSetAttr;
	FncRet = 0;
	
l_Exit:
	return FncRet;
l_ErrorOpen:
	k_DPrint("SystemError[ID=0007](open errno=%d)\n", errno);
	FncRet = -1;
	goto l_Exit;
l_ErrorFlush:
	k_DPrint("SystemError[ID=0008](Flush errno=%d)\n", errno);
	FncRet = -1;
	goto l_Exit;
l_ErrorSetAttr:
	k_DPrint("SystemError[ID=0009](SetAttr errno=%d)\n", errno);
	FncRet = -1;
	goto l_Exit;
}

//================================================
//  k_RS_Write
//================================================
static int k_RS_Write(char*	Buf, int Size)
{
	int	WriteSize = 0;
	int	FncRet;

	while(WriteSize != Size)
	{
		FncRet = write(g_SERISH, Buf + WriteSize, Size - WriteSize);
		if(FncRet < 0)
			goto l_ErrorWrite;
		WriteSize += FncRet;
	}	
	FncRet = WriteSize;
l_Exit:
	return FncRet;	
l_ErrorWrite:
	k_DPrint("SystemError[ID=0010](write errno=%d)\n", errno);
	FncRet = -1;
	goto l_Exit;
}

//================================================
//  k_RS_Read
//================================================
static int k_RS_Read(char* Buf, int Size)
{
	int	FncRet;

	int RetValue = -1;

	FncRet = read(g_SERISH, Buf, Size);
	if(FncRet < 0)
		goto l_ErrorRead;
	RetValue = FncRet;
l_Exit:
	return RetValue;
l_ErrorRead:
	k_DPrint("SystemError[ID=0011](read errno=%d)\n", errno);
	RetValue = -1;
	goto l_Exit;
}

//================================================
//  k_RS_Close
//================================================
static void k_RS_Close(void)
{
	if(g_SERISH != -1)
	{
		close(g_SERISH);
		g_SERISH = -1;
	}
}
//================================================
//  k_FIFO_WRIET
//================================================
static int k_Write_PS2Packet(int Num)
{
	int RetValue;
	int FuncRet;
	int Writesize;
	int ii;
	fd_set WriteFs;

	for(ii = 0; ii < Num; ii++)
	{
		Writesize = 0;
		while(Writesize != 3)
		{
			FD_ZERO(&WriteFs);
			FD_SET(g_FIFOSH, &WriteFs);

			FuncRet = select(g_FIFOSH + 1, NULL, &WriteFs, NULL, NULL);
			if(FD_ISSET(g_FIFOSH, &WriteFs))
			{
				FuncRet = write(g_FIFOSH, (char*)&g_ps2Packet[ii] + Writesize, 3 - Writesize);
				if(FuncRet < 0)
					goto l_WErr;
				Writesize += FuncRet;
			}
		}
	}

	RetValue = 0;
	
l_Exit:
	return RetValue;
l_WErr:
	k_DPrint("SystemError[ID=0012](write errno=%d)\n", errno);
	RetValue = -1;
	goto l_Exit;
}


//================================================//  k_DPrint//================================================static void k_DPrint(const char *fmt, ...)
{
	va_list	argList;
	
	va_start(argList, fmt);
	vsprintf(g_OutData, fmt, argList);
	syslog(LOG_CRIT, g_OutData);
}

//================================================//  k_Dump//================================================
static void k_Dump(const char *Tag, void *msg, int len)
{
	int ii;
	unsigned char strbuf[64];	unsigned char *temp;		if(len <= 0)		goto l_NoData;		temp = (char*)strbuf;
	
	for(ii = 0; ii < len; ii++)
	{
		sprintf(temp, "%02X", (unsigned char*)msg);
		temp += sizeof(char) * 2;
		msg++;		if((ii+1)%16 == 0)		{			syslog(LOG_CRIT, "%s %s\n", Tag, strbuf);			temp = strbuf;		}
	}	if((ii%16) != 0)
		syslog(LOG_CRIT, "%s %s\n", Tag, strbuf);	l_Exit:	return;l_NoData:	goto l_Exit;
} 
//================================================
//  k_GetProfileInt
//================================================
int k_GetProfileInt(const char *Section, const char *key, int Default)
{
	int RetValue;
	int  Fd = -1;
	char *Buffer = NULL;
	char *BufTemp, *temp, *temps, *tempf;
	int FileLen;
	int FuncRet;

	Fd = open(D_TP_PROFILE_NAME, O_RDONLY);
	if(Fd < 0)
		goto l_EOpen;

	FileLen = lseek(Fd, 0, SEEK_END);
	lseek(Fd, 0, SEEK_SET);

	Buffer = malloc(FileLen + 1);
	if(Buffer == NULL)
		goto l_Emalloc;

	FuncRet = read(Fd, Buffer, FileLen);
	if(FuncRet < 0)
		goto l_ERead;

	Buffer[FuncRet] = 0;

	temps = Buffer;

	while(1)
	{
		temps = strstr(temps, "[");
		if(temps == NULL)
			goto l_Nothing;
		tempf = strstr(temps, "]");
		if(!memcmp(temps+1, Section, tempf-temps-1))
			break;
		else
			temps = tempf;
	}
	temps = strstr(tempf + 1, "[");
	if(temps == NULL)
		temp = Buffer + FileLen + 1;
	else
		temp = temps;
	BufTemp = strtok(tempf + 1, " \r\n=");
	if(BufTemp == NULL)
		goto l_Nothing;
	while(1)
	{ 
		FuncRet = memcmp(BufTemp, key, strlen(key));
		if(FuncRet == 0)
		{
			temps = strtok(NULL, "\r\n");
			if(temps != NULL)
			{
				RetValue = atoi(temps);
				break;
			}
			else
				goto l_Nothing;
		}
		else
		{
			BufTemp = strtok(NULL, " \r\n=");
			if((BufTemp == NULL) || (temp < BufTemp)) 
				goto l_Nothing;
		}
	}

l_Exit:
	if(Buffer != NULL) free(Buffer);
	if(0 < Fd) close(Fd);
	return RetValue;
l_EOpen:
l_ERead:
l_Emalloc:
l_Nothing:
	RetValue = Default;
	goto l_Exit;
}
//================================================
//  k_GetProfileString
//================================================
int k_GetProfileString(const char *Section, const char *key, char *string, int stringsize,const char* Default)
{
	int RetValue;
	int  Fd = -1;
	char *Buffer = NULL;
	char *BufTemp, *temp, *temps, *tempf;
	int FileLen;
	int FuncRet;

	Fd = open(D_TP_PROFILE_NAME, O_RDONLY);
	if(Fd < 0)
		goto l_EOpen;

	FileLen = lseek(Fd, 0, SEEK_END);
	lseek(Fd, 0, SEEK_SET);

	Buffer = malloc(FileLen + 1);
	if(Buffer == NULL)
		goto l_Emalloc;

	FuncRet = read(Fd, Buffer, FileLen);
	if(FuncRet < 0)
		goto l_ERead;

	Buffer[FuncRet] = 0;

	temps = Buffer;

	while(1)
	{
		temps = strstr(temps, "[");
		if(temps == NULL)
			goto l_Nothing;
		tempf = strstr(temps, "]");
		if(!memcmp(temps+1, Section, tempf-temps-1))
			break;
		else
			temps = tempf;
	}
	temps = strstr(tempf + 1, "[");
	if(temps == NULL)
		temp = Buffer + FileLen + 1;
	else
		temp = temps;
	BufTemp = strtok(tempf + 1, " \r\n=");
	if(BufTemp == NULL)
		goto l_Nothing;
	while(1)
	{ 
		FuncRet = memcmp(BufTemp, key, strlen(key));
		if(FuncRet == 0)
		{
			temps = strtok(NULL, "\r\n");
			if(temps != NULL)
			{
				if(strlen(temps) + 1 < stringsize)
				{
					memcpy(string, temps, strlen(temps) + 1);
					RetValue = strlen(temps);
				}
				else
				{
					temps[stringsize -1] = 0x00;
					memcpy(string, temps, strlen(temps) + 1);
					RetValue = stringsize-1;
				}
				break;
			}
			else
				goto l_Nothing;
		}
		else
		{
			BufTemp = strtok(NULL, " \r\n=");
			if((BufTemp == NULL) || (temp < BufTemp)) 
				goto l_Nothing;
		}
	}

l_Exit:
	if(Buffer != NULL)	free(Buffer);
	if(0 < Fd) close(Fd);
	return RetValue;
l_EOpen:
l_ERead:
l_Emalloc:
l_Nothing:
	memcpy(string, Default, strlen(Default));
	RetValue = strlen(Default);
	goto l_Exit;
}
//================================================
//  k_GetProf
//================================================
static void k_ValueInit(void)
{
	g_TPProf.mode = k_GetProfileInt("TPDrv", "Mode",1 );
	if(g_TPProf.mode != 1 && g_TPProf.mode != 2)		g_TPProf.mode = 1;	g_TPProf.SamplingRate = k_GetProfileInt("TPDrv", "SamplingRate", 5);
	if(g_TPProf.SamplingRate < 2 || 31 < g_TPProf.SamplingRate) g_TPProf.SamplingRate = 5;
	g_TPProf.X_Resolution = k_GetProfileInt("TPDrv", "X_Resolution", 640);
	g_TPProf.Y_Resolution = k_GetProfileInt("TPDrv", "Y_Resolution", 480);
	g_TPProf.Scalling		= k_GetProfileInt("TPDrv", "Scalling", 3);
	g_TPProf.Threashold   = k_GetProfileInt("TPDrv", "Threashold", 5);
	k_GetProfileString("TPDrv", "Port", g_TPProf.Port, sizeof(g_TPProf.Port), "ttyS4");
	k_GetProfileString("TPDrv", "DevName", g_TPProf.DevName, sizeof(g_TPProf.DevName), "/dev/mouse");

	g_MAXMOVE = g_TPProf.Threashold + (255 - g_TPProf.Threashold)*g_TPProf.Scalling;

}//================================================
//  k_Sig_Chld
//================================================
static void k_Sig_Chld(int SigID)
{
#if D_TPDRV_DEBUG
	k_DPrint("LogTrace(Sig Chld Handler)\n");
#endif
}
//================================================
//  k_mkfifo
//================================================
static int k_mkfifo(char *FIFOName){	int	iRet, fd;	char recvbuf[PIPE_BUF];		iRet = access(FIFOName, F_OK);		if(iRet == -1)	/* FIFO偑懚嵼偟側偄 */	{		iRet = mkfifo(FIFOName, 0666);		if(iRet != 0)			goto l_MKFIFOErr;	}	else												{		/* 嬻撉傒偡傞堊ReadNonBlock偱OPEN */		fd = open(FIFOName, (O_RDONLY | O_NONBLOCK));		if(fd != -1)		{			read(fd, recvbuf, PIPE_BUF);			close(fd);									}		else			goto l_EOpen;														}	iRet = 0;l_Exit:	return iRet;								l_MKFIFOErr:	iRet = -1;	goto l_Exit;l_EOpen:	iRet = -1;	goto l_Exit;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -