Tarea 6 Procesadores_Infrarojo Ernesto Jose Guevara Sequeda Juan Salvador Payares Iriarte

Dependencies:   DebouncedIn Pulse1 TextLCD mbed

Fork of irda_V_aula by Gustavo Ramirez

main.cpp

Committer:
payarito32
Date:
2014-05-16
Revision:
4:8c523cea7a26
Parent:
3:82bebaf2a06a

File content as of revision 4:8c523cea7a26:

#include "mbed.h"
#include <Pulse1.h>
#include "TextLCD.h"
#include "math.h"
#include "stdio.h"
#include "DebouncedIn.h"

//control remoto videobeam aula
PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo
Serial pc(USBTX, USBRX);
//DigitalOut led(LED1);
//DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
DigitalOut led5(LED1);
bool encendido1; //Banderas de indicacin led encendido
bool encendido2;
bool encendido3;
int decimal;
int i; 
int c2=3;
float  p;
float c;
int potencia=1;
int header =0; //tiempo de cabecera pulso abajo
const int head_H = 4500+450; //+10% medida con osciloscopio en microsegundos
const int head_L = 4500-450;//-10%  medida con osciloscopio

const int T_alto=1600;//ponga su tiempo de la prueba
const int T_bajo=560;//ponga su tiempo de la prueba
const int num_bits = 64;//ponga su numero de bits
int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos
int dato; // tiempo de cada dato que se lee
TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); 
int dec[64];
int j;
int bit1;
int bit0;
//entradas
int t=0;

DebouncedIn p1(PTC3);
DebouncedIn p2(PTC4);
DebouncedIn p3(PTC5);

int main(){

lcd.printf("COMENZAR");
lcd.locate(0,0);
    while(1){
            
            ini1: fflush( stdin );
                  header=0;
                  
                 // led=1;
                  header = irda.read_low_us();    //funcion para leer un pulso de caida o bajo en header
                  if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20%
                  else goto ini1;
                  
            seguir:
                  lcd.cls();
                  decimal = 0; 
                  
                   //leo los datos de la trama y se meten a un arreglo
                  wait_us(4500);// ES EL TIEMPO DE HEADER QUE NO SE Lee O EL ALTO 
                 // led2=0;
                  
                  
                  for(i=0;i<(num_bits-1);++i){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
                  dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control
                  num[i]=dato;
                  wait_us(300);   
                  }
                  
                  
                  wait(0.5); //espero un poquito luego de leer todo el areglo y ponerlo en pantalla 
                  pc.printf(",%d",header);
                  
                  for(i=0;i<(num_bits-1);++i){  
                  pc.printf(",%d",num[i]);
                  }
                  wait(0.1);  //espero e imprimo en binario 
                  pc.printf("\n\n");
                  
                  lcd.locate(0,0);
                  for(i=42;i<(num_bits-1);++i){  
                    
                          if(num[i] > ((T_alto+T_bajo)/2)){
                               //lcd.printf("%d", 1);
                               dec[i]=1;
                               bit1 = num[i];
                               //potencia de 2
                               
                                  
                               }else{
                               //     lcd.printf("%d", 0);
                                    dec[i] = 0;
                                    bit0 = num[i];
                                    }
                                       
                             
                             // lcd.printf("Bits Fin: %d %d",dec[i-1],dec[i-2]);
                                    
                          }//cierro for
                          
                          //Utilizo
                          c=0;
                          for(i=42; i<=63;++i){
                              if(dec[i] == 1){
                                  p = c; 
                                  float potencia = 1;
                               
                                 for(j = 1; j <= p; ++j){
                                     potencia = potencia*2;
                                     }
                                    if(p ==0){
                                        potencia=1;
                                        }
                               decimal = decimal + potencia;   
                               }//fin if
                               c++;
                            }//Fin for
                            
                          //lcd.printf("%10d", decimal);
                          
                          if(decimal == 1933827){
                              if(encendido2 == false && encendido3 == false){
                              
                              
                              c2= c2+1;
                              
                                      if(c2 % 2 != 0){
                                      led3=0;
                                      lcd.printf("Tecla 1 ON"); 
                                      encendido1 = true;
                                      }else{
                                          led3=1;
                                          encendido1 = false; 
                                          lcd.printf("Tecla 1 OFF"); 
                                          }//Fin else C2 %2 =0
                                      
                              }else{
                                lcd.cls(); 
                                lcd.printf("Boton Incorrecto!"); 
                                  }//cierro else
                              
                              ++t;
                                if(t==1){
                                lcd.locate(0,1);
                                lcd.printf("Cabecera H: %d ", header);
                                }
                                
                                if(t==2){
                                lcd.locate(0,1);
                                lcd.printf("T Alto: %d ", bit1);
                                
                                }
                                if(t==3){
                                lcd.locate(0,1);
                                lcd.printf("T Bajo: %d ", bit0);
                                    t=0;
                                    }
                              }//cierro if decimal 1
                          
                          if(decimal == 1607427){
                                  if(encendido1 ==  false && encendido3 == false){
                                      
                                      c2= c2+1;
                                      encendido2= true;
                                          if(c2 % 2 != 0){
                                        lcd.printf("Tecla 9 ON"); 
                                          led4=0;
                                          }else{
                                              led4=1;
                                              encendido2 = false; 
                                              lcd.printf("Tecla 9 OFF"); 
                                              }
                                    }else{
                                          lcd.cls(); 
                                          lcd.printf("Boton Incorrecto!"); 
                                            }
                                            
                                            
                              
                              ++t;
                                if(t==1){
                                lcd.locate(0,1);
                                lcd.printf("Cabecera H: %d ", header);
                                }
                                
                                if(t==2){
                                lcd.locate(0,1);
                                lcd.printf("T Alto: %d ", bit1);
                                
                                }
                                if(t==3){
                                lcd.locate(0,1);
                                lcd.printf("T Bajo: %d ", bit0);
                                    t=0;
                                    }
                                    }// fin decimal 9
                                      
                                      
                          if(decimal == 1640067){
                              if(encendido1 ==  false && encendido2 == false){
                              
                              c2= c2+1;
                              encendido3 = true;
                                  if(c2 % 2 != 0){
                                  led5=0;
                                 lcd.printf("Tecla 8 ON"); 
                                  }else{
                                      led5=1;
                                      encendido3 = false;
                                    lcd.printf("Tecla 8 OFF"); 
                                      }
                              
                              }else{
                              lcd.cls(); 
                              lcd.printf("Boton Incorrecto!"); 
                              
                              }
                              
                              
                              ++t;
                                if(t==1){
                                lcd.locate(0,1);
                                lcd.printf("Cabecera H: %d ", header);
                                }
                                
                                if(t==2){
                                lcd.locate(0,1);
                                lcd.printf("T Alto: %d ", bit1);
                                
                                }
                                if(t==3){
                                lcd.locate(0,1);
                                lcd.printf("T Bajo: %d ", bit0);
                                    t=0;
                                    }
                              }//cierro if decimal 8
                                                        
                                                
                                
                  
                              }//cierro while
                            
                                    
                                  if(p1.falling()){
                                ++t;
                                if(t==1){
                                lcd.cls();
                                
                                }//T1
                              
                                if(t==2){
                                lcd.cls();
                                lcd.locate(0,0);
                                lcd.printf("T_alto: %d ", bit1);
                                lcd.locate(0,1);
                                lcd.printf("T_bajo: %d ", bit0);
                                t=0; 
                                }//T1
                                                   
                                                   
                                                   }
                                                                                      
                                
                                if(p2.falling()){
                                lcd.cls();
                                
                                lcd.printf("%d  %d", t, num[t]);
                                lcd.locate(0,1);
                                lcd.printf("%d  %d", t+1, num[t+1]);
                                }
                                                   
                                
                              //Fin pulsadores
                                  
                          
      
      
      }