#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "includes.h"
#include "beep.h"
#include "key.h"
#include "lcd.h"
#include "touch.h"
//start_task
#define START_TASK_PRIO 10
#define START_STK_SIZE 64
OS_STK START_TASK_STK[START_STK_SIZE];
void start_task(void *pdata);
//TOUCH_TASK_PRIO
#define TOUCH_TASK_PRIO 9
#define TOUCH_STK_SIZE 128
OS_STK TOUCH_TASK_STK[TOUCH_STK_SIZE];
void touch_task(void* pdata);
//LED0_task
#define LED0_TASK_PRIO 7
#define LED0_STK_SIZE 64
OS_STK LED0_TASK_STK[LED0_STK_SIZE];
void led0_task(void *pdata);
//LED1_task
#define LED1_TASK_PRIO 6
#define LED1_STK_SIZE 64
OS_STK LED1_TASK_STK[LED1_STK_SIZE];
void led1_task(void *pdata);
//printf_task
#define PRINTF_TASK_PRIO 8
#define PRINTF_STK_SIZE 64
OS_STK PRINTF_TASK_STK[PRINTF_STK_SIZE];
void printf_task(void* pdata);
//key_scan_task
#define KEY_TASK_PRIO 3
#define KEY_STK_SIZE 65
OS_STK KEY_TASK_STK[KEY_STK_SIZE];
void key_task(void* pdata);
//·äÃùÆ÷èÎÎñ
//éèÖÃèÎÎñóÅÏ輶
#define BEEP_TASK_PRIO 5
//éèÖÃèÎÎñ¶ÑÕ»′óD¡
#define BEEP_STK_SIZE 64
//′′½¨èÎÎñ¶ÑÕ»¿Õ¼ä
OS_STK BEEP_TASK_STK[BEEP_STK_SIZE];
//èÎÎñoˉêy½ó¿ú
void beep_task(void *pdata);
//main_task
#define MAIN_TASK_PRIO 4
#define MAIN_STK_SIZE 64
OS_STK MAIN_TASK_STK[KEY_STK_SIZE];
void main_task(void* pdata);
OS_EVENT *msg_key;
OS_EVENT *sem_beep;
void ucos_load_main_ui(void )
{
LCD_Clear(WHITE);
POINT_COLOR = RED;
LCD_ShowString (30,10,200,16,16,"Explorer 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
S1 KEY_UP:ADJUST");
LCD_ShowString(30,95,200,16,16,"KEY1:BEEP KEY2: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;//éèÖÃ×ÖìåÎaà¶é«
LCD_ShowString(160,30,200,16,16,"CPU: %");
LCD_ShowString(160,50,200,16,16,"SEM:000");
}
int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init (168);
uart_init(115200);
LED_Init ();
KEY_Init ();
BEEP_Init ();
LCD_Init ();
tp_dev.init ();
ucos_load_main_ui ();
OSInit();
OSTaskCreate (start_task ,(void*)0, (OS_STK*)&START_TASK_STK[START_STK_SIZE], START_TASK_PRIO);
OSStart();
}
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)// draw lines from outside
{
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--;
}
// draw lines from inside (center)
gui_draw_hline(x0-x,y0+i,2*x,color);
gui_draw_hline(x0-x,y0-i,2*x,color);
}
}
u16 my_abs(u16 x1, u16 x2)
{
if(x1>x2)
return x1-x2;
else
return x2-x1;
}
void lcd_draw_bline(u16 x1, u16 y1, u16 x2, u16 y2,u8 size,u16 color)
{
u16 t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
if(x1<size|| x2<size||y1<size|| y2<size)return;
delta_x=x2-x1; //¼ÆËã×ø±êÔöá¿
delta_y=y2-y1;
uRow=x1;
uCol=y1;
if(delta_x>0)incx=1; //éèÖÃμ¥2½·½Ïò
else if(delta_x==0)incx=0;//′1Ö±Ïß
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if(delta_y==0)incy=0;//ˮƽÏß
else{incy=-1;delta_y=-delta_y;}
if( delta_x>delta_y)distance=delta_x; //Ñ¡è¡»ù±¾Ôöá¿×ø±êÖá
else distance=delta_y;
for(t=0;t<=distance+1;t++ )//»-Ïßêä3ö
{
gui_fill_circle(uRow,uCol,size,color);//»-μã
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
void start_task(void* pdata)
{
OS_CPU_SR cpu_sr = 0;
pdata = pdata;
msg_key = OSMboxCreate ((void*)0);
sem_beep = OSSemCreate(0);
OS_ENTER_CRITICAL();
OSTaskCreate(touch_task,(void*)0,(OS_STK*)&TOUCH_TASK_STK[TOUCH_STK_SIZE], TOUCH_TASK_PRIO);
OSTaskCreate (printf_task ,(void*)0, (OS_STK*)&
RINTF_TASK_STK[PRINTF_STK_SIZE-1], PRINTF_TASK_PRIO);
OSTaskCreate (led0_task , (void*)0, (OS_STK*)&LED0_TASK_STK[LED0_STK_SIZE-1], LED0_TASK_PRIO);
OSTaskCreate (led1_task , (void*)0, (OS_STK*)&LED1_TASK_STK[LED1_STK_SIZE-1], LED1_TASK_PRIO);
OSTaskCreate (key_task ,(void*)0, (OS_STK *)&KEY_TASK_STK[KEY_STK_SIZE-1], KEY_TASK_PRIO);
OSTaskCreate(beep_task,(void *)0,(OS_STK*)&BEEP_TASK_STK[BEEP_STK_SIZE-1],BEEP_TASK_PRIO);
OSTaskCreate (main_task , (void*)0,(OS_STK*)&MAIN_TASK_STK [MAIN_STK_SIZE ],MAIN_TASK_PRIO);
OSTaskSuspend (START_TASK_PRIO);
OS_EXIT_CRITICAL();
}
void led1_task(void* pdata)
{
while(1)
{
LED1 = 1;
delay_ms(300);
}
}
void led0_task(void* pdata)
{
while(1)
{
LED0 = 0;
delay_ms (300);
LED0 =1 ;
delay_ms(300);
}
}
void printf_task(void* pdata)
{
while(1)
{
//OSSchedLock();
printf("Task is created successfully!
");
//OSSchedUnlock();
delay_ms(300);
}
}
void key_task(void* pdata)
{
u8 key;
while(1)
{
key = KEY_Scan (0);
if(key)
OSMboxPost(msg_key , (void*)key);
delay_ms (10);
}
}
void beep_task(void *pdata)
{
u8 err;
while(1)
{
OSSemPend(sem_beep , 0 , &err); //ÇëÇóDÅoÅá¿sem_beep,μè′yê±¼äÎaÎTÏT3¤¡£
BEEP=1;
delay_ms(60);
BEEP=0;
delay_ms(940);
}
}
void touch_task(void*pdata)
{
u32 cpu_sr;
u16 lastpos[2];
while(1)
{
tp_dev.scan(0);
if(tp_dev.sta&TP_PRES_DOWN) //′¥ÃtÆá±»°′ÏÂ
{
if(tp_dev.x[0]<lcddev.width&&tp_dev.y[0]<lcddev.height&&tp_dev.y[0]>120)
{
if(lastpos[0]==0XFFFF)
{
lastpos[0]=tp_dev.x[0];
lastpos[1]=tp_dev.y[0];
}
OS_ENTER_CRITICAL();//½øèëáù½ç¶Î,·àÖ1ÆäËûèÎÎñ,′ò¶ÏLCD2ù×÷,μ¼ÖÂòo¾§ÂòDò.
lcd_draw_bline(lastpos[0],lastpos[1],tp_dev.x[0],tp_dev.y[0],2,RED);//»-Ïß
OS_EXIT_CRITICAL();
lastpos[0]=tp_dev.x[0];
lastpos[1]=tp_dev.y[0];
}
}else
{
lastpos[0]=0XFFFF;
delay_ms(10); //ûóD°′¼ü°′ÏÂμÄê±oò
}
}
}
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 1:
LED1 = !LED1 ;
break;
case 2:
semmask =1;
OSSemPost(sem_beep);
break ;
case 3:
LCD_Fill (0,121,lcddev.width,lcddev.height,WHITE);
break ;
case 4:
OSTaskSuspend(TOUCH_TASK_PRIO); //1òÆe′¥ÃtÆáèÎÎñ
if((tp_dev.touchtype & 0X80) ==0)
TP_Adjust();
OSTaskResume(TOUCH_TASK_PRIO); //½a1ò
ucos_load_main_ui(); //ÖØD¼óÔØÖ÷½çÃæ
break;
}
if(semmask||sem_beep->OSEventCnt)//DèòaÏÔê¾sem
{
POINT_COLOR=BLUE;
LCD_ShowxNum(192,50,sem_beep->OSEventCnt,3,16,0X80);//ÏÔê¾DÅoÅá¿μÄÖμ
if(sem_beep->OSEventCnt==0)semmask=0; //í£Ö1¸üDÂ
}
if(tcnt==50)//0.5Ãë¸üDÂò»′ÎCPUê1óÃÂê
{
tcnt=0;
POINT_COLOR=BLUE;
LCD_ShowxNum(192,30,OSCPUUsage,3,16,0); //ÏÔê¾CPUê1óÃÂê
}
tcnt++;
delay_ms(10);
}
}
OSSemPend(sem_beep , 0 , &err); //请求信号量,等待时间无限,没请求成功一次,计数值减一。
一周热门 更多>