![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Simon
Dependencies: mbed
Diff: main.cpp
- Revision:
- 4:ddba0fd2caa4
- Parent:
- 3:1610e2f8803b
--- a/main.cpp Mon Jun 17 04:52:45 2019 +0000 +++ b/main.cpp Mon Jun 17 05:44:14 2019 +0000 @@ -1,16 +1,15 @@ +/* + FERNANDEZ-CLERICI + EJER02 - TP01 + +PTB0 -- Pulsador Azul +PTB1 -- Pulsador Verde +PTB2 -- Pulsador Rojo + +*/ + +/* 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 @@ -19,32 +18,32 @@ #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(bool lectura); 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 = 0; bool flagPulsador = 0; char pulsador = 0; char estado = ESTADO_COMENZAR; @@ -53,22 +52,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); @@ -289,17 +278,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(PA)) { flagPulsador = 1; pulsador = 1; return 0; } - if (PV.rising()) { + if (antirrebote(PV)) { flagPulsador = 1; pulsador = 2; return 0; } - if (PA.rising()) { + if (antirrebote(PR)) { flagPulsador = 1; pulsador = 3; return 0; @@ -310,10 +299,39 @@ return 0; } -void invierte() +bool antirrebote(bool lectura) +{ + //Funcion que hace un anti rebote y detecta el cambio de un pulsador + static bool lecant = 0; + + switch(estado) { + case INICIO_P: + //Si tengo un flanco ascendente + if((lectura == 1) && (lecant == 0)) { + estado = RISING_P; + } + break; + case RISING_P: + /*Devuelvo el estado uno , una unica vez (Saco el rebote)*/ + estado = RETENCION_P; + tp = 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 >= 3) && (lectura == 0)) { + estado = INICIO_P; + } + break; + } + lecant = 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++; } \ No newline at end of file