systask.c

来自「一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上」· C语言 代码 · 共 1,313 行 · 第 1/3 页

C
1,313
字号

	/* pen calibrated!!! */
	PenCalibration();
		

	//kx = 0.04776526;
	//ky = 0.07117437;
	//bx = -17.27396;	
	//by = -21.45908;

	
	/* init some global values for Asixwin */
	ASIXInit(); /* it should run after timer_task, so we put it into shell_task */
	
	/* start running rtc */
//	run_rtc();
}

void PenCalibration( void )
{
	CHAR 	PenDown=0, new_check = 1, quit=0, lefttop_pressed=0, rightbottom_pressed=0, center_pressed = 0;
	SHORT 	x1=0,y1=0,x2=0,y2=0;
	SHORT 	x3=0,y3=0;
	SHORT	last_x1,last_y1,last_x2,last_y2,last_x3,last_y3;
//	SHORT	avx1, avy1, avx2, avy2;
	static 	UINT flgptn;
	BYTE	msgtype;	/* message type */
	GC 		*pGC;
	CHAR	showCross = 1;
	ID		lastid;

#define AD_WUCHA 0x30
	en_disp_ad = 1;
	
	kx = 0;
	ky = 0;
	bx = 0;
	by = 0;
		
	/* Create One GC */
	dbgprintf("Init GC\n");
//	if( gSysTcbTbl[SYSTASK_ID-1].gc == NULL )
	{
		pGC = (GC *)SysInitGC(PHY_LCD_W, PHY_LCD_H);
		gSysTcbTbl[SYSTASK_ID-1].gc = pGC;
	}
	lastid = gLcdOwnerTskId;
	gLcdOwnerTskId = SYSTASK_ID;
			
	/* draw two crosses: one is on left-top, the other is on right-down */
	SysSetBkColor(( DWORD )pGC, GPC_BLUE);
	SysSetColor(( DWORD )pGC, GPC_WHITE);
	SysClearScreen(( DWORD )pGC, GPC_WHITE);

	SysDrawText(( DWORD )pGC, GPC_BLACK, 10, 20, 140, 84, "Pen Calibration: Please press left-top cross first, then right-bottom, and center last!", GPC_REPLACE_STYLE);
	
/*
#define DEFAULT_LEFT	0x0258
#define DEFAULT_TOP		0x01E0
#define DEFAULT_RIGHT	0x0D88
#define DEFAULT_BOTTOM	0x0E40

	avx1 = DEFAULT_LEFT;
	avy1 = DEFAULT_TOP;
	avx2 = DEFAULT_RIGHT;
	avy2 = DEFAULT_BOTTOM;
*/	
	DisableUartInt( UART_ALL );	// disable uart interrupt
	
	while(!quit)
	{
		if( showCross )
		{
			/* (10,10) */
			SysDrawHorz(( DWORD )pGC, GPC_BLACK, 5, 10, 11, GPC_SOLID_LINE, GPC_REPLACE_STYLE);
			SysDrawVert(( DWORD )pGC, GPC_BLACK, 10, 5, 11, GPC_SOLID_LINE, GPC_REPLACE_STYLE);
			
			/* (150,230) */
			SysDrawHorz(( DWORD )pGC, GPC_BLACK, 145, 230, 11, GPC_SOLID_LINE, GPC_REPLACE_STYLE);
			SysDrawVert(( DWORD )pGC, GPC_BLACK, 150, 225, 11, GPC_SOLID_LINE, GPC_REPLACE_STYLE);
		
			/* (80,120) */
			SysDrawHorz(( DWORD )pGC, GPC_BLACK, 75, 120, 11, GPC_SOLID_LINE, GPC_REPLACE_STYLE);
			SysDrawVert(( DWORD )pGC, GPC_BLACK, 80, 115, 11, GPC_SOLID_LINE, GPC_REPLACE_STYLE);
			
			showCross = 0;
		}
		
		flgptn = 0;
		/* wait for Interrupt or Tasks' Message */
    	wai_flg(&flgptn, SYS_EVENT, 0xFF, TWF_ORW);
    	
    	if( flgptn & INTPEN_FLG )
    	{
			msgtype = PEN_BUF[PEN_BUF_readcount].flag;
			/* read pen message directly from interrupt buffer PEN_BUF */
			switch(msgtype)
			{
				case SM_PENDOWN:
					last_x1=0;
					last_y1=0;
					last_x2=0;
					last_y2=0;
					last_x3=0;
					last_y3=0;

					act_cyc(TOUCHTIMER,TCY_ON|TCY_INI);
					break;	
			
				case SM_PENMOVE:
//					if(!PenDown)
					{
						PenDown++;
						if (!lefttop_pressed)
						{				
							y1 = PEN_BUF[PEN_BUF_readcount].y;
							x1 = PEN_BUF[PEN_BUF_readcount].x;
							if(!(last_x1&&last_y1)){
								last_x1 = x1;
								last_y1 = y1;
							}else{
								if ( x1 > last_x1 + AD_WUCHA 
									|| x1 < last_x1 - AD_WUCHA
									|| y1 > last_y1 + AD_WUCHA 
									|| y1 < last_y1 - AD_WUCHA ){
										x1 = ( x1 + last_x1 * PenDown )/( PenDown + 1 );
										y1 = ( y1 + last_y1 * PenDown )/( PenDown + 1 );
								}
								
							}
							if( PenDown == 3 )
								SysDrawCircle( (DWORD)pGC, GPC_BLACK, 10, 10, 5, GPC_REPLACE_STYLE );
						}else if( !rightbottom_pressed )
						{
							y2 = PEN_BUF[PEN_BUF_readcount].y;
							x2 = PEN_BUF[PEN_BUF_readcount].x;
							if(!(last_x2&&last_y2)){
								last_x2 = x2;
								last_y2 = y2;
							}else{
								if ( x2 > last_x2 + AD_WUCHA 
									|| x2 < last_x2 - AD_WUCHA
									|| y2 > last_y2 + AD_WUCHA 
									|| y2 < last_y2 - AD_WUCHA ){
										x2 = ( x2 + last_x2 * PenDown )/( PenDown + 1 );
										y2 = ( y2 + last_y2 * PenDown )/( PenDown + 1 );
								}
							}
							if( PenDown == 3 )
								SysDrawCircle( (DWORD)pGC, GPC_BLACK, 150, 230, 5, GPC_REPLACE_STYLE );
						}else
						{	
							x3 = kx * PEN_BUF[PEN_BUF_readcount].x + bx;
							y3 = ky * PEN_BUF[PEN_BUF_readcount].y + by;
							
							if( new_check )
							{	
								last_x3 = x3;
								last_y3 = y3;
								new_check = 0;
							}
							else
							{
								
/*								if ( next_x > x3 + POS_WUCHA )
									next_x = x3 + POS_WUCHA;
								else if ( next_x < x3 - POS_WUCHA )
									next_x = x3 - POS_WUCHA;
								
								if ( next_y > y3 + POS_WUCHA )
									next_y = y3 + POS_WUCHA;
								else if ( next_y < y3 - POS_WUCHA )
									next_y = y3 - POS_WUCHA;
							
								x3 = next_x;
								y3 = next_y;
*/
								if ( x3 > last_x3 + POS_WUCHA 
									|| x3 < last_x3 - POS_WUCHA
									|| y3 > last_y3 + POS_WUCHA 
									|| y3 < last_y3 - POS_WUCHA ){
										x3 = ( x3 + last_x3 * PenDown )/( PenDown + 1 );
										y3 = ( y3 + last_y3 * PenDown )/( PenDown + 1 );
								}
							}
							if( PenDown == 3 )
								SysDrawCircle( (DWORD)pGC, GPC_BLACK, 80, 120, 5, GPC_REPLACE_STYLE );
						}
					}
					break;
					
				case SM_PENUP:
					if( PenDown > 2 )
					{
						if ( rightbottom_pressed ){
							center_pressed = 1;
							SysClearRec(( DWORD )pGC, GPC_WHITE, 75, 115, 10, 10, GPC_REPLACE_STYLE);
						}
												
						if (lefttop_pressed ){
							kx =  (150.0-10.0) / (x2 - x1);
							ky =  (230.0-10.0) / (y2 - y1);
							bx = (160.0 -(x1+x2) * kx) / 2 ;
							by = (240.0 -(y1+y2) * ky) / 2 ;
	
	/*						kx =  (150.0-10.0) / (avx2 - avx1);
							ky =  (230.0-10.0) / (avy2 - avy1);
							bx = (160.0 -(avx1+avx2) * kx) / 2 ;
							by = (240.0 -(avy1+avy2) * ky) / 2 ;
	*/						
							rightbottom_pressed = 1;
							SysClearRec(( DWORD )pGC, GPC_WHITE, 140, 220, 18, 18, GPC_REPLACE_STYLE);	
						}
						
						if (!lefttop_pressed ){						
							lefttop_pressed = 1;
							SysClearRec(( DWORD )pGC, GPC_WHITE, 1, 1, 15, 15, GPC_REPLACE_STYLE);
						}
						
						if ( center_pressed ){						
							new_check = 1;
							if ( x3 > 85 || x3 < 75	|| y3 >  125 || y3 < 115 )
							{
								lefttop_pressed=0, rightbottom_pressed=0, center_pressed = 0;
								last_x1 = 0,last_y1 = 0,last_x2 = 0,last_y2 = 0,last_x3 = 0,last_y3 = 0;
								
								SysClearRec(( DWORD )pGC, GPC_WHITE, 10, 20, 140, 60, GPC_REPLACE_STYLE );
							
								SysDrawText(( DWORD )pGC, GPC_BLACK, 10, 20, 140, 84, "Pen Calibration is invalid.\nTry again!", GPC_REPLACE_STYLE);
								
								showCross = 1;
							}
							else
								quit = 1;
						}
					}
					
					PenDown = 0;
					
//					PenEnable();
					break;
					
				default:
					break;
			}
			PEN_BUF_readcount = (PEN_BUF_readcount +1) % MAX_PENBUF;
		}
		if( flgptn & INTKEY_FLG )
		{	
			msgtype = KEY_BUF[KEY_BUF_readcount].flag;
			switch(msgtype)
			{
				case SM_KEY:
					break;
				case SM_KEYDOWN:
					act_cyc(KEYSCANTIMER,TCY_ON|TCY_INI);
				case SM_KEYREPEAT:
					break;					
				case SM_KEYUP:					
					if( KEY_BUF[KEY_BUF_readcount].keyvalue == KEY_Cancel )
					{
						quit = 1;
					}
					KeyEnable();
					break;
					
				default:
					break;
			}
			KEY_BUF_readcount++;
			if (KEY_BUF_readcount >= MAX_KEY_BUF)
				KEY_BUF_readcount = 0;	
		}
		if( flgptn & TASKMSG_FLG )
		{
			process_msg();
		}

		
		clr_flg( SYS_EVENT, ~ALLINT_FLG );
		flgptn = 0;
	
		if (KEY_BUF_readcount != KEY_BUF_writecount)
			flgptn |= INTKEY_FLG;
		if (PEN_BUF_readcount != PEN_BUF_writecount)
			flgptn |= INTPEN_FLG;
		
		/* yes */
		if( flgptn > 0x1 )
			set_flg(SYS_EVENT, flgptn);
	}
	
	en_disp_ad = 0;
	
	gLcdOwnerTskId = lastid;
	
	loc_cpu();
	dbgprintf("Clear SYSEVENT\n");
	clr_flg(SYS_EVENT, ~ALLINT_FLG); 	/* clear SYSEVENT */
	unl_cpu();

	EnableUartInt( UART_ALL );	// enable uart interrupt
	SysFreeGC( pGC );
	
	dbgprintf("Systask Init End\n\n");
}

void InitialSystem()
{
	DWORD	hGC;
	char	inforstr[50];
	WORD	info_y;
	DWORD	result = 0;

	hGC = SysInitGC(PHY_LCD_W, PHY_LCD_H);
	gSysTcbTbl[SYSTASK_ID-1].gc = (GC *)hGC;
	gLcdOwnerTskId = SYSTASK_ID;
	SysClearScreen( hGC, GPC_WHITE );

	// System Initial
	// Initialize file system
	info_y = 10;
	SysTextOut( hGC, GPC_BLACK, 10, info_y, "初始化文件系统.....", GPC_REPLACE_STYLE );
	result = InitFileSystem();
	if( result != 0 )
	{
		info_y += 16;
		sprintf( inforstr, "FAIL!(0x%08x)", result );
		SysTextOut( hGC, GPC_BLACK, 10, info_y, inforstr, GPC_REPLACE_STYLE );
	}
	else
	{
		info_y += 16;
		SysTextOut( hGC, GPC_BLACK, 10, info_y, "OK!", GPC_REPLACE_STYLE );
	}
	
	// Create Application Data Base
	info_y += 16;
	SysTextOut( hGC, GPC_BLACK, 10, info_y, "初始化数据库.....", GPC_REPLACE_STYLE );
	CreateDataBase();
	info_y += 16;
	if( result != 0 )
	{
		sprintf( inforstr, "FAIL!(0x%08x)", result );
		SysTextOut( hGC, GPC_BLACK, 10, info_y, inforstr, GPC_REPLACE_STYLE );
	}
	else
		SysTextOut( hGC, GPC_BLACK, 10, info_y, "OK!", GPC_REPLACE_STYLE );

	// initialize GSM module
	info_y += 16;
	SysTextOut( hGC, GPC_BLACK, 10, info_y, "初始化GSM.....", GPC_REPLACE_STYLE );
	result = 0;	//InitGSM(); we will never do this in the simulator
	info_y += 16;
	if( result != 0 )
	{
		sprintf( inforstr, "FAIL!(0x%08x)", result );
		SysTextOut( hGC, GPC_BLACK, 10, info_y, inforstr, GPC_REPLACE_STYLE );
	}
	else
		SysTextOut( hGC, GPC_BLACK, 10, info_y, "OK!", GPC_REPLACE_STYLE );
	
	
	SysFreeGC( hGC );
}

/* init systcb table */
void SysTcbTableInit( void )
{
	int i;
	
	memset(gSysTcbTbl, 0, sizeof(SYSTCB)*TASKNUM);
	
	/* gSysTcbTbl init */
	for(i=0;i<TASKNUM-1;i++)
	{
		gSysTcbTbl[i].next = &gSysTcbTbl[i+1];
		gSysTcbTbl[i].id = (DWORD)&gSysTcbTbl[i];
		gSysTcbTbl[i].status = DORMANT;
	}
	gSysTcbTbl[i].next = NULL;
	gSysTcbTbl[i].id = (DWORD)&gSysTcbTbl[i];
	gSysTcbTbl[i].status = DORMANT;
}

void power_down(void)
{
	register UINT old_imr;
	
	
	//extern void TurnOffLCD( void );
	
	
	
#ifndef PCTLR
#define PCTLR *((unsigned char *)0xFFFFF207 )	/* Power Control Register */
#endif
#ifndef PLLCR
#define PLLCR *((unsigned short *)0xFFFFF200 )	/* PLL Control Register */
#endif
	
	/*************************************************************\
	 *  DISPATCH FORBIDDEN
	\*************************************************************/
	dis_dsp();
	
	/* Mask all interrupts */
	old_imr = IMR;
	IMR = 0xFFFFFFFF;

	
	/* Turn off LCD. */
	TurnOffLCD();
	/* Change RTC's frequence to 1Hz. */
	//set_rtc( RTCIENR_1HZ );
	stop_rtc();
	/* change sys_clk()'s tick */
	g_uhIntTime = 1000;  /* 1000/RTC's frequence(1Hz) */
	
	/* Restore all interrupts */
	//IMR &= ( ( old_imr |IMR_MTMR1 ) & ( ~IMR_MRTC ) );

	/* Close PLL */
	PLLCR |= 0x0008;
	asm("	STOP #$2000");
	
	/* CPU halt! */
	/* .............  */
	
	IMR = old_imr;
	TurnOnLCD();
	ena_dsp();
	
	/*************************************************************\
	 *  DISPATCH OK
	\*************************************************************/
}

⌨️ 快捷键说明

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