Nazareno Amico / Mbed 2 deprecated AMICO_Maquinas_de_Estado

Dependencies:   mbed tsi_sensor MMA8451Q

Files at this revision

API Documentation at this revision

Comitter:
Naza00
Date:
Tue Jun 23 00:54:41 2020 +0000
Commit message:
h

Changed in this revision

MMA8451Q.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
tsi_sensor.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8451Q.lib	Tue Jun 23 00:54:41 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/virtua-pinnnn/code/MMA8451Q/#7af81c19174d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 23 00:54:41 2020 +0000
@@ -0,0 +1,452 @@
+#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;  
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jun 23 00:54:41 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tsi_sensor.lib	Tue Jun 23 00:54:41 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Kojto/code/tsi_sensor/#976904559b5c