.

Dependencies:   mbed tsi_sensor MMA8451Q

Files at this revision

API Documentation at this revision

Comitter:
Naza00
Date:
Tue May 12 18:11:04 2020 +0000
Parent:
0:6c94c5ac5e8c
Commit message:
AMICO ALONSO

Changed in this revision

main.cpp 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
--- a/main.cpp	Tue May 12 15:55:25 2020 +0000
+++ b/main.cpp	Tue May 12 18:11:04 2020 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "MMA8451Q.h"
+#include "tsi_sensor.h"
 
 #define ESPERA 0
 #define ORDEN 1
@@ -11,6 +12,31 @@
 #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;
@@ -40,7 +66,7 @@
 
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
-DigitalOut led3(LED3);
+DigitalOut led(LED3);
 
 //Callback cuando se detecta una entrada
 void onCharReceived(){
@@ -49,6 +75,9 @@
     c = pc.getc();
     newdata = true;
 }
+
+Ticker tick;
+
  //Funciones
 int acelerometro_x();
 int acelerometro_y();
@@ -56,6 +85,9 @@
 int adc();
 int in_digital();
 int lrc();
+void led_deshabilitado();
+void led_habilitado();
+void timer();
 
 int estado=ESPERA;
 float x,y,z;
@@ -70,19 +102,58 @@
 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;
-    led3 = 1;
+    led= 1;
     
     //Ejecutar onCharReceived por cada entrada por puerto
     pc.attach(&onCharReceived);
     
     while(true){
-        if(newdata){
+        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){
                 
@@ -267,4 +338,64 @@
     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++;           
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tsi_sensor.lib	Tue May 12 18:11:04 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Kojto/code/tsi_sensor/#976904559b5c