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