h

Dependencies:   mbed tsi_sensor MMA8451Q

main.cpp

Committer:
Naza00
Date:
2020-06-23
Revision:
0:72263a224c3f

File content as of revision 0:72263a224c3f:

#include "mbed.h"
#include "tsi_sensor.h"
#include "MMA8451Q.h"

#define DESHABILITADO 0
#define HABILITADO 1

#define HAB_1 0
#define HAB_2 1
#define HAB_3 2

#define DES_ON 0
#define DES_OFF 1

#define HAB_ON 0
#define HAB_OFF_1 1
#define HAB_OFF_2 2

#define ON 0
#define OFF 1

#define ESPERA 0
#define ORDEN 1
#define FIN 2
#define AUXILIAR 3

AnalogIn voltaje_entrada(PTC1);
DigitalIn entrada_digital(PTC7);
DigitalOut led_habilitacion (PTB1);

DigitalOut led_rojo(LED1);
DigitalOut led_verde(LED2);
DigitalOut led_azul(LED3);

/* This defines will be replaced by PinNames soon */
#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
  #define ELEC0 9
  #define ELEC1 10
#elif defined (TARGET_KL05Z)
  #define ELEC0 9
  #define ELEC1 8
#else
  #error TARGET NOT DEFINED
#endif

#if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
  PinName const SDA = PTE25;
  PinName const SCL = PTE24;
#elif defined (TARGET_KL05Z)
  PinName const SDA = PTB4;
  PinName const SCL = PTB3;
#elif defined (TARGET_K20D50M)
  PinName const SDA = PTB1;
  PinName const SCL = PTB0;
#else
  #error TARGET NOT DEFINED
#endif
#define MMA8451_I2C_ADDRESS (0x1d<<1)

//Definimos que el puerto serie se llama pc
Serial pc(USBTX, USBRX);
//Variable donde se guarda lo leido
char   c = '\0';
//bit usado como flag para procesar datos
bool newdata = false;   //Se pone en true cuando hay nuevos datos

//Callback cuando se detecta una entrada
void onCharReceived(){
    //Copiamos lo leido en c
    c = pc.getc();
    newdata = true;
}


void timer();
void led_deshabilitado();
void led_habilitado();
int acelerometro_x();
int acelerometro_y();
int acelerometro_z();
int adc();
int in_digital();
char lrc();

Ticker tick;

int habilitacion=0;
char estado_en_hab = HAB_ON,cont=1,estado_hab=DESHABILITADO;
char aux=0,var=0;
int tiempo_des=0, tiempo_rebote=0,tiempo_hab=0,tiempo_espera=0;
char estado_des = DES_ON;
int estado=ESPERA;
char vector_respuesta[10]= {0};
int orden=0,a=0,j=0;
int  valor_lrc=0;
float x,y,z;
int valor_dig=0;
unsigned int valor_adc;
float in_analog;
int valor_analog;
char adc_decena=0,adc_unidad=0;

int main (){ 

    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    
    tick.attach(&timer, 0.001);
    
    TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
    
    char estado_hab = DESHABILITADO;

    led_rojo = 1;
    led_verde = 1;
    led_azul = 1;
    led_habilitacion = 0; 
    
    pc.attach(&onCharReceived);
    
    while(true){
        switch(estado_hab){
            
            default:
            case DESHABILITADO:
                habilitacion=0;
                led_deshabilitado();
                led_habilitacion = 0;
                //CUANDO HAGAS ADC FIJATE ESTO,ES LO DE MONITOREAR Y MOSTRAR EL ESTADO DE LA ENTRADA ANALOGICA 
                
                adc();
                
                if(valor_analog<=10){
                    led_rojo = 0;
                    led_verde = 1;
                    led_azul = 1;        
                }
                if(valor_analog>10 && adc_decena<=20){
                    led_rojo = 1;
                    led_verde = 0;
                    led_azul = 1;        
                }
                if(valor_analog>20){
                    led_rojo = 1;
                    led_verde = 1;
                    led_azul = 0;        
                }
                
                if(tsi.readPercentage()!=0 && aux==0){
                    tiempo_rebote=0; 
                    aux=1;
                    led_habilitacion = 0;
                }
                if(tiempo_rebote>=500 && aux==1){
                    aux=0;
                    estado_hab = HABILITADO;  
                }
            break;
            
            case HABILITADO:
                habilitacion=1;
                led_habilitado();
                
                adc();
                
                if(valor_analog<=10){
                    led_rojo = 0;
                    led_verde = 1;
                    led_azul = 1;        
                }
                if(valor_analog>10 && adc_decena<=20){
                    led_rojo = 1;
                    led_verde = 0;
                    led_azul = 1;        
                }
                if(valor_analog>20){
                    led_rojo = 1;
                    led_verde = 1;
                    led_azul = 0;        
                }
                
                if(tsi.readPercentage()!=0 && aux==0){
                    tiempo_rebote=0; 
                    aux=1;
                }
                if(tiempo_rebote>=500 && aux==1){
                    aux=0;
                    estado_hab = DESHABILITADO;  
                }
            break;          
        } //del switch
        
        if(newdata && habilitacion==1){
            newdata = false;
            
            switch(estado){
            
                case ESPERA:
                    if(c==':'){
                        estado=ORDEN;
                        vector_respuesta[0]=':'; 
                        c = '\0';   
                    }
                break;
                
                case ORDEN:
                    switch(c){
                        
                        default:
                            estado = ESPERA;
                        break;
                            
                        case 'X':                        
                            acelerometro_x();
                                                        
                            vector_respuesta[1]='X'; 
                            
                            vector_respuesta[2]=x/10;
                            vector_respuesta[3]=x - (vector_respuesta[2] * 10);
                            
                            a=vector_respuesta[2];
                            j=vector_respuesta[3];
                            orden='X';
                            
                            lrc(); 
                            
                            if(c=='#'){
                                estado=FIN;    
                            }
                                        
                            c = '\0';        
                        break; 
                        
                        case 'Y':
                            acelerometro_y();
                            
                            vector_respuesta[1]='Y'; 
                            
                            vector_respuesta[2]=y/10;
                            vector_respuesta[3]=y - (vector_respuesta[2] * 10);
                            
                            a=vector_respuesta[2];
                            j=vector_respuesta[3];
                            orden='Y';
                            
                            lrc();
                            
                            if(c=='#'){
                                estado=FIN;    
                            }
                                                        
                            c = '\0';
                        break; 
                        
                        case 'Z':
                            acelerometro_z();
                            
                            vector_respuesta[1]='Z'; 
                        
                            vector_respuesta[2]=z/10;
                            vector_respuesta[3]=z - (vector_respuesta[2] * 10);
                            
                            a=vector_respuesta[2];
                            j=vector_respuesta[3];
                            orden='Z';
                            
                            lrc();
                            
                            if(c=='#'){
                                estado=FIN;    
                            }
                            
                            c = '\0';
                        break; 
                        
                        case 'V':
                            adc();
                            
                            vector_respuesta[1]='V'; 
                            
                            vector_respuesta[2]=valor_analog/10;
                            vector_respuesta[3]=valor_analog-(vector_respuesta[2]*10);
                            
                            a=vector_respuesta[2];
                            j=vector_respuesta[3];
                            orden='V';
                            
                            lrc();
                            
                            tiempo_espera=0;
                            estado=AUXILIAR; 
                        
                            c = '\0';
                        break; 
                        
                        case 'S':
                            in_digital();
                            vector_respuesta[1]='S'; 
                            
                            vector_respuesta[2]=0;
                            vector_respuesta[3]=valor_dig;
                            
                            a=0;
                            j=vector_respuesta[3];
                            orden='S';
                            
                            lrc();
                            
                            tiempo_espera=0;
                            estado=AUXILIAR; 
                            
                            c = '\0';
                        break; 
                    
                        case ':':
                        break;  
                         
                    }
                break;  
                
                case AUXILIAR:
                    if(tiempo_espera>=250){
                        estado=FIN;    
                    }
                       
                break;
                
                case FIN:
                    vector_respuesta[4]='#';
                    printf("\r\n%c%c%d%d%x%c",vector_respuesta[0],vector_respuesta[1],vector_respuesta[2],vector_respuesta[3],valor_lrc,vector_respuesta[4]);
                    estado=ESPERA;
                    c = '\0';
                break;

            } //switch
        }// if newdata   
    } //del while siempre arriba del main  
} //del main


void timer(){
    tiempo_rebote++;
    tiempo_des++; 
    tiempo_hab++;  
    tiempo_espera++;         
}


void led_deshabilitado(){
    switch(estado_des){
        case DES_ON:
            led_habilitacion = 1;
            if(tiempo_des >= 250){
                tiempo_des=0;
                estado_des=DES_OFF;    
            }
        break;  
        
        case DES_OFF:
            led_habilitacion = 0;
            if(tiempo_des >=250){
                tiempo_des=0;
                estado_des=DES_ON;    
            }
        break;   
    }    
}


void led_habilitado(){
    switch(estado_en_hab){
        
        case HAB_ON:
        led_habilitacion = 1;
            led_habilitacion = 1;
            if(tiempo_hab>=100 && var==0){
                tiempo_hab=0;
                estado_en_hab = HAB_OFF_1;       
            }   
            if(tiempo_hab>=100 && var==1){
                tiempo_hab=0;
                estado_en_hab = HAB_OFF_2;    
            } 
        break;
        
        case HAB_OFF_1:
            led_habilitacion = 0;
            if(tiempo_hab>=100){
                tiempo_hab=0;
                var=1;
                estado_en_hab = HAB_ON;    
            }
        break;
        
        case HAB_OFF_2:
            led_habilitacion = 0;
            if(tiempo_hab>=2000){
                var=0;
                tiempo_hab=0;
                estado_en_hab = HAB_ON;    
            }          
        break;  
    }
}    


int acelerometro_x(){
    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    x = abs(acc.getAccX());
    x=x*100;
    return x;   
}

int acelerometro_y(){
    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    y = abs(acc.getAccY());
    y=y*100;
    return y;   
}

int acelerometro_z(){
    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    z = abs(acc.getAccZ());
    z=z*100;
    return z;   
}


int adc(){
    valor_adc = voltaje_entrada.read_u16(); 
    in_analog = valor_adc * (3.3f / 65535.0f); 
    valor_analog = in_analog*10;
    return valor_analog;      
}


int in_digital(){
    if(entrada_digital==1)valor_dig=1;
    else valor_dig=0;
    
    return valor_dig;                
}

char lrc(){ 
    int inicio= ':';
    valor_lrc=0;
    valor_lrc ^= inicio;
    valor_lrc ^= orden;
    valor_lrc ^= (a + 48);
    valor_lrc ^= (j + 48);
    
    return valor_lrc;  
}