Debounce_Library
antirrebote.cpp@2:d1e2599de47c, 2018-06-12 (annotated)
- Committer:
- Tom_87
- Date:
- Tue Jun 12 17:08:53 2018 +0000
- Revision:
- 2:d1e2599de47c
- Parent:
- 0:16d62113f1d5
Programa de funcionamiento de una cafetera automatica
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tom_87 | 0:16d62113f1d5 | 1 | #include "antirrebote.h" |
Tom_87 | 0:16d62113f1d5 | 2 | |
Tom_87 | 2:d1e2599de47c | 3 | enum {PRESS=0, NO_PRESS=1, S_NO_PRESS, S_DETECT_PULS, S_DEB1, S_PRESS, S_RETENTION, S_DEB2, DEB_TIME = 20}; |
Tom_87 | 0:16d62113f1d5 | 4 | |
Tom_87 | 2:d1e2599de47c | 5 | AntReb::~AntReb() |
Tom_87 | 2:d1e2599de47c | 6 | { |
Tom_87 | 2:d1e2599de47c | 7 | } |
Tom_87 | 2:d1e2599de47c | 8 | AntReb::AntReb() |
Tom_87 | 2:d1e2599de47c | 9 | { |
Tom_87 | 2:d1e2599de47c | 10 | state = S_NO_PRESS; |
Tom_87 | 2:d1e2599de47c | 11 | deb_time = DEB_TIME; |
Tom_87 | 2:d1e2599de47c | 12 | hab=0; |
Tom_87 | 2:d1e2599de47c | 13 | out=0; |
Tom_87 | 2:d1e2599de47c | 14 | } |
Tom_87 | 0:16d62113f1d5 | 15 | |
Tom_87 | 0:16d62113f1d5 | 16 | void AntReb::setPin(DigitalIn pin) |
Tom_87 | 0:16d62113f1d5 | 17 | { |
Tom_87 | 2:d1e2599de47c | 18 | Pulsador = pin.read(); |
Tom_87 | 2:d1e2599de47c | 19 | } |
Tom_87 | 2:d1e2599de47c | 20 | void AntReb::DebTime() |
Tom_87 | 2:d1e2599de47c | 21 | { |
Tom_87 | 2:d1e2599de47c | 22 | if(deb_time>0) |
Tom_87 | 2:d1e2599de47c | 23 | deb_time--; |
Tom_87 | 2:d1e2599de47c | 24 | else |
Tom_87 | 2:d1e2599de47c | 25 | deb_time = DEB_TIME; |
Tom_87 | 0:16d62113f1d5 | 26 | } |
Tom_87 | 0:16d62113f1d5 | 27 | |
Tom_87 | 2:d1e2599de47c | 28 | pinEstado_t AntReb::antiRebote() |
Tom_87 | 2:d1e2599de47c | 29 | { |
Tom_87 | 0:16d62113f1d5 | 30 | |
Tom_87 | 2:d1e2599de47c | 31 | switch(state) { |
Tom_87 | 2:d1e2599de47c | 32 | case S_NO_PRESS: |
Tom_87 | 0:16d62113f1d5 | 33 | default: |
Tom_87 | 2:d1e2599de47c | 34 | if (Pulsador == PRESS) { |
Tom_87 | 2:d1e2599de47c | 35 | state = S_DETECT_PULS; |
Tom_87 | 2:d1e2599de47c | 36 | } |
Tom_87 | 2:d1e2599de47c | 37 | break; |
Tom_87 | 2:d1e2599de47c | 38 | |
Tom_87 | 2:d1e2599de47c | 39 | case S_DETECT_PULS: |
Tom_87 | 2:d1e2599de47c | 40 | if (Pulsador == PRESS && deb_time < 20){ |
Tom_87 | 2:d1e2599de47c | 41 | hab=1; |
Tom_87 | 2:d1e2599de47c | 42 | state = S_DEB1; |
Tom_87 | 2:d1e2599de47c | 43 | } else if(Pulsador == NO_PRESS) |
Tom_87 | 2:d1e2599de47c | 44 | state = S_NO_PRESS; |
Tom_87 | 0:16d62113f1d5 | 45 | break; |
Tom_87 | 2:d1e2599de47c | 46 | |
Tom_87 | 2:d1e2599de47c | 47 | case S_DEB1: |
Tom_87 | 2:d1e2599de47c | 48 | if (deb_time == 0 && hab == 1) { |
Tom_87 | 2:d1e2599de47c | 49 | state = S_PRESS; |
Tom_87 | 2:d1e2599de47c | 50 | hab=0; |
Tom_87 | 2:d1e2599de47c | 51 | |
Tom_87 | 0:16d62113f1d5 | 52 | } |
Tom_87 | 0:16d62113f1d5 | 53 | break; |
Tom_87 | 2:d1e2599de47c | 54 | |
Tom_87 | 2:d1e2599de47c | 55 | case S_PRESS: |
Tom_87 | 2:d1e2599de47c | 56 | out = 1; |
Tom_87 | 2:d1e2599de47c | 57 | state= S_RETENTION; |
Tom_87 | 0:16d62113f1d5 | 58 | break; |
Tom_87 | 2:d1e2599de47c | 59 | |
Tom_87 | 2:d1e2599de47c | 60 | case S_RETENTION: |
Tom_87 | 2:d1e2599de47c | 61 | out = 0; |
Tom_87 | 2:d1e2599de47c | 62 | if (Pulsador == NO_PRESS) |
Tom_87 | 2:d1e2599de47c | 63 | state = S_DEB2; |
Tom_87 | 2:d1e2599de47c | 64 | break; |
Tom_87 | 2:d1e2599de47c | 65 | |
Tom_87 | 2:d1e2599de47c | 66 | case S_DEB2: |
Tom_87 | 2:d1e2599de47c | 67 | if (deb_time == 20) |
Tom_87 | 2:d1e2599de47c | 68 | hab=1; |
Tom_87 | 2:d1e2599de47c | 69 | else if (deb_time == 0 && hab == 1) { |
Tom_87 | 2:d1e2599de47c | 70 | state = S_NO_PRESS; |
Tom_87 | 2:d1e2599de47c | 71 | hab=0; |
Tom_87 | 0:16d62113f1d5 | 72 | } |
Tom_87 | 0:16d62113f1d5 | 73 | break; |
Tom_87 | 0:16d62113f1d5 | 74 | } |
Tom_87 | 2:d1e2599de47c | 75 | return out; |
Tom_87 | 0:16d62113f1d5 | 76 | } |
Tom_87 | 0:16d62113f1d5 | 77 |