![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
VersionFinal
main.cpp@0:9475a54ade9e, 2019-06-16 (annotated)
- Committer:
- NIcolasFernandezSanz
- Date:
- Sun Jun 16 01:19:26 2019 +0000
- Revision:
- 0:9475a54ade9e
FERNANDEZ_CLERICI;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NIcolasFernandezSanz | 0:9475a54ade9e | 1 | /* |
NIcolasFernandezSanz | 0:9475a54ade9e | 2 | FERNANDEZ-CLERICI |
NIcolasFernandezSanz | 0:9475a54ade9e | 3 | EJER03 - TP01 |
NIcolasFernandezSanz | 0:9475a54ade9e | 4 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 5 | PTB0 -- Pulsador Verde (CONECTADO A GND) |
NIcolasFernandezSanz | 0:9475a54ade9e | 6 | PTB1 -- Pulsador Azul (CONECTADO A GND) |
NIcolasFernandezSanz | 0:9475a54ade9e | 7 | PTB3 -- Pulsador Rojo (CONECTADO A GND) |
NIcolasFernandezSanz | 0:9475a54ade9e | 8 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 9 | */ |
NIcolasFernandezSanz | 0:9475a54ade9e | 10 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 11 | /*Librerias*/ |
NIcolasFernandezSanz | 0:9475a54ade9e | 12 | #include "mbed.h" |
NIcolasFernandezSanz | 0:9475a54ade9e | 13 | #include "tsi_sensor.h" |
NIcolasFernandezSanz | 0:9475a54ade9e | 14 | #include "DebouncedIn.h" |
NIcolasFernandezSanz | 0:9475a54ade9e | 15 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 16 | /* This defines will be replaced by PinNames soon */ |
NIcolasFernandezSanz | 0:9475a54ade9e | 17 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
NIcolasFernandezSanz | 0:9475a54ade9e | 18 | #define ELEC0 9 |
NIcolasFernandezSanz | 0:9475a54ade9e | 19 | #define ELEC1 10 |
NIcolasFernandezSanz | 0:9475a54ade9e | 20 | #elif defined (TARGET_KL05Z) |
NIcolasFernandezSanz | 0:9475a54ade9e | 21 | #define ELEC0 9 |
NIcolasFernandezSanz | 0:9475a54ade9e | 22 | #define ELEC1 8 |
NIcolasFernandezSanz | 0:9475a54ade9e | 23 | #else |
NIcolasFernandezSanz | 0:9475a54ade9e | 24 | #error TARGET NOT DEFINED |
NIcolasFernandezSanz | 0:9475a54ade9e | 25 | #endif |
NIcolasFernandezSanz | 0:9475a54ade9e | 26 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 27 | #define ESTADO_COMENZAR 0 |
NIcolasFernandezSanz | 0:9475a54ade9e | 28 | #define ESTADO_REINICIO 1 |
NIcolasFernandezSanz | 0:9475a54ade9e | 29 | #define ESTADO_MUESTREO 2 |
NIcolasFernandezSanz | 0:9475a54ade9e | 30 | #define ESTADO_JUEGO 3 |
NIcolasFernandezSanz | 0:9475a54ade9e | 31 | #define ESTADO_PERDER 4 |
NIcolasFernandezSanz | 0:9475a54ade9e | 32 | #define ESTADO_GANAR 5 |
NIcolasFernandezSanz | 0:9475a54ade9e | 33 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 34 | #define NIVELES 25 |
NIcolasFernandezSanz | 0:9475a54ade9e | 35 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 36 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 37 | TSIAnalogSlider tsi(ELEC0, ELEC1, 40); |
NIcolasFernandezSanz | 0:9475a54ade9e | 38 | //Definimos que el puerto serie se llama pc |
NIcolasFernandezSanz | 0:9475a54ade9e | 39 | Serial pc(USBTX, USBRX); |
NIcolasFernandezSanz | 0:9475a54ade9e | 40 | //Variable donde se guarda lo leido |
NIcolasFernandezSanz | 0:9475a54ade9e | 41 | char c = '\0'; |
NIcolasFernandezSanz | 0:9475a54ade9e | 42 | unsigned int semilla = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 43 | //bit usado como flag para procesar datos |
NIcolasFernandezSanz | 0:9475a54ade9e | 44 | bool newdata = false; //Se pone en true cuando hay nuevos datos |
NIcolasFernandezSanz | 0:9475a54ade9e | 45 | //Creamos los tres leds pra usar |
NIcolasFernandezSanz | 0:9475a54ade9e | 46 | DigitalOut Rojo(LED1); |
NIcolasFernandezSanz | 0:9475a54ade9e | 47 | DigitalOut Verde(LED2); |
NIcolasFernandezSanz | 0:9475a54ade9e | 48 | DigitalOut Azul(LED3); |
NIcolasFernandezSanz | 0:9475a54ade9e | 49 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 50 | void invierte(); |
NIcolasFernandezSanz | 0:9475a54ade9e | 51 | void simon(); |
NIcolasFernandezSanz | 0:9475a54ade9e | 52 | char Pulsador (); |
NIcolasFernandezSanz | 0:9475a54ade9e | 53 | void leds(int num); // led(0b000) = apagados, led(0b100) = rojo, led(0b010) = Verde, led(0b001) = Azul, led(0b111) = Blanco, |
NIcolasFernandezSanz | 0:9475a54ade9e | 54 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 55 | char sec[50] = {0}; |
NIcolasFernandezSanz | 0:9475a54ade9e | 56 | char nivelSec = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 57 | char posSec = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 58 | char posJuego = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 59 | int times = 0, espera = 0;; |
NIcolasFernandezSanz | 0:9475a54ade9e | 60 | bool flagPulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 61 | char pulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 62 | char estado = ESTADO_COMENZAR; |
NIcolasFernandezSanz | 0:9475a54ade9e | 63 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 64 | bool pulsador1 = 1, pulsador2 = 1, pulsador3 = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 65 | bool st1= 0, st2 = 0, st3 = 0, loop = 1, apagado = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 66 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 67 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 68 | DebouncedIn PV(PTB1); |
NIcolasFernandezSanz | 0:9475a54ade9e | 69 | DebouncedIn PA(PTB0); |
NIcolasFernandezSanz | 0:9475a54ade9e | 70 | DebouncedIn PR(PTB2); |
NIcolasFernandezSanz | 0:9475a54ade9e | 71 | Ticker ti; |
NIcolasFernandezSanz | 0:9475a54ade9e | 72 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 73 | //Callback cuando se detecta una entrada |
NIcolasFernandezSanz | 0:9475a54ade9e | 74 | void onCharReceived() |
NIcolasFernandezSanz | 0:9475a54ade9e | 75 | { |
NIcolasFernandezSanz | 0:9475a54ade9e | 76 | //Copiamos lo leido en c |
NIcolasFernandezSanz | 0:9475a54ade9e | 77 | c = pc.getc(); |
NIcolasFernandezSanz | 0:9475a54ade9e | 78 | newdata = true; |
NIcolasFernandezSanz | 0:9475a54ade9e | 79 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 80 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 81 | int main() |
NIcolasFernandezSanz | 0:9475a54ade9e | 82 | { |
NIcolasFernandezSanz | 0:9475a54ade9e | 83 | ti.attach(&invierte, 0.1); |
NIcolasFernandezSanz | 0:9475a54ade9e | 84 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 85 | //Prendemos los LEDS |
NIcolasFernandezSanz | 0:9475a54ade9e | 86 | leds(0b111); |
NIcolasFernandezSanz | 0:9475a54ade9e | 87 | printf("Toque cualquier pulsador para comenzar: \r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 88 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 89 | while(true) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 90 | //Cambio el estado de los pulsadores |
NIcolasFernandezSanz | 0:9475a54ade9e | 91 | Pulsador(); |
NIcolasFernandezSanz | 0:9475a54ade9e | 92 | //Ejecuto la funcion principal |
NIcolasFernandezSanz | 0:9475a54ade9e | 93 | simon(); |
NIcolasFernandezSanz | 0:9475a54ade9e | 94 | //Aseguro el reset de la habilitacion del pulsador |
NIcolasFernandezSanz | 0:9475a54ade9e | 95 | flagPulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 96 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 97 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 98 | void simon() |
NIcolasFernandezSanz | 0:9475a54ade9e | 99 | { |
NIcolasFernandezSanz | 0:9475a54ade9e | 100 | static int i; |
NIcolasFernandezSanz | 0:9475a54ade9e | 101 | switch(estado) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 102 | case ESTADO_COMENZAR: |
NIcolasFernandezSanz | 0:9475a54ade9e | 103 | //Espero que pulsen un pulsador para empezar |
NIcolasFernandezSanz | 0:9475a54ade9e | 104 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 105 | if(pulsador != 0 && flagPulsador == 1) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 106 | leds(0b000); |
NIcolasFernandezSanz | 0:9475a54ade9e | 107 | printf("Comencemos: \r\n\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 108 | estado = ESTADO_REINICIO; |
NIcolasFernandezSanz | 0:9475a54ade9e | 109 | flagPulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 110 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 111 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 112 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 113 | case ESTADO_REINICIO: |
NIcolasFernandezSanz | 0:9475a54ade9e | 114 | //Seteo la semilla |
NIcolasFernandezSanz | 0:9475a54ade9e | 115 | srand (semilla*4920*1912); |
NIcolasFernandezSanz | 0:9475a54ade9e | 116 | //Genero la secuencia |
NIcolasFernandezSanz | 0:9475a54ade9e | 117 | for(i = 0; i <= NIVELES ; i ++) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 118 | sec[i] = rand() % 3 + 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 119 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 120 | nivelSec = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 121 | posSec = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 122 | posJuego = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 123 | estado = ESTADO_MUESTREO; |
NIcolasFernandezSanz | 0:9475a54ade9e | 124 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 125 | loop = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 126 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 127 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 128 | case ESTADO_MUESTREO: |
NIcolasFernandezSanz | 0:9475a54ade9e | 129 | //Muestro la secuencia generada |
NIcolasFernandezSanz | 0:9475a54ade9e | 130 | if(loop) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 131 | //Cada vez que termino de mostrar un LED |
NIcolasFernandezSanz | 0:9475a54ade9e | 132 | if (apagado == 0) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 133 | //Evaluo si mostre la cantidad de LEDs segun el nivel |
NIcolasFernandezSanz | 0:9475a54ade9e | 134 | if(posSec >= nivelSec) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 135 | estado = ESTADO_JUEGO; |
NIcolasFernandezSanz | 0:9475a54ade9e | 136 | printf("\nA jugar Bro :) \r"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 137 | printf("\r\n\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 138 | espera = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 139 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 140 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 141 | switch(sec[posSec]) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 142 | case 1: |
NIcolasFernandezSanz | 0:9475a54ade9e | 143 | //Muestro LED rojo |
NIcolasFernandezSanz | 0:9475a54ade9e | 144 | leds(0b100); |
NIcolasFernandezSanz | 0:9475a54ade9e | 145 | if(times > 5) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 146 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 147 | printf("ROJO\r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 148 | apagado = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 149 | posSec++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 150 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 151 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 152 | case 2: |
NIcolasFernandezSanz | 0:9475a54ade9e | 153 | //Muestro LED verde |
NIcolasFernandezSanz | 0:9475a54ade9e | 154 | leds(0b010); |
NIcolasFernandezSanz | 0:9475a54ade9e | 155 | if(times > 5) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 156 | apagado = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 157 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 158 | printf("VERDE\r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 159 | posSec++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 160 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 161 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 162 | case 3: |
NIcolasFernandezSanz | 0:9475a54ade9e | 163 | //Muestro LED azul |
NIcolasFernandezSanz | 0:9475a54ade9e | 164 | leds(0b001); |
NIcolasFernandezSanz | 0:9475a54ade9e | 165 | if(times > 5) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 166 | apagado = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 167 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 168 | printf("AZUL\r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 169 | posSec++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 170 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 171 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 172 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 173 | } else { |
NIcolasFernandezSanz | 0:9475a54ade9e | 174 | //Dejo un espacio apagado entre los colores de las secuencias |
NIcolasFernandezSanz | 0:9475a54ade9e | 175 | leds(0b000); |
NIcolasFernandezSanz | 0:9475a54ade9e | 176 | if (times > 5) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 177 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 178 | apagado = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 179 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 180 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 181 | } else { |
NIcolasFernandezSanz | 0:9475a54ade9e | 182 | //Espero 5 times el ultimo LED encendido y lo apago |
NIcolasFernandezSanz | 0:9475a54ade9e | 183 | if (times > 5) |
NIcolasFernandezSanz | 0:9475a54ade9e | 184 | leds(0b000); |
NIcolasFernandezSanz | 0:9475a54ade9e | 185 | //Espero 5 times mas para separar el final de muestreo con el inicio del Juego |
NIcolasFernandezSanz | 0:9475a54ade9e | 186 | if (times > 10) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 187 | loop = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 188 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 189 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 190 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 191 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 192 | case ESTADO_JUEGO: |
NIcolasFernandezSanz | 0:9475a54ade9e | 193 | //Detecto si el jugador tarda mucho tiempo en apretar un boton |
NIcolasFernandezSanz | 0:9475a54ade9e | 194 | if (espera > 50) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 195 | printf("Tardaste demaciado tiempo en elegir\r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 196 | estado = ESTADO_PERDER; |
NIcolasFernandezSanz | 0:9475a54ade9e | 197 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 198 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 199 | //Detecto un cambio en los pulsadores |
NIcolasFernandezSanz | 0:9475a54ade9e | 200 | if(flagPulsador) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 201 | espera = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 202 | flagPulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 203 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 204 | //Detecto que pulsador precionaron y enciendo el LED |
NIcolasFernandezSanz | 0:9475a54ade9e | 205 | switch(pulsador) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 206 | case 1: |
NIcolasFernandezSanz | 0:9475a54ade9e | 207 | printf("ROJO \r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 208 | leds(0b100); |
NIcolasFernandezSanz | 0:9475a54ade9e | 209 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 210 | case 2: |
NIcolasFernandezSanz | 0:9475a54ade9e | 211 | printf("VERDE \r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 212 | leds(0b010); |
NIcolasFernandezSanz | 0:9475a54ade9e | 213 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 214 | case 3: |
NIcolasFernandezSanz | 0:9475a54ade9e | 215 | printf("AZUL \r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 216 | leds(0b001); |
NIcolasFernandezSanz | 0:9475a54ade9e | 217 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 218 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 219 | //Comparo si el pulsador apretado es el correcto segun la secuencia |
NIcolasFernandezSanz | 0:9475a54ade9e | 220 | if(pulsador == sec[posJuego]) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 221 | posJuego++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 222 | //Si alcancé el nivel en el que estoy paso de nivel |
NIcolasFernandezSanz | 0:9475a54ade9e | 223 | if(posJuego == nivelSec) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 224 | nivelSec++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 225 | printf("\n\t--------------------------------\r"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 226 | //Si alcancé el máximo nivel paso al estado GANAR |
NIcolasFernandezSanz | 0:9475a54ade9e | 227 | if (nivelSec == NIVELES + 1) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 228 | estado = ESTADO_GANAR; |
NIcolasFernandezSanz | 0:9475a54ade9e | 229 | } else { |
NIcolasFernandezSanz | 0:9475a54ade9e | 230 | //Si no llegué al máximo nivel paso de nivel |
NIcolasFernandezSanz | 0:9475a54ade9e | 231 | printf("\nPasaste al nivel %d!\r\n\n", nivelSec); |
NIcolasFernandezSanz | 0:9475a54ade9e | 232 | posSec = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 233 | posJuego = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 234 | estado = ESTADO_MUESTREO; |
NIcolasFernandezSanz | 0:9475a54ade9e | 235 | loop = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 236 | times = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 237 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 238 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 239 | } else { |
NIcolasFernandezSanz | 0:9475a54ade9e | 240 | //Si el pulsador no corresponde con la secuencia paso al estado PERDER |
NIcolasFernandezSanz | 0:9475a54ade9e | 241 | estado = ESTADO_PERDER; |
NIcolasFernandezSanz | 0:9475a54ade9e | 242 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 243 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 244 | //Espero 5 times el LED encendido |
NIcolasFernandezSanz | 0:9475a54ade9e | 245 | if (times > 5) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 246 | leds(0b000); |
NIcolasFernandezSanz | 0:9475a54ade9e | 247 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 248 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 249 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 250 | case ESTADO_PERDER: |
NIcolasFernandezSanz | 0:9475a54ade9e | 251 | //Prendo el LED blanco para mostrar que termino el juego y vuelvo al estado COMENZAR |
NIcolasFernandezSanz | 0:9475a54ade9e | 252 | printf("\nPerdiste Bro :(\r\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 253 | printf("Apreta cualquier boton para reiniciar!\r\n\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 254 | leds(0b111); |
NIcolasFernandezSanz | 0:9475a54ade9e | 255 | estado = ESTADO_COMENZAR; |
NIcolasFernandezSanz | 0:9475a54ade9e | 256 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 257 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 258 | case ESTADO_GANAR: |
NIcolasFernandezSanz | 0:9475a54ade9e | 259 | //Mustro el LED blanco para mostrar que termino el juego y aviso que completo la secuencia |
NIcolasFernandezSanz | 0:9475a54ade9e | 260 | printf("\nGANASTE!!! :)\r\n\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 261 | printf("Apreta cualquier boton para reiniciar!\r\n\n"); |
NIcolasFernandezSanz | 0:9475a54ade9e | 262 | leds(0b111); |
NIcolasFernandezSanz | 0:9475a54ade9e | 263 | estado = ESTADO_COMENZAR; |
NIcolasFernandezSanz | 0:9475a54ade9e | 264 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 265 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 266 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 267 | void leds(int num) |
NIcolasFernandezSanz | 0:9475a54ade9e | 268 | { |
NIcolasFernandezSanz | 0:9475a54ade9e | 269 | //Funcion que codifica el encendido y apagado de los LEDs |
NIcolasFernandezSanz | 0:9475a54ade9e | 270 | switch(num) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 271 | case 0: |
NIcolasFernandezSanz | 0:9475a54ade9e | 272 | Rojo = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 273 | Verde = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 274 | Azul = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 275 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 276 | case 4: |
NIcolasFernandezSanz | 0:9475a54ade9e | 277 | Rojo = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 278 | Verde = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 279 | Azul = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 280 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 281 | case 2: |
NIcolasFernandezSanz | 0:9475a54ade9e | 282 | Rojo = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 283 | Verde = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 284 | Azul = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 285 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 286 | case 1: |
NIcolasFernandezSanz | 0:9475a54ade9e | 287 | Rojo = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 288 | Verde = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 289 | Azul = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 290 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 291 | case 7: |
NIcolasFernandezSanz | 0:9475a54ade9e | 292 | Rojo = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 293 | Verde = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 294 | Azul = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 295 | break; |
NIcolasFernandezSanz | 0:9475a54ade9e | 296 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 297 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 298 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 299 | char Pulsador() |
NIcolasFernandezSanz | 0:9475a54ade9e | 300 | { |
NIcolasFernandezSanz | 0:9475a54ade9e | 301 | //Funcion que aciva un flag con el cambio del pulsador y guarda el pulsado en una variable |
NIcolasFernandezSanz | 0:9475a54ade9e | 302 | pulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 303 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 304 | if (PR.rising()) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 305 | flagPulsador = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 306 | pulsador = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 307 | return 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 308 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 309 | if (PV.rising()) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 310 | flagPulsador = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 311 | pulsador = 2; |
NIcolasFernandezSanz | 0:9475a54ade9e | 312 | return 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 313 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 314 | if (PA.rising()) { |
NIcolasFernandezSanz | 0:9475a54ade9e | 315 | flagPulsador = 1; |
NIcolasFernandezSanz | 0:9475a54ade9e | 316 | pulsador = 3; |
NIcolasFernandezSanz | 0:9475a54ade9e | 317 | return 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 318 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 319 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 320 | pulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 321 | flagPulsador = 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 322 | return 0; |
NIcolasFernandezSanz | 0:9475a54ade9e | 323 | } |
NIcolasFernandezSanz | 0:9475a54ade9e | 324 | |
NIcolasFernandezSanz | 0:9475a54ade9e | 325 | void invierte() |
NIcolasFernandezSanz | 0:9475a54ade9e | 326 | { |
NIcolasFernandezSanz | 0:9475a54ade9e | 327 | //Funcion que detecta las interrupciones del timer |
NIcolasFernandezSanz | 0:9475a54ade9e | 328 | times++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 329 | semilla ++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 330 | espera++; |
NIcolasFernandezSanz | 0:9475a54ade9e | 331 | } |