Version Final
Dependencies: Led7Seg mbed tsi_sensor
Diff: main.cpp
- Revision:
- 0:6cc6c33ebb7e
- Child:
- 1:ec5d430be5a2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jun 16 00:37:27 2019 +0000 @@ -0,0 +1,452 @@ +/*Incluyo las Librerias*/ +#include "mbed.h" +#include "tsi_sensor.h" +#include "Led7Seg.h" + +/* Declaraciones pertinentes al TSI */ +#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 + +TSIAnalogSlider tsi(ELEC0, ELEC1, 40); + +/*Maquina de estados Principal*/ +#define ESTADO_INICIO 0 +#define ESTADO_RESET 1 +#define ESTADO_FASE1 2 +#define ESTADO_FASE2 3 +#define ESTADO_FASE3 4 +#define ESTADO_FASE4 5 +#define ESTADO_PERDER 6 +#define ESTADO_GANAR 7 +#define ESTADO_ESPERAR 8 + +/*Maquina de estados de los displays*/ +#define Unidades 0 +#define Decenas 1 +#define Espera 3 + +int ac = 0; //Acumulador de la maquina de displays +int ED = 0; //Variable de estado de la maquina de displays + +Led7Seg disp(D9,D10,D2,D3,D4,D5,D6,1); //Instancio el display +unsigned char u = 0, d = 0; +/*Seteo las habilitaciones*/ +DigitalOut H1(D8); +DigitalOut H2(D7); + + +#define CANTCABLES 4 //Cantidad de cables con los que funcionara el sistema +#define TIEMPO 60 //Cantidad de segundos que tengo para desactivar la secuencia + +/*Declaracion LEDs*/ +DigitalOut Rojo(LED1); +DigitalOut Verde(LED2); +DigitalOut Azul(LED3); + +/*Declaraciones Entradas...*/ +DigitalIn C1(PTB0); +DigitalIn C2(PTB1); +DigitalIn C3(PTB2); +DigitalIn C4(PTB3); + +/*Instancio los Timmer*/ +Ticker ti; +Ticker D; +Ticker BC; + +int estado = ESTADO_INICIO, tespera = 0; +int semilla = 0, tiempo = 0, cp = 0; +int sec[CANTCABLES+1] = {0}; +bool loop1 = 1, loop2 = 1, parpadeo = 0; +int pos = 1, ant = 0; +bool estadop = 0; +float tsii = 0; + +int tmo = TIEMPO; //Variable de la cuenta regresiva +bool entmo = 0; //Habilitacion de la cuenta regresiva.. + +/*Prototipos funciones*/ +void leds(int num); //Controlador del LEd +void bombita (); //Maquina de estados principal +void temporizar(); //Funcion relacionada al Timmer +bool isPart(int numero, int vector[]); //Me notifica si un numero forma parte de un vector +int cables(); //Me devuelve un num binario en resultado a todos los cables +char desc(); +void Displays(unsigned char num, bool blink); //Maquina de estados de manejo de los displays +void tim_D(void); //Timmer de los displays +void seg(void); //Timmer cuenta regresiva + + +int main() +{ + /*Inicializo los Ticker*/ + ti.attach(&temporizar, 0.1); + D.attach(&tim_D,0.001); + BC.attach(&seg,1); + + /*Seteo los Pull Ups INTERNOS para los cables*/ + C1.mode(PullUp); + C2.mode(PullUp); + C3.mode(PullUp); + C4.mode(PullUp); + + /*Mensaje de inicio...*/ + leds(0b111); + printf("Toque el TOUCH para comenzar: \r\n"); + + while (true) { + //Ejecuto la maquina de estados principal.. + bombita(); + //Ejecuto la maquina de estados de los displays + Displays(tmo,parpadeo); + } +} + +void bombita () +{ + int i = 1, g = 1, u = 0; + switch(estado) { + default: + case ESTADO_INICIO: + tiempo = 0; + parpadeo = 0; + entmo = 0; //No descuento ahora... + /*Me fijo si apretaron el tsi*/ + tsii = tsi.readPercentage(); + if((tsii < 0.99)&&(tsii > 0.01)) { + if (cables() == 0b0000) { + leds(0b000); + estado = ESTADO_RESET; + printf("Comencemos: \r\n\n"); + } else { + if(loop1) { + printf("CONECTE TODOS LOS CABLES >:(\r\n\n"); + loop1 = 0; + } + } + } else { + loop1 = 1; + } + break; + case ESTADO_RESET: + //Reinicio la secuencia + srand (semilla); + memset(sec, 0, sizeof(sec)); + while(i <= 4) { + u = rand() % 4 + 1; + if(!(isPart(u,sec))) { + sec[i] = u; + i++; + } + } + printf("La secuencia es: CABLE %d - CABLE %d - CABLE %d - CABLE %d \r\n\n", sec[1], sec[2], sec[3], sec[4]); + entmo = 1; //Empieza la cuenta regresiva... + estado = ESTADO_FASE1; + /*Reinicio el tmo*/ + tmo = TIEMPO; + break; + case ESTADO_FASE1: + //Paso 1 + pos = estado - 1 ; + g = desc(); + if(g == sec[pos]) { + //Pase de nivel + estado = ESTADO_FASE2; + printf("MUY BIEN ZAFASTE POR AHORA :(\r\n\n"); + break; + } + if((g != sec[pos] && g != 0) || tmo == 0) { + //Perdiste + estado = ESTADO_PERDER; + entmo = 0; //No descuento ahora... + break; + } + break; + case ESTADO_FASE2: + //Paso 2 + pos = estado - 1; + g = desc(); + if(g == sec[pos]) { + //Pase de nivel + estado = ESTADO_FASE3; + printf("ULTIMA OPORTUNIDAD PARA EXPLOTARLA >:) >:)\r\n\n"); + break; + } + if((g != sec[pos] && g != 0) || tmo == 0) { + //Perdiste + estado = ESTADO_PERDER; + entmo = 0; //No descuento ahora... + break; + } + break; + case ESTADO_FASE3: + pos = estado - 1; + g = desc(); + if(g == sec[pos]) { + //Pase de nivel + estado = ESTADO_FASE4; + printf("SE TE ACABA EL TIEMPO\r\n\n"); + break; + } + if((g != sec[pos] && g != 0) || tmo == 0) { + //Perdiste + estado = ESTADO_PERDER; + entmo = 0; //No descuento ahora... + break; + } + break; + case ESTADO_FASE4: + pos = estado - 1; + + if(tmo == 0){ + //Perdiste + entmo = 0; //No descuento ahora... + estado = ESTADO_PERDER; + break; + } + + if(cables() == 0b1111) { + //Gane! + entmo = 0; //No descuento ahora... + estado = ESTADO_GANAR; + printf("GANASTEE!! :)\r\n\n"); + printf("Reconecte los cables para volver a jugar\r\n"); + break; + } + break; + case ESTADO_PERDER: + //Perdiste + if(loop2) { + leds(4); + printf("BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM!\r\n"); + printf("Perdiste bro :)\r\n\n"); + printf("Reconecte los cables para volver a jugar\r\n"); + parpadeo = 1; + loop2 = 0; + } + if(tiempo == 5) + leds(0b100); + if(tiempo == 10) + { + leds(0b000); + tiempo = 0; + } + //Si me reconectan todos los cables vuelvo al inicio... + if(cables() == 0b0000) { + estado = ESTADO_ESPERAR; + tespera = 0; + loop2 = 1; + } + + break; + + case ESTADO_GANAR: + //Ganastee + leds(0b010); + //Hacer parpadear disply + parpadeo = 1; + //Si me conectan todos los cables reinicio... + if(cables() == 0b0000) { + estado = 8; + tespera = 0; + } + + break; + + case ESTADO_ESPERAR: + //Espero un toque para reiniciar y avisoo.. + leds(0b111); + + if(tespera > 20) { + tmo = TIEMPO; + printf("Reset! Toque el TOUCH para iniciar\r\n\n"); + semilla = 0; + estado = ESTADO_INICIO; + tespera = 0; + semilla = 0; + tiempo = 0; + cp = 0; + loop1 = 1; + loop2 = 1; + parpadeo = 0; + pos = 1; + ant = 0b0000; + } + break; + } +} + +void leds(int num) +{ + switch(num) { + case 0: + Rojo = 1; + Verde = 1; + Azul = 1; + break; + case 4: + Rojo = 0; + Verde = 1; + Azul = 1; + break; + case 2: + Rojo = 1; + Verde = 0; + Azul = 1; + break; + case 1: + Rojo = 1; + Verde = 1; + Azul = 0; + break; + case 7: + Rojo = 0; + Verde = 0; + Azul = 0; + break; + } +} + + +int cables() +{ + return (C1 | C2 << 1 | C3 << 2 | C4 << 3); +} + +bool isPart(int numero, int vector[]) +{ + int c = 0; + for(c = 1; c <= 4; c ++) { + if(numero == vector[c]) + return 1; + } + return 0; +} + +char desc() +{ + static int validacion = 0; + int ant1 = cables(); + + //Cable 1 + if(((ant & 0001) != C1) && validacion != 1) { + validacion = 1; + return 1; + } + //Cable 2 + if((((ant >> 1) & 0001) != C2) && validacion != 2) { + validacion = 2; + return 2; + } + //Cable 3 + if((((ant >> 2) & 0001) != C3) && validacion != 3) { + validacion = 3; + return 3; + } + //Cable 4 + if((((ant >> 3) & 0001) != C4) && validacion != 4) { + validacion = 4; + return 4; + } + + ant = ant1; + + return 0; +} + +void temporizar() +{ + //Funcion de parpadeo + if(parpadeo) { + cp ++; + if(cp > 10) { + estadop = !estadop; + cp = 0; + } + } else { + //Apago todo lo que parpadea de forma definitiva + estadop = 0; + + } + tiempo ++; + semilla++; + tespera++; +} + + +void Displays(unsigned char num, bool blink){ + + /*Si supero lo maximo que puedo mostrar parpadeo y pongo el max*/ + if(num > 99){ + num = 99; + blink = 1; + } + + /*Maquina de estados*/ + switch(ED){ + + /*Calculo y coloco las unidades*/ + case Unidades: + u = num % 10; + disp.write(u); + H1 = 1; + + if(ac >= 15){ + H1 = 0; + ED = Decenas; + ac = 0; + break; + } + + break; + + /*Calculo y coloco las decenas*/ + case Decenas: + d = int(num/10) ; + disp.write(d); + H2 = 1; + + /*Transicion comun*/ + if(ac >= 15 && blink == 0){ + H2 = 0; + ED = Unidades; + ac = 0; + break; + } + + /*Transicion al estado de espera para blink*/ + if(ac >= 15 && blink){ + H2 = 0; + ED = Espera; + ac = 0; + break; + } + + break; + + case Espera: + if(ac >= 200){ + ac = 0; + ED = Unidades; + break; + } + break; + } + +} + +void tim_D(void){ + ac++; +} + +void seg(void){ + if(tmo > 0 && entmo ) + tmo--; +} \ No newline at end of file