#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Serial.h"
#include "FreeRTOS.h"
#include "task.h"
#include <stdlib.h>
#include "PWM.h"
#include "pin.h"


void task_1_function(void *argument);
void task_2_function(void *argument);
void task_3_function(void *argument);
void task_4_function(void *argument);
//int rxDate=4568;
uint8_t gnumber=0;
uint16_t gturn=15;
uint16_t gformad=0;
uint16_t gback=0;
uint8_t gmode=0;
uint8_t gtype=0;

int gcount=0;

int main(void)
{
	/*模块初始化*/
	Serial_Init();		//串口初始化
	PWM_Init();     //PWM初始化
	pin_Init();
	//GPIO_ResetBits(GPIOB, GPIO_Pin_10);
	xTaskCreate(task_1_function, "Task 1", 128, NULL, 2, NULL);
	xTaskCreate(task_2_function, "Task 2", 128, NULL, 2, NULL);
	xTaskCreate(task_3_function, "Task 3", 128, NULL,1, NULL);
	xTaskCreate(task_4_function, "Task 4", 128, NULL,2, NULL);
	
	vTaskStartScheduler();
	//设置发送数据包数组的初始值，用于测试
	
	 
	while (1)
	{
		
	}
	
}

void task_3_function(void *argument)
{
	while(1)
	{
		
		 if (Serial_GetRxFlag() == 1)
		{
			gcount=0;
			gmode=Serial_RxPacket[0];
			gtype=Serial_RxPacket[1];
			gnumber=Serial_RxPacket[2];
			
		//Serial_SendString(Serial_RxPacket);
			if(gmode==0x01)
			{
				//USART1_SendNumber(Serial_RxPacket[0]);
				//gnumber=atoi(Serial_RxPacket[2]);
				
				if(gtype==0x01)
				{
					//Serial_SendString("a");
					//gspeedcount=0;
					gturn=gnumber;
				}
				if(gtype==0x02)
				{
					//gspeedcount=0;
					gformad=gnumber;
					if(100>gformad&&gformad>0)
					{
						gback=0;
					}
			
				}
				if(gtype==0x03)
				{
					//gspeedcount=0;
					gback=gnumber;
					if(100>gback&&gback>0)
					{
						gformad=0;
					}
				}
			
			}//USART1_SendNumber(gnumber);
		
			
			if(gmode==0x02)
			{
				pinBSet(gtype,gnumber);
			}
			gmode=0;
			gtype=0;
			//gnumber=0;
		}
		vTaskDelay(pdMS_TO_TICKS(2));
	}	
}


void task_1_function(void *argument)
{
	//(void)argument;
	while(1)
	{
		
		 PWM_SetCompare1(gturn);
		if(gcount>500)
		{
			gturn=15;
			
		}
		//vTaskDelay(4); 
		vTaskDelay(pdMS_TO_TICKS(3));
	}
	
		
}
void task_2_function(void *argument)
{
	//(void)argument;
	while(1)
	{
		
		
	PWM_SetCompare2(gformad);
	PWM_SetCompare3(gback);
		if(gcount>500)
		{
			gformad=100;
			gback=100;
			gcount=301;
		}
	//vTaskDelay(4);
	vTaskDelay(pdMS_TO_TICKS(5));	
	}	
	
}

void task_4_function(void *argument)
{
	while(1)
	{
		gcount++;
		int readpin=Read_Apin();
			if(readpin==1)
			{
			Serial_SendByte(0xAA);
			Serial_SendByte(0x01);
			Serial_SendByte(0xFE);
//			Serial_SendString("A4:hit");
				readpin=0;
			}
			if(readpin==2)
			{
			Serial_SendByte(0xAA);
			Serial_SendByte(0x02);
			Serial_SendByte(0xFE);
//			Serial_SendString("A5:hit");
			readpin=0;
			}
			if(readpin==3)
			{
			Serial_SendByte(0xAA);
			Serial_SendByte(0x03);
			Serial_SendByte(0xFE);
			//Serial_SendString("A6:hit");	
			readpin=0;	
			}
			if(readpin==4)
			{
			Serial_SendByte(0xAA);
			Serial_SendByte(0x04);
			Serial_SendByte(0xFE);
			//Serial_SendString("A7:hit");	
			readpin=0;	
			}
			
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
}
