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

📄 main.c

📁 综合实验源代码-计算器
💻 C
字号:
#include"..\ucos-ii\includes.h"               /* uC/OS interface */
#include "..\inc\drv.h"
#include "..\ucos-ii\add\osaddition.h"
#include "..\inc\OSFile.h"
#include "..\inc\drv\Ustring.h"
#include "..\inc\drv\tchScr.h"
#include <string.h>

///******************任务定义***************///

OS_STK Main_Stack[STACKSIZE*8]={0, };   //Main_Test_Task堆栈
void Main_Task(void *Id);             //Main_Test_Task
#define Main_Task_Prio     12

OS_STK Led_Flash_Stack[STACKSIZE]= {0, }; //LED闪烁任务堆栈
void Led_Flash_Task(void *Id);            //LED闪烁任务
#define Led_Flash_Prio           60



/**************已经定义的OS任务*************
#define Touch_Screen_Task_Prio		9
#define Main_Task_Prio     12
#define Key_Scan_Task_Prio      58
#define Lcd_Fresh_prio           59
#define Led_Flash_Prio           60

/*****************************************************/
///*****************事件定义*****************///

OS_EVENT *Nand_Rw_Sem;                   //Nand_Flash读写控制权旗语
//and you can use it as folloeing:
//    Nand_Rw_Sem=OSSemCreate(1);        //创建Nand-Flash读写控制权旗语,初值为1满足互斥条件//
//    OSSemPend(Nand_Rw_Sem,0,&err);
//    OSSemPost(Nand_Rw_Sem);

OS_EVENT *Uart_Rw_Sem;                   //Uart读写控制权旗语
//and you can use it as folloeing:
//    Uart_Rw_Sem=OSSemCreate(1);        //创建Uart读写控制权旗语,初值为1满足互斥条件//
//    OSSemPend(Uart_Rw_Sem,0,&err);
//    OSSemPost(Uart_Rw_Sem);

///////////////////////////////////////////////////////////////
int existnum1=0;
//0表示没有操作数opnum1 不存在
int opnum1,opnum2,result;
int op1=0;
int newop=0;
////////////////////////////////////////////////////////////////
//                                                            //
//0-->no operation 14-->puls 12-->multiple 3-->substract 7-->divide//
//                                                            //
////////////////////////////////////////////////////////////////
U8 empty=TRUE;
U8 secondnum=FALSE;
//表示开始输入第二个数字,用在当使用第二个操作符号的作用
//产生答案时,又开始输入第二个操作数的标志
void onKey(int nkey, int fnkey);
void Led_Flash_Task(void *Id)//指示RTOS处于正常工作中
{
  unsigned char led_state;
  Uart_Printf(0,"\n10");
  for (;;)
  {
    Led_Display(led_state);
    led_state=~led_state;
    OSTimeDly(250);
  }
}//Led_Flash_Task
			
		

void initOSGUI()	//初始化操作系统的图形界面
{
	initOSMessage();
	initOSList();
	initOSDC();
	initOSCtrl();
	initOSFile();
}
/////////////////////////////////////////////////////
//                  Main function.                //
////////////////////////////////////////////////////

int Main(int argc, char **argv)
{

	ARMTargetInit();       // do target (uHAL based ARM system) initialisation //
	OSInit();               // needed by uC/OS-II //
	uHALr_ResetMMU();
	
    Port_Init(); //端口初始化
	LCD_Init();			//LCD初始化,因要用到LCD输出结果
				
  
	
	LCD_printf("LCD initialization is OK\n");
	LCD_printf("320 x 240  Text Mode\n");
	LoadFont();
	LoadConfigSys();	
  // create the tasks in uC/OS and assign increasing //
  // priorities to them so that Task3 at the end of  //
  // the pipeline has the highest priority.          //
	LCD_printf("Create task on uCOS-II...\n");
	OSTaskCreate(Main_Task,  (void *)0,  (OS_STK *)&Main_Stack[STACKSIZE*8-1],  Main_Task_Prio);// 1
	OSTaskCreate(Led_Flash_Task,  (void *)0,  (OS_STK *)&Led_Flash_Stack[STACKSIZE-1],  Led_Flash_Prio);// 10

	

	OSAddTask_Init();
  
	LCD_printf("Starting uCOS-II...\n");
	LCD_printf("Entering graph mode...\n");
	LCD_ChangeMode(DspGraMode);
	initOSGUI();
	InitRtc();	
	Nand_Rw_Sem=OSSemCreate(1);   //创建Nand-Flash读写控制权旗语,初值为1满足互斥条件//

	
	ARMTargetStart();      //Start the (uHAL based ARM system) system running //
	OSStart();              // start the game //
 // never reached //
	return 0;
}//main

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PTextCtrl pTextCtrl;//文本框结构
#define ID_MainTextCtrl 101//文本框ID

void CreateText()
{
    static char TimeKeyTable[]={'1','2','3',0,'4','5','6',0,'7','8','9',0,':','0','\b',0};//键值映射表
	structRECT rect;//定义矩形结构	
       //U16 str[20];
	SetRect(&rect, 40,30,180,50);	//设置文本框
	pTextCtrl=CreateTextCtrl(ID_MainTextCtrl, &rect, FONTSIZE_MIDDLE, CTRL_STYLE_FRAME,TimeKeyTable,NULL);//创建文本框

	SetWndCtrlFocus(NULL,ID_MainTextCtrl);//焦点移到文本框中
	DrawTextCtrl(pTextCtrl);//重绘文本框	
}



void onnumchar(char mid);
void coputing();
void docompute(); 

//OS_EVENT *Rtc_Updata_Sem;//时钟更新控制权
void Main_Task(void *Id)             //Main_Test_Task
{
         int i=0;
         int j=0;
         int x,y;
	POSMSG pMsg=0;//消息结构
	PDC pdc;
	char bmpnamelogo[12]={'1','4','5','_','5','9',' ',' ','B','M','P',0}; 
	U16    ch0={'1'};
	U16    ch1={'2'};
    U16    ch2={'3'};
    U16    ch3={'+'};
	U16    ch4={'4'};
    U16    ch5={'5'};
	U16    ch6={'6'};
	U16    ch7={'-'};
	U16    ch8={'7'};
	U16    ch9={'8'};
	U16    ch10={'9'};
	U16    ch11={'*'};
	U16    ch12={'0'};
	U16    ch13={'='};
	U16    ch14={'C'};
	U16    ch15={'/'};
	
	pdc = CreateDC();
	ClearScreen();//清屏
	ShowBmp(pdc,bmpnamelogo, 1, 1);
	OSTimeDly(2000);
	ClearScreen();//清屏
	CreateText(); //创建文本框
    DrawRectFrame(pdc,20,20,205,239);
	DrawRectFrame(pdc,40,30,180,50); // add a frame for the pTextCtrl
	for(j=0;j<4;j++)
		{
	      for(i=0;i<4;i++)
		   {
		   DrawRectFrame(pdc,25+i*45,60+j*45,65+i*45,100+j*45);
		
		   }
		}
	
		 TextOut(pdc,40,75,&ch0,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,85,75,&ch1,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,130,75,&ch2,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,175,75,&ch3,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,40,120,&ch4,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,85,120,&ch5,TRUE,FONTSIZE_SMALL);		 
		 TextOut(pdc,130,120,&ch6,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,175,120,&ch7,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,40,165,&ch8,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,85,165,&ch9,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,130,165,&ch10,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,175,165,&ch11,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,40,210,&ch12,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,85,210,&ch13,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,130,210,&ch14,TRUE,FONTSIZE_SMALL);
		 TextOut(pdc,175,210,&ch15,TRUE,FONTSIZE_SMALL);
		 
//	Rtc_Updata_Sem=OSSemCreate(1);	
	//消息循环
	for(;;){
		pMsg=WaitMessage(0);//等待消息
			switch(pMsg->Message){
			case OSM_KEY:  //键盘消息
					onKey(pMsg->WParam,pMsg->LParam); 
					
					break;	
			case OSM_TOUCH_SCREEN: 
				    //删除消息来自触摸屏
				     i=-1;
			         x=pMsg->WParam&0xffff;
			         y=pMsg->WParam>>16;
			        

			         
		 	        if((x>25)&&(x<65)&&(y>60)&&(y<100))
			        {
			        	   i=0;
			        }
			        else if((x>25)&&(x<65)&&(y>105)&&(y<145))
			        	 {
			        	    i=4;
			        	  }
			        else if((x>25)&&(x<65)&&(y>150)&&(y<190))
			        	 {
			        	    i=8;
			        	   
			        	  }
			        else if((x>25)&&(x<65)&&(y>195)&&(y<235))
			        	  {
			        	     i=13;
			        	  }
			         else if((x>70)&&(x<110)&&(y>60)&&(y<100))
     			          {
     			               i=1;
     			          }
			         else if((x>70)&&(x<110)&&(y>105)&&(y<145))
			        	  {
			        	     i=5;
			        	   }
			         else if((x>70)&&(x<110)&&(y>150)&&(y<190))
			        	  {
			        	       i=9;
			        	   }
			         else if((x>70)&&(x<110)&&(y>195)&&(y<235))
			        	  {
			        	      i=11;
			        	   }
			         else if((x>115)&&(x<110)&&(y>60)&&(y<100))
			         	  {
			                  i=2;
			               }
			         else if((x>115)&&(x<155)&&(y>105)&&(y<145))
			        	  {
			        	      i=6;
			        	   }
			         else if((x>115)&&(x<155)&&(y>150)&&(y<190))
			              {
			                  i=10;
			               }
			         else  if((x>115)&&(x<155)&&(y>195)&&(y<235))
			        	   {
			        	       i=15;
			       	   }
			         else  if((x>160)&&(x<200)&&(y>60)&&(y<100))
			 	           {
			 	               i=14;
			 	            }
			         else  if((x>160)&&(x<200)&&(y>105)&&(y<145))
			               {
			                   i=3;
			                }
			         else  if((x>160)&&(x<200)&&(y>150)&&(y<190))
			               {
			                  i=12;
			                }
			         else  if((x>160)&&(x<200)&&(y>195)&&(y<235))
  			               {
  			                    i=7;
  			                }
			         	
			         	
				    onKey(i,0);
			         break;
				
				}
		    DeleteMessage(pMsg);
        	OSTimeDly(100);
	      	}    	
		DestoryDC(pdc);			
	}

void onKey(int nkey,int fnkey)
{	
	U16 str[20];
	INT8U err;		
	
    char keytable[]={'1','2','3',0,'4','5','6',0,'7','8','9',0,0,'0'};	
    SetTextCtrlEdit(pTextCtrl, TRUE);//焦点移到文本框控件
	switch(nkey) {	
		
	 case 11:
	 		if(!empty){
	 			TextCtrlDeleteChar(pTextCtrl,TRUE);
	 			}
	 		break;
	 
	 case 15://Cancel
		if(!empty){
		//	OSSemPend(Rtc_Updata_Sem, 0,&err);
			pTextCtrl->text[0]=0;
			DrawTextCtrl(pTextCtrl);
			empty=TRUE;	}		
		//if there is nothing in pTextCtrl,set astatus=none and make sure
		else{
		   existnum1=0; //that there is nothing in the pTextCtrl 
		   pTextCtrl->text[0]=0;
		   DrawTextCtrl(pTextCtrl);
		   op1=newop=0; }
		
		break;		
		case 14://plus
			newop=14;
			docompute();		
			break;
		case 12://multiple
			newop=12;
			docompute();
			break;
		case 3:		
			newop=3;
			docompute();		
		    break;
		case 7://divide
		    newop=7;
		    docompute();
		    break;
		case 1:
			onnumchar(keytable[nkey]);
			break;
		case 2:
			onnumchar(keytable[nkey]);
			break;
		case 0:
			onnumchar(keytable[nkey]);
			break;
		case 5:
			onnumchar(keytable[nkey]);
			break;
		case 4:
			onnumchar(keytable[nkey]);
			break;
		case 6:
			onnumchar(keytable[nkey]);
			break;
		case 8:
			onnumchar(keytable[nkey]);
			break;
		case 9:
			onnumchar(keytable[nkey]);
			break;
		case 10:
			onnumchar(keytable[nkey]);
			break;
		case 13:
			onnumchar(keytable[nkey]);
			break;		
	
	}  
}

void onnumchar(char mid)//need modifying......
{
    empty=FALSE;
    if(secondnum){
    	SetTextCtrlEdit(pTextCtrl, TRUE);
    	pTextCtrl->text[0]=0;
    	DrawTextCtrl(pTextCtrl);
	    AppendChar2TextCtrl(pTextCtrl,mid,TRUE);
	    secondnum=FALSE; 	}
      else{
      	AppendChar2TextCtrl(pTextCtrl,mid,TRUE);
      	}	
}
	
void docompute()
{  
  if(!existnum1){
	//不存在opnum1
		if(!empty){
			 opnum1=Unicode2Int(pTextCtrl->text);
			 existnum1=1;
             op1=newop;
			 pTextCtrl->text[0]=0;//是不是每次操作文本框的时候都需要信号量呢澹垮?
			 DrawTextCtrl(pTextCtrl);
			 empty=TRUE;
			 newop=0; }
			 else{
				newop=0;//抛弃输入的操作符?
			    }
  	}				    	
 else{  
 	   if(op1==0){
			op1=newop;
			newop=0;
			pTextCtrl->text[0]=0;//按确定后计算出结果后的状态是:opnum1存在op等于free
			DrawTextCtrl(pTextCtrl);//但文本框不为空;此时按操作键;要将文本清空,防止
			empty=TRUE;	//再次按下操作符号,跳如下一个IF语句,将结果误做第二个操作数					   
			}
		else{
			if(!empty){
				opnum2=Unicode2Int(pTextCtrl->text);					 	  	      
				pTextCtrl->text[0]=0;					     
			    SetTextCtrlEdit(pTextCtrl, TRUE);//使文本框处于编辑状态
			    empty=TRUE;
                coputing();
			 // DrawTextCtrl(pTextCtrl);//重绘文本框
			   		}
			else{
				op1=newop;
				newop=0;}
				}		
  	}
}
void coputing()	
{
    U16 str2[20];
    INT8U err2;   
	switch(op1){
    	         case 0:						 	
				 case 14:
				    	   result=opnum1+opnum2;
				    	   opnum1=result;
					       existnum1=1;
					       op1=newop;					       
					       SetTextCtrlEdit(pTextCtrl,TRUE);
					       Int2Unicode(result,str2);
					       SetTextCtrlText(pTextCtrl,str2,TRUE);
					       DrawTextCtrl(pTextCtrl);
					       secondnum=TRUE;					       
					       break;
			case 12:
					       result=opnum1*opnum2;
					       opnum1=result;
					       existnum1=1;
					       op1=newop;				   
					       SetTextCtrlEdit(pTextCtrl,TRUE);
					       Int2Unicode(result,str2);
					       SetTextCtrlText(pTextCtrl,str2,TRUE);
					       DrawTextCtrl(pTextCtrl);
					       secondnum=TRUE;
					       break;
		     case 3:
					       result=opnum1-opnum2;
					       opnum1=result;
					       existnum1=1;
					       op1=newop;				   
					       SetTextCtrlEdit(pTextCtrl,TRUE);
					       Int2Unicode(result,str2);
					       SetTextCtrlText(pTextCtrl,str2,TRUE);
					       DrawTextCtrl(pTextCtrl);
					       secondnum=TRUE;
					       break;
			 case 7:
					     	if(opnum2!=0)
					     		{  result=opnum1/opnum2;
					               opnum1=result;
					               existnum1=1;
					               op1=newop;				   
					               SetTextCtrlEdit(pTextCtrl,TRUE);
					               Int2Unicode(result,str2);
					               SetTextCtrlText(pTextCtrl,str2,TRUE);
					               DrawTextCtrl(pTextCtrl);
					               secondnum=TRUE;	}
					     	break;
			default:					     	
					       newop=0;		   
					}
		}

////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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