TP 1 Ejer 2 simon Cobas, Montero

Dependencies:   mbed tsi_sensor

Revision:
0:694030aeaef8
Child:
1:2ec64607a2a4
diff -r 000000000000 -r 694030aeaef8 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 21 13:44:17 2019 +0000
@@ -0,0 +1,238 @@
+#include "mbed.h"
+#include "tsi_sensor.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 ESPERAR_A 300
+#define MOSTRAR 301
+#define ESPERO_JUG 302
+#define MUESTREO 303
+#define ESPERAR_B 304
+#define ESPERAR_C 305
+#define JUGANDO 306
+
+Ticker timer;
+void t();
+
+DigitalOut ledr(LED1);
+DigitalOut ledg(LED2);
+DigitalOut ledb(LED3);
+
+
+void GENERAL();
+void MOSTRAR_LED();
+void JUEGO();
+void PULSADORES();
+
+int secuencia[20]= {1,2,3,2,2,3,1,3,1,2,3,1,2,3,1,2,3};
+int cant=0, finsec=0, n=0, P1=0, P2=0, P3=0;
+int habmostrar=0,  habjug=0, fin_mostrar=0, finjug=0, perdiste=0;
+volatile unsigned int tiempo=100;
+static int traba=0, principio=0;
+
+TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
+
+int main()
+{
+    ledr=1;
+    ledb=1;
+    ledg=1;
+    timer.attach(&t,0.01);
+    while(1) {
+        GENERAL();
+        MOSTRAR_LED();
+        JUEGO();
+        PULSADORES();
+    }
+}
+
+
+void GENERAL()
+{
+    static int paso_a=ESPERAR_A;
+    switch(paso_a) {
+        case ESPERAR_A:
+            habmostrar=0;
+            habjug=0;
+            fin_mostrar=0;
+            finjug=0;
+            perdiste=0;
+            cant=0;
+            principio=0;
+            if((P1 == 1 || P2 == 1 || P3 == 1) && traba == 0) {
+                traba=1;
+                paso_a=MOSTRAR;
+                principio++;
+                printf("Inicio del juego\n\r");
+            }
+            break;
+        case MOSTRAR:
+            habmostrar=1;
+            finjug=0;
+            if(fin_mostrar==1) {
+                paso_a=ESPERO_JUG;
+            }
+            break;
+        case ESPERO_JUG:
+            habjug=1;
+            fin_mostrar=0;
+            if(finjug==1) {
+                cant++;
+                paso_a=MOSTRAR;
+            }
+            if(perdiste==1) {
+                paso_a=ESPERAR_A;
+            }
+            break;
+    }
+}
+
+
+void MOSTRAR_LED()
+{
+    static int paso_b=ESPERAR_B;
+    switch(paso_b) {
+        case ESPERAR_B:
+            if(habmostrar==1) {
+                paso_b=MUESTREO;
+            }
+            break;
+        case MUESTREO:
+            if(finsec==0) {
+                if (tiempo == 0) {
+                    if(cant < n) {
+                        finsec=1;
+                    } else if(secuencia[n]== 1) {
+                        ledr=0;
+                        ledg=1;
+                        ledb=1;
+                        printf("LED_ROJO\n\r");
+                    } else if(secuencia[n]== 2) {
+                        ledr=1;
+                        ledg=0;
+                        ledb=1;
+                        printf("LED_VERDE\n\r");
+                    } else if(secuencia[n]== 3) {
+                        ledr=1;
+                        ledg=1;
+                        ledb=0;
+                        printf("LED_AZUL\n\r");
+                    }
+                    tiempo = 130;
+                    n++;
+                }
+                if(tiempo == 40) {
+                    ledr=1;
+                    ledg=1;
+                    ledb=1;
+                }
+            } else if(finsec==1) {
+                ledr=1;
+                ledg=1;
+                ledb=1;
+                fin_mostrar=1;
+                habmostrar=0;
+                paso_b=ESPERAR_B;
+            }
+            break;
+    }
+}
+
+
+void JUEGO()
+{
+    static int paso_c=ESPERAR_C;
+    switch(paso_c) {
+        case ESPERAR_C:
+            if(habjug==1) {
+                paso_c=JUGANDO;
+                n=0;
+                finsec=0;
+            }
+            break;
+        case JUGANDO:
+            if(finsec == 0) {
+                if((P1 == 1 || P2 == 1 || P3 == 1) && traba == 0) {
+                    traba=1;
+                    if((secuencia[n] == 1 && P1 == 1) || (secuencia[n] == 2 && P2 == 1) || (secuencia[n] == 3 && P3 == 1)) {
+                        n++;
+                    } else {
+                        printf("Game Over\n\r");
+                        perdiste=1;
+                        principio=0;
+                        habjug=0;
+                        break;
+                    }
+                    if(cant < n) {
+                        finsec=1;
+                        printf("Enhorabuena!\n\r");
+                    }
+                }
+            } else if(finsec == 1) {
+                finjug=1;
+                habjug=0;
+                paso_c=ESPERAR_C;
+            }
+            break;
+
+    }
+}
+
+
+void t()
+{
+    if (tiempo > 0)
+        tiempo--;
+}
+
+
+void PULSADORES()
+{
+    static int lectura=0, color=0;
+    float valor=tsi.readPercentage();
+    if(tiempo == 0) {
+        tiempo=3;
+        if(valor != 0 && lectura == 0) {
+            lectura=1;
+            if(valor > 0.37 && valor < 0.73) {
+                P2=1;
+            } else if(valor <= 0.33) {
+                P1=1;
+            } else if(valor >= 0.77) {
+                P3=1;
+            }
+        } else if( valor == 0 && lectura == 1) {
+            ledr=1;
+            ledg=1;
+            ledb=1;
+            traba=0;
+            color=0;
+            lectura=0;
+            P1=0;
+            P2=0;
+            P3=0;
+        }
+        if(valor != 0 && principio > 0 && color == 0) {
+            color=1;
+            if(valor > 0.37 && valor < 0.73) {
+                printf("Tocaste el color VERDE\n\r");
+                ledg=0;
+            } else if(valor <= 0.33) {
+                printf("Tocaste el color ROJO\n\r");
+                ledr=0;
+            } else if(valor >= 0.77) {
+                printf("Tocaste el color AZUL\n\r");
+                ledb=0;
+            }
+        }
+    }
+}
\ No newline at end of file