s

Dependencies:   mbed

Fork of VOLTIMETRO1 by Gustavo Ramirez

Committer:
tony63
Date:
Tue Apr 14 02:05:54 2015 +0000
Revision:
0:4676a90107ca
Programa que transmite cadenas, util para verificar si APPINVENTOR recibe cadenas, tres entradas digitales suicheadas controlan en modo toggle, tres colores en la pantalla.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony63 0:4676a90107ca 1 #include "DebouncedIn.h"
tony63 0:4676a90107ca 2 #include "mbed.h"
tony63 0:4676a90107ca 3
tony63 0:4676a90107ca 4 /*
tony63 0:4676a90107ca 5 * Constructor
tony63 0:4676a90107ca 6 */
tony63 0:4676a90107ca 7 DebouncedIn::DebouncedIn(PinName in)
tony63 0:4676a90107ca 8 : _in(in) {
tony63 0:4676a90107ca 9
tony63 0:4676a90107ca 10 // reset all the flags and counters
tony63 0:4676a90107ca 11 _samples = 0;
tony63 0:4676a90107ca 12 _output = 0;
tony63 0:4676a90107ca 13 _output_last = 0;
tony63 0:4676a90107ca 14 _rising_flag = 0;
tony63 0:4676a90107ca 15 _falling_flag = 0;
tony63 0:4676a90107ca 16 _state_counter = 0;
tony63 0:4676a90107ca 17
tony63 0:4676a90107ca 18 // Attach ticker
tony63 0:4676a90107ca 19 _ticker.attach(this, &DebouncedIn::_sample, 0.005);
tony63 0:4676a90107ca 20 }
tony63 0:4676a90107ca 21
tony63 0:4676a90107ca 22 void DebouncedIn::_sample() {
tony63 0:4676a90107ca 23
tony63 0:4676a90107ca 24 // take a sample
tony63 0:4676a90107ca 25 _samples = _samples >> 1; // shift left
tony63 0:4676a90107ca 26
tony63 0:4676a90107ca 27 if (_in) {
tony63 0:4676a90107ca 28 _samples |= 0x80;
tony63 0:4676a90107ca 29 }
tony63 0:4676a90107ca 30
tony63 0:4676a90107ca 31 // examine the sample window, look for steady state
tony63 0:4676a90107ca 32 if (_samples == 0x00) {
tony63 0:4676a90107ca 33 _output = 0;
tony63 0:4676a90107ca 34 }
tony63 0:4676a90107ca 35 else if (_samples == 0xFF) {
tony63 0:4676a90107ca 36 _output = 1;
tony63 0:4676a90107ca 37 }
tony63 0:4676a90107ca 38
tony63 0:4676a90107ca 39
tony63 0:4676a90107ca 40 // Rising edge detection
tony63 0:4676a90107ca 41 if ((_output == 1) && (_output_last == 0)) {
tony63 0:4676a90107ca 42 _rising_flag++;
tony63 0:4676a90107ca 43 _state_counter = 0;
tony63 0:4676a90107ca 44 }
tony63 0:4676a90107ca 45
tony63 0:4676a90107ca 46 // Falling edge detection
tony63 0:4676a90107ca 47 else if ((_output == 0) && (_output_last == 1)) {
tony63 0:4676a90107ca 48 _falling_flag++;
tony63 0:4676a90107ca 49 _state_counter = 0;
tony63 0:4676a90107ca 50 }
tony63 0:4676a90107ca 51
tony63 0:4676a90107ca 52 // steady state
tony63 0:4676a90107ca 53 else {
tony63 0:4676a90107ca 54 _state_counter++;
tony63 0:4676a90107ca 55 }
tony63 0:4676a90107ca 56
tony63 0:4676a90107ca 57 // update the output
tony63 0:4676a90107ca 58 _output_last = _output;
tony63 0:4676a90107ca 59
tony63 0:4676a90107ca 60 }
tony63 0:4676a90107ca 61
tony63 0:4676a90107ca 62
tony63 0:4676a90107ca 63
tony63 0:4676a90107ca 64 // return number of rising edges
tony63 0:4676a90107ca 65 int DebouncedIn::rising(void) {
tony63 0:4676a90107ca 66 int return_value = _rising_flag;
tony63 0:4676a90107ca 67 _rising_flag = 0;
tony63 0:4676a90107ca 68 return(return_value);
tony63 0:4676a90107ca 69 }
tony63 0:4676a90107ca 70
tony63 0:4676a90107ca 71 // return number of falling edges
tony63 0:4676a90107ca 72 int DebouncedIn::falling(void) {
tony63 0:4676a90107ca 73 int return_value = _falling_flag;
tony63 0:4676a90107ca 74 _falling_flag = 0;
tony63 0:4676a90107ca 75 return(return_value);
tony63 0:4676a90107ca 76 }
tony63 0:4676a90107ca 77
tony63 0:4676a90107ca 78 // return number of ticsk we've bene steady for
tony63 0:4676a90107ca 79 int DebouncedIn::steady(void) {
tony63 0:4676a90107ca 80 return(_state_counter);
tony63 0:4676a90107ca 81 }
tony63 0:4676a90107ca 82
tony63 0:4676a90107ca 83 // return the debounced status
tony63 0:4676a90107ca 84 int DebouncedIn::read(void) {
tony63 0:4676a90107ca 85 return(_output);
tony63 0:4676a90107ca 86 }
tony63 0:4676a90107ca 87
tony63 0:4676a90107ca 88 // shorthand for read()
tony63 0:4676a90107ca 89 DebouncedIn::operator int() {
tony63 0:4676a90107ca 90 return read();
tony63 0:4676a90107ca 91 }
tony63 0:4676a90107ca 92
tony63 0:4676a90107ca 93