An updated and more polished version of the earlier TeaBasic. This is now finished and works like a treat.
DebouncedIn.cpp@0:36264f703f1c, 2011-06-29 (annotated)
- Committer:
- Eggotape
- Date:
- Wed Jun 29 13:20:33 2011 +0000
- Revision:
- 0:36264f703f1c
Haven\t tried it with water yet, so may want to consider slowing the Servo down to avoid splashing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Eggotape | 0:36264f703f1c | 1 | #include "DebouncedIn.h" |
Eggotape | 0:36264f703f1c | 2 | #include "mbed.h" |
Eggotape | 0:36264f703f1c | 3 | |
Eggotape | 0:36264f703f1c | 4 | /* |
Eggotape | 0:36264f703f1c | 5 | * Constructor |
Eggotape | 0:36264f703f1c | 6 | */ |
Eggotape | 0:36264f703f1c | 7 | DebouncedIn::DebouncedIn(PinName in) |
Eggotape | 0:36264f703f1c | 8 | : _in(in) { |
Eggotape | 0:36264f703f1c | 9 | |
Eggotape | 0:36264f703f1c | 10 | // reset all the flags and counters |
Eggotape | 0:36264f703f1c | 11 | _samples = 0; |
Eggotape | 0:36264f703f1c | 12 | _output = 0; |
Eggotape | 0:36264f703f1c | 13 | _output_last = 0; |
Eggotape | 0:36264f703f1c | 14 | _rising_flag = 0; |
Eggotape | 0:36264f703f1c | 15 | _falling_flag = 0; |
Eggotape | 0:36264f703f1c | 16 | _state_counter = 0; |
Eggotape | 0:36264f703f1c | 17 | |
Eggotape | 0:36264f703f1c | 18 | // Attach ticker |
Eggotape | 0:36264f703f1c | 19 | _ticker.attach(this, &DebouncedIn::_sample, 0.005); |
Eggotape | 0:36264f703f1c | 20 | } |
Eggotape | 0:36264f703f1c | 21 | |
Eggotape | 0:36264f703f1c | 22 | void DebouncedIn::_sample() { |
Eggotape | 0:36264f703f1c | 23 | |
Eggotape | 0:36264f703f1c | 24 | // take a sample |
Eggotape | 0:36264f703f1c | 25 | _samples = _samples >> 1; // shift left |
Eggotape | 0:36264f703f1c | 26 | |
Eggotape | 0:36264f703f1c | 27 | if (_in) { |
Eggotape | 0:36264f703f1c | 28 | _samples |= 0x80; |
Eggotape | 0:36264f703f1c | 29 | } |
Eggotape | 0:36264f703f1c | 30 | |
Eggotape | 0:36264f703f1c | 31 | // examine the sample window, look for steady state |
Eggotape | 0:36264f703f1c | 32 | if (_samples == 0x00) { |
Eggotape | 0:36264f703f1c | 33 | _output = 0; |
Eggotape | 0:36264f703f1c | 34 | } |
Eggotape | 0:36264f703f1c | 35 | else if (_samples == 0xFF) { |
Eggotape | 0:36264f703f1c | 36 | _output = 1; |
Eggotape | 0:36264f703f1c | 37 | } |
Eggotape | 0:36264f703f1c | 38 | |
Eggotape | 0:36264f703f1c | 39 | |
Eggotape | 0:36264f703f1c | 40 | // Rising edge detection |
Eggotape | 0:36264f703f1c | 41 | if ((_output == 1) && (_output_last == 0)) { |
Eggotape | 0:36264f703f1c | 42 | _rising_flag++; |
Eggotape | 0:36264f703f1c | 43 | _state_counter = 0; |
Eggotape | 0:36264f703f1c | 44 | } |
Eggotape | 0:36264f703f1c | 45 | |
Eggotape | 0:36264f703f1c | 46 | // Falling edge detection |
Eggotape | 0:36264f703f1c | 47 | else if ((_output == 0) && (_output_last == 1)) { |
Eggotape | 0:36264f703f1c | 48 | _falling_flag++; |
Eggotape | 0:36264f703f1c | 49 | _state_counter = 0; |
Eggotape | 0:36264f703f1c | 50 | } |
Eggotape | 0:36264f703f1c | 51 | |
Eggotape | 0:36264f703f1c | 52 | // steady state |
Eggotape | 0:36264f703f1c | 53 | else { |
Eggotape | 0:36264f703f1c | 54 | _state_counter++; |
Eggotape | 0:36264f703f1c | 55 | } |
Eggotape | 0:36264f703f1c | 56 | |
Eggotape | 0:36264f703f1c | 57 | // update the output |
Eggotape | 0:36264f703f1c | 58 | _output_last = _output; |
Eggotape | 0:36264f703f1c | 59 | |
Eggotape | 0:36264f703f1c | 60 | } |
Eggotape | 0:36264f703f1c | 61 | |
Eggotape | 0:36264f703f1c | 62 | |
Eggotape | 0:36264f703f1c | 63 | |
Eggotape | 0:36264f703f1c | 64 | // return number of rising edges |
Eggotape | 0:36264f703f1c | 65 | int DebouncedIn::rising(void) { |
Eggotape | 0:36264f703f1c | 66 | int return_value = _rising_flag; |
Eggotape | 0:36264f703f1c | 67 | _rising_flag = 0; |
Eggotape | 0:36264f703f1c | 68 | return(return_value); |
Eggotape | 0:36264f703f1c | 69 | } |
Eggotape | 0:36264f703f1c | 70 | |
Eggotape | 0:36264f703f1c | 71 | // return number of falling edges |
Eggotape | 0:36264f703f1c | 72 | int DebouncedIn::falling(void) { |
Eggotape | 0:36264f703f1c | 73 | int return_value = _falling_flag; |
Eggotape | 0:36264f703f1c | 74 | _falling_flag = 0; |
Eggotape | 0:36264f703f1c | 75 | return(return_value); |
Eggotape | 0:36264f703f1c | 76 | } |
Eggotape | 0:36264f703f1c | 77 | |
Eggotape | 0:36264f703f1c | 78 | // return number of ticsk we've bene steady for |
Eggotape | 0:36264f703f1c | 79 | int DebouncedIn::steady(void) { |
Eggotape | 0:36264f703f1c | 80 | return(_state_counter); |
Eggotape | 0:36264f703f1c | 81 | } |
Eggotape | 0:36264f703f1c | 82 | |
Eggotape | 0:36264f703f1c | 83 | // return the debounced status |
Eggotape | 0:36264f703f1c | 84 | int DebouncedIn::read(void) { |
Eggotape | 0:36264f703f1c | 85 | return(_output); |
Eggotape | 0:36264f703f1c | 86 | } |
Eggotape | 0:36264f703f1c | 87 | |
Eggotape | 0:36264f703f1c | 88 | // shorthand for read() |
Eggotape | 0:36264f703f1c | 89 | DebouncedIn::operator int() { |
Eggotape | 0:36264f703f1c | 90 | return read(); |
Eggotape | 0:36264f703f1c | 91 | } |
Eggotape | 0:36264f703f1c | 92 | |
Eggotape | 0:36264f703f1c | 93 |