
/**
* ===================================================================================================
 *
 *       @file  RS232Comm.c
 *      @brief  Este é o programa filho que estabelece a comunicação RS232   
 *
 * ===================================================================================================
 */

#ifndef RS232COMM_C
#define RS232COMM_C


#include "header.h"
#include "RS232Comm.h"

int RS232Comunication(void)
{	
	int n, n_env, Comunic_Estado=0, pid_p, rc_type;
	char msg_rc[20];
	unsigned char  receive, high, low, Nmsg;
	unsigned char NumId, data_env, str_env[2];  
    
	char NameKeyRec[20]="main.c";
	char NameKeyEnv[20]="RS232Comm.c";
	char NameKey_robEnv[20]="RobCommFunc.c";     
	   
	//Criacao das ShareMemories
	shm_rec = GetSharedMem(NameKeyRec, &shm_id_rec);	
	shm_env = GetSharedMem(NameKeyEnv, &shm_id_env);	
	shm_robEnv = GetSharedMem(NameKey_robEnv, &shm_id_robEnv);
	
	rc_type = 16;
	shm_env->type=16;	
	int count_exp=0;
	
	pid_p=getppid();
	signal(SIGINT,Kill_RS232Comm);  
	signal(SIGUSR1,kill_filho_quit);  	

	do
	{	
		EstadoLigacao(shm_env, pid_p, &Comunic_Estado);
				
		if((Comunic_Estado==0) | (Comunic_Estado==2))
		{
		    continue;
		}
		
		n=read(fd,msg_rc,1);
		if(n<0) 
		{
 			perror("filho");
		}
		if(n>0) 
		{
			if (msg_rc[0] == '\n')
				continue;
			receive=msg_rc[0];
			Nmsg=(receive >> 7);			
// 			printf("Valor recebido: %hhx\n",msg_rc[0]);
						
			 if ((Nmsg == 0) & (rc_type == 16))
			{
				high = ((receive & 0x78) >> 3);
				low = (receive & 0x07);
			
				if (high == ESTADO) 		
				{
					rc_type = ESTADO;
				}


				if (high == MOTORES) 			
				{
					rc_type = MOTORES;
				}


				if (high == DISTANCIA) 			
				{
					rc_type = DISTANCIA;
				}
				
				if (high == TEMPERATURA) 		
				{
					rc_type = TEMPERATURA;
				}
				
				if (high == GARRA) 				
				{
					rc_type = GARRA;
				}
				

			}
			else if ((Nmsg == 1) & (rc_type == ESTADO))
			{
				shm_env->command = OTHER;
				shm_rec->type = ESTADO;
				shm_rec->param1 = low;
				shm_rec->param2 = (msg_rc[0] & 0x7F);
			
				if(kill(pid_p , SIGUSR1)==-1)
						perror("sinal para o pai\n");	
				rc_type = 16;							
			}

				else if ((Nmsg == 1) & (rc_type == DISTANCIA))
			{
				shm_env->command = OTHER;
				shm_rec->type = DISTANCIA;
				shm_rec->param1 = low;
				shm_rec->param2 = (msg_rc[0] & 0x7F);
     
				if (shm_rec->param1==SENS2)
				{                               
// 				    unsigned int dist_int=(msg_rc[0] & 0x7F);
				    double dist_doub=(msg_rc[0] & 0x7F);
// 				    printf("Distancia: %lf\n",dist_doub); 
			    
				    VarrimentoSensor(&count_exp, dist_doub);				   

				}

				if(kill(pid_p , SIGUSR1)==-1)
						perror("sinal para o pai\n");	
				rc_type = 16;							
			}
			
			else if ((Nmsg == 1) & (rc_type == MOTORES))
			{
				shm_env->command = OTHER;
				shm_rec->type = MOTORES;
				shm_rec->param1 = low;
				shm_rec->param2 = (msg_rc[0] & 0x7F);
			
// 				printf("enviei para o pai inf motores\n");	
				if(kill(pid_p , SIGUSR1)==-1)
						perror("sinal para o pai\n");	
				rc_type = 16;							
			}
			
			else if ((Nmsg == 1) & (rc_type == TEMPERATURA))
			{
				shm_env->command = OTHER;
				shm_rec->type = TEMPERATURA;
				shm_rec->param1 = low;
				shm_rec->param2 = (msg_rc[0] & 0x7F);
			
// 				printf("enviei para o pai temperatura\n");	
				if(kill(pid_p , SIGUSR1)==-1)
						perror("sinal para o pai\n");	
				rc_type = 16;							
			}

			else if ((Nmsg == 1) & (rc_type == GARRA))
			{
				shm_env->command = OTHER;
				shm_rec->type = GARRA;
				shm_rec->param1 = low;
				shm_rec->param2 = (msg_rc[0] & 0x7F);
			
// 				printf("enviei estado da garra para o pai\n");	
				if(kill(pid_p , SIGUSR1)==-1)
						perror("sinal para o pai\n");	
				rc_type = 16;							
			}

		}
		
		/*Envio de dados para o PIC*/		
		if(shm_env->type <16)
		{
			NumId = (0 << 7 | shm_env->type << 3 | shm_env->param1);	
			data_env = (1 << 7 | shm_env->param2);
			
			str_env[0] = NumId;
			str_env[1] = data_env;			
 			n_env =	 write (fd, str_env, 2);
			
/*			printf("Enviei pela porta serie:\n" );
			printf("String 1 enviada: %hhx\n",str_env[0] );
			printf("String 2 enviada: %hhx\n",str_env[1] );	*/		
			shm_env->type = 16;		
		}
	}while(1);
	
	printf("Morre Filho\n");	
	return 0;
}

#endif

/*				    int GetCoordRobBegin;
				    double Dist_Array[1024], DataMedian[1024],Tempo_Seg[1024], CoordRobot[1024];		*/
					
// 				    DataConvert_mm(dist_doub,Dist_Array,count_exp);

// 				    printf("Valor convertido >> %d\t%lf\n",count_exp,Dist_Array[count_exp]);
// 				    count_exp++;
// 
// 				    if(count_exp==200)
// 				    {
// 					 					
// 
// 					DataMedianFunc(Dist_Array, DataMedian, count_exp);
// /*					printf("Fez a media\n");					
// 					for(i=0;i<(count_exp-8);i++)
// 					{
// 					    printf("media>>%d %lf\n",i,DataMedian[i]);					    
// 					}*/
// // 
// 					WriteFile(DataMedian,count_exp);				     				
// // 
// 					count_exp=0;
// 				    }
