
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include "stdint.h"
#include "RS232_komunikace.h"
#include "My_usart.h"			// Driver pro praci se seriovou linkou
#include "My_PWM.h"				// Driver pro rizeni PWM vystupu -> ovladani jasu v jednotlivych LED kanalech
#include "My_rtc.h"				// Driver interniho RTC
#include "PIN_config.h"
#include "server.h"

// Externi promenne
extern uint16_t PWM_max[5];
extern uint8_t AutomatickyRezim;
extern uint8_t ServerEnable;

// Globalni promenne
uint8_t PrikazovyBuffer[3] = {0x00,0x00,0x00};
uint8_t PrikazIndex = 0;

/* ------------------------------------------
 Funkce zkontroluje jestli neprisly nejake 
 data na seriovou linku a  jestli se nejedna 
 o platne prikazy
 ----------------------------------------- */
void Check_RS232(void)
{
	if(USART1_ReadIf(&PrikazovyBuffer[PrikazIndex]))
	{
		PrikazIndex++;
	}
	else
	{
		return;
	}
	if((PrikazovyBuffer[0]!='A')&(PrikazovyBuffer[0]!='B')&(PrikazovyBuffer[0]!='C')&
	   (PrikazovyBuffer[0]!='L')&(PrikazovyBuffer[0]!='P')&(PrikazovyBuffer[0]!='S'))
	{
		PrikazIndex=0;								// Zajisti, aby i v pripade prijeti kombinace nesmyslnych dat 		
		return;										// a platnych prikazu doslo k spravnemu dekodovani
	}
	if(PrikazIndex==2)								// Overeni jestli byly prijaty vsechny byte 2 bajtoveho prikazu
	{
		switch(PrikazovyBuffer[0])
		{			
			case 'A':								// Odeslani nastaveneho vykonu LED
			{
				if((PrikazovyBuffer[1]>0)|(PrikazovyBuffer[1]<=6))
				{
					USART1_Write(PrikazovyBuffer[0]);
					switch(PrikazovyBuffer[1])
					{
						case 1:
						{							
							USART1_Write(((uint32_t)PWM_Get_LED_1()*100)/PWM_max[0]);
							PEZO_BEEP();
							break;
						}
						case 2:
						{						
							USART1_Write(((uint32_t)PWM_Get_LED_2()*100)/PWM_max[1]);
							PEZO_BEEP();
							break;
						}
						case 3:
						{						
							USART1_Write(((uint32_t)PWM_Get_LED_3()*100)/PWM_max[2]);
							PEZO_BEEP();
							break;
						}
						case 4:
						{						
							USART1_Write(((uint32_t)PWM_Get_LED_4()*100)/PWM_max[3]);
							PEZO_BEEP();
							break;
						}
						case 5:
						{
							USART1_Write(((uint32_t)PWM_Get_LED_5()*100)/PWM_max[4]);
							PEZO_BEEP();
							break;
						}
						case 6:
						{							
							if(PWM_Get_LED6())
							{
								USART1_Write(100);
							}
							else
							{
								USART1_Write(0);
							}
							PEZO_BEEP();
							break;
						}
					}
				}
				PrikazIndex=0;
				break;
			}
			case 'B':								// Odeslani rezimu planovace LED
			{
				if((PrikazovyBuffer[1]>0)|(PrikazovyBuffer[1]<=6))
				{
					USART1_Write(PrikazovyBuffer[0]);
					USART1_Write((AutomatickyRezim&(1<<PrikazovyBuffer[1]))>>PrikazovyBuffer[1]);
					PEZO_BEEP();
				}
				PrikazIndex=0;
				break;
			}
			case 'C':								// Odeslani stavu jednotlivych casti etherentoveho serveru
			{
				if((PrikazovyBuffer[1]>0)|(PrikazovyBuffer[1]<=3))
				{
					USART1_Write(PrikazovyBuffer[0]);
					switch(PrikazovyBuffer[1])
					{
						case 1:
						{
							if(ServerEnable&(1<<SERVER_ENABLE_WWW))
							{
								USART1_Write(1);
							}
							else
							{
								USART1_Write(0);
							}
							PEZO_BEEP();
							break;
						}
						case 2:
						{
							if(ServerEnable&(1<<SERVER_ENABLE_UDP))
							{
								USART1_Write(1);
							}
							else
							{
								USART1_Write(0);
							}							
							PEZO_BEEP();
							break;
						}
						case 3:
						{
							if(ServerEnable&(1<<SERVER_ENABLE_PING))
							{
								USART1_Write(1);
							}
							else
							{
								USART1_Write(0);
							}							
							PEZO_BEEP();
							break;
						}
					}				
				}
				PrikazIndex=0;
				break;
			}			
		}
	}
	else if(PrikazIndex>2)							// Overeni jestli byly prijaty vsechny byte 3 bajtoveho prikazu
	{
		switch(PrikazovyBuffer[0])
		{
			case 'L':								// Nastaveni vykonu LED
			{
				if((PrikazovyBuffer[1]>0)&(PrikazovyBuffer[1]<=6))
				{
					if(PrikazovyBuffer[2]<=100)
					{
						switch(PrikazovyBuffer[1])
						{
							case 1:
							{								
								PWM_Set_LED_1(((uint32_t)PWM_max[0] * (uint32_t)PrikazovyBuffer[2])/100);
								PEZO_BEEP();
								break;
							}
							case 2:
							{								
								PWM_Set_LED_2(((uint32_t)PWM_max[1] * (uint32_t)PrikazovyBuffer[2])/100);
								PEZO_BEEP();
								break;
							}
							case 3:
							{								
								PWM_Set_LED_3(((uint32_t)PWM_max[2] * (uint32_t)PrikazovyBuffer[2])/100);
								PEZO_BEEP();
								break;
							}
							case 4:
							{								
								PWM_Set_LED_4(((uint32_t)PWM_max[3] * (uint32_t)PrikazovyBuffer[2])/100);
								PEZO_BEEP();
								break;
							}
							case 5:
							{								
								PWM_Set_LED_5(((uint32_t)PWM_max[4] * (uint32_t)PrikazovyBuffer[2])/100);
								PEZO_BEEP();
								break;
							}
							case 6:
							{								
								if(PrikazovyBuffer[2]==0)
								{
									PWM_Set_LED_6_OFF();
									PEZO_BEEP();
								}
								else if(PrikazovyBuffer[2]==100)
								{
									PWM_Set_LED_6_ON();
									PEZO_BEEP();
								}
								break;
							}
						}
					}
				}
				PrikazIndex=0;
				break;
			}
			case 'P':								// Nastaveni rezimu planovace pro LED
			{
				if((PrikazovyBuffer[1]>0)&(PrikazovyBuffer[1]<=6))
				{							
					if(PrikazovyBuffer[2]==1)							// Manualni rezim
					{
						AutomatickyRezim&=~(1<<PrikazovyBuffer[1]);		// Deaktivace automatickeho nastavovani pomoci planovace
						PEZO_BEEP();
					}
					else if(PrikazovyBuffer[2]==0)						// Automaticky rezim
					{
						AutomatickyRezim|=(1<<PrikazovyBuffer[1]);		// Aktivace automatickeho nastavovani pomoci planovace
						PEZO_BEEP();
					}
				}
				PrikazIndex=0;
				break;
			}
			case 'S':								// Povolovani/zakazovani jednotlivych casti serveru
			{
				if((PrikazovyBuffer[1]>0)&(PrikazovyBuffer[1]<=3))
				{					
					switch(PrikazovyBuffer[1])
					{
						case 1:
						{								
							if(PrikazovyBuffer[2]==0)
							{
								ServerEnable&=~(1<<SERVER_ENABLE_WWW);
								PEZO_BEEP();
							}
							else if(PrikazovyBuffer[2]==1)
							{
								ServerEnable|=(1<<SERVER_ENABLE_WWW);
								PEZO_BEEP();
							}
							break;
						}
						case 2:
						{								
							if(PrikazovyBuffer[2]==0)
							{
								ServerEnable&=~(1<<SERVER_ENABLE_UDP);
								PEZO_BEEP();
							}
							else if(PrikazovyBuffer[2]==1)
							{
								ServerEnable|=(1<<SERVER_ENABLE_UDP);
								PEZO_BEEP();
							}
							break;
						}
						case 3:
						{								
							if(PrikazovyBuffer[2]==0)
							{
								ServerEnable&=~(1<<SERVER_ENABLE_PING);
								PEZO_BEEP();
							}
							else if(PrikazovyBuffer[2]==1)
							{
								ServerEnable|=(1<<SERVER_ENABLE_PING);
								PEZO_BEEP();
							}
							break;
						}						
					}					
				}
				PrikazIndex=0;
				break;
			}						
		}
	}
	
}

/* Konec souboru */
