.

Dependencies:   mbed tsi_sensor MMA8451Q

main.cpp

Committer:
Naza00
Date:
2020-05-12
Revision:
1:57b0bfa26afb
Parent:
0:6c94c5ac5e8c

File content as of revision 1:57b0bfa26afb:

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

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

#define X 0
#define Y 1
#define Z 2
#define V 3
#define S 4

#define DESHABILITADO 0
#define HABILITADO 1

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

#define DES_ON 0
#define DES_OFF 1

#define ON 0
#define OFF 1

/* 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


AnalogIn voltaje_entrada(PTC1);
DigitalIn entrada_digital(PTC7);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led(LED3);

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

Ticker tick;

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

int estado=ESPERA;
float x,y,z;
int valor_x; //
int valor_y; // VALORES BIEN DEL ACELEROMETRO
int valor_z; //
unsigned int valor_adc;
float in_analog; //VALOR BIEN DE LA ENTRADA ANALOGICA
int valor_dig=0; // VALOR BIEN DE LA ENTRADA DIGITAL
int valor_analog;
char vector_respuesta[10]= {0};
int decena_valor_medido=0,unidad_valor_medido=0;
unsigned char valor_lrc=0;
int orden=0,a=0,j=0;
int tiempo_des=0,tiempo_hab=0,tiempo_rebote=0;
char aux=0;
int habilitacion=0;
char estado_des = DES_ON;
char estado_hab = HAB_ON,cont=1;

int main() {
    
    tick.attach(&timer, 0.001);
    
    TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
    
    char estado_hab = DESHABILITADO;  
    
    //Apagamos los leds
    led1 = 1;
    led2 = 1;
    led= 1;
    
    //Ejecutar onCharReceived por cada entrada por puerto
    pc.attach(&onCharReceived);
    
    while(true){
        switch(estado_hab){
            case DESHABILITADO:
                habilitacion=0;
                led_deshabilitado(); 
                if(tsi.readPercentage()!=0 && aux==0){
                    tiempo_rebote=0; 
                    aux=1;
                }
                if(tiempo_rebote>=500 && aux==1){
                    aux=0;
                    estado_hab = HABILITADO;  
                }
            break;
            
            case HABILITADO:
                habilitacion=1;
                led_habilitado();
                if(tsi.readPercentage()!=0 && aux==0){
                    tiempo_rebote=0; 
                    aux=1;
                }
                if(tiempo_rebote>=500 && aux==1){
                    aux=0;
                    estado_hab = DESHABILITADO;  
                }
            break;          
        }
        
        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]=valor_x/10;
                            vector_respuesta[3]=valor_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]=valor_y/10;
                            vector_respuesta[3]=valor_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]=valor_z/10;
                            vector_respuesta[3]=valor_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();
                            wait_ms(250);
                            
                            if(c=='%'){
                                estado=FIN;    
                            }
                        
                            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();
                            wait_ms(250);
                            
                            if(c=='%'){
                                estado=FIN;    
                            }
                            
                            c = '\0';
                        break; 
                    
                        case '@':
                        break;   
                    }
                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
    } //while
} //main


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

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

int acelerometro_z(){
    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    z = abs(acc.getAccZ());
    wait(0.1f);
    valor_z=z*100;
    return valor_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;                
}

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

void led_deshabilitado(){
    switch(estado_des){
        case DES_ON:
            led=ON;
            if(tiempo_des==250){
                tiempo_des=0;
                estado_des=DES_OFF;    
            }
        break;  
        
        case DES_OFF:
            led=OFF;
            if(tiempo_des==250){
                tiempo_des=0;
                estado_des=DES_ON;    
            }
        break;   
    }    
}

void led_habilitado(){
    switch(estado_hab){
        case HAB_ON:
            led=ON;
            if(tiempo_hab>=150 && cont<=2){
                cont++;
                tiempo_hab=0;
                estado_hab = HAB_OFF_1;       
            }   
            if(tiempo_hab>=150 && cont==3){
                tiempo_hab=0;
                estado_hab = HAB_OFF_2;    
            } 
        break;
        
        case HAB_OFF_1:
            led=OFF;
            if(tiempo_hab==500){
                tiempo_hab=0;
                estado_hab = HAB_ON;    
            }
        break;
        
        case HAB_OFF_2:
            led=OFF;
            if(tiempo_hab==1000){
                cont=0;
                tiempo_hab=0;
                estado_hab = HAB_ON;    
            }
        break;  
        }
}

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