Simon
Dependencies: mbed
Revision 4:ddba0fd2caa4, committed 2019-06-17
- Comitter:
- JuanClerici
- Date:
- Mon Jun 17 05:44:14 2019 +0000
- Parent:
- 3:1610e2f8803b
- Commit message:
- Sin probar el anti rebote
Changed in this revision
diff -r 1610e2f8803b -r ddba0fd2caa4 DebouncedIn.cpp --- a/DebouncedIn.cpp Mon Jun 17 04:52:45 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -#include "DebouncedIn.h" -#include "mbed.h" - -/* - * Constructor - */ -DebouncedIn::DebouncedIn(PinName in) - : _in(in) { - - // reset all the flags and counters - _samples = 0; - _output = 0; - _output_last = 0; - _rising_flag = 0; - _falling_flag = 0; - _state_counter = 0; - - // Attach ticker - _ticker.attach(this, &DebouncedIn::_sample, 0.005); -} - -void DebouncedIn::_sample() { - - // take a sample - _samples = _samples >> 1; // shift left - - if (_in) { - _samples |= 0x80; - } - - // examine the sample window, look for steady state - if (_samples == 0x00) { - _output = 0; - } - else if (_samples == 0xFF) { - _output = 1; - } - - - // Rising edge detection - if ((_output == 1) && (_output_last == 0)) { - _rising_flag++; - _state_counter = 0; - } - - // Falling edge detection - else if ((_output == 0) && (_output_last == 1)) { - _falling_flag++; - _state_counter = 0; - } - - // steady state - else { - _state_counter++; - } - - // update the output - _output_last = _output; - -} - - - -// return number of rising edges -int DebouncedIn::rising(void) { - int return_value = _rising_flag; - _rising_flag = 0; - return(return_value); -} - -// return number of falling edges -int DebouncedIn::falling(void) { - int return_value = _falling_flag; - _falling_flag = 0; - return(return_value); -} - -// return number of ticsk we've bene steady for -int DebouncedIn::steady(void) { -return(_state_counter); -} - -// return the debounced status -int DebouncedIn::read(void) { - return(_output); -} - -// shorthand for read() -DebouncedIn::operator int() { - return read(); -} - -
diff -r 1610e2f8803b -r ddba0fd2caa4 DebouncedIn.h --- a/DebouncedIn.h Mon Jun 17 04:52:45 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#include "mbed.h" - - class DebouncedIn { - public: - DebouncedIn(PinName in); - - int read (void); - operator int(); - - int rising(void); - int falling(void); - int steady(void); - - private : - // objects - DigitalIn _in; - Ticker _ticker; - - // function to take a sample, and update flags - void _sample(void); - - // counters and flags - int _samples; - int _output; - int _output_last; - int _rising_flag; - int _falling_flag; - int _state_counter; - - }; - \ No newline at end of file
diff -r 1610e2f8803b -r ddba0fd2caa4 main.cpp --- 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
diff -r 1610e2f8803b -r ddba0fd2caa4 tsi_sensor.lib --- a/tsi_sensor.lib Mon Jun 17 04:52:45 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/Kojto/code/tsi_sensor/#976904559b5c