/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usbd_hid_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "key.h"


#ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
  #if defined ( __ICCARM__ ) /*!< IAR Compiler */
    #pragma data_alignment = 4   
  #endif
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
__ALIGN_BEGIN USB_OTG_CORE_HANDLE  USB_OTG_dev __ALIGN_END;
  

uint8_t Buffer[65];
uint8_t MyBuffer[65]={1,2};


int main(void)
{
	  u16 i,j,key;
    u16 FirstCommand,SecondCommand,ThirdCommand;
	  u16 Length;
	
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
		delay_init(168);     //初始化延时函数
		uart_init(115200);	 //初始化串口波特率为115200
		LED_Init();					 //初始化LED  
		LCD_Init();					 //LCD初始化  
		KEY_Init();					 //按键初始化  
		//配置USB HID
		USBD_Init(&USB_OTG_dev,
							USB_OTG_FS_CORE_ID,
							&USR_desc, 
							&USBD_HID_cb, 
							&USR_cb);
		delay_ms(10);
		
	
		while(1)
		{
				DCD_EP_PrepareRx(&USB_OTG_dev,HID_OUT_EP,Buffer,64);//准备接收数据  PC端发送指令连续且小于64位
				if(UsbHidReceiveComplete==1)
				{
					UsbHidReceiveComplete=0;
					//USBD_HID_SendReport (&USB_OTG_dev, Buffer, 64);	
					Length=sizeof(Buffer);
					for(i=0;i<Length;i++)
					{
						 MyBuffer[i]=Buffer[i];
						 j=i;//第一个空白字符位置
						 if (Buffer[i]==' ')
		             break;
						 												  
					}
					
//					USBD_HID_SendReport (&USB_OTG_dev, MyBuffer, j);
//					USBD_HID_SendReport (&USB_OTG_dev,"\r\n", 64);
		
					
					if((Buffer[0]=='S')&(Buffer[1]=='S')&(Buffer[j-1]=='P')&(Buffer[j-2]=='P')&(j>=6))
					{
						 if (j>=6)
						    FirstCommand=(Buffer[2]-0x30)*10+(Buffer[3]-0x30);
						 if (j>=8)
							  SecondCommand=(Buffer[4]-0x30)*10+(Buffer[5]-0x30);
						 if (j>=10)	
  							ThirdCommand=(Buffer[6]-0x30)*10+(Buffer[7]-0x30);
					   switch(FirstCommand)
						 {
							 case 1:LED0=0;USBD_HID_SendReport (&USB_OTG_dev,"Led0 is on!\r", 64);
							        if (SecondCommand==1)
											{ LED1=0;USBD_HID_SendReport (&USB_OTG_dev,"Led1 is on!\r\n", 64);}
											if (SecondCommand==2)
											{LED1=1;USBD_HID_SendReport (&USB_OTG_dev,"Led1 is off!\r\n", 64);}
											break;	
						   case 2:LED0=1;USBD_HID_SendReport (&USB_OTG_dev,"Led0 is off!\r", 64);
											if (SecondCommand==1)
											  {LED1=0;USBD_HID_SendReport (&USB_OTG_dev,"Led1 is on!\r\n", 64);}
											if (SecondCommand==2)
											  {LED1=1;USBD_HID_SendReport (&USB_OTG_dev,"Led1 is off!\r\n", 64);}		
											break;
						   default:USBD_HID_SendReport (&USB_OTG_dev,"Command is out range!\r\n", 64);break;
						 }					
					
					}
					else
						USBD_HID_SendReport (&USB_OTG_dev,"Command Form is Wrong!\r\n", 64);											
				}
			
				key=KEY_Scan(0);
		    if(key)
				{
				 switch(key)
				 {
					 case KEY0_PRES:USBD_HID_SendReport (&USB_OTG_dev,"KEY0 is press!\r\n", 64);;break;
				   case KEY1_PRES:USBD_HID_SendReport (&USB_OTG_dev,"KEY1 is press!\r\n", 64);;break;
				   case KEY2_PRES:USBD_HID_SendReport (&USB_OTG_dev,"KEY2 is press!\r\n", 64);;break;
				   case WKUP_PRES:USBD_HID_SendReport (&USB_OTG_dev,"WKUP is press!\r\n", 64);;break;
				 }				
				}
				
				delay_us(1000);				
   }
  
}







/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
