Demo entry 6771814

C++

   

Submitted by anonymous on Nov 21, 2018 at 03:48
Language: C++. Code size: 8.7 kB.

#include "led.h"
#include "delay.h"
#include "sys.h"
#include "includes.h" 
#include "24cxx.h"	
#include "touch.h"			 	
#include "key.h"
#include "lcd.h"
#include "myiic.h"
#include "usart.h"

//
//Lab10 UCOSIIʵÑé-ÈÎÎñ¼äͨÐÅ 
// 

 
/////////////////////////UCOSIIÈÎÎñÉèÖÃ///////////////////////////////////
//START ÈÎÎñ
//ÉèÖÃÈÎÎñÓÅÏȼ¶

#define START_TASK_PRIO      			10 //¿ªÊ¼ÈÎÎñµÄÓÅÏȼ¶ÉèÖÃΪ×îµÍ
//ÉèÖÃÈÎÎñ¶ÑÕ»´óС
#define START_STK_SIZE  				64
//ÈÎÎñ¶ÑÕ»	
OS_STK START_TASK_STK[START_STK_SIZE];
//ÈÎÎñº¯Êý
void start_task(void *pdata);	
 			   
//LED0ÈÎÎñ
//ÉèÖÃÈÎÎñÓÅÏȼ¶
#define LED0_TASK_PRIO       			7 
//ÉèÖÃÈÎÎñ¶ÑÕ»´óС
#define LED0_STK_SIZE  		    		64
//ÈÎÎñ¶ÑÕ»	
OS_STK LED0_TASK_STK[LED0_STK_SIZE];
//ÈÎÎñº¯Êý
void led0_task(void *pdata);
 
//´¥ÃþÆÁÈÎÎñ
//ÉèÖÃÈÎÎñÓÅÏȼ¶
#define TOUCH_TASK_PRIO       		 	6
//ÉèÖÃÈÎÎñ¶ÑÕ»´óС
#define TOUCH_STK_SIZE  				64
//ÈÎÎñ¶ÑÕ»	
OS_STK TOUCH_TASK_STK[TOUCH_STK_SIZE];
//ÈÎÎñº¯Êý
void touch_task(void *pdata);

//LED1ÈÎÎñ
//ÉèÖÃÈÎÎñÓÅÏȼ¶
#define LED1_TASK_PRIO       			5 
//ÉèÖÃÈÎÎñ¶ÑÕ»´óС
#define LED1_STK_SIZE  					64
//ÈÎÎñ¶ÑÕ»	
OS_STK LED1_TASK_STK[LED1_STK_SIZE];
//ÈÎÎñº¯Êý
void led1_task(void *pdata);


//Ö÷ÈÎÎñ
//ÉèÖÃÈÎÎñÓÅÏȼ¶
#define MAIN_TASK_PRIO       			4 
//ÉèÖÃÈÎÎñ¶ÑÕ»´óС
#define MAIN_STK_SIZE  					128
//ÈÎÎñ¶ÑÕ»	
OS_STK MAIN_TASK_STK[MAIN_STK_SIZE];


int color_flag;
//ÈÎÎñº¯Êý
void main_task(void *pdata);

//°´¼üɨÃèÈÎÎñ
//ÉèÖÃÈÎÎñÓÅÏȼ¶
#define KEY_TASK_PRIO       			3 
//ÉèÖÃÈÎÎñ¶ÑÕ»´óС
#define KEY_STK_SIZE  					64
//ÈÎÎñ¶ÑÕ»	
OS_STK KEY_TASK_STK[KEY_STK_SIZE];
//ÈÎÎñº¯Êý
void key_task(void *pdata);
//////////////////////////////////////////////////////////////////////////////
OS_EVENT * msg_key;			//°´¼üÓÊÏäʼþ¿éÖ¸Õë
OS_EVENT * sem_led1;		//LED1ÐźÅÁ¿Ö¸Õë	 	  
//¼ÓÔØÖ÷½çÃæ   
void ucos_load_main_ui(void)
{
	LCD_Clear(WHITE);	//ÇåÆÁ
 	POINT_COLOR=RED;	//ÉèÖÃ×ÖÌåΪºìÉ« 
	LCD_ShowString(30,10,200,16,16,"Mini STM32");	
	LCD_ShowString(30,30,200,16,16,"UCOSII TEST2");	
	LCD_ShowString(30,50,200,16,16,"ATOM@ALIENTEK");
  LCD_ShowString(30,75,200,16,16,"KEY0:LED1 KEY_UP:ADJUST");	
  LCD_ShowString(30,95,200,16,16,"KEY1:CLEAR"); 
	LCD_ShowString(80,210,200,16,16,"Touch Area");	
	LCD_DrawLine(0,120,lcddev.width,120);
	LCD_DrawLine(0,70,lcddev.width,70);
	LCD_DrawLine(150,0,150,70);
 	POINT_COLOR=BLUE;//ÉèÖÃ×ÖÌåΪÀ¶É« 
  LCD_ShowString(160,30,200,16,16,"CPU:   %");	
  LCD_ShowString(160,50,200,16,16,"SEM:000");	
}	  									

 int main(void)
 {	
	delay_init();	    	 //ÑÓʱº¯Êý³õʼ»¯	
  uart_init(9600);
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// ÉèÖÃÖжÏÓÅÏȼ¶·Ö×é2
	LED_Init();		  	//³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
	LCD_Init();			   	//³õʼ»¯LCD
	KEY_Init();				//°´¼ü³õʼ»¯
  tp_dev.init();		    //´¥ÃþÆÁ³õʼ»¯
	ucos_load_main_ui();	//¼ÓÔØÖ÷½çÃæ	 
  OSInit();  	 			//³õʼ»¯UCOSII
  OSTaskCreate(start_task,(void *)0,(OS_STK *)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO );//´´½¨ÆðʼÈÎÎñ
	OSStart();	
 }
 
 
 //¿ªÊ¼ÈÎÎñ
void start_task(void *pdata)
{
  OS_CPU_SR cpu_sr=0;
	pdata = pdata; 		  
	msg_key=OSMboxCreate((void*)0);	//´´½¨ÏûÏ¢ÓÊÏä
	sem_led1=OSSemCreate(0);		//´´½¨ÐźÅÁ¿		 			  
	OSStatInit();					//³õʼ»¯Í³¼ÆÈÎÎñ.ÕâÀï»áÑÓʱ1ÃëÖÓ×óÓÒ	
 	OS_ENTER_CRITICAL();			//½øÈëÁÙ½çÇø(ÎÞ·¨±»Öжϴò¶Ï)    
 	OSTaskCreate(led0_task,(void *)0,(OS_STK*)&LED0_TASK_STK[LED0_STK_SIZE-1],LED0_TASK_PRIO);						   
 	OSTaskCreate(touch_task,(void *)0,(OS_STK*)&TOUCH_TASK_STK[TOUCH_STK_SIZE-1],TOUCH_TASK_PRIO);	 				   
 	OSTaskCreate(led1_task,(void *)0,(OS_STK*)&LED1_TASK_STK[LED1_STK_SIZE-1],LED1_TASK_PRIO);	 				   
 	OSTaskCreate(main_task,(void *)0,(OS_STK*)&MAIN_TASK_STK[MAIN_STK_SIZE-1],MAIN_TASK_PRIO);	 				   
 	OSTaskCreate(key_task,(void *)0,(OS_STK*)&KEY_TASK_STK[KEY_STK_SIZE-1],KEY_TASK_PRIO);	 				   
 	OSTaskSuspend(START_TASK_PRIO);	//¹ÒÆðÆðʼÈÎÎñ.
	OS_EXIT_CRITICAL();				//Í˳öÁÙ½çÇø(¿ÉÒÔ±»Öжϴò¶Ï)
}	  


//LED0ÈÎÎñ
void led0_task(void *pdata)
{
	u8 t;
	while(1)
	{
		t++;
		delay_ms(10);
		if(t==8)LED0=1;	//LED0Ãð
		if(t==100)		//LED0ÁÁ
		{
			t=0;
			LED0=0;
		}
	}									 
}	   

void gui_draw_hline(u16 x0, u16 y0, u16 len, u16 color){
	if (len == 0) return;
	LCD_Fill(x0, y0, x0 + len - 1, y0, color);
}

void gui_fill_circle(u16 x0, u16 y0, u16 r, u16 color){
	u32 i;
	u32 imax = ((u32)r * 707) / 1000 + 1;
	u32 sqmax = (u32)r * (u32)r + (u32)r / 2;
	u32 x = r;
	gui_draw_hline(x0- r, y0, 2 * r, color);
	for (i = 1; i <= imax; i++){
		if ((i * i + x * x) > sqmax){
			if (x > imax){
				gui_draw_hline(x0 - i + 1, y0 + x, 2 * (i - 1), color);
				gui_draw_hline(x0 - i + 1, y0 - x, 2 * (i - 1), color);
			}
			x--;
		}
		gui_draw_hline(x0 - x, y0 + i, 2 * x, color);
		gui_draw_hline(x0 - x, y0 - i, 2 * x, color);
	}
}
//LED1ÈÎÎñ
void led1_task(void *pdata)
{
	u8 err;
	while(1)
	{
		OSSemPend(sem_led1,0,&err);
		LED1=0;
		delay_ms(200);
		LED1=1;
		delay_ms(800);
	}									 
}
void Circle(u16 x0, u16 y0, u8 r){
	gui_fill_circle(x0, y0, r, RED);
}
void Tri(u16 x0, u16 y0){
	u16 x1 = x0 + 10;
	u16 y1 = y0;
	u16 x2 = x0;
	u16 y2 = y0 + 5;
	u16 h1 = y0 - 10;
	u16 h2 = x0 - 10;
	LCD_DrawLine(x1, y1, x2, y2);
	LCD_DrawLine(x2, y2, h1, h2);
	LCD_DrawLine(h1, h2, x1, y1);
	
}
//´¥ÃþÆÁÈÎÎñ
void touch_task(void *pdata)
{	  	
	while(1)
	{
		tp_dev.scan(0); 		 
		if(tp_dev.sta&TP_PRES_DOWN)		//´¥ÃþÆÁ±»°´ÏÂ
		{	
		 	if(tp_dev.x[0]<lcddev.width&&tp_dev.y[0]<lcddev.height&&tp_dev.y[0]>120)
			{	
				switch (color_flag){
					case 0:	
						//LINE
						printf("the current task is Line\n");
					  printf("the coordinate is : %d %d\n", tp_dev.x[0], tp_dev.y[0]);
						TP_Draw_Big_Point(tp_dev.x[0],tp_dev.y[0],GREEN);			
						delay_ms(2);

						break;
					case 1:
						//NAME
						//TP_Draw_Big_Point(tp_dev.x[0],tp_dev.y[0],BLUE);			
						printf("the current task is Name\n");
					  printf("the coordinate is : %d , %d\n", tp_dev.x[0], tp_dev.y[0]);
						LCD_Fill(0,121,lcddev.width,lcddev.height,WHITE);
						LCD_ShowString(tp_dev.x[0], tp_dev.y[0], 120, 240, 16, "Chen Su Qin");
						
						break;
					case 2:
						//SANJIAO
						//TP_Draw_Big_Point(tp_dev.x[0],tp_dev.y[0],BLACK);
						printf("the current task is Triangle\n");
					  printf("the coordinate is : %d , %d\n", tp_dev.x[0], tp_dev.y[0]);
						LCD_Fill(0,121,lcddev.width,lcddev.height,GREEN);
						Tri(tp_dev.x[0], tp_dev.y[0]);
						break;
					case 3:
						//CIRCLE
						//TP_Draw_Big_Point(tp_dev.x[0],tp_dev.y[0],RED);
					//»­Í¼	 
						LCD_Fill(0,121,lcddev.width,lcddev.height,BLUE);
						Circle(tp_dev.x[0], tp_dev.y[0], 10);
						printf("the current task is Circle\n");
						printf("the coordinate is : %d , %d\n", tp_dev.x[0], tp_dev.y[0]);
						break;

				}
				//printf(tp_dev.x[0] + "  " + tp_dev.y[0]);
				delay_ms(0); 			   
			}
		}else delay_ms(10);	//ûÓа´¼ü°´ÏµÄʱºò 
	}
}
//Ö÷ÈÎÎñ
void main_task(void *pdata)
{							 
	u32 key=0;	
	u8 err;	
	u8 semmask=0;
	u8 tcnt=0;						 
	while(1)
	{
		key=(u32)OSMboxPend(msg_key,10,&err);   
		switch(key)
		{ 
			case KEY0_PRES://·¢ËÍÐźÅÁ¿
				semmask=1;
				OSSemPost(sem_led1);
				break;
			case KEY1_PRES://Çå³ý
				//u16 COLOR = BLACK;
				switch (color_flag){
					case 0: 
						LCD_Fill(0,121,lcddev.width,lcddev.height,WHITE);
					//NAME
						color_flag++;
						break;
					case 1:
						LCD_Fill(0,121,lcddev.width,lcddev.height,GREEN);
					//SANJIAO
						color_flag++;
						break;
					case 2:
						LCD_Fill(0,121,lcddev.width,lcddev.height,BLUE);
					//CIRCLE
						color_flag++;
						break;
					case 3:
						LCD_Fill(0,121,lcddev.width,lcddev.height,YELLOW);
					//LINE
						color_flag = 0;
						break;
					deflaut:
						break;
				}
				break;
			case WKUP_PRES://У׼
				OSTaskSuspend(TOUCH_TASK_PRIO);	//¹ÒÆð´¥ÃþÆÁÈÎÎñ		 
 				if((tp_dev.touchtype&0X80)==0)TP_Adjust();	   
 				OSTaskResume(TOUCH_TASK_PRIO);	//½â¹Ò
				ucos_load_main_ui();			//ÖØмÓÔØÖ÷½çÃæ		 
				break;
		}
   		if(semmask||sem_led1->OSEventCnt)//ÐèÒªÏÔʾsem		
		{
			POINT_COLOR=BLUE;
			LCD_ShowxNum(192,50,sem_led1->OSEventCnt,3,16,0X80);//ÏÔʾÐźÅÁ¿µÄÖµ
			if(sem_led1->OSEventCnt==0)semmask=0;	//Í£Ö¹¸üÐÂ
		} 
		if(tcnt==50)//0.5Ãë¸üÐÂÒ»´ÎCPUʹÓÃÂÊ
		{
			tcnt=0;
			POINT_COLOR=BLUE;		  
			LCD_ShowxNum(192,30,OSCPUUsage,3,16,0);	//ÏÔʾCPUʹÓÃÂÊ   
		}
		tcnt++;
		delay_ms(10);
	}
}

//°´¼üɨÃèÈÎÎñ
void key_task(void *pdata)
{	
	u8 key;		    						 
	while(1)
	{
		key=KEY_Scan(0);   
		if(key)OSMboxPost(msg_key,(void*)key);//·¢ËÍÏûÏ¢
 		delay_ms(10);
	}
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).