VersionFinal

Dependencies:   mbed

Revision:
1:2aa790d91fb0
Parent:
0:9475a54ade9e
--- a/main.cpp	Sun Jun 16 01:19:26 2019 +0000
+++ b/main.cpp	Mon Jun 17 21:09:36 2019 +0000
@@ -1,28 +1,15 @@
 /*
                 FERNANDEZ-CLERICI
-                  EJER03 - TP01
-
-PTB0 -- Pulsador Verde (CONECTADO A GND)
-PTB1 -- Pulsador Azul  (CONECTADO A GND)
-PTB3 -- Pulsador Rojo  (CONECTADO A GND)
+                  EJER02 - TP01
+ 
+PTB0 -- Pulsador Azul
+PTB1 -- Pulsador Verde
+PTB2 -- Pulsador Rojo
 
 */
 
-/*Librerias*/
+/* Librerias*/
 #include "mbed.h"
-#include "tsi_sensor.h"
-#include "DebouncedIn.h"
-
-/* 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
 
 #define ESTADO_COMENZAR     0
 #define ESTADO_REINICIO     1
@@ -31,32 +18,35 @@
 #define ESTADO_PERDER       4
 #define ESTADO_GANAR        5
 
-#define NIVELES 25
-
+#define INICIO_P            0
+#define RISING_P            1
+#define RETENCION_P         2
 
-TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
-//Definimos que el puerto serie se llama pc
-Serial pc(USBTX, USBRX);
-//Variable donde se guarda lo leido
-char   c = '\0';
-unsigned int semilla = 0;
-//bit usado como flag para procesar datos
-bool newdata = false;   //Se pone en true cuando hay nuevos datos
-//Creamos los tres leds pra usar
+#define NIVELES 25
+#define PA      PTB0
+#define PV      PTB1
+#define PR      PTB2
+
+//Creamos los tres leds para usar
 DigitalOut Rojo(LED1);
 DigitalOut Verde(LED2);
 DigitalOut Azul(LED3);
 
-void invierte();
+void timer();
 void simon();
+bool antirrebote_A(bool lectura);
+bool antirrebote_R(bool lectura);
+bool antirrebote_V(bool lectura);
+int estado_a = 0, estado_r = 0, estado_v = 0;
 char Pulsador ();
 void leds(int num); // led(0b000) = apagados,  led(0b100) = rojo, led(0b010) = Verde, led(0b001) = Azul, led(0b111) = Blanco,
 
+unsigned int semilla = 0;
 char sec[50] = {0};
 char nivelSec = 1;
 char posSec = 0;
 char posJuego = 0;
-int times = 0, espera = 0;;
+int times = 0, espera = 0, tp_v = 0, tp_r = 0, tp_a = 0;
 bool flagPulsador = 0;
 char pulsador = 0;
 char estado = ESTADO_COMENZAR;
@@ -65,22 +55,12 @@
 bool st1= 0, st2 = 0, st3 = 0, loop = 1, apagado = 0;
 
 
-DebouncedIn PV(PTB1);
-DebouncedIn PA(PTB0);
-DebouncedIn PR(PTB2);
 Ticker ti;
 
-//Callback cuando se detecta una entrada
-void onCharReceived()
-{
-    //Copiamos lo leido en c
-    c = pc.getc();
-    newdata = true;
-}
 
 int main()
 {
-    ti.attach(&invierte, 0.1);
+    ti.attach(&timer, 0.1);
 
 //Prendemos los LEDS
     leds(0b111);
@@ -301,17 +281,17 @@
     //Funcion que aciva un flag con el cambio del pulsador y guarda el pulsado en una variable
     pulsador = 0;
 
-    if (PR.rising()) {
+    if (antirrebote_A(PA)) {
         flagPulsador = 1;
         pulsador = 1;
         return 0;
     }
-    if (PV.rising()) {
+    if (antirrebote_V(PV)) {
         flagPulsador = 1;
         pulsador = 2;
         return 0;
     }
-    if (PA.rising()) {
+    if (antirrebote_R(PR)) {
         flagPulsador = 1;
         pulsador = 3;
         return 0;
@@ -322,10 +302,97 @@
     return 0;
 }
 
-void invierte()
+bool antirrebote_V(bool lectura)
+{
+    //Funcion que hace un anti rebote y detecta el cambio de un pulsador
+    static bool lecant_v = 0;
+ 
+    switch(estado_v) {
+        case INICIO_P:
+            //Si tengo un flanco ascendente
+            if((lectura == 1) && (lecant_v == 0)) {
+                estado_v = RISING_P;
+            }
+            break;
+        case RISING_P:
+            /*Devuelvo el estado uno , una unica vez (Saco el rebote)*/
+            estado_v = RETENCION_P;
+            tp_v = 0;
+            return 1;   //Devuelvo el estado alto ya que tuve un flanco
+        case RETENCION_P:
+            /*Durante un segundo y siempre que la lectura siga siendo alta retengo*/
+            if((tp_v >= 3) && (lectura == 0)) {
+                estado_v = INICIO_P;
+            }
+            break;
+    }
+    lecant_v = lectura;   //Asigno el estado previo para el proximo ciclo..
+    return 0;           //Devuelvo el estado nulo o cero
+}
+
+bool antirrebote_R(bool lectura)
+{
+    //Funcion que hace un anti rebote y detecta el cambio de un pulsador
+    static bool lecant_r=0;
+ 
+    switch(estado_r) {
+        case INICIO_P:
+            //Si tengo un flanco ascendente
+            if((lectura == 1) && (lecant_r == 0)) {
+                estado_r = RISING_P;
+            }
+            break;
+        case RISING_P:
+            /*Devuelvo el estado uno , una unica vez (Saco el rebote)*/
+            estado_r = RETENCION_P;
+            tp_r = 0;
+            return 1;   //Devuelvo el estado alto ya que tuve un flanco
+        case RETENCION_P:
+            /*Durante un segundo y siempre que la lectura siga siendo alta retengo*/
+            if((tp_r >= 3) && (lectura == 0)) {
+                estado_r = INICIO_P;
+            }
+            break;
+    }
+    lecant_r = lectura;   //Asigno el estado previo para el proximo ciclo..
+    return 0;           //Devuelvo el estado nulo o cero
+}
+
+bool antirrebote_A(bool lectura)
+{
+    //Funcion que hace un anti rebote y detecta el cambio de un pulsador
+    static bool lecant_a = 0;
+ 
+    switch(estado_a) {
+        case INICIO_P:
+            //Si tengo un flanco ascendente
+            if((lectura == 1) && (lecant_a == 0)) {
+                estado_a = RISING_P;
+            }
+            break;
+        case RISING_P:
+            /*Devuelvo el estado uno , una unica vez (Saco el rebote)*/
+            estado_a = RETENCION_P;
+            tp_a = 0;
+            return 1;   //Devuelvo el estado alto ya que tuve un flanco
+        case RETENCION_P:
+            /*Durante un segundo y siempre que la lectura siga siendo alta retengo*/
+            if((tp_a >= 3) && (lectura == 0)) {
+                estado_a = INICIO_P;
+            }
+            break;
+    }
+    lecant_a = lectura;   //Asigno el estado previo para el proximo ciclo..
+    return 0;           //Devuelvo el estado nulo o cero
+}
+
+void timer()
 {
     //Funcion que detecta las interrupciones del timer
     times++;
     semilla ++;
     espera++;
+    tp_a++;
+    tp_v++;
+    tp_r++;
 }
\ No newline at end of file