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

📄 uihw_zg.c

📁 嵌入工linux开发的源码
💻 C
字号:
#include <pr2k.h>
#include <uiwnd.h>
#include <wtpen.h>
#include <uiSw_Kbd.h>
#include <uiHW_ZG.h>
#include <stdio.h>
#include <sysvar.h>

#ifdef _DEBUG
#ifndef __WIN32__
//#define _DEBUG_HANDWRITE_
#endif
#endif


#ifndef __WIN32__
//#define HW_DATA 0XBFF00000
#endif
/**************************************************************************/
//global variable
/**************************************************************************/
//zg hw need

char gHWR_RamAddress[HWR_RAM_SIZE];


//save point message
unsigned char gHWR_Stroke_XY[HWR_REGION_NUMBER][MAX_POINT_IN_ONE_CHAR][2];


//candidate buffer
unsigned short gpHWR_Candidate[HWR_CANDIDATE_BUFFER_SIZE];

//keyboard handle
static HNDL gh_HWR_KB=NULL;
//the number of saved point;
static int gHWR_Count=0;

//the region of responding pen's message
static int gHWR_Response_Region=-1;

//the region of recognition.express by bit,for example, bit 0 express region 0;
static DWORD gHWR_Recognition_Region=0;

//the handwrite is or not active;
//static int gHWR_Active=0;
static int gHWR_tid=-1;

//is moving;
static BOOL gHWR_Move=0;

//all region coordinate
static THWR_Region gHWR_Region[HWR_REGION_NUMBER];

//handwrite message queue
static TGuiMessageQueue *gpHWR_MsgQueue;

//handwrite timer
static HNDL gHWR_Timer;

BOOL guiHWR_Handler(WORD msg, WORD x, WORD y)
{
	int i;
	unsigned char Related_X,Related_Y;
	THWR_Point *pPoint,tempPoint;

	TGuiMessage tMsg;
	//if no active,return ;
	if(gHWR_tid==-1)
	{
		return FALSE;
	}

	if(guiQueryWindowOfControl(gh_HWR_KB)!=(HNDL)gpTopWindow)
	{
		return FALSE;
	}
	//finish one stroke;
	if(msg==WIN_PEN_UP&&gHWR_Move)
	{
		if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-1)
		{
			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=0xff;
			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=0x0;
			 gHWR_Count++;
		}
		gHWR_Move=FALSE;

		_guiHWR_Timer_Start();
		return TRUE;
	}
	else if(msg==WIN_PEN_DOWN)
	{
		for(i=0;i<HWR_REGION_NUMBER;i++)
		{
			//judge if in recognition region;
			if(x>=gHWR_Region[i].left&&x<=gHWR_Region[i].right
				&&y>=gHWR_Region[i].top&&y<=gHWR_Region[i].bottom)
			{
				break;
			}
		}

		//outside of region
		if(i==HWR_REGION_NUMBER)
		{
			if(gHWR_Response_Region!=-1)
			{
				_guiHWR_Timer_Stop();
				tMsg.messageType=WIN_HWR_RECOGNIZE;
				tMsg.x=gHWR_Response_Region;
				tMsg.y=gHWR_Count;
				guiEnqueue(gpHWR_MsgQueue,&tMsg);
				gHWR_Recognition_Region|=1<<gHWR_Response_Region;
				gHWR_Response_Region=-1;
				gHWR_Count=0;
			}
			return FALSE;
		}
		//in response region
		else if(gHWR_Response_Region==i)
		{
			if(guiTimer_IsRun(gHWR_Timer)==0)
			{
				//sc_logString("Timer not run",TO_MONITOR);
				gHWR_Move=FALSE;
				return TRUE;
			}

			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;
			if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-2)
			{
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
				gHWR_Count++;

				guiDrawPixel(0,x,y,GUI_BLACK);
			}

			_guiHWR_Timer_Stop();
			gHWR_Move=TRUE;
			return TRUE;
		}
		//new response region
		else if(gHWR_Response_Region==-1)
		{

			if(gHWR_Recognition_Region&(1<<i))
			{
				return TRUE;
			}
			gHWR_Response_Region=i;
			gHWR_Count=0;
			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;

			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
			gHWR_Count++;
			guiDrawPixel(0,x,y,GUI_BLACK);

			gHWR_Move=TRUE;
			return TRUE;
		}
		//other region
		else
		{
			_guiHWR_Timer_Stop();
			//send recognition message;
			tMsg.messageType=WIN_HWR_RECOGNIZE;
			tMsg.x=gHWR_Response_Region;
			tMsg.y=gHWR_Count;
			guiEnqueue(gpHWR_MsgQueue,&tMsg);
			gHWR_Recognition_Region|=1<<gHWR_Response_Region;


			//modify gHWR_Count;
			gHWR_Count=0;

			if(gHWR_Recognition_Region&(1<<i))
			{
				gHWR_Response_Region=-1;
				return TRUE;
			}
			//modify gHWR_Response_Region
			gHWR_Response_Region=i;

			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;

			if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-2)
			{
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
				gHWR_Count++;
				guiDrawPixel(0,x,y,GUI_BLACK);
			}
			gHWR_Move=TRUE;
			return TRUE;
		}

	}
	else if(msg==WIN_PEN_MOVE)
	{
		if(gHWR_Move==0||gHWR_Response_Region==-1)
		{
			return FALSE;
		}

		//judge if in recognition region;
		if(x>=gHWR_Region[gHWR_Response_Region].left&&x<=gHWR_Region[gHWR_Response_Region].right&&
		   y>=gHWR_Region[gHWR_Response_Region].top&&y<=gHWR_Region[gHWR_Response_Region].bottom)
		{
			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;

			if(gHWR_Count==0)
			{
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
				gHWR_Count++;

				guiDrawPixel(0,x,y,GUI_BLACK);
			}
			else if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-2)
			{
				//pPoint=&gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count-1];
				tempPoint.x=gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count-1][0];
				tempPoint.y=gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count-1][1];
				pPoint=&tempPoint;


				if(abs(Related_X-pPoint->x)>=HWR_POINT_INTERVAL||abs(Related_Y-pPoint->y)>=HWR_POINT_INTERVAL)
				{
					guiDrawLine(0,pPoint->x+gHWR_Region[gHWR_Response_Region].left,
							  pPoint->y+gHWR_Region[gHWR_Response_Region].top,
							  x,y,
					          GUI_BLACK,0);
					gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
					gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
					gHWR_Count++;
				}
			}
			return TRUE;
		}
		else
		{

			return FALSE;
		}
	}
	return FALSE;
}


userThread guiHWR_Thread(void)
{
	int recognition_type;
	short left,top,right,bottom;//region coordinate

	int iRegion;
	short iCount;
	TGuiMessage tMsg;
	struct tagGuiSw_Kbd *pKB;
                 
	short hError;

	//allocate message queue and timer;
	gpHWR_MsgQueue=guiAllocateMessageQueue();
	if(gpHWR_MsgQueue==NULL)
	{
		sc_exit();
		return;
	}
	gHWR_Timer=gui_allocateTimer();
	if(gHWR_Timer==NULL)
	{
		sc_free(gpHWR_MsgQueue);
		sc_exit();
		return;
	}
	//associate timer and MsgQueue;
	((TGuiTimer *)gHWR_Timer)->pMsgQueue=gpHWR_MsgQueue;

	pKB=(struct tagGuiSw_Kbd *)gh_HWR_KB;

//	recognition_type=0x1e3;
//	recognition_type=0x3;
        
	recognition_type = 0x03;
	//sys_GetSysVar_Integer(SYSVAR_INTEGER_HW_RECOGNITION_TYPE,&recognition_type);
	hError = WTSetRange((short)recognition_type);

	while(1)
	{
		if(guiDequeue(gpHWR_MsgQueue,&tMsg)==-1)
		{
			continue;
		}
		switch(tMsg.messageType)
		{
		case TIMER_TIMEOUT:
			guiEnterWCS();

			if(guiPequeue(gpHWR_MsgQueue,&tMsg)==STATUS_OK)
			{
				if(tMsg.messageType==WIN_HWR_RECOGNIZE)
				{
					guiExitWCS();
					break;
				}
			}

			tMsg.x=gHWR_Response_Region;
			tMsg.y=gHWR_Count;

			gHWR_Recognition_Region|=1<<gHWR_Response_Region;
			gHWR_Response_Region=-1;
			gHWR_Count=0;
			guiExitWCS();

		case WIN_HWR_RECOGNIZE:
			iRegion=tMsg.x;
			iCount=tMsg.y;

			left=gHWR_Region[iRegion].left;
			top=gHWR_Region[iRegion].top;
			right=gHWR_Region[iRegion].right;
			bottom=gHWR_Region[iRegion].bottom;

			gHWR_Stroke_XY[iRegion][iCount][0]=0xff;
			gHWR_Stroke_XY[iRegion][iCount][1]=0xff;
			
			if(iRegion!=2)
			{
				//recognition_type = 0x03;
				recognition_type = 0x07;
				hError = WTSetRange((short)recognition_type);			
			}
			else
			{
				recognition_type = 0x20;           // bit5 阿拉伯数字
				hError = WTSetRange((short)recognition_type);						
			}
			
			if(0!=WTRecognize((unsigned char *)gHWR_Stroke_XY[iRegion],0,&gpHWR_Candidate[1]))
			{
				sc_logString("WTRecognize failure",TO_MONITOR);
				while(1);
			}

			guiClearBlock(0,left,top,right,bottom,GUI_WHITE,0);

			//tong. call keyboard's action function
			if((short)gpHWR_Candidate[1]==-1)
			{
				gpHWR_Candidate[0]=0;
			}
			else
			{
				gpHWR_Candidate[0]=strlen((char *)&gpHWR_Candidate[1])/2;
			}

			gpKbd_Info[pKB->base.style].showFun(gh_HWR_KB,iRegion,gpHWR_Candidate);

			//can reuse this region
			gHWR_Recognition_Region&=~(1<<iRegion);

			break;
		default:
			break;
		}
	}
}

/****************************************************************/
//external function
/****************************************************************/
STATUS guiHWR_Init(void)
{
	char * LibStartAddress=0;//tong warning
	if(WTRecognizeInit(gHWR_RamAddress,HWR_RAM_SIZE,(char *)HW_DATA)!=0)
	{
		printStringLn("WTRecognizeInit failure");
		return STATUS_ERR;
	}

	if(WTSetSpeed(3)!=0)
	{
		printStringLn("WTSetSpeed failure");
		return STATUS_ERR;
	}

	gHWR_Move=0;

	return STATUS_OK;
}
                                            
STATUS guiHWR_Begin(HNDL hKB, THWR_Region *pHWR_Region)
{
	int i;

	struct tagGuiSw_Kbd *pKB;
#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_Begin 0",TO_MONITOR);
#endif

	guiEnterWCS();

	if(gHWR_tid!=-1)
	{
		guiExitWCS();
		return STATUS_ERR;
	}

	for(i=0;i<HWR_REGION_NUMBER;i++)
	{
		gHWR_Stroke_XY[i][MAX_POINT_IN_ONE_CHAR-1][0]=-1;
		gHWR_Stroke_XY[i][MAX_POINT_IN_ONE_CHAR-1][1]=-1;
	}

	gHWR_Count=0;
	gHWR_Response_Region=-1;
	gHWR_Recognition_Region=0;

	gHWR_Move=0;

	//copy region
	memcpy(gHWR_Region,pHWR_Region,sizeof(gHWR_Region));
	gh_HWR_KB=hKB;
	//gHWR_tid=sc_createThread(guiHWR_Thread,100,4*1024,NULL,0);
	gHWR_tid=sc_createThread(guiHWR_Thread,100,32*1024,NULL,0);

	if(gHWR_tid==-1)
	{
		guiExitWCS();
		return STATUS_ERR;
	}

	guiExitWCS();
#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_Begin 1",TO_MONITOR);
#endif
	return STATUS_OK;
}

STATUS guiHWR_End(void)
{
	int result;
	guiEnterWCS();

#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_End 0",TO_MONITOR);
#endif

	if(gHWR_tid!=-1)
	{
		sc_deleteThread(gHWR_tid);
	}

	gHWR_tid=-1;
	gh_HWR_KB=NULL;

	if(gHWR_Timer)
	{
		_guiTimer_Delete((TGuiTimer *)gHWR_Timer);
		gHWR_Timer=NULL;
	}

	if(gpHWR_MsgQueue)
	{
		sc_free(gpHWR_MsgQueue);
		gpHWR_MsgQueue=NULL;
	}

	guiExitWCS();

#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_End",TO_MONITOR);
#endif
	return STATUS_OK;
}

/****************************************************************/
//internal function
/****************************************************************/
STATUS _guiHWR_Timer_Start(void)
{
	int count;
	if(gHWR_Timer==0)
	{
		return STATUS_ERR;
	}
	sys_GetSysVar_Integer(SYSVAR_INTEGER_HW_WAITTIME,&count);
	gui_restartTimer(gHWR_Timer,count,0);
	return STATUS_OK;
}

STATUS _guiHWR_Timer_Stop(void)
{
	if(gHWR_Timer==0)
	{
		return STATUS_ERR;
	}
	gui_stopTimer(gHWR_Timer);
	return STATUS_OK;
}



#ifdef __WIN32__
//#if 1
/**************************************************************/
//temp function(should be contained in penpower.cpp)
/**************************************************************/
const unsigned char HW_DATA[1]={1};
short WTRecognizeInit(char *RamAddress,long RamSize,char *LibStartAddress)
{
	return STATUS_OK;
}

short WTSetRange(short Range)
{
	return 0;
}

short WTRecognize(unsigned char * PointBuf,short PointsNumber,unsigned short *return_buffer)
{
	int i,j;
	WORD code;

	j=abs(rand());
	j%=10;
	j=j>6?6:j;
//	return_buffer[0]=j;

	for(i=0;i<j;i++)
	{
		code=abs(rand())%94;
		code<<=8;
		code+=0xa100;
		return_buffer[i]=code;

		code=abs(rand())%71;
		code+=0x00b0;
		return_buffer[i]+=code;
	}
	return_buffer[i]=0;
	return 0;
}

short WTRecognizeEnd()
{
	return STATUS_OK;
}

short WTSetSpeed(short Speed)
{
	return STATUS_OK;
}


#endif 

⌨️ 快捷键说明

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